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