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