-
Notifications
You must be signed in to change notification settings - Fork 59
/
contract.coffee
134 lines (105 loc) · 2.09 KB
/
contract.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
### contract =====================================================================
Tags
----
scripting, JS, internal, treenode, general concept
Parameters
----------
a,i,j
General description
-------------------
Contract across tensor indices i.e. returns "a" summed over indices i and j.
If i and j are omitted then 1 and 2 are used.
contract(m) is equivalent to the trace of matrix m.
###
Eval_contract = ->
push(cadr(p1))
Eval()
if (cddr(p1) == symbol(NIL))
push_integer(1)
push_integer(2)
else
push(caddr(p1))
Eval()
push(cadddr(p1))
Eval()
contract()
contract = ->
save()
yycontract()
restore()
yycontract = ->
h = 0
i = 0
j = 0
k = 0
l = 0
m = 0
n = 0
ndim = 0
nelem = 0
ai = []
an = []
p3 = pop()
p2 = pop()
p1 = pop()
if (!istensor(p1))
if (!isZeroAtomOrTensor(p1))
stop("contract: tensor expected, 1st arg is not a tensor")
push(zero)
return
push(p2)
l = pop_integer()
push(p3)
m = pop_integer()
ndim = p1.tensor.ndim
if (l < 1 || l > ndim || m < 1 || m > ndim || l == m \
|| p1.tensor.dim[l - 1] != p1.tensor.dim[m - 1])
stop("contract: index out of range")
l--
m--
n = p1.tensor.dim[l]
# nelem is the number of elements in "b"
nelem = 1
for i in [0...ndim]
if (i != l && i != m)
nelem *= p1.tensor.dim[i]
#console.log "nelem:" + nelem
p2 = alloc_tensor(nelem)
#console.log "p2:" + p2
p2.tensor.ndim = ndim - 2
j = 0
for i in [0...ndim]
if (i != l && i != m)
p2.tensor.dim[j++] = p1.tensor.dim[i]
a = p1.tensor.elem
b = p2.tensor.elem
#console.log "a: " + a
#console.log "b: " + b
for i in [0...ndim]
ai[i] = 0
an[i] = p1.tensor.dim[i]
for i in [0...nelem]
push(zero)
for j in [0...n]
ai[l] = j
ai[m] = j
h = 0
for k in [0...ndim]
h = (h * an[k]) + ai[k]
push(a[h])
#console.log "a[h]: " + a[h]
add()
#console.log "tos: " + stack[tos-1]
b[i] = pop()
#console.log "b[i]: " + b[i]
for j in [(ndim - 1)..0]
if (j == l || j == m)
continue
if (++ai[j] < an[j])
break
ai[j] = 0
if (nelem == 1)
push(b[0])
else
push(p2)
#console.log "returning: " + stack[tos-1]