1 |
|
2 |
|
3 |
|
4 | new_string = (s) ->
|
5 | save()
|
6 | p1 = new U()
|
7 | p1.k = STR
|
8 | p1.str = s
|
9 | push(p1)
|
10 | restore()
|
11 |
|
12 | out_of_memory = ->
|
13 | stop("out of memory")
|
14 |
|
15 |
|
16 | push_zero_matrix = (i,j) ->
|
17 | push(alloc_tensor(i * j))
|
18 | stack[tos - 1].tensor.ndim = 2
|
19 | stack[tos - 1].tensor.dim[0] = i
|
20 | stack[tos - 1].tensor.dim[1] = j
|
21 |
|
22 | push_identity_matrix = (n) ->
|
23 | push_zero_matrix(n, n)
|
24 | i = 0
|
25 | for i in [0...n]
|
26 | stack[tos - 1].tensor.elem[i * n + i] = one
|
27 |
|
28 | check_tensor_dimensions stack[tos - 1]
|
29 |
|
30 | push_cars = (p) ->
|
31 | while (iscons(p))
|
32 | push(car(p))
|
33 | p = cdr(p)
|
34 |
|
35 | peek = ->
|
36 | save()
|
37 | p1 = pop()
|
38 | push(p1)
|
39 | restore()
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | equal = (p1,p2) ->
|
47 | if (cmp_expr(p1, p2) == 0)
|
48 | return 1
|
49 | else
|
50 | return 0
|
51 |
|
52 | lessp = (p1,p2) ->
|
53 | if (cmp_expr(p1, p2) < 0)
|
54 | return 1
|
55 | else
|
56 | return 0
|
57 |
|
58 | sign = (n) ->
|
59 | if (n < 0)
|
60 | return -1
|
61 | else if (n > 0)
|
62 | return 1
|
63 | else
|
64 | return 0
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | cmp_expr = (p1, p2) ->
|
80 | n = 0
|
81 |
|
82 | if (p1 == p2)
|
83 | return 0
|
84 |
|
85 | if (p1 == symbol(NIL))
|
86 | return -1
|
87 |
|
88 | if (p2 == symbol(NIL))
|
89 | return 1
|
90 |
|
91 | if (isnum(p1) && isnum(p2))
|
92 | return sign(compare_numbers(p1, p2))
|
93 |
|
94 | if (isnum(p1))
|
95 | return -1
|
96 |
|
97 | if (isnum(p2))
|
98 | return 1
|
99 |
|
100 | if (isstr(p1) && isstr(p2))
|
101 | return sign(strcmp(p1.str,p2.str))
|
102 |
|
103 | if (isstr(p1))
|
104 | return -1
|
105 |
|
106 | if (isstr(p2))
|
107 | return 1
|
108 |
|
109 | if (issymbol(p1) && issymbol(p2))
|
110 | return sign(strcmp(get_printname(p1),get_printname(p2)))
|
111 |
|
112 | if (issymbol(p1))
|
113 | return -1
|
114 |
|
115 | if (issymbol(p2))
|
116 | return 1
|
117 |
|
118 | if (istensor(p1) && istensor(p2))
|
119 | return compare_tensors(p1, p2)
|
120 |
|
121 | if (istensor(p1))
|
122 | return -1
|
123 |
|
124 | if (istensor(p2))
|
125 | return 1
|
126 |
|
127 |
|
128 | while (iscons(p1) && iscons(p2))
|
129 | n = cmp_expr(car(p1), car(p2))
|
130 | if (n != 0)
|
131 | return n
|
132 | p1 = cdr(p1)
|
133 | p2 = cdr(p2)
|
134 |
|
135 | if (iscons(p2))
|
136 | return -1
|
137 |
|
138 | if (iscons(p1))
|
139 | return 1
|
140 |
|
141 | return 0
|
142 |
|
143 | length = (p) ->
|
144 | n = 0
|
145 | while (iscons(p))
|
146 | p = cdr(p)
|
147 | n++
|
148 | return n
|
149 |
|
150 | unique = (p) ->
|
151 | save()
|
152 | p1 = symbol(NIL)
|
153 | p2 = symbol(NIL)
|
154 | unique_f(p)
|
155 | if (p2 != symbol(NIL))
|
156 | p1 = symbol(NIL)
|
157 | p = p1
|
158 | restore()
|
159 | return p
|
160 |
|
161 | unique_f = (p) ->
|
162 | if (isstr(p))
|
163 | if (p1 == symbol(NIL))
|
164 | p1 = p
|
165 | else if (p != p1)
|
166 | p2 = p
|
167 | return
|
168 | while (iscons(p))
|
169 | unique_f(car(p))
|
170 | if (p2 != symbol(NIL))
|
171 | return
|
172 | p = cdr(p)
|
173 |
|
174 |
|
175 | ssqrt = ->
|
176 | push_rational(1, 2)
|
177 | power()
|
178 |
|
179 | yyexpand = ->
|
180 | prev_expanding = expanding
|
181 | expanding = 1
|
182 | Eval()
|
183 | expanding = prev_expanding
|
184 |
|
185 | exponential = ->
|
186 | push_symbol(E)
|
187 | swap()
|
188 | power()
|
189 |
|
190 | square = ->
|
191 | push_integer(2)
|
192 | power()
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | sort_stack = (n) ->
|
199 |
|
200 |
|
201 | h = tos - n
|
202 | subsetOfStack = stack.slice(h,h+n)
|
203 | subsetOfStack.sort(cmp_expr)
|
204 | stack = stack.slice(0,h).concat(subsetOfStack).concat(stack.slice(h+n))
|
205 |
|
206 |
|
207 | $.equal = equal
|
208 | $.length = length
|