UNPKG

2.89 kBtext/coffeescriptView Raw
1###
2Convert complex z to rectangular form
3
4 Input: push z
5
6 Output: Result on stack
7###
8
9
10DEBUG_RECT = false
11
12Eval_rect = ->
13 push(cadr(p1))
14 Eval()
15 rect()
16
17rect = ->
18 save()
19 p1 = pop()
20 input = p1
21
22 if DEBUG_RECT then console.log "RECT of " + input
23
24 if DEBUG_RECT then console.log "any clock forms in : " + input + " ? " + findPossibleClockForm(input)
25
26
27 # if we assume real variables, then the
28 # rect of any symbol is the symbol itself
29 # (note that 'i' is not a symbol, it's made of (-1)^(1/2))
30 # otherwise we have to leave unevalled
31 if issymbol(p1)
32 if DEBUG_RECT then console.log " rect: simple symbol: " + input
33 if !iszero(get_binding(symbol(ASSUME_REAL_VARIABLES)))
34 push(p1)
35 else
36 push_symbol(YYRECT)
37 push(p1)
38 list(2)
39
40 # TODO this is quite dirty, ideally we don't need this
41 # but removing this creates a few failings in the tests
42 # that I can't investigate right now.
43 # --
44 # if we assume all variables are real AND
45 # it's not an exponential nor a polar nor a clock form
46 # THEN rect(_) = _
47 # note that these matches can be quite sloppy, one can find expressions
48 # which shouldn't match but do
49 #
50 else if !iszero(get_binding(symbol(ASSUME_REAL_VARIABLES))) and
51 !findPossibleExponentialForm(p1) and # no exp form?
52 !findPossibleClockForm(p1) and # no clock form?
53 !(Find(p1, symbol(SIN)) and Find(p1, symbol(COS)) and Find(p1, imaginaryunit)) # no polar form?
54 if DEBUG_RECT then console.log " rect: simple symbol: " + input
55 push(p1)
56
57 # ib
58 else if (car(p1) == symbol(MULTIPLY) and isimaginaryunit(cadr(p1)) and !iszero(get_binding(symbol(ASSUME_REAL_VARIABLES))))
59 push(p1)
60
61 # sum
62 else if (car(p1) == symbol(ADD))
63 if DEBUG_RECT then console.log " rect - " + input + " is a sum "
64 push_integer(0)
65 p1 = cdr(p1)
66 while (iscons(p1))
67 push(car(p1))
68 rect()
69 add()
70 p1 = cdr(p1)
71
72 else
73 # try to get to the rectangular form by doing
74 # abs(p1) * (cos (theta) + i * sin(theta))
75 # where theta is arg(p1)
76 if DEBUG_RECT then console.log " rect - " + input + " is NOT a sum "
77
78 push(p1); # abs(z) * (cos(arg(z)) + i sin(arg(z)))
79 abs()
80
81 if DEBUG_RECT then console.log " rect - " + input + " abs: " + stack[tos-1].toString()
82 push(p1)
83 arg()
84 if DEBUG_RECT then console.log " rect - " + input + " arg of " + p1 + " : " + stack[tos-1].toString()
85 p1 = pop()
86 push(p1)
87 cosine()
88 if DEBUG_RECT then console.log " rect - " + input + " cosine: " + stack[tos-1].toString()
89 push(imaginaryunit)
90 push(p1)
91 sine()
92 if DEBUG_RECT then console.log " rect - " + input + " sine: " + stack[tos-1].toString()
93 multiply()
94 if DEBUG_RECT then console.log " rect - " + input + " i * sine: " + stack[tos-1].toString()
95 add()
96 if DEBUG_RECT then console.log " rect - " + input + " cos + i * sine: " + stack[tos-1].toString()
97 multiply()
98 restore()
99 if DEBUG_RECT then console.log "rect of " + input + " : " + stack[tos-1]
100