Revert "Revert "Made use of ::= in core libraries and defined a RebindError condition...
[cslatevm.git] / src / lib / complex.slate
blob549a5b86d0e084e7dd9a3745d007d9430f8af678
1 define: #Complex &parents: {Number. CoercibleNumberMixin} &slots: {
2   #real -> 0. "The real component."
3   #imaginary -> 0 "The imaginary component."
4 }.
5 "Represents a complex number, having a real and imaginary part, which should
6 not be altered per instance."
7 Complex traits level ::= 4.
9 c1@(Complex traits) = c2@(Complex traits)
11   c1 real = c2 real /\ [c1 imaginary = c2 imaginary]
14 c@(Complex traits) = n@(Number traits)
16   c imaginary isZero /\ [c real = n]
19 c@(Complex traits) hash
21   c real hash bitXor: c imaginary hash
24 c@(Complex traits) real: real imaginary: imaginary
25 [c clone `setting: #{#real. #imaginary} to: {real. imaginary}].
27 r@(Number traits) plusImaginary: i@(Number traits)
28 "A convenient method for creating Complex quantities."
29 [Complex real: r imaginary: i].
31 x@(Number traits) as: c@(Complex traits)
32 "Creates the corresponding complex number for the value, using a corresponding
33 zero coordinate."
35   c real: x imaginary: x zero
38 c@(Complex traits) as: x@(Number traits)
39 "Converts to a number if possible, but returns itself otherwise."
41   c imaginary isZero
42     ifTrue: [c real]
43     ifFalse: [c]
46 c@(Complex traits) unit
48   c real unit as: c
51 c@(Complex traits) zero
53   c real zero as: c
56 c@(Complex traits) isZero
58   c real isZero /\ [c imaginary isZero]
61 c@(Complex traits) isPositive
63   c real isPositive \/
64     [c real isZero /\ [c imaginary isPositive]]
67 c@(Complex traits) isNegative
69   c real isNegative \/
70     [c real isZero /\ [c imaginary isNegative]]
73 c@(Complex traits) sign
74 "Returns a complex number of unit dimensions in the same quadrant."
76   c real: c real sign imaginary: c imaginary sign
79 c@(Complex traits) radius
81   (c real squared + c imaginary squared) sqrt
84 c@(Complex traits) abs
85 "The magnitude of the complex as a vector."
87   c radius
90 c1@(Complex traits) < c2@(Complex traits)
91 "This is highly dubious, since Complex arithmetic and relationships are
92 totally different than linearly-ordered Comparables. This is here solely to
93 satisfy Comparable protocol, embedding Complex into Real numbers."
95   deprecated
96   c1 real < c2 real
99 I ::= Complex real: 0 imaginary: 1.
100 "The square root of -1."
102 c@(Complex traits) theta
103 "The angle between the complex coordinates and the positive real-axis."
105   c real isZero
106      ifTrue:
107        [c imaginary isPositive
108           ifTrue: [Pi / 2]
109           ifFalse: [Pi * 3 / 2]]
110      ifFalse:
111        [result ::= (c imaginary / c real) arctan.
112         c real isPositive
113           ifTrue: [result]
114           ifFalse: [Pi + result]]
117 c@(Complex traits) conjugated
118 "Returns the complex conjugate."
120   c real: c real imaginary: c imaginary negated
123 c1@(Complex traits) + c2@(Complex traits)
125   c1 real: c1 real + c2 real
126      imaginary: c1 imaginary + c2 imaginary
129 c1@(Complex traits) - c2@(Complex traits)
131   c1 real: c1 real - c2 real
132      imaginary: c1 imaginary - c2 imaginary
135 c1@(Complex traits) * c2@(Complex traits)
137   c1 real: c2 real * c1 real - (c1 imaginary * c2 imaginary)
138      imaginary: c1 real * c2 imaginary + (c1 imaginary * c2 real)
141 c1@(Complex traits) / c2@(Complex traits)
143   denom ::= c2 real squared + c2 imaginary squared.
144   c1 real: c1 real * c2 real + (c1 imaginary * c2 imaginary) / denom
145      imaginary: c2 real * c1 imaginary - (c2 imaginary * c1 real) / denom
148 c@(Complex traits) sqrt
150   c imaginary isZero /\ [c real isNegative not]
151      ifTrue: [^ c real sqrt].
152   c real: c imaginary / 2 / (r ::= (c radius - c real / 2) sqrt)
153     imaginary: r
156 c@(Complex traits) exp
158   im ::= c imaginary.
159   c real exp * (im cos + (im sin * I))
162 c@(Complex traits) ln
164   c real: c radius ln imaginary: c theta
167 c@(Complex traits) cos
169   (c * I) exp + (c * I) negated exp / 2
172 c@(Complex traits) sin
174   (c * I) exp - (c * I) negated exp / 2
177 c@(Complex traits) tan
179   c sin / c cos
182 c@(Complex traits) raisedTo: n
184   n isZero ifTrue: [^ 1].
185   n isPositive
186     ifTrue: [(n * c ln) exp]
187     ifFalse: [(c raisedTo: n negated) reciprocal]
190 n@(Number traits) i [n * I].
192 c@(Complex traits) printOn: s
194   s ; '('.
195   c real isZero ifFalse: [c real printOn: s. s ; ' + '].
196   c imaginary printOn: s.
197   s ; ' i)'.
198   "(c real isZero
199     ifTrue: [`(c imaginary `unquote i)]
200     ifFalse: [`(c real `unquote + c imaginary `unquote i)])
201     printOn: s."
202   s
205 c1@(Complex traits) isCloseTo: c2@(Complex traits)
207   (c1 real isCloseTo: c2 real) /\ [c1 imaginary isCloseTo: c2 imaginary]
210 c@(Complex traits) isCloseTo: n@(Number traits)
212   (c imaginary isCloseTo: 0.0) /\ [c real isCloseTo: n]
215 n@(Number traits) isCloseTo: c@(Complex traits)
217   c isCloseTo: n