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