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