author | Claus Gittinger <cg@exept.de> |
Mon, 03 Jul 2017 14:09:11 +0200 | |
changeset 21947 | 40e2852e1422 |
parent 21933 | 56fbff93ec73 |
child 21987 | 9427ca525596 |
permissions | -rw-r--r-- |
3637 | 1 |
" |
2 |
This is a Manchester Goodie. It is distributed freely on condition |
|
3 |
that you observe these conditions in respect of the whole Goodie, and on |
|
4 |
any significant part of it which is separately transmitted or stored: |
|
5287
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
5 |
* You must ensure that every copy includes this notice, and that |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
6 |
source and author(s) of the material are acknowledged. |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
7 |
* These conditions must be imposed on anyone who receives a copy. |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
8 |
* The material shall not be used for commercial gain without the prior |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
9 |
written consent of the author(s). |
3637 | 10 |
|
13548 | 11 |
For more information about the Manchester Goodies Library (from which |
3637 | 12 |
this file was distributed) send e-mail: |
5287
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
13 |
To: goodies-lib@cs.man.ac.uk |
13548 | 14 |
Subject: help |
3637 | 15 |
|
16 |
This is an additional goody-class, which is NOT covered by the |
|
17 |
ST/X license. It has been packaged with the ST/X distribution to |
|
18 |
make your live easier instead. NO WARRANTY. |
|
19 |
" |
|
13548 | 20 |
"{ Package: 'stx:libbasic' }" |
6500 | 21 |
|
18836 | 22 |
"{ NameSpace: Smalltalk }" |
23 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
24 |
ArithmeticValue subclass:#Complex |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
25 |
instanceVariableNames:'real imaginary' |
7355 | 26 |
classVariableNames:'ComplexOne ComplexZero' |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
27 |
poolDictionaries:'' |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
28 |
category:'Magnitude-Numbers' |
265 | 29 |
! |
30 |
||
3637 | 31 |
!Complex class methodsFor:'documentation'! |
32 |
||
33 |
copyright |
|
34 |
" |
|
35 |
This is a Manchester Goodie. It is distributed freely on condition |
|
36 |
that you observe these conditions in respect of the whole Goodie, and on |
|
37 |
any significant part of it which is separately transmitted or stored: |
|
5287
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
38 |
* You must ensure that every copy includes this notice, and that |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
39 |
source and author(s) of the material are acknowledged. |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
40 |
* These conditions must be imposed on anyone who receives a copy. |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
41 |
* The material shall not be used for commercial gain without the prior |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
42 |
written consent of the author(s). |
3637 | 43 |
|
13548 | 44 |
For more information about the Manchester Goodies Library (from which |
3637 | 45 |
this file was distributed) send e-mail: |
5287
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
46 |
To: goodies-lib@cs.man.ac.uk |
13548 | 47 |
Subject: help |
3637 | 48 |
|
49 |
This is an additional goody-class, which is NOT covered by the |
|
50 |
ST/X license. It has been packaged with the ST/X distribution to |
|
51 |
make your live easier instead. NO WARRANTY. |
|
52 |
" |
|
53 |
! |
|
54 |
||
55 |
documentation |
|
56 |
" |
|
13548 | 57 |
This class implements complex numbers. |
58 |
A complex number has real and imaginary parts which must be manipulated simultaneously |
|
7355 | 59 |
in any numeric processing. |
60 |
Complex numbers can be used in many of the same places that regular numbers |
|
61 |
can be used with one major exception of comparisons, since complex numbers cannot |
|
13548 | 62 |
be directly compared for size |
7355 | 63 |
(except through lengths of vectors (see absolute value)). |
265 | 64 |
|
7355 | 65 |
[Instance variables:] |
66 |
real <Number> the part of the number which can be expressed as a Real number |
|
67 |
imaginary <Number> the part of the number which, in terms of how the number behaves, |
|
13548 | 68 |
has been multiplied by 'i' (-1 sqrt) |
265 | 69 |
|
7355 | 70 |
[Author:] |
13548 | 71 |
Kurt Hebel (hebel@uinova.cerl.uiuc.edu) |
72 |
minor changes and double dispatching code by cg. |
|
3637 | 73 |
" |
6500 | 74 |
! |
75 |
||
7355 | 76 |
examples |
6500 | 77 |
" |
21932 | 78 |
-25 sqrt -> error |
79 |
Complex trapImaginary:[ -25 sqrt ] -> 5i (0.0+5.0i) |
|
80 |
Complex trapImaginary:[ -25 integerSqrt ] -> 5i (0+5i) |
|
81 |
||
82 |
(Complex trapImaginary:[ -5397346292805549782720214077673687804022210808238353958670041357153884304 integerSqrt ]) |
|
83 |
squared |
|
84 |
||
85 |
1 + 3i |
|
86 |
||
87 |
Number i + 1 |
|
88 |
1 + Number i |
|
89 |
||
90 |
1i * 1i |
|
91 |
Number i * Number i |
|
92 |
||
6500 | 93 |
(5 % 7) real |
94 |
(5 % 7) imaginary |
|
95 |
(5 % 7) = 5 |
|
13548 | 96 |
(5 % 0) = 5 |
97 |
(5.0 % 0) = 5 |
|
7221 | 98 |
|
13548 | 99 |
(1 % 0) + (2 % 0) |
100 |
(1 % 0) + (0 % 2) |
|
7221 | 101 |
(1 % 0) + (2 % 3) |
102 |
||
13548 | 103 |
(1 % 0) * (2 % 0) |
104 |
(1 % 0) * (0 % 2) |
|
7221 | 105 |
(1 % 0) * (2 % 3) |
7355 | 106 |
|
107 |
(1 % 2) + 2 |
|
108 |
(1 % 2) * 2 |
|
13548 | 109 |
2 + (1 % 2) |
110 |
2 * (1 % 2) |
|
7355 | 111 |
|
21932 | 112 |
(Number i raisedTo:-3) -> Number i |
113 |
(Number i raisedTo:-2) -> -1 |
|
114 |
(Number i raisedTo:-1) -> Number i negated |
|
115 |
||
116 |
(Number i raisedTo:0) -> 1 |
|
117 |
(Number i raisedTo:1) -> Number i |
|
118 |
(Number i raisedTo:2) -> -1 |
|
119 |
(Number i raisedTo:3) -> Number i negated |
|
120 |
(Number i raisedTo:4) -> 1 |
|
121 |
(Number i raisedTo:6) -> -1 |
|
21933 | 122 |
|
123 |
3 raisedTo:Number i |
|
124 |
3 i raisedTo:Number i |
|
125 |
||
21932 | 126 |
5i * 5i -> -25 |
127 |
5i squared -> -25 |
|
6500 | 128 |
" |
3637 | 129 |
! ! |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
130 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
131 |
!Complex class methodsFor:'instance creation'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
132 |
|
9128 | 133 |
abs:aNumber1 arg:aNumber2 |
134 |
|real imaginary| |
|
135 |
||
136 |
real := aNumber1 * aNumber2 cos. |
|
137 |
imaginary := aNumber1 * aNumber2 sin. |
|
138 |
^ real + imaginary i |
|
139 |
! |
|
140 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
141 |
fromReal: aNumber |
7355 | 142 |
"Create a new complex number from the given real number." |
143 |
||
144 |
^ self basicNew setReal: aNumber setImaginary: 0 |
|
21798 | 145 |
|
146 |
" |
|
147 |
Complex fromReal:1.0 |
|
148 |
" |
|
149 |
||
150 |
"Modified (comment): / 12-06-2017 / 20:42:56 / cg" |
|
7355 | 151 |
! |
152 |
||
153 |
imaginary: v |
|
154 |
"Create a new complex number with 0 as real and given imaginary parts. |
|
155 |
If the imaginary part is zero, return the real part of the number." |
|
156 |
||
157 |
v = 0 ifTrue: [^ 0]. |
|
158 |
^ self basicNew setReal: 0 setImaginary: v |
|
21798 | 159 |
|
160 |
" |
|
161 |
Complex imaginary:1.0 |
|
162 |
(0.0 % 1.0) |
|
163 |
" |
|
164 |
||
165 |
"Modified (comment): / 12-06-2017 / 20:44:51 / cg" |
|
7355 | 166 |
! |
167 |
||
168 |
real: aNumber |
|
169 |
"Create a new complex number from the given real number." |
|
170 |
||
171 |
^ self basicNew setReal: aNumber setImaginary: 0 |
|
21798 | 172 |
|
173 |
" |
|
174 |
Complex real:1.0 |
|
175 |
" |
|
176 |
||
177 |
"Modified (comment): / 12-06-2017 / 20:42:14 / cg" |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
178 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
179 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
180 |
real: u imaginary: v |
7355 | 181 |
"Create a new complex number with the given real and imaginary parts. If the |
182 |
imaginary part is zero, return the real part of the number." |
|
183 |
||
184 |
v = 0 ifTrue: [^ u]. |
|
185 |
^ self basicNew setReal: u setImaginary: v |
|
21798 | 186 |
|
187 |
" |
|
188 |
Complex real:1.0 imaginary:2.0 |
|
189 |
(1.0 % 2.0) |
|
190 |
" |
|
191 |
||
192 |
"Modified (comment): / 12-06-2017 / 20:44:32 / cg" |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
193 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
194 |
|
21873 | 195 |
!Complex class methodsFor:'coercing & converting'! |
21817 | 196 |
|
197 |
coerce: aNumber |
|
198 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
|
199 |
||
200 |
^ aNumber asComplex |
|
201 |
! ! |
|
202 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
203 |
!Complex class methodsFor:'constants access'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
204 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
205 |
unity |
7355 | 206 |
"Answer the value which allows, for any given arithmetic value, the following to be true: |
13548 | 207 |
aNumber * aNumber class unity = aNumber |
7355 | 208 |
This must be true regardless of how a given subclass chooses to define #*" |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
209 |
|
7355 | 210 |
ComplexOne isNil ifTrue:[ |
13548 | 211 |
ComplexOne := self fromReal: 1 |
7355 | 212 |
]. |
213 |
^ ComplexOne |
|
265 | 214 |
! |
215 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
216 |
zero |
7355 | 217 |
"Answer the value which allows, for any given arithmetic value, the following to be true: |
13548 | 218 |
aNumber + aNumber class zero = aNumber |
7355 | 219 |
This must be true regardless of how a given subclass chooses to define #+" |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
220 |
|
7355 | 221 |
ComplexZero isNil ifTrue:[ |
13548 | 222 |
ComplexZero := self fromReal: 0 |
7355 | 223 |
]. |
224 |
^ ComplexZero |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
225 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
226 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
227 |
!Complex class methodsFor:'exception handling'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
228 |
|
7404 | 229 |
trapImaginary:aBlock |
230 |
"evaluate aBlock; if any DomainError occurs inside, with respect to square roots, |
|
231 |
convert the root to a complex root and proceed. |
|
232 |
This allows for regular (failing) code to transparently convert to complex." |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
233 |
|
21932 | 234 |
^ ImaginaryResultError |
235 |
handle: [:ex | |
|
236 |
|msgSend selector rcvr| |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
237 |
|
21932 | 238 |
msgSend := ex parameter. |
239 |
selector := msgSend selector. |
|
240 |
rcvr := msgSend receiver. |
|
241 |
(selector = #sqrt or: [selector = #sqrtTruncated]) ifTrue: [ |
|
242 |
(rcvr isNumber and:[rcvr isReal]) ifTrue:[ |
|
243 |
ex proceedWith:(rcvr abs perform:selector) i |
|
244 |
] ifFalse:[ |
|
245 |
msgSend receiver: rcvr asComplex. |
|
246 |
ex proceedWith: msgSend value |
|
247 |
]. |
|
248 |
] ifFalse: [ |
|
249 |
(selector = #integerSqrt) ifTrue: [ |
|
250 |
(rcvr isInteger) ifTrue:[ |
|
251 |
ex proceedWith:(rcvr abs integerSqrt) i |
|
252 |
] ifFalse:[ |
|
253 |
ex proceedWith:(rcvr abs asComplex sqrt floor) i |
|
254 |
]. |
|
255 |
] ifFalse: [ |
|
256 |
ex reject |
|
257 |
] |
|
258 |
] |
|
259 |
] do: |
|
260 |
aBlock |
|
7404 | 261 |
|
262 |
" |
|
13548 | 263 |
Complex trapImaginary: [-2 sqrt] |
7404 | 264 |
" |
265 |
||
266 |
"failing code: |
|
21932 | 267 |
|a| |
7404 | 268 |
|
21932 | 269 |
a := -2. |
270 |
(a sqrt + 5) * 17. |
|
7404 | 271 |
" |
272 |
"complex code: |
|
21932 | 273 |
|a| |
7404 | 274 |
|
21932 | 275 |
Complex trapImaginary:[ |
276 |
a := -2. |
|
277 |
(a sqrt + 5) * 2. |
|
278 |
] |
|
7404 | 279 |
" |
21932 | 280 |
|
281 |
"Modified: / 01-07-2017 / 20:17:15 / cg" |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
282 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
283 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
284 |
!Complex methodsFor:'accessing'! |
265 | 285 |
|
286 |
imaginary |
|
7355 | 287 |
"Return the imaginary part of the complex number." |
288 |
||
289 |
^ imaginary |
|
290 |
! |
|
291 |
||
292 |
imaginaryPart |
|
293 |
"Return the imaginary part of the complex number. |
|
294 |
An alias for imaginary (for compatibility with other complex implementations)" |
|
295 |
||
296 |
^ imaginary |
|
265 | 297 |
! |
298 |
||
299 |
real |
|
7355 | 300 |
"Return the real part of the complex number." |
301 |
||
302 |
^ real |
|
303 |
! |
|
304 |
||
305 |
realPart |
|
306 |
"Return the real part of the complex number. |
|
307 |
An alias for real (for compatibility with other complex implementations)" |
|
308 |
||
309 |
^ real |
|
265 | 310 |
! ! |
311 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
312 |
!Complex methodsFor:'arithmetic'! |
265 | 313 |
|
13548 | 314 |
* aNumber |
7355 | 315 |
"Return the product of the receiver and the argument." |
265 | 316 |
|
7355 | 317 |
"/ | u v r i | |
318 |
"/ |
|
319 |
"/ aNumber isComplex ifTrue:[ |
|
320 |
"/ u := aNumber real. |
|
321 |
"/ v := aNumber imaginary. |
|
322 |
"/ r := (real * u) - (imaginary * v). |
|
323 |
"/ i := (real * v) + (imaginary * u). |
|
324 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
325 |
"/ ^ Complex real:r imaginary:i |
|
326 |
"/ ]. |
|
13548 | 327 |
^ aNumber productFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
328 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
329 |
"Modified: / 8.7.1998 / 12:12:37 / cg" |
265 | 330 |
! |
331 |
||
13548 | 332 |
+ aNumber |
7355 | 333 |
"Return the sum of the receiver and the argument." |
265 | 334 |
|
7355 | 335 |
"/ | r i | |
336 |
"/ |
|
337 |
"/ aNumber isComplex ifTrue: [ |
|
338 |
"/ r := aNumber real + real. |
|
339 |
"/ i := aNumber imaginary + imaginary. |
|
340 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
341 |
"/ ^ Complex real:r imaginary:i |
|
342 |
"/ ]. |
|
13548 | 343 |
^ aNumber sumFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
344 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
345 |
"Modified: / 8.7.1998 / 12:15:42 / cg" |
265 | 346 |
! |
347 |
||
348 |
- aNumber |
|
7355 | 349 |
"Return the difference of the receiver and the argument." |
265 | 350 |
|
7355 | 351 |
"/ | r i | |
352 |
"/ |
|
353 |
"/ aNumber isComplex ifTrue: [ |
|
354 |
"/ r := real - aNumber real. |
|
355 |
"/ i := imaginary - aNumber imaginary. |
|
356 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
357 |
"/ ^ Complex real:r imaginary:i. |
|
358 |
"/ ]. |
|
13548 | 359 |
^ aNumber differenceFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
360 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
361 |
"Modified: / 8.7.1998 / 12:15:38 / cg" |
265 | 362 |
! |
363 |
||
13548 | 364 |
/ aNumber |
7355 | 365 |
"Return the quotient of the receiver and the argument." |
265 | 366 |
|
7355 | 367 |
"/ | denom u v r i | |
368 |
"/ |
|
13548 | 369 |
"/ aNumber isComplex ifTrue:[ |
7355 | 370 |
"/ u := aNumber real. |
371 |
"/ v := aNumber imaginary. |
|
372 |
"/ denom := u * u + (v * v). |
|
373 |
"/ r := u * real + (v * imaginary) / denom. |
|
374 |
"/ i := u * imaginary - (v * real) / denom. |
|
375 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
376 |
"/ ^ Complex real:r imaginary:i |
|
377 |
"/ ]. |
|
13548 | 378 |
^ aNumber quotientFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
379 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
380 |
"Modified: / 8.7.1998 / 12:15:34 / cg" |
265 | 381 |
! |
382 |
||
383 |
abs |
|
7355 | 384 |
"Return the magnitude (or absolute value) of the complex number |
20408 | 385 |
(that's the distance from the origin in the complex plane)." |
265 | 386 |
|
7355 | 387 |
^ (real * real + (imaginary * imaginary)) sqrt |
388 |
||
389 |
" |
|
390 |
(1 % 1) abs |
|
391 |
" |
|
265 | 392 |
! |
393 |
||
394 |
conjugated |
|
7355 | 395 |
"Return the complex conjugate of this complex number |
396 |
(i.e. with imaginary part negated)." |
|
397 |
||
16449 | 398 |
^ self class |
399 |
real: real |
|
400 |
imaginary: imaginary negated |
|
7355 | 401 |
! |
402 |
||
403 |
modulus |
|
404 |
| absReal absImag multiplicand quotient | |
|
405 |
||
406 |
absReal := real abs. |
|
407 |
absImag := imaginary abs. |
|
408 |
||
13548 | 409 |
absReal >= absImag ifTrue: [ |
410 |
multiplicand := absReal. |
|
411 |
quotient := imaginary / real |
|
412 |
] ifFalse: [ |
|
413 |
multiplicand := absImag. |
|
414 |
quotient := real / imaginary |
|
7355 | 415 |
]. |
416 |
^ multiplicand * ((1 + (quotient * quotient)) sqrt) |
|
417 |
! |
|
418 |
||
419 |
negated |
|
420 |
"return a new complex with both real and imaginary parts negated" |
|
421 |
||
16449 | 422 |
^ self class |
423 |
real: real negated |
|
424 |
imaginary: imaginary negated |
|
265 | 425 |
! ! |
426 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
427 |
!Complex methodsFor:'coercing'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
428 |
|
21873 | 429 |
generality |
430 |
^ 150 |
|
431 |
! ! |
|
432 |
||
433 |
!Complex methodsFor:'coercing & converting'! |
|
434 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
435 |
coerce: aNumber |
18836 | 436 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
437 |
||
7355 | 438 |
^ aNumber asComplex |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
439 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
440 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
441 |
!Complex methodsFor:'comparing'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
442 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
443 |
< aNumber |
7355 | 444 |
"raises an error - complex numbers are not well ordered" |
445 |
||
446 |
^ Number |
|
13548 | 447 |
raise: #unorderedSignal |
448 |
receiver: self |
|
449 |
selector: #< |
|
450 |
arg: aNumber |
|
451 |
errorString: 'Complex numbers are not well ordered' |
|
7355 | 452 |
|
453 |
" |
|
454 |
1 < (2 % 2) |
|
455 |
(2 % 2) < 1 |
|
456 |
" |
|
6500 | 457 |
! |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
458 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
459 |
= aNumber |
7355 | 460 |
"return true, if the argument represents the same numeric value |
7471 | 461 |
as the receiver, false otherwise." |
7355 | 462 |
|
13548 | 463 |
^ aNumber equalFromComplex:self |
21798 | 464 |
|
465 |
" |
|
466 |
(Complex real:1.0 imaginary:2.0) = (Complex real:1.0 imaginary:2.0) |
|
467 |
(Complex real:1.0 imaginary:0) = 1.0 |
|
468 |
" |
|
469 |
||
470 |
"Modified (comment): / 12-06-2017 / 20:43:41 / cg" |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
471 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
472 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
473 |
hash |
7355 | 474 |
"Hash is implemented because equals is implemented." |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
475 |
|
7377 | 476 |
^ (real hash) bitXor:(imaginary hash bitShift:16) |
477 |
||
478 |
" |
|
13548 | 479 |
(1+0i) hash |
7377 | 480 |
(1+1i) hash |
481 |
" |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
482 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
483 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
484 |
!Complex methodsFor:'converting'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
485 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
486 |
asComplex |
7355 | 487 |
"I am a complex - so return the receiver" |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
488 |
|
7355 | 489 |
^ self |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
490 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
491 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
492 |
asFloat |
7355 | 493 |
imaginary = 0 ifTrue: [^ real asFloat]. |
494 |
^ Number |
|
13548 | 495 |
raise: #coercionErrorSignal |
496 |
receiver: self |
|
497 |
selector: #asFloat |
|
498 |
errorString: 'Can''t coerce an instance of Complex to a Float' |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
499 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
500 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
501 |
asInteger |
7355 | 502 |
imaginary = 0 ifTrue: [^real asInteger]. |
503 |
^ Number |
|
13548 | 504 |
raise: #coercionErrorSignal |
505 |
receiver: self |
|
506 |
selector: #asInteger |
|
507 |
errorString: 'Can''t coerce an instance of Complex to an Integer' |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
508 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
509 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
510 |
asPoint |
7355 | 511 |
"Return the complex number as a point." |
512 |
||
513 |
^ real @ imaginary |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
514 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
515 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
516 |
reduceGeneralityIfPossible |
13548 | 517 |
"Answer the receiver transformed to a lower generality, if such a |
518 |
transformation is possible without losing information. |
|
7355 | 519 |
If not, answer the receiver" |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
520 |
|
7355 | 521 |
imaginary isZero |
13548 | 522 |
ifTrue: [^ real] |
523 |
ifFalse: [^ self] |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
524 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
525 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
526 |
!Complex methodsFor:'double dispatching'! |
265 | 527 |
|
9128 | 528 |
differenceFromComplex:aComplex |
529 |
"Return the difference of the argument, aComplex and the receiver." |
|
530 |
||
531 |
| r i | |
|
532 |
||
533 |
r := aComplex real - real. |
|
534 |
i := aComplex imaginary - imaginary. |
|
535 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 536 |
^ self class real:r imaginary:i. |
9128 | 537 |
! |
538 |
||
20740 | 539 |
differenceFromFixedPoint: aFixedPoint |
21110 | 540 |
"Return the difference of the argument, aFixedPoint and the receiver." |
541 |
||
20740 | 542 |
^ aFixedPoint asComplex - self |
543 |
! |
|
544 |
||
7355 | 545 |
differenceFromFloat:aFloat |
546 |
"Return the difference of the argument, aFloat and the receiver." |
|
547 |
||
548 |
"/ ^ aFloat asComplex - self |
|
549 |
||
550 |
| r | |
|
265 | 551 |
|
7355 | 552 |
r := aFloat - real. |
553 |
imaginary = 0 ifTrue:[ ^ r ]. |
|
16449 | 554 |
^ self class real:r imaginary:imaginary negated |
7355 | 555 |
|
556 |
" |
|
13548 | 557 |
(1 % 1) - 1.0 |
558 |
1.0 - (1 % 1) |
|
7355 | 559 |
" |
265 | 560 |
! |
561 |
||
7355 | 562 |
differenceFromFraction: aFraction |
21110 | 563 |
"Return the difference of the argument, aFraction and the receiver." |
564 |
||
7355 | 565 |
^ aFraction asComplex - self |
566 |
! |
|
567 |
||
568 |
differenceFromInteger: anInteger |
|
21110 | 569 |
"Return the difference of the argument, anInteger and the receiver." |
570 |
||
7355 | 571 |
^ anInteger asComplex - self |
265 | 572 |
! |
573 |
||
7355 | 574 |
equalFromComplex:aComplex |
21798 | 575 |
"return true if aComplex represents the same number as myself" |
576 |
||
7355 | 577 |
^ (aComplex real = real) and:[aComplex imaginary = imaginary] |
21798 | 578 |
|
579 |
"Modified (comment): / 12-06-2017 / 20:41:04 / cg" |
|
265 | 580 |
! |
581 |
||
7355 | 582 |
equalFromFloat:aFloat |
21798 | 583 |
"return true if aFloat represents the same number as myself" |
584 |
||
7355 | 585 |
imaginary = 0 ifFalse:[^ false]. |
586 |
^ real = aFloat |
|
21798 | 587 |
|
588 |
"Modified (comment): / 12-06-2017 / 20:41:00 / cg" |
|
265 | 589 |
! |
590 |
||
13548 | 591 |
productFromComplex:aComplex |
9128 | 592 |
"Return the product of the receiver and the argument, aComplex." |
593 |
||
594 |
| u v r i | |
|
595 |
||
596 |
u := aComplex real. |
|
597 |
v := aComplex imaginary. |
|
598 |
r := (real * u) - (imaginary * v). |
|
599 |
i := (real * v) + (imaginary * u). |
|
600 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 601 |
^ self class real:r imaginary:i |
9128 | 602 |
! |
603 |
||
20740 | 604 |
productFromFixedPoint: aFixedPoint |
21110 | 605 |
"Return the product of the receiver and the argument, aFixedPoint." |
606 |
||
20740 | 607 |
^ aFixedPoint asComplex * self |
608 |
! |
|
609 |
||
7355 | 610 |
productFromFloat: aFloat |
611 |
"Return the product of the receiver and the argument, aFloat." |
|
612 |
||
613 |
"/ ^ aFloat asComplex * self |
|
614 |
||
615 |
| u r i | |
|
616 |
||
617 |
u := aFloat. |
|
618 |
r := (real * aFloat). |
|
619 |
i := (imaginary * aFloat). |
|
620 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 621 |
^ self class real:r imaginary:i |
7355 | 622 |
|
623 |
" |
|
13548 | 624 |
(1 % 1) * 2.0 |
625 |
(1 % 1) * 0.0 |
|
7355 | 626 |
2.0 * (1 % 1) |
627 |
" |
|
265 | 628 |
! |
629 |
||
7355 | 630 |
productFromFraction: aFraction |
21110 | 631 |
"Return the product of the receiver and the argument, aFraction." |
632 |
||
7355 | 633 |
^ aFraction asComplex * self |
265 | 634 |
! |
635 |
||
7355 | 636 |
productFromInteger: anInteger |
21798 | 637 |
"sent when an integer does not know how to multiply the receiver, a complex. |
638 |
Return the product of the receiver and the argument, anInteger." |
|
21110 | 639 |
|
7355 | 640 |
^ anInteger asComplex * self |
21798 | 641 |
|
642 |
"Modified (comment): / 12-06-2017 / 20:30:20 / cg" |
|
7355 | 643 |
! |
644 |
||
13548 | 645 |
quotientFromComplex:aComplex |
9128 | 646 |
"Return the quotient of the argument, aComplex and the receiver." |
647 |
||
648 |
| denom nr ni r i | |
|
649 |
||
650 |
nr := aComplex real. |
|
651 |
ni := aComplex imaginary. |
|
652 |
denom := real * real + (imaginary * imaginary). |
|
16449 | 653 |
r := (real * nr + (imaginary * ni)) / denom. |
654 |
i := (real * ni - (imaginary * nr)) / denom. |
|
9128 | 655 |
i = 0 ifTrue:[ ^ r ]. |
16449 | 656 |
^ self class real:r imaginary:i |
9128 | 657 |
|
658 |
"/ is the stuff below better ? |
|
13548 | 659 |
"/ "Implement complex division (a + ib) / (c + id). |
9128 | 660 |
"/ Due to double dispatch, in this routine |
661 |
"/ self = (c + id) and aComplex = (a + ib)." |
|
662 |
"/ |
|
663 |
"/ | quotient denominator | |
|
664 |
"/ |
|
665 |
"/ self realPart abs >= (self imaginaryPart abs) |
|
13548 | 666 |
"/ ifTrue: [ |
9128 | 667 |
"/ quotient := self imaginaryPart / self realPart. |
668 |
"/ denominator := self realPart + (self imaginaryPart * quotient). |
|
13548 | 669 |
"/ ^ Complex |
9128 | 670 |
"/ real: (aComplex realPart + (aComplex imaginaryPart * quotient)) / denominator |
671 |
"/ imaginary: (aComplex imaginaryPart - (aComplex realPart * quotient)) / denominator ] |
|
13548 | 672 |
"/ ifFalse: [ |
9128 | 673 |
"/ quotient := self realPart / self imaginaryPart. |
674 |
"/ denominator := (self realPart * quotient) + self imaginaryPart. |
|
13548 | 675 |
"/ ^ Complex |
9128 | 676 |
"/ real: ((aComplex realPart * quotient) + aComplex imaginaryPart) / denominator |
677 |
"/ imaginary: ((aComplex imaginaryPart * quotient) - aComplex realPart) / denominator ] |
|
678 |
! |
|
679 |
||
20740 | 680 |
quotientFromFixedPoint:aFixedPoint |
681 |
"Return the quotient of the argument, aFixedPoint and the receiver." |
|
682 |
||
683 |
^ aFixedPoint asComplex / self |
|
684 |
! |
|
685 |
||
7442 | 686 |
quotientFromFloat:aFloat |
687 |
"Return the quotient of the argument, aFloat and the receiver." |
|
688 |
||
7355 | 689 |
^ aFloat asComplex / self |
265 | 690 |
! |
691 |
||
7442 | 692 |
quotientFromFraction:aFraction |
693 |
"Return the quotient of the argument, aFraction and the receiver." |
|
694 |
||
7355 | 695 |
^ aFraction asComplex / self |
696 |
! |
|
697 |
||
7442 | 698 |
quotientFromInteger:anInteger |
699 |
"Return the quotient of the argument, anInteger and the receiver." |
|
700 |
||
7355 | 701 |
^ anInteger asComplex / self |
265 | 702 |
! |
703 |
||
21933 | 704 |
raisedFromNumber:aNumber |
705 |
"see http://www.math.toronto.edu/mathnet/questionCorner/complexexp.html" |
|
706 |
||
707 |
"/ a ^ (b+i*c) = a^b * ( cos(c*ln(a)) + i*sin(c*ln(a)) ) |
|
708 |
||
709 |
|cLNa a_b| |
|
710 |
||
711 |
cLNa := imaginary * aNumber ln. |
|
712 |
a_b := aNumber raisedTo:real. |
|
713 |
||
714 |
^ Complex |
|
715 |
real:(a_b * cLNa cos) |
|
716 |
imaginary:(a_b * cLNa sin) |
|
717 |
||
21947 | 718 |
" |
719 |
2 raisedTo:(2 + 2i) |
|
720 |
2 ** (2 + 2i) |
|
721 |
" |
|
722 |
||
21933 | 723 |
"Created: / 01-07-2017 / 20:33:47 / cg" |
21947 | 724 |
"Modified (comment): / 03-07-2017 / 14:08:45 / cg" |
21933 | 725 |
! |
726 |
||
13548 | 727 |
sumFromComplex:aComplex |
9128 | 728 |
"Return the sum of the receiver and the argument, aComplex." |
729 |
||
730 |
| r i | |
|
731 |
||
732 |
r := aComplex real + real. |
|
733 |
i := aComplex imaginary + imaginary. |
|
734 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 735 |
^ self class real:r imaginary:i |
9128 | 736 |
! |
737 |
||
20740 | 738 |
sumFromFixedPoint: aFixedPoint |
21110 | 739 |
"Return the sum of the receiver and the argument, aFixedPoint." |
740 |
||
20740 | 741 |
^ aFixedPoint asComplex + self |
742 |
! |
|
743 |
||
7355 | 744 |
sumFromFloat: aFloat |
745 |
"Return the sum of the receiver and the argument, aFloat." |
|
746 |
||
747 |
"/ ^ aFloat asComplex + self |
|
748 |
||
749 |
| r | |
|
750 |
||
751 |
r := aFloat + real. |
|
752 |
imaginary = 0 ifTrue:[ ^ r ]. |
|
16449 | 753 |
^ self class real:r imaginary:imaginary |
7355 | 754 |
|
755 |
" |
|
13548 | 756 |
(1 % 1) + 1.0 |
757 |
1.0 + (1 % 1) |
|
7355 | 758 |
" |
265 | 759 |
! |
760 |
||
7355 | 761 |
sumFromFraction: aFraction |
21110 | 762 |
"Return the sum of the receiver and the argument, aFraction." |
763 |
||
7355 | 764 |
^ aFraction asComplex + self |
265 | 765 |
! |
766 |
||
7355 | 767 |
sumFromInteger: anInteger |
21110 | 768 |
"Return the sum of the receiver and the argument, anInteger." |
769 |
||
7355 | 770 |
^ anInteger asComplex + self |
265 | 771 |
! ! |
772 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
773 |
!Complex methodsFor:'mathematical functions'! |
265 | 774 |
|
775 |
angle |
|
7355 | 776 |
"Return the radian angle for this Complex number." |
265 | 777 |
|
7355 | 778 |
real < 0 ifTrue: [ |
13548 | 779 |
imaginary < 0 ifTrue: [ |
780 |
^ (imaginary / real) arcTan - Float pi |
|
781 |
]. |
|
782 |
^ Float pi + (imaginary / real) arcTan |
|
7355 | 783 |
]. |
784 |
^ (imaginary / real) arcTan |
|
785 |
||
786 |
" |
|
787 |
(1 % 1) angle radiansToDegrees |
|
788 |
" |
|
265 | 789 |
! |
790 |
||
791 |
exp |
|
7355 | 792 |
"Return the complex exponential of the receiver." |
265 | 793 |
|
7355 | 794 |
^ (imaginary cos % imaginary sin) * real exp |
265 | 795 |
! |
796 |
||
797 |
sqrt |
|
7355 | 798 |
"Return the square root of the receiver" |
799 |
||
800 |
| w quotient absReal absImag | |
|
801 |
||
13548 | 802 |
((real = 0) and: [ imaginary = 0 ]) ifTrue: [ |
16449 | 803 |
^ self class zero |
7355 | 804 |
]. |
805 |
absReal := real abs. |
|
806 |
absImag := imaginary abs. |
|
807 |
||
13548 | 808 |
absReal >= absImag ifTrue:[ |
16449 | 809 |
quotient := imaginary / real. |
810 |
w := (absReal sqrt) * (((1 + (1 + (quotient * quotient)) sqrt) / 2) sqrt) |
|
13548 | 811 |
] ifFalse: [ |
16449 | 812 |
quotient := real / imaginary. |
813 |
w := (absImag sqrt) * (((quotient abs + (1 + (quotient * quotient)) sqrt) / 2) sqrt) |
|
7355 | 814 |
]. |
265 | 815 |
|
13548 | 816 |
real >= 0 ifTrue:[ |
16449 | 817 |
^ self class real: w imaginary: (imaginary / (2 * w)) |
7355 | 818 |
]. |
13548 | 819 |
imaginary >= 0 ifTrue: [ |
16449 | 820 |
^ self class real: absImag / (2 * w) imaginary: w |
7355 | 821 |
]. |
16449 | 822 |
^ self class real: absImag / (2 * w) imaginary: -1 * w |
7355 | 823 |
! |
824 |
||
825 |
sqrt_bad |
|
826 |
"Return the square root of the receiver" |
|
265 | 827 |
|
7355 | 828 |
| u v | |
829 |
||
830 |
(imaginary = 0 and: [real >= 0]) ifTrue: [^ real sqrt]. |
|
16449 | 831 |
v := ((self abs - real) / 2) sqrt. |
7355 | 832 |
u := imaginary / 2 / v. |
16449 | 833 |
^ self class real: u imaginary: v |
7355 | 834 |
|
835 |
" |
|
13548 | 836 |
-4 asComplex sqrt |
837 |
4 asComplex sqrt |
|
7355 | 838 |
" |
839 |
" |
|
13548 | 840 |
-4 asComplex sqrt squared |
7355 | 841 |
" |
265 | 842 |
! ! |
843 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
844 |
!Complex methodsFor:'printing'! |
265 | 845 |
|
16742 | 846 |
displayOn: aGCOrStream |
847 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
|
21537 | 848 |
"/ old ST80 means: draw-yourself on a GC. |
16742 | 849 |
(aGCOrStream isStream) ifFalse:[ |
850 |
^ super displayOn:aGCOrStream |
|
851 |
]. |
|
21537 | 852 |
|
16742 | 853 |
aGCOrStream nextPut: $(. |
854 |
self realPart printOn: aGCOrStream. |
|
7355 | 855 |
self imaginaryPart >= 0 |
16742 | 856 |
ifTrue: [ aGCOrStream nextPut: $+ ] |
857 |
ifFalse: [ aGCOrStream nextPut: $- ]. |
|
858 |
self imaginaryPart abs printOn: aGCOrStream. |
|
859 |
aGCOrStream nextPutAll: 'i)' |
|
7355 | 860 |
|
861 |
" |
|
862 |
Complex real:1 imaginary:1 |
|
863 |
" |
|
21537 | 864 |
|
865 |
"Modified (format): / 22-02-2017 / 17:01:02 / cg" |
|
7355 | 866 |
! |
867 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
868 |
printOn: aStream |
7355 | 869 |
aStream nextPut: $(. |
870 |
real storeOn: aStream. |
|
871 |
aStream nextPutAll: '%'. |
|
872 |
imaginary storeOn: aStream. |
|
873 |
aStream nextPut: $). |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
874 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
875 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
876 |
printString |
7355 | 877 |
^ '(' , real printString, '%', imaginary printString, ')' |
265 | 878 |
! |
879 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
880 |
storeOn: aStream |
7355 | 881 |
self printOn:aStream |
265 | 882 |
! ! |
883 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
884 |
!Complex methodsFor:'private'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
885 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
886 |
setReal: u setImaginary: v |
7355 | 887 |
real := u. |
888 |
imaginary := v. |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
889 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
890 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
891 |
!Complex methodsFor:'testing'! |
265 | 892 |
|
893 |
isComplex |
|
7355 | 894 |
"Answer whether the receiver has an imaginary part |
895 |
(i.e. if it is a complex number). Always true here." |
|
265 | 896 |
|
7355 | 897 |
^ true |
265 | 898 |
! |
899 |
||
900 |
isReal |
|
7355 | 901 |
"Return true if this Complex number has a zero imaginary part." |
902 |
||
903 |
^ imaginary = 0 |
|
265 | 904 |
! |
905 |
||
906 |
isZero |
|
13548 | 907 |
"Answer whether 'self = self class zero'. |
7355 | 908 |
We can't use #= because #= is defined in terms of #isZero" |
265 | 909 |
|
7355 | 910 |
^real isZero and: [imaginary isZero] |
265 | 911 |
! |
912 |
||
913 |
sign |
|
7471 | 914 |
"return a new complex, consisting of the signs of the real and imaginary parts. |
915 |
Q: is this a good thing to do ?" |
|
265 | 916 |
|
16449 | 917 |
^ self class real: real sign imaginary: imaginary sign |
7355 | 918 |
! ! |
919 |
||
920 |
!Complex methodsFor:'truncation & rounding'! |
|
921 |
||
922 |
ceiling |
|
923 |
"blocked: complex numbers have no ceiling" |
|
924 |
||
925 |
^ self shouldNotImplement |
|
926 |
! |
|
927 |
||
928 |
floor |
|
929 |
"blocked: complex numbers have no floor" |
|
930 |
||
931 |
^ self shouldNotImplement |
|
265 | 932 |
! ! |
933 |
||
3637 | 934 |
!Complex class methodsFor:'documentation'! |
935 |
||
936 |
version |
|
18836 | 937 |
^ '$Header$' |
3637 | 938 |
! ! |
16449 | 939 |