Skip to content

Commit

Permalink
adding "shape" function, plus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidedc committed May 2, 2016
1 parent c7283d0 commit 05ca6e2
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 1 deletion.
1 change: 1 addition & 0 deletions run-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ selftest = ->
test_simplify()
test_roots()
test_eigen()
test_shape()
mini_test()


Expand Down
1 change: 1 addition & 0 deletions runtime/defs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ SGN = counter++
SIMPLIFY = counter++
SIN = counter++
SINH = counter++
SHAPE = counter++
SQRT = counter++
STOP = counter++
SUBST = counter++
Expand Down
1 change: 1 addition & 0 deletions runtime/init.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ init = ->
std_symbol("simplify", SIMPLIFY)
std_symbol("sin", SIN)
std_symbol("sinh", SINH)
std_symbol("shape", SHAPE)
std_symbol("sqrt", SQRT)
std_symbol("stop", STOP)
std_symbol("subst", SUBST)
Expand Down
1 change: 1 addition & 0 deletions sources/eval.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Eval_cons = ->
when SIMPLIFY then Eval_simplify()
when SIN then Eval_sin()
when SINH then Eval_sinh()
when SHAPE then Eval_shape()
when SQRT then Eval_sqrt()
when STOP then Eval_stop()
when SUBST then Eval_subst()
Expand Down
50 changes: 50 additions & 0 deletions sources/shape.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# shape of tensor



Eval_shape = ->
push(cadr(p1))
Eval()
shape()

shape = ->
i = 0
ndim = 0
t = 0
ai = []
an = []
for i in [0...MAXDIM]
ai[i] = 0
an[i] = 0

#U **a, **b

save()

p1 = pop()

if (!istensor(p1))
if (!iszero(p1))
stop("transpose: tensor expected, 1st arg is not a tensor")
push(zero)
restore()
return

ndim = p1.tensor.ndim


p2 = alloc_tensor(ndim)

p2.tensor.ndim = 1
p2.tensor.dim[0] = ndim



for i in [0...ndim]
push_integer(p1.tensor.dim[i])
p2.tensor.elem[i] = pop()

push(p2)

restore()

9 changes: 8 additions & 1 deletion sources/transpose.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ transpose = ->
ndim = p1.tensor.ndim
nelem = p1.tensor.nelem

# vector?
# is it a vector?
# so here it's something curious - note how vectors are
# not really special two-dimensional matrices, but rather
# 1-dimension objects (like tensors can be). So since
# they have one dimension, transposition has no effect.
# (as opposed as if they were special two-dimensional
# matrices)
# see also Ran Pan, Tensor Transpose and Its Properties. CoRR abs/1411.1503 (2014)

if (ndim == 1)
push(p1)
Expand Down
26 changes: 26 additions & 0 deletions tests/shape.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
test_shape = ->
run_test [

# see transpose function source to see why
# transposition has no effect on vectors.

"shape((A,B,C))",
"(3)",

"shape(transpose((A,B,C)))",
"(3)",

"shape(((A),(B),(C)))",
"(3)",

"shape(transpose(((A),(B),(C))))",
"(3)",

"shape(((A,B),(C,D),(E,F)))",
"(3,2)",

"shape(transpose(((A,B),(C,D),(E,F))))",
"(2,3)",

]

0 comments on commit 05ca6e2

Please sign in to comment.