UNPKG

1.63 kBtext/coffeescriptView Raw
1###
2 Convert complex z to clock form
3
4 Input: push z
5
6 Output: Result on stack
7
8 clock(z) = abs(z) * (-1) ^ (arg(z) / pi)
9
10 For example, clock(exp(i pi/3)) gives the result (-1)^(1/3)
11###
12
13# P.S. I couldn't find independent definition/aknowledgment
14# of the naming "clock form" anywhere on the web, seems like a
15# naming specific to eigenmath.
16# Clock form is another way to express a complex number, and
17# it has three advantages
18# 1) it's uniform with how for example
19# i is expressed i.e. (-1)^(1/2)
20# 2) it's very compact
21# 3) it's a straighforward notation for roots of 1 and -1
22
23
24DEBUG_CLOCKFORM = false
25
26Eval_clock = ->
27 push(cadr(p1))
28 Eval()
29 clockform()
30
31clockform = ->
32 save()
33 #if 1
34 p1 = pop()
35 push(p1)
36 abs()
37 if DEBUG_CLOCKFORM then console.log "clockform: abs of " + p1 + " : " + stack[tos-1]
38
39 # pushing the expression (-1)^... but note
40 # that we can't use "power", as "power" evaluates
41 # clock forms into rectangular form (see "-1 ^ rational"
42 # section in power)
43 push_symbol(POWER)
44
45 push_integer(-1)
46
47 push(p1)
48 arg()
49 if DEBUG_CLOCKFORM then console.log "clockform: arg of " + p1 + " : " + stack[tos-1]
50 if evaluatingAsFloats
51 push_double(Math.PI)
52 else
53 push(symbol(PI))
54 divide()
55 if DEBUG_CLOCKFORM then console.log "clockform: divide : " + stack[tos-1]
56 list(3)
57
58 if DEBUG_CLOCKFORM then console.log "clockform: power : " + stack[tos-1]
59 multiply()
60 if DEBUG_CLOCKFORM then console.log "clockform: multiply : " + stack[tos-1]
61 #else
62 ###
63 p1 = pop()
64 push(p1)
65 abs()
66 push(symbol(E))
67 push(p1)
68 arg()
69 push(imaginaryunit)
70 multiply()
71 power()
72 multiply()
73 ###
74 #endif
75 restore()
76
77
78