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