-
Notifications
You must be signed in to change notification settings - Fork 59
/
outer.coffee
70 lines (52 loc) · 1012 Bytes
/
outer.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Outer product of tensors
Eval_outer = ->
p1 = cdr(p1)
push(car(p1))
Eval()
p1 = cdr(p1)
while (iscons(p1))
push(car(p1))
Eval()
outer()
p1 = cdr(p1)
outer = ->
save()
p2 = pop()
p1 = pop()
if (istensor(p1) && istensor(p2))
yyouter()
else
push(p1)
push(p2)
if (istensor(p1))
tensor_times_scalar()
else if (istensor(p2))
scalar_times_tensor()
else
multiply()
restore()
yyouter = ->
i = 0
j = 0
k = 0
ndim = 0
nelem = 0
ndim = p1.tensor.ndim + p2.tensor.ndim
if (ndim > MAXDIM)
stop("outer: rank of result exceeds maximum")
nelem = p1.tensor.nelem * p2.tensor.nelem
p3 = alloc_tensor(nelem)
p3.tensor.ndim = ndim
for i in [0...p1.tensor.ndim]
p3.tensor.dim[i] = p1.tensor.dim[i]
j = i
for i in [0...p2.tensor.ndim]
p3.tensor.dim[j + i] = p2.tensor.dim[i]
k = 0
for i in [0...p1.tensor.nelem]
for j in [0...p2.tensor.nelem]
push(p1.tensor.elem[i])
push(p2.tensor.elem[j])
multiply()
p3.tensor.elem[k++] = pop()
push(p3)