author | Claus Gittinger <cg@exept.de> |
Thu, 15 Jun 2017 13:56:59 +0200 | |
changeset 21817 | 5d251e1af13a |
parent 21798 | eeac3e49a0af |
child 21873 | a2ed604af5c4 |
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 |
|
21817 | 164 |
!Complex class methodsFor:'coercing'! |
165 |
||
166 |
coerce: aNumber |
|
167 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
|
168 |
||
169 |
^ aNumber asComplex |
|
170 |
! ! |
|
171 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
172 |
!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
|
173 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
174 |
unity |
7355 | 175 |
"Answer the value which allows, for any given arithmetic value, the following to be true: |
13548 | 176 |
aNumber * aNumber class unity = aNumber |
7355 | 177 |
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
|
178 |
|
7355 | 179 |
ComplexOne isNil ifTrue:[ |
13548 | 180 |
ComplexOne := self fromReal: 1 |
7355 | 181 |
]. |
182 |
^ ComplexOne |
|
265 | 183 |
! |
184 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
185 |
zero |
7355 | 186 |
"Answer the value which allows, for any given arithmetic value, the following to be true: |
13548 | 187 |
aNumber + aNumber class zero = aNumber |
7355 | 188 |
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
|
189 |
|
7355 | 190 |
ComplexZero isNil ifTrue:[ |
13548 | 191 |
ComplexZero := self fromReal: 0 |
7355 | 192 |
]. |
193 |
^ ComplexZero |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
194 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
195 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
196 |
!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
|
197 |
|
7404 | 198 |
trapImaginary:aBlock |
199 |
"evaluate aBlock; if any DomainError occurs inside, with respect to square roots, |
|
200 |
convert the root to a complex root and proceed. |
|
201 |
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
|
202 |
|
7404 | 203 |
|send| |
204 |
||
205 |
^ ImaginaryResultError handle: [:ex | |
|
13548 | 206 |
|selector| |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
207 |
|
13548 | 208 |
send := ex parameter. |
209 |
selector := send selector. |
|
210 |
(selector = #sqrt or: [selector = #sqrtTruncated]) ifTrue: [ |
|
211 |
send receiver: send receiver asComplex. |
|
212 |
ex proceedWith: send value |
|
213 |
] ifFalse: [ |
|
214 |
ex reject |
|
215 |
] |
|
7404 | 216 |
] do: aBlock |
217 |
||
218 |
" |
|
13548 | 219 |
Complex trapImaginary: [-2 sqrt] |
7404 | 220 |
" |
221 |
||
222 |
"failing code: |
|
13548 | 223 |
|a| |
7404 | 224 |
|
13548 | 225 |
a := -2. |
226 |
(a sqrt + 5) * 17. |
|
7404 | 227 |
" |
228 |
"complex code: |
|
13548 | 229 |
|a| |
7404 | 230 |
|
13548 | 231 |
Complex trapImaginary:[ |
232 |
a := -2. |
|
233 |
(a sqrt + 5) * 2. |
|
234 |
] |
|
7404 | 235 |
" |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
236 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
237 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
238 |
!Complex methodsFor:'accessing'! |
265 | 239 |
|
240 |
imaginary |
|
7355 | 241 |
"Return the imaginary part of the complex number." |
242 |
||
243 |
^ imaginary |
|
244 |
! |
|
245 |
||
246 |
imaginaryPart |
|
247 |
"Return the imaginary part of the complex number. |
|
248 |
An alias for imaginary (for compatibility with other complex implementations)" |
|
249 |
||
250 |
^ imaginary |
|
265 | 251 |
! |
252 |
||
253 |
real |
|
7355 | 254 |
"Return the real part of the complex number." |
255 |
||
256 |
^ real |
|
257 |
! |
|
258 |
||
259 |
realPart |
|
260 |
"Return the real part of the complex number. |
|
261 |
An alias for real (for compatibility with other complex implementations)" |
|
262 |
||
263 |
^ real |
|
265 | 264 |
! ! |
265 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
266 |
!Complex methodsFor:'arithmetic'! |
265 | 267 |
|
13548 | 268 |
* aNumber |
7355 | 269 |
"Return the product of the receiver and the argument." |
265 | 270 |
|
7355 | 271 |
"/ | u v r i | |
272 |
"/ |
|
273 |
"/ aNumber isComplex ifTrue:[ |
|
274 |
"/ u := aNumber real. |
|
275 |
"/ v := aNumber imaginary. |
|
276 |
"/ r := (real * u) - (imaginary * v). |
|
277 |
"/ i := (real * v) + (imaginary * u). |
|
278 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
279 |
"/ ^ Complex real:r imaginary:i |
|
280 |
"/ ]. |
|
13548 | 281 |
^ aNumber productFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
282 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
283 |
"Modified: / 8.7.1998 / 12:12:37 / cg" |
265 | 284 |
! |
285 |
||
13548 | 286 |
+ aNumber |
7355 | 287 |
"Return the sum of the receiver and the argument." |
265 | 288 |
|
7355 | 289 |
"/ | r i | |
290 |
"/ |
|
291 |
"/ aNumber isComplex ifTrue: [ |
|
292 |
"/ r := aNumber real + real. |
|
293 |
"/ i := aNumber imaginary + imaginary. |
|
294 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
295 |
"/ ^ Complex real:r imaginary:i |
|
296 |
"/ ]. |
|
13548 | 297 |
^ aNumber sumFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
298 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
299 |
"Modified: / 8.7.1998 / 12:15:42 / cg" |
265 | 300 |
! |
301 |
||
302 |
- aNumber |
|
7355 | 303 |
"Return the difference of the receiver and the argument." |
265 | 304 |
|
7355 | 305 |
"/ | r i | |
306 |
"/ |
|
307 |
"/ aNumber isComplex ifTrue: [ |
|
308 |
"/ r := real - aNumber real. |
|
309 |
"/ i := imaginary - aNumber imaginary. |
|
310 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
311 |
"/ ^ Complex real:r imaginary:i. |
|
312 |
"/ ]. |
|
13548 | 313 |
^ aNumber differenceFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
314 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
315 |
"Modified: / 8.7.1998 / 12:15:38 / cg" |
265 | 316 |
! |
317 |
||
13548 | 318 |
/ aNumber |
7355 | 319 |
"Return the quotient of the receiver and the argument." |
265 | 320 |
|
7355 | 321 |
"/ | denom u v r i | |
322 |
"/ |
|
13548 | 323 |
"/ aNumber isComplex ifTrue:[ |
7355 | 324 |
"/ u := aNumber real. |
325 |
"/ v := aNumber imaginary. |
|
326 |
"/ denom := u * u + (v * v). |
|
327 |
"/ r := u * real + (v * imaginary) / denom. |
|
328 |
"/ i := u * imaginary - (v * real) / denom. |
|
329 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
330 |
"/ ^ Complex real:r imaginary:i |
|
331 |
"/ ]. |
|
13548 | 332 |
^ aNumber quotientFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
333 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
334 |
"Modified: / 8.7.1998 / 12:15:34 / cg" |
265 | 335 |
! |
336 |
||
337 |
abs |
|
7355 | 338 |
"Return the magnitude (or absolute value) of the complex number |
20408 | 339 |
(that's the distance from the origin in the complex plane)." |
265 | 340 |
|
7355 | 341 |
^ (real * real + (imaginary * imaginary)) sqrt |
342 |
||
343 |
" |
|
344 |
(1 % 1) abs |
|
345 |
" |
|
265 | 346 |
! |
347 |
||
348 |
conjugated |
|
7355 | 349 |
"Return the complex conjugate of this complex number |
350 |
(i.e. with imaginary part negated)." |
|
351 |
||
16449 | 352 |
^ self class |
353 |
real: real |
|
354 |
imaginary: imaginary negated |
|
7355 | 355 |
! |
356 |
||
357 |
modulus |
|
358 |
| absReal absImag multiplicand quotient | |
|
359 |
||
360 |
absReal := real abs. |
|
361 |
absImag := imaginary abs. |
|
362 |
||
13548 | 363 |
absReal >= absImag ifTrue: [ |
364 |
multiplicand := absReal. |
|
365 |
quotient := imaginary / real |
|
366 |
] ifFalse: [ |
|
367 |
multiplicand := absImag. |
|
368 |
quotient := real / imaginary |
|
7355 | 369 |
]. |
370 |
^ multiplicand * ((1 + (quotient * quotient)) sqrt) |
|
371 |
! |
|
372 |
||
373 |
negated |
|
374 |
"return a new complex with both real and imaginary parts negated" |
|
375 |
||
16449 | 376 |
^ self class |
377 |
real: real negated |
|
378 |
imaginary: imaginary negated |
|
265 | 379 |
! ! |
380 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
381 |
!Complex methodsFor:'coercing'! |
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 |
coerce: aNumber |
18836 | 384 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
385 |
||
7355 | 386 |
^ aNumber asComplex |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
387 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
388 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
389 |
generality |
7355 | 390 |
^ 150 |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
391 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
392 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
393 |
!Complex methodsFor:'comparing'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
394 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
395 |
< aNumber |
7355 | 396 |
"raises an error - complex numbers are not well ordered" |
397 |
||
398 |
^ Number |
|
13548 | 399 |
raise: #unorderedSignal |
400 |
receiver: self |
|
401 |
selector: #< |
|
402 |
arg: aNumber |
|
403 |
errorString: 'Complex numbers are not well ordered' |
|
7355 | 404 |
|
405 |
" |
|
406 |
1 < (2 % 2) |
|
407 |
(2 % 2) < 1 |
|
408 |
" |
|
6500 | 409 |
! |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
410 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
411 |
= aNumber |
7355 | 412 |
"return true, if the argument represents the same numeric value |
7471 | 413 |
as the receiver, false otherwise." |
7355 | 414 |
|
13548 | 415 |
^ aNumber equalFromComplex:self |
21798 | 416 |
|
417 |
" |
|
418 |
(Complex real:1.0 imaginary:2.0) = (Complex real:1.0 imaginary:2.0) |
|
419 |
(Complex real:1.0 imaginary:0) = 1.0 |
|
420 |
" |
|
421 |
||
422 |
"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
|
423 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
424 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
425 |
hash |
7355 | 426 |
"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
|
427 |
|
7377 | 428 |
^ (real hash) bitXor:(imaginary hash bitShift:16) |
429 |
||
430 |
" |
|
13548 | 431 |
(1+0i) hash |
7377 | 432 |
(1+1i) hash |
433 |
" |
|
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 |
!Complex methodsFor:'converting'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
437 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
438 |
asComplex |
7355 | 439 |
"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
|
440 |
|
7355 | 441 |
^ self |
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 |
asFloat |
7355 | 445 |
imaginary = 0 ifTrue: [^ real asFloat]. |
446 |
^ Number |
|
13548 | 447 |
raise: #coercionErrorSignal |
448 |
receiver: self |
|
449 |
selector: #asFloat |
|
450 |
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
|
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 |
asInteger |
7355 | 454 |
imaginary = 0 ifTrue: [^real asInteger]. |
455 |
^ Number |
|
13548 | 456 |
raise: #coercionErrorSignal |
457 |
receiver: self |
|
458 |
selector: #asInteger |
|
459 |
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
|
460 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
461 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
462 |
asPoint |
7355 | 463 |
"Return the complex number as a point." |
464 |
||
465 |
^ real @ imaginary |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
466 |
! |
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 |
reduceGeneralityIfPossible |
13548 | 469 |
"Answer the receiver transformed to a lower generality, if such a |
470 |
transformation is possible without losing information. |
|
7355 | 471 |
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
|
472 |
|
7355 | 473 |
imaginary isZero |
13548 | 474 |
ifTrue: [^ real] |
475 |
ifFalse: [^ self] |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
476 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
477 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
478 |
!Complex methodsFor:'double dispatching'! |
265 | 479 |
|
9128 | 480 |
differenceFromComplex:aComplex |
481 |
"Return the difference of the argument, aComplex and the receiver." |
|
482 |
||
483 |
| r i | |
|
484 |
||
485 |
r := aComplex real - real. |
|
486 |
i := aComplex imaginary - imaginary. |
|
487 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 488 |
^ self class real:r imaginary:i. |
9128 | 489 |
! |
490 |
||
20740 | 491 |
differenceFromFixedPoint: aFixedPoint |
21110 | 492 |
"Return the difference of the argument, aFixedPoint and the receiver." |
493 |
||
20740 | 494 |
^ aFixedPoint asComplex - self |
495 |
! |
|
496 |
||
7355 | 497 |
differenceFromFloat:aFloat |
498 |
"Return the difference of the argument, aFloat and the receiver." |
|
499 |
||
500 |
"/ ^ aFloat asComplex - self |
|
501 |
||
502 |
| r | |
|
265 | 503 |
|
7355 | 504 |
r := aFloat - real. |
505 |
imaginary = 0 ifTrue:[ ^ r ]. |
|
16449 | 506 |
^ self class real:r imaginary:imaginary negated |
7355 | 507 |
|
508 |
" |
|
13548 | 509 |
(1 % 1) - 1.0 |
510 |
1.0 - (1 % 1) |
|
7355 | 511 |
" |
265 | 512 |
! |
513 |
||
7355 | 514 |
differenceFromFraction: aFraction |
21110 | 515 |
"Return the difference of the argument, aFraction and the receiver." |
516 |
||
7355 | 517 |
^ aFraction asComplex - self |
518 |
! |
|
519 |
||
520 |
differenceFromInteger: anInteger |
|
21110 | 521 |
"Return the difference of the argument, anInteger and the receiver." |
522 |
||
7355 | 523 |
^ anInteger asComplex - self |
265 | 524 |
! |
525 |
||
7355 | 526 |
equalFromComplex:aComplex |
21798 | 527 |
"return true if aComplex represents the same number as myself" |
528 |
||
7355 | 529 |
^ (aComplex real = real) and:[aComplex imaginary = imaginary] |
21798 | 530 |
|
531 |
"Modified (comment): / 12-06-2017 / 20:41:04 / cg" |
|
265 | 532 |
! |
533 |
||
7355 | 534 |
equalFromFloat:aFloat |
21798 | 535 |
"return true if aFloat represents the same number as myself" |
536 |
||
7355 | 537 |
imaginary = 0 ifFalse:[^ false]. |
538 |
^ real = aFloat |
|
21798 | 539 |
|
540 |
"Modified (comment): / 12-06-2017 / 20:41:00 / cg" |
|
265 | 541 |
! |
542 |
||
13548 | 543 |
productFromComplex:aComplex |
9128 | 544 |
"Return the product of the receiver and the argument, aComplex." |
545 |
||
546 |
| u v r i | |
|
547 |
||
548 |
u := aComplex real. |
|
549 |
v := aComplex imaginary. |
|
550 |
r := (real * u) - (imaginary * v). |
|
551 |
i := (real * v) + (imaginary * u). |
|
552 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 553 |
^ self class real:r imaginary:i |
9128 | 554 |
! |
555 |
||
20740 | 556 |
productFromFixedPoint: aFixedPoint |
21110 | 557 |
"Return the product of the receiver and the argument, aFixedPoint." |
558 |
||
20740 | 559 |
^ aFixedPoint asComplex * self |
560 |
! |
|
561 |
||
7355 | 562 |
productFromFloat: aFloat |
563 |
"Return the product of the receiver and the argument, aFloat." |
|
564 |
||
565 |
"/ ^ aFloat asComplex * self |
|
566 |
||
567 |
| u r i | |
|
568 |
||
569 |
u := aFloat. |
|
570 |
r := (real * aFloat). |
|
571 |
i := (imaginary * aFloat). |
|
572 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 573 |
^ self class real:r imaginary:i |
7355 | 574 |
|
575 |
" |
|
13548 | 576 |
(1 % 1) * 2.0 |
577 |
(1 % 1) * 0.0 |
|
7355 | 578 |
2.0 * (1 % 1) |
579 |
" |
|
265 | 580 |
! |
581 |
||
7355 | 582 |
productFromFraction: aFraction |
21110 | 583 |
"Return the product of the receiver and the argument, aFraction." |
584 |
||
7355 | 585 |
^ aFraction asComplex * self |
265 | 586 |
! |
587 |
||
7355 | 588 |
productFromInteger: anInteger |
21798 | 589 |
"sent when an integer does not know how to multiply the receiver, a complex. |
590 |
Return the product of the receiver and the argument, anInteger." |
|
21110 | 591 |
|
7355 | 592 |
^ anInteger asComplex * self |
21798 | 593 |
|
594 |
"Modified (comment): / 12-06-2017 / 20:30:20 / cg" |
|
7355 | 595 |
! |
596 |
||
13548 | 597 |
quotientFromComplex:aComplex |
9128 | 598 |
"Return the quotient of the argument, aComplex and the receiver." |
599 |
||
600 |
| denom nr ni r i | |
|
601 |
||
602 |
nr := aComplex real. |
|
603 |
ni := aComplex imaginary. |
|
604 |
denom := real * real + (imaginary * imaginary). |
|
16449 | 605 |
r := (real * nr + (imaginary * ni)) / denom. |
606 |
i := (real * ni - (imaginary * nr)) / denom. |
|
9128 | 607 |
i = 0 ifTrue:[ ^ r ]. |
16449 | 608 |
^ self class real:r imaginary:i |
9128 | 609 |
|
610 |
"/ is the stuff below better ? |
|
13548 | 611 |
"/ "Implement complex division (a + ib) / (c + id). |
9128 | 612 |
"/ Due to double dispatch, in this routine |
613 |
"/ self = (c + id) and aComplex = (a + ib)." |
|
614 |
"/ |
|
615 |
"/ | quotient denominator | |
|
616 |
"/ |
|
617 |
"/ self realPart abs >= (self imaginaryPart abs) |
|
13548 | 618 |
"/ ifTrue: [ |
9128 | 619 |
"/ quotient := self imaginaryPart / self realPart. |
620 |
"/ denominator := self realPart + (self imaginaryPart * quotient). |
|
13548 | 621 |
"/ ^ Complex |
9128 | 622 |
"/ real: (aComplex realPart + (aComplex imaginaryPart * quotient)) / denominator |
623 |
"/ imaginary: (aComplex imaginaryPart - (aComplex realPart * quotient)) / denominator ] |
|
13548 | 624 |
"/ ifFalse: [ |
9128 | 625 |
"/ quotient := self realPart / self imaginaryPart. |
626 |
"/ denominator := (self realPart * quotient) + self imaginaryPart. |
|
13548 | 627 |
"/ ^ Complex |
9128 | 628 |
"/ real: ((aComplex realPart * quotient) + aComplex imaginaryPart) / denominator |
629 |
"/ imaginary: ((aComplex imaginaryPart * quotient) - aComplex realPart) / denominator ] |
|
630 |
! |
|
631 |
||
20740 | 632 |
quotientFromFixedPoint:aFixedPoint |
633 |
"Return the quotient of the argument, aFixedPoint and the receiver." |
|
634 |
||
635 |
^ aFixedPoint asComplex / self |
|
636 |
! |
|
637 |
||
7442 | 638 |
quotientFromFloat:aFloat |
639 |
"Return the quotient of the argument, aFloat and the receiver." |
|
640 |
||
7355 | 641 |
^ aFloat asComplex / self |
265 | 642 |
! |
643 |
||
7442 | 644 |
quotientFromFraction:aFraction |
645 |
"Return the quotient of the argument, aFraction and the receiver." |
|
646 |
||
7355 | 647 |
^ aFraction asComplex / self |
648 |
! |
|
649 |
||
7442 | 650 |
quotientFromInteger:anInteger |
651 |
"Return the quotient of the argument, anInteger and the receiver." |
|
652 |
||
7355 | 653 |
^ anInteger asComplex / self |
265 | 654 |
! |
655 |
||
13548 | 656 |
sumFromComplex:aComplex |
9128 | 657 |
"Return the sum of the receiver and the argument, aComplex." |
658 |
||
659 |
| r i | |
|
660 |
||
661 |
r := aComplex real + real. |
|
662 |
i := aComplex imaginary + imaginary. |
|
663 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 664 |
^ self class real:r imaginary:i |
9128 | 665 |
! |
666 |
||
20740 | 667 |
sumFromFixedPoint: aFixedPoint |
21110 | 668 |
"Return the sum of the receiver and the argument, aFixedPoint." |
669 |
||
20740 | 670 |
^ aFixedPoint asComplex + self |
671 |
! |
|
672 |
||
7355 | 673 |
sumFromFloat: aFloat |
674 |
"Return the sum of the receiver and the argument, aFloat." |
|
675 |
||
676 |
"/ ^ aFloat asComplex + self |
|
677 |
||
678 |
| r | |
|
679 |
||
680 |
r := aFloat + real. |
|
681 |
imaginary = 0 ifTrue:[ ^ r ]. |
|
16449 | 682 |
^ self class real:r imaginary:imaginary |
7355 | 683 |
|
684 |
" |
|
13548 | 685 |
(1 % 1) + 1.0 |
686 |
1.0 + (1 % 1) |
|
7355 | 687 |
" |
265 | 688 |
! |
689 |
||
7355 | 690 |
sumFromFraction: aFraction |
21110 | 691 |
"Return the sum of the receiver and the argument, aFraction." |
692 |
||
7355 | 693 |
^ aFraction asComplex + self |
265 | 694 |
! |
695 |
||
7355 | 696 |
sumFromInteger: anInteger |
21110 | 697 |
"Return the sum of the receiver and the argument, anInteger." |
698 |
||
7355 | 699 |
^ anInteger asComplex + self |
265 | 700 |
! ! |
701 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
702 |
!Complex methodsFor:'mathematical functions'! |
265 | 703 |
|
704 |
angle |
|
7355 | 705 |
"Return the radian angle for this Complex number." |
265 | 706 |
|
7355 | 707 |
real < 0 ifTrue: [ |
13548 | 708 |
imaginary < 0 ifTrue: [ |
709 |
^ (imaginary / real) arcTan - Float pi |
|
710 |
]. |
|
711 |
^ Float pi + (imaginary / real) arcTan |
|
7355 | 712 |
]. |
713 |
^ (imaginary / real) arcTan |
|
714 |
||
715 |
" |
|
716 |
(1 % 1) angle radiansToDegrees |
|
717 |
" |
|
265 | 718 |
! |
719 |
||
720 |
exp |
|
7355 | 721 |
"Return the complex exponential of the receiver." |
265 | 722 |
|
7355 | 723 |
^ (imaginary cos % imaginary sin) * real exp |
265 | 724 |
! |
725 |
||
726 |
sqrt |
|
7355 | 727 |
"Return the square root of the receiver" |
728 |
||
729 |
| w quotient absReal absImag | |
|
730 |
||
13548 | 731 |
((real = 0) and: [ imaginary = 0 ]) ifTrue: [ |
16449 | 732 |
^ self class zero |
7355 | 733 |
]. |
734 |
absReal := real abs. |
|
735 |
absImag := imaginary abs. |
|
736 |
||
13548 | 737 |
absReal >= absImag ifTrue:[ |
16449 | 738 |
quotient := imaginary / real. |
739 |
w := (absReal sqrt) * (((1 + (1 + (quotient * quotient)) sqrt) / 2) sqrt) |
|
13548 | 740 |
] ifFalse: [ |
16449 | 741 |
quotient := real / imaginary. |
742 |
w := (absImag sqrt) * (((quotient abs + (1 + (quotient * quotient)) sqrt) / 2) sqrt) |
|
7355 | 743 |
]. |
265 | 744 |
|
13548 | 745 |
real >= 0 ifTrue:[ |
16449 | 746 |
^ self class real: w imaginary: (imaginary / (2 * w)) |
7355 | 747 |
]. |
13548 | 748 |
imaginary >= 0 ifTrue: [ |
16449 | 749 |
^ self class real: absImag / (2 * w) imaginary: w |
7355 | 750 |
]. |
16449 | 751 |
^ self class real: absImag / (2 * w) imaginary: -1 * w |
7355 | 752 |
! |
753 |
||
754 |
sqrt_bad |
|
755 |
"Return the square root of the receiver" |
|
265 | 756 |
|
7355 | 757 |
| u v | |
758 |
||
759 |
(imaginary = 0 and: [real >= 0]) ifTrue: [^ real sqrt]. |
|
16449 | 760 |
v := ((self abs - real) / 2) sqrt. |
7355 | 761 |
u := imaginary / 2 / v. |
16449 | 762 |
^ self class real: u imaginary: v |
7355 | 763 |
|
764 |
" |
|
13548 | 765 |
-4 asComplex sqrt |
766 |
4 asComplex sqrt |
|
7355 | 767 |
" |
768 |
" |
|
13548 | 769 |
-4 asComplex sqrt squared |
7355 | 770 |
" |
265 | 771 |
! ! |
772 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
773 |
!Complex methodsFor:'printing'! |
265 | 774 |
|
16742 | 775 |
displayOn: aGCOrStream |
776 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
|
21537 | 777 |
"/ old ST80 means: draw-yourself on a GC. |
16742 | 778 |
(aGCOrStream isStream) ifFalse:[ |
779 |
^ super displayOn:aGCOrStream |
|
780 |
]. |
|
21537 | 781 |
|
16742 | 782 |
aGCOrStream nextPut: $(. |
783 |
self realPart printOn: aGCOrStream. |
|
7355 | 784 |
self imaginaryPart >= 0 |
16742 | 785 |
ifTrue: [ aGCOrStream nextPut: $+ ] |
786 |
ifFalse: [ aGCOrStream nextPut: $- ]. |
|
787 |
self imaginaryPart abs printOn: aGCOrStream. |
|
788 |
aGCOrStream nextPutAll: 'i)' |
|
7355 | 789 |
|
790 |
" |
|
791 |
Complex real:1 imaginary:1 |
|
792 |
" |
|
21537 | 793 |
|
794 |
"Modified (format): / 22-02-2017 / 17:01:02 / cg" |
|
7355 | 795 |
! |
796 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
797 |
printOn: aStream |
7355 | 798 |
aStream nextPut: $(. |
799 |
real storeOn: aStream. |
|
800 |
aStream nextPutAll: '%'. |
|
801 |
imaginary storeOn: aStream. |
|
802 |
aStream nextPut: $). |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
803 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
804 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
805 |
printString |
7355 | 806 |
^ '(' , real printString, '%', imaginary printString, ')' |
265 | 807 |
! |
808 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
809 |
storeOn: aStream |
7355 | 810 |
self printOn:aStream |
265 | 811 |
! ! |
812 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
813 |
!Complex methodsFor:'private'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
814 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
815 |
setReal: u setImaginary: v |
7355 | 816 |
real := u. |
817 |
imaginary := v. |
|
3633
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 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
820 |
!Complex methodsFor:'testing'! |
265 | 821 |
|
822 |
isComplex |
|
7355 | 823 |
"Answer whether the receiver has an imaginary part |
824 |
(i.e. if it is a complex number). Always true here." |
|
265 | 825 |
|
7355 | 826 |
^ true |
265 | 827 |
! |
828 |
||
829 |
isReal |
|
7355 | 830 |
"Return true if this Complex number has a zero imaginary part." |
831 |
||
832 |
^ imaginary = 0 |
|
265 | 833 |
! |
834 |
||
835 |
isZero |
|
13548 | 836 |
"Answer whether 'self = self class zero'. |
7355 | 837 |
We can't use #= because #= is defined in terms of #isZero" |
265 | 838 |
|
7355 | 839 |
^real isZero and: [imaginary isZero] |
265 | 840 |
! |
841 |
||
842 |
sign |
|
7471 | 843 |
"return a new complex, consisting of the signs of the real and imaginary parts. |
844 |
Q: is this a good thing to do ?" |
|
265 | 845 |
|
16449 | 846 |
^ self class real: real sign imaginary: imaginary sign |
7355 | 847 |
! ! |
848 |
||
849 |
!Complex methodsFor:'truncation & rounding'! |
|
850 |
||
851 |
ceiling |
|
852 |
"blocked: complex numbers have no ceiling" |
|
853 |
||
854 |
^ self shouldNotImplement |
|
855 |
! |
|
856 |
||
857 |
floor |
|
858 |
"blocked: complex numbers have no floor" |
|
859 |
||
860 |
^ self shouldNotImplement |
|
265 | 861 |
! ! |
862 |
||
3637 | 863 |
!Complex class methodsFor:'documentation'! |
864 |
||
865 |
version |
|
18836 | 866 |
^ '$Header$' |
3637 | 867 |
! ! |
16449 | 868 |