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