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