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 | DEBUG_ABS = false
|
51 |
|
52 | Eval_abs = ->
|
53 | push(cadr(p1))
|
54 | Eval()
|
55 | abs()
|
56 |
|
57 | absValFloat = ->
|
58 | Eval()
|
59 | absval()
|
60 | Eval();
|
61 | zzfloat()
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | abs = ->
|
71 | save()
|
72 | p1 = pop()
|
73 | push(p1)
|
74 | numerator()
|
75 | absval()
|
76 | push(p1)
|
77 | denominator()
|
78 | absval()
|
79 | divide()
|
80 | restore()
|
81 |
|
82 | absval = ->
|
83 | save()
|
84 | p1 = pop()
|
85 | input = p1
|
86 |
|
87 | if DEBUG_ABS then console.log "ABS of " + p1
|
88 |
|
89 |
|
90 |
|
91 | if (iszero(p1))
|
92 | if DEBUG_ABS then console.log " abs: " + p1 + " just zero"
|
93 | push(zero)
|
94 | if DEBUG_ABS then console.log " --> ABS of " + input + " : " + stack[tos-1]
|
95 | restore()
|
96 | return
|
97 |
|
98 | if (isnegativenumber(p1))
|
99 | if DEBUG_ABS then console.log " abs: " + p1 + " just a negative"
|
100 | push(p1)
|
101 | negate()
|
102 | restore()
|
103 | return
|
104 |
|
105 | if (ispositivenumber(p1))
|
106 | if DEBUG_ABS then console.log " abs: " + p1 + " just a positive"
|
107 | push(p1)
|
108 | if DEBUG_ABS then console.log " --> ABS of " + input + " : " + stack[tos-1]
|
109 | restore()
|
110 | return
|
111 |
|
112 | if (p1 == symbol(PI))
|
113 | if DEBUG_ABS then console.log " abs: " + p1 + " of PI"
|
114 | push(p1)
|
115 | if DEBUG_ABS then console.log " --> ABS of " + input + " : " + stack[tos-1]
|
116 | restore()
|
117 | return
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | if (car(p1) == symbol(ADD) and (
|
131 | findPossibleClockForm(p1) or
|
132 | findPossibleExponentialForm(p1) or
|
133 | Find(p1,imaginaryunit))
|
134 | )
|
135 | if DEBUG_ABS then console.log " abs: " + p1 + " is a sum"
|
136 | if DEBUG_ABS then console.log "abs of a sum"
|
137 |
|
138 | push(p1)
|
139 | rect()
|
140 | p1 = pop()
|
141 | push(p1)
|
142 | real()
|
143 | push_integer(2)
|
144 | power()
|
145 | push(p1)
|
146 | imag()
|
147 | push_integer(2)
|
148 | power()
|
149 | add()
|
150 | push_rational(1, 2)
|
151 | power()
|
152 | simplify_trig()
|
153 | if DEBUG_ABS then console.log " --> ABS of " + input + " : " + stack[tos-1]
|
154 | restore()
|
155 | return
|
156 |
|
157 | if (car(p1) == symbol(POWER) && equaln(cadr(p1), -1))
|
158 | if DEBUG_ABS then console.log " abs: " + p1 + " is -1 to any power"
|
159 |
|
160 | if evaluatingAsFloats
|
161 | if DEBUG_ABS then console.log " abs: numeric, so result is 1.0"
|
162 | push_double(1.0)
|
163 | else
|
164 | if DEBUG_ABS then console.log " abs: symbolic, so result is 1"
|
165 | push_integer(1)
|
166 | if DEBUG_ABS then console.log " --> ABS of " + input + " : " + stack[tos-1]
|
167 | restore()
|
168 | return
|
169 |
|
170 |
|
171 | if (car(p1) == symbol(POWER) && ispositivenumber(caddr(p1)))
|
172 | if DEBUG_ABS then console.log " abs: " + p1 + " is something to the power of a positive number"
|
173 | push cadr(p1)
|
174 | abs()
|
175 | push caddr(p1)
|
176 | power()
|
177 | if DEBUG_ABS then console.log " --> ABS of " + input + " : " + stack[tos-1]
|
178 | restore()
|
179 | return
|
180 |
|
181 |
|
182 | if (car(p1) == symbol(POWER) && cadr(p1) == symbol(E))
|
183 | if DEBUG_ABS then console.log " abs: " + p1 + " is an exponential"
|
184 |
|
185 | push(caddr(p1))
|
186 | real()
|
187 | exponential()
|
188 | if DEBUG_ABS then console.log " --> ABS of " + input + " : " + stack[tos-1]
|
189 | restore()
|
190 | return
|
191 |
|
192 | if (car(p1) == symbol(MULTIPLY))
|
193 | if DEBUG_ABS then console.log " abs: " + p1 + " is a product"
|
194 |
|
195 | push_integer(1)
|
196 | p1 = cdr(p1)
|
197 | while (iscons(p1))
|
198 | push(car(p1))
|
199 | abs()
|
200 | multiply()
|
201 | p1 = cdr(p1)
|
202 | if DEBUG_ABS then console.log " --> ABS of " + input + " : " + stack[tos-1]
|
203 | restore()
|
204 | return
|
205 |
|
206 | if (car(p1) == symbol(ABS))
|
207 | if DEBUG_ABS then console.log " abs: " + p1 + " is abs of a abs"
|
208 |
|
209 | push_symbol(ABS)
|
210 | push cadr(p1)
|
211 | list(2)
|
212 | if DEBUG_ABS then console.log " --> ABS of " + input + " : " + stack[tos-1]
|
213 | restore()
|
214 | return
|
215 |
|
216 | |
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | if (istensor(p1))
|
248 | absval_tensor()
|
249 | restore()
|
250 | return
|
251 |
|
252 | if (isnegativeterm(p1) || (car(p1) == symbol(ADD) && isnegativeterm(cadr(p1))))
|
253 | push(p1)
|
254 | negate()
|
255 | p1 = pop()
|
256 |
|
257 | if DEBUG_ABS then console.log " abs: " + p1 + " is nothing decomposable"
|
258 | push_symbol(ABS)
|
259 | push(p1)
|
260 | list(2)
|
261 |
|
262 | if DEBUG_ABS then console.log " --> ABS of " + input + " : " + stack[tos-1]
|
263 | restore()
|
264 |
|
265 |
|
266 | absval_tensor = ->
|
267 | if (p1.tensor.ndim != 1)
|
268 | stop("abs(tensor) with tensor rank > 1")
|
269 | push(p1)
|
270 | push(p1)
|
271 | conjugate()
|
272 | inner()
|
273 | push_rational(1, 2)
|
274 | power()
|
275 | simplify()
|
276 | Eval()
|