author | Claus Gittinger <cg@exept.de> |
Sun, 01 Dec 2019 05:17:27 +0100 | |
changeset 25006 | c70ecfb14931 |
parent 24936 | eaca1260a80c |
child 25062 | 1704e428aea4 |
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 |
||
23167 | 24 |
Number subclass:#Complex |
3633
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, |
|
22729 | 68 |
has been multiplied by 'i' (-1 sqrt) |
69 |
||
70 |
[Constructors:] |
|
71 |
5 i |
|
72 |
6 + 7 i. |
|
73 |
5.6 - 8 i. |
|
74 |
Complex real: 10 imaginary: 5. |
|
75 |
Complex abs: 5 arg: (Float pi / 4) |
|
76 |
||
23134 | 77 |
NOTE (from the original author): |
78 |
Although Complex seems similiar to the Smalltalk''s Number class, |
|
22729 | 79 |
it would not be a good idea to make a Complex to be a subclass of a Number because: |
80 |
- Number is subclass of Magnitude and Complex is certainly not a magnitude. |
|
81 |
Complex does not behave very well as a Magnitude. Operations such as |
|
82 |
< |
|
83 |
> |
|
84 |
<= |
|
85 |
>= |
|
86 |
do not make sense in case of complex numbers. |
|
87 |
- Methods in the following Number methods'' categories do not make sense for a Complex numbers |
|
88 |
truncation and round off |
|
89 |
testing |
|
90 |
intervals |
|
91 |
comparing |
|
92 |
||
93 |
However the following Number methods'' categories do have sense for a Complex number |
|
94 |
arithmetic (with the exception of operation |
|
95 |
// |
|
96 |
\\ |
|
97 |
quo: |
|
98 |
rem: |
|
99 |
mathematical functions |
|
100 |
||
101 |
Thus Complex is somewhat similar to a Number but it is not a subclass of it. |
|
102 |
Some operations we would like to inherit (e.g. #abs, #negated, #reciprocal) |
|
103 |
but some of the Number operations do not have sens to inherit or to overload. |
|
104 |
Classes are not always neat mechanism. |
|
105 |
||
106 |
!!!!!! We had to COPY the implementation of some methods |
|
107 |
abs |
|
108 |
negated |
|
109 |
reciprocal |
|
110 |
log: |
|
111 |
isZero |
|
112 |
reciprocal |
|
113 |
... |
|
114 |
methods from the Number class to the Complex class. |
|
115 |
Awful solution. Now I begin to appreciate Self. |
|
265 | 116 |
|
23167 | 117 |
NOTE (from porter): |
118 |
moved to Number hierarchy. |
|
119 |
Makes live of users much easier (isNumber, reading, etc) |
|
120 |
||
7355 | 121 |
[Author:] |
22729 | 122 |
Kurt Hebel (hebel@uinova.cerl.uiuc.edu) |
123 |
minor changes and double dispatching code by cg. |
|
124 |
additions (trigonometric) and fixes by cg. |
|
3637 | 125 |
" |
6500 | 126 |
! |
127 |
||
7355 | 128 |
examples |
6500 | 129 |
" |
21932 | 130 |
-25 sqrt -> error |
131 |
Complex trapImaginary:[ -25 sqrt ] -> 5i (0.0+5.0i) |
|
132 |
Complex trapImaginary:[ -25 integerSqrt ] -> 5i (0+5i) |
|
133 |
||
134 |
(Complex trapImaginary:[ -5397346292805549782720214077673687804022210808238353958670041357153884304 integerSqrt ]) |
|
135 |
squared |
|
136 |
||
137 |
1 + 3i |
|
138 |
||
139 |
Number i + 1 |
|
140 |
1 + Number i |
|
141 |
||
142 |
1i * 1i |
|
143 |
Number i * Number i |
|
144 |
||
6500 | 145 |
(5 % 7) real |
146 |
(5 % 7) imaginary |
|
147 |
(5 % 7) = 5 |
|
13548 | 148 |
(5 % 0) = 5 |
149 |
(5.0 % 0) = 5 |
|
7221 | 150 |
|
13548 | 151 |
(1 % 0) + (2 % 0) |
152 |
(1 % 0) + (0 % 2) |
|
7221 | 153 |
(1 % 0) + (2 % 3) |
154 |
||
13548 | 155 |
(1 % 0) * (2 % 0) |
156 |
(1 % 0) * (0 % 2) |
|
7221 | 157 |
(1 % 0) * (2 % 3) |
7355 | 158 |
|
159 |
(1 % 2) + 2 |
|
160 |
(1 % 2) * 2 |
|
13548 | 161 |
2 + (1 % 2) |
162 |
2 * (1 % 2) |
|
7355 | 163 |
|
21932 | 164 |
(Number i raisedTo:-3) -> Number i |
165 |
(Number i raisedTo:-2) -> -1 |
|
166 |
(Number i raisedTo:-1) -> Number i negated |
|
167 |
||
168 |
(Number i raisedTo:0) -> 1 |
|
169 |
(Number i raisedTo:1) -> Number i |
|
170 |
(Number i raisedTo:2) -> -1 |
|
171 |
(Number i raisedTo:3) -> Number i negated |
|
172 |
(Number i raisedTo:4) -> 1 |
|
173 |
(Number i raisedTo:6) -> -1 |
|
21933 | 174 |
|
175 |
3 raisedTo:Number i |
|
176 |
3 i raisedTo:Number i |
|
177 |
||
21932 | 178 |
5i * 5i -> -25 |
179 |
5i squared -> -25 |
|
6500 | 180 |
" |
3637 | 181 |
! ! |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
182 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
183 |
!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
|
184 |
|
9128 | 185 |
abs:aNumber1 arg:aNumber2 |
22729 | 186 |
^ self |
187 |
real:(aNumber1 * aNumber2 cos) |
|
188 |
imaginary:(aNumber1 * aNumber2 sin). |
|
9128 | 189 |
|
22729 | 190 |
" |
191 |
self abs:10 arg:2 -> (-4.16146836547142387+9.092974268256816954i) |
|
192 |
" |
|
9128 | 193 |
! |
194 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
195 |
fromReal: aNumber |
7355 | 196 |
"Create a new complex number from the given real number." |
197 |
||
22729 | 198 |
^ self basicNew |
199 |
setReal:aNumber setImaginary:0 |
|
21798 | 200 |
|
201 |
" |
|
202 |
Complex fromReal:1.0 |
|
203 |
" |
|
204 |
||
205 |
"Modified (comment): / 12-06-2017 / 20:42:56 / cg" |
|
7355 | 206 |
! |
207 |
||
22729 | 208 |
imaginary:v |
7355 | 209 |
"Create a new complex number with 0 as real and given imaginary parts. |
210 |
If the imaginary part is zero, return the real part of the number." |
|
211 |
||
212 |
v = 0 ifTrue: [^ 0]. |
|
22729 | 213 |
^ self basicNew setReal:0 setImaginary:v |
21798 | 214 |
|
215 |
" |
|
216 |
Complex imaginary:1.0 |
|
217 |
(0.0 % 1.0) |
|
218 |
" |
|
219 |
||
220 |
"Modified (comment): / 12-06-2017 / 20:44:51 / cg" |
|
7355 | 221 |
! |
222 |
||
22729 | 223 |
real:aNumber |
7355 | 224 |
"Create a new complex number from the given real number." |
225 |
||
22729 | 226 |
^ self basicNew setReal:aNumber setImaginary:0 |
21798 | 227 |
|
228 |
" |
|
229 |
Complex real:1.0 |
|
230 |
" |
|
231 |
||
232 |
"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
|
233 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
234 |
|
22729 | 235 |
real:u imaginary:v |
236 |
"Create a new complex number with the given real and imaginary parts. |
|
237 |
If the imaginary part is zero, return the real part of the number." |
|
7355 | 238 |
|
239 |
v = 0 ifTrue: [^ u]. |
|
22729 | 240 |
^ self basicNew setReal:u setImaginary:v |
21798 | 241 |
|
242 |
" |
|
243 |
Complex real:1.0 imaginary:2.0 |
|
244 |
(1.0 % 2.0) |
|
245 |
" |
|
246 |
||
247 |
"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
|
248 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
249 |
|
21873 | 250 |
!Complex class methodsFor:'coercing & converting'! |
21817 | 251 |
|
252 |
coerce: aNumber |
|
25006 | 253 |
"convert the argument aNumber into an instance of the receiver (class) and return it." |
21817 | 254 |
|
255 |
^ aNumber asComplex |
|
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 class methodsFor:'constants access'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
259 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
260 |
unity |
7355 | 261 |
"Answer the value which allows, for any given arithmetic value, the following to be true: |
22729 | 262 |
aNumber * aNumber class unity = aNumber |
7355 | 263 |
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
|
264 |
|
7355 | 265 |
ComplexOne isNil ifTrue:[ |
22729 | 266 |
ComplexOne := self basicNew setReal:1 setImaginary:0 |
7355 | 267 |
]. |
268 |
^ ComplexOne |
|
265 | 269 |
! |
270 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
271 |
zero |
7355 | 272 |
"Answer the value which allows, for any given arithmetic value, the following to be true: |
22729 | 273 |
aNumber + aNumber class zero = aNumber |
7355 | 274 |
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
|
275 |
|
7355 | 276 |
ComplexZero isNil ifTrue:[ |
22729 | 277 |
ComplexZero := self basicNew setReal:0 setImaginary:0 |
7355 | 278 |
]. |
279 |
^ ComplexZero |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
280 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
281 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
282 |
!Complex methodsFor:'accessing'! |
265 | 283 |
|
284 |
imaginary |
|
7355 | 285 |
"Return the imaginary part of the complex number." |
286 |
||
287 |
^ imaginary |
|
288 |
! |
|
289 |
||
290 |
imaginaryPart |
|
291 |
"Return the imaginary part of the complex number. |
|
292 |
An alias for imaginary (for compatibility with other complex implementations)" |
|
293 |
||
294 |
^ imaginary |
|
265 | 295 |
! |
296 |
||
297 |
real |
|
7355 | 298 |
"Return the real part of the complex number." |
299 |
||
300 |
^ real |
|
301 |
! |
|
302 |
||
303 |
realPart |
|
304 |
"Return the real part of the complex number. |
|
305 |
An alias for real (for compatibility with other complex implementations)" |
|
306 |
||
307 |
^ real |
|
265 | 308 |
! ! |
309 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
310 |
!Complex methodsFor:'arithmetic'! |
265 | 311 |
|
13548 | 312 |
* aNumber |
7355 | 313 |
"Return the product of the receiver and the argument." |
265 | 314 |
|
22729 | 315 |
"/ calling productFromComplex:self |
316 |
"/ is just as fast than an isComplex call |
|
317 |
"/ (unless the VM cheats) |
|
318 |
||
7355 | 319 |
"/ | u v r i | |
320 |
"/ |
|
321 |
"/ aNumber isComplex ifTrue:[ |
|
322 |
"/ u := aNumber real. |
|
323 |
"/ v := aNumber imaginary. |
|
324 |
"/ r := (real * u) - (imaginary * v). |
|
325 |
"/ i := (real * v) + (imaginary * u). |
|
326 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
327 |
"/ ^ Complex real:r imaginary:i |
|
328 |
"/ ]. |
|
13548 | 329 |
^ aNumber productFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
330 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
331 |
"Modified: / 8.7.1998 / 12:12:37 / cg" |
265 | 332 |
! |
333 |
||
13548 | 334 |
+ aNumber |
7355 | 335 |
"Return the sum of the receiver and the argument." |
265 | 336 |
|
22729 | 337 |
"/ calling sumFromComplex:self |
338 |
"/ is just as fast than an isComplex call |
|
339 |
"/ (unless the VM cheats) |
|
340 |
||
7355 | 341 |
"/ | r i | |
342 |
"/ |
|
343 |
"/ aNumber isComplex ifTrue: [ |
|
344 |
"/ r := aNumber real + real. |
|
345 |
"/ i := aNumber imaginary + imaginary. |
|
346 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
347 |
"/ ^ Complex real:r imaginary:i |
|
348 |
"/ ]. |
|
13548 | 349 |
^ aNumber sumFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
350 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
351 |
"Modified: / 8.7.1998 / 12:15:42 / cg" |
265 | 352 |
! |
353 |
||
354 |
- aNumber |
|
7355 | 355 |
"Return the difference of the receiver and the argument." |
265 | 356 |
|
22729 | 357 |
"/ calling differenceFromComplex:self |
358 |
"/ is just as fast than an isComplex call |
|
359 |
"/ (unless the VM cheats) |
|
360 |
||
7355 | 361 |
"/ | r i | |
362 |
"/ |
|
363 |
"/ aNumber isComplex ifTrue: [ |
|
364 |
"/ r := real - aNumber real. |
|
365 |
"/ i := imaginary - aNumber imaginary. |
|
366 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
367 |
"/ ^ Complex real:r imaginary:i. |
|
368 |
"/ ]. |
|
13548 | 369 |
^ aNumber differenceFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
370 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
371 |
"Modified: / 8.7.1998 / 12:15:38 / cg" |
265 | 372 |
! |
373 |
||
13548 | 374 |
/ aNumber |
7355 | 375 |
"Return the quotient of the receiver and the argument." |
265 | 376 |
|
22729 | 377 |
"/ calling quotientFromComplex:self |
378 |
"/ is just as fast than an isComplex call |
|
379 |
"/ (unless the VM cheats) |
|
380 |
||
7355 | 381 |
"/ | denom u v r i | |
382 |
"/ |
|
13548 | 383 |
"/ aNumber isComplex ifTrue:[ |
7355 | 384 |
"/ u := aNumber real. |
385 |
"/ v := aNumber imaginary. |
|
386 |
"/ denom := u * u + (v * v). |
|
387 |
"/ r := u * real + (v * imaginary) / denom. |
|
388 |
"/ i := u * imaginary - (v * real) / denom. |
|
389 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
390 |
"/ ^ Complex real:r imaginary:i |
|
391 |
"/ ]. |
|
13548 | 392 |
^ aNumber quotientFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
393 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
394 |
"Modified: / 8.7.1998 / 12:15:34 / cg" |
265 | 395 |
! |
396 |
||
397 |
abs |
|
7355 | 398 |
"Return the magnitude (or absolute value) of the complex number |
22729 | 399 |
that's the distance from zero (the origin in the complex plane)." |
265 | 400 |
|
7355 | 401 |
^ (real * real + (imaginary * imaginary)) sqrt |
402 |
||
403 |
" |
|
22729 | 404 |
(1 + 1i) abs |
405 |
" |
|
406 |
! |
|
407 |
||
408 |
absSecure |
|
409 |
"Answer the distance of the receiver from zero (0 + 0 i). |
|
410 |
Try avoiding overflow and/or underflow" |
|
411 |
||
412 |
| scale | |
|
413 |
||
414 |
scale := real abs max: imaginary abs. |
|
415 |
scale isZero ifTrue: [^ scale]. |
|
416 |
^ (self class |
|
417 |
real: real / scale |
|
418 |
imaginary: imaginary / scale) |
|
419 |
squaredNorm sqrt * scale |
|
420 |
||
421 |
" |
|
422 |
(10 + 4i) abs -> 10.770329614269 |
|
423 |
(10 + 4i) absSecure -> 10.770329614269 |
|
7355 | 424 |
" |
265 | 425 |
! |
426 |
||
427 |
conjugated |
|
7355 | 428 |
"Return the complex conjugate of this complex number |
429 |
(i.e. with imaginary part negated)." |
|
430 |
||
16449 | 431 |
^ self class |
432 |
real: real |
|
433 |
imaginary: imaginary negated |
|
7355 | 434 |
! |
435 |
||
22729 | 436 |
divideFastAndSecureBy: anObject |
437 |
"Answer the result of dividing receiver by aNumber" |
|
438 |
" Both operands are scaled to avoid arithmetic overflow. |
|
439 |
This algorithm works for a wide range of values, and it needs only three divisions. |
|
440 |
Note: #reciprocal uses #/ for devision " |
|
441 |
||
442 |
| r d newReal newImaginary | |
|
443 |
||
444 |
anObject isComplex ifTrue:[ |
|
445 |
(anObject real abs) > (anObject imaginary abs) ifTrue:[ |
|
446 |
r := anObject imaginary / anObject real. |
|
447 |
d := r*anObject imaginary + anObject real. |
|
448 |
newReal := r*imaginary + real/d. |
|
449 |
newImaginary := r negated * real + imaginary/d. |
|
450 |
] ifFalse:[ |
|
451 |
r := anObject real / anObject imaginary. |
|
452 |
d := r*anObject real + anObject imaginary. |
|
453 |
newReal := r*real + imaginary/d. |
|
454 |
newImaginary := r*imaginary - real/d. |
|
455 |
]. |
|
456 |
||
457 |
^ Complex real: newReal imaginary: newImaginary |
|
458 |
]. |
|
459 |
^ anObject adaptToComplex: self andSend: #/. |
|
460 |
! |
|
461 |
||
462 |
divideSecureBy:anObject |
|
463 |
"Answer the result of dividing receiver by aNumber" |
|
464 |
||
465 |
"Both operands are scaled to avoid arithmetic overflow. |
|
466 |
This algorithm works for a wide range of values, |
|
467 |
but it requires six divisions. |
|
468 |
#divideFastAndSecureBy: is also quite good, |
|
469 |
but it uses only 3 divisions. |
|
470 |
Note: #reciprocal uses #/ for devision" |
|
471 |
||
472 |
| s ars ais brs bis newReal newImaginary | |
|
473 |
||
474 |
anObject isComplex ifTrue:[ |
|
475 |
s := anObject real abs + anObject imaginary abs. |
|
476 |
ars := self real / s. |
|
477 |
ais := self imaginary / s. |
|
478 |
brs := anObject real / s. |
|
479 |
bis := anObject imaginary / s. |
|
480 |
s := brs squared + bis squared. |
|
481 |
||
482 |
newReal := ars*brs + (ais*bis) /s. |
|
483 |
newImaginary := ais*brs - (ars*bis)/s. |
|
484 |
^ Complex real:newReal imaginary:newImaginary |
|
485 |
]. |
|
486 |
^ anObject adaptToComplex:self andSend:#/. |
|
487 |
! |
|
488 |
||
489 |
i |
|
490 |
"Answer the result of multiplying the receiver with pure imaginary. |
|
24936 | 491 |
^ self * 1i |
22729 | 492 |
This is an obvious extension of method i implemented in Number." |
493 |
||
494 |
^ self class |
|
495 |
real: imaginary negated |
|
496 |
imaginary: real |
|
497 |
||
498 |
" |
|
24936 | 499 |
(10+4i)i -> (-4+10i) |
500 |
1i * 1i -> -1 |
|
501 |
1i i -> -1 |
|
502 |
(3+4i) raisedTo: 2 |
|
22729 | 503 |
" |
504 |
! |
|
505 |
||
7355 | 506 |
modulus |
507 |
| absReal absImag multiplicand quotient | |
|
508 |
||
509 |
absReal := real abs. |
|
510 |
absImag := imaginary abs. |
|
511 |
||
13548 | 512 |
absReal >= absImag ifTrue: [ |
513 |
multiplicand := absReal. |
|
514 |
quotient := imaginary / real |
|
515 |
] ifFalse: [ |
|
516 |
multiplicand := absImag. |
|
517 |
quotient := real / imaginary |
|
7355 | 518 |
]. |
519 |
^ multiplicand * ((1 + (quotient * quotient)) sqrt) |
|
520 |
! |
|
521 |
||
522 |
negated |
|
523 |
"return a new complex with both real and imaginary parts negated" |
|
524 |
||
16449 | 525 |
^ self class |
526 |
real: real negated |
|
527 |
imaginary: imaginary negated |
|
265 | 528 |
! ! |
529 |
||
22729 | 530 |
!Complex methodsFor:'coercing & converting'! |
531 |
||
532 |
asComplex |
|
533 |
"I am a complex - so return the receiver" |
|
534 |
||
535 |
^ self |
|
536 |
! |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
537 |
|
22729 | 538 |
asFloat |
539 |
imaginary = 0 ifTrue: [^ real asFloat]. |
|
540 |
^ Number |
|
541 |
raise: #coercionErrorSignal |
|
542 |
receiver: self |
|
543 |
selector: #asFloat |
|
544 |
errorString: 'Can''t coerce an instance of Complex to a Float' |
|
545 |
! |
|
21873 | 546 |
|
22729 | 547 |
asInteger |
548 |
imaginary = 0 ifTrue: [^real asInteger]. |
|
549 |
^ Number |
|
550 |
raise: #coercionErrorSignal |
|
551 |
receiver: self |
|
552 |
selector: #asInteger |
|
553 |
errorString: 'Can''t coerce an instance of Complex to an Integer' |
|
554 |
! |
|
555 |
||
556 |
asPoint |
|
557 |
"Return the complex number as a point." |
|
558 |
||
559 |
^ real @ imaginary |
|
560 |
! |
|
21873 | 561 |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
562 |
coerce: aNumber |
18836 | 563 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
564 |
||
7355 | 565 |
^ aNumber asComplex |
22729 | 566 |
! |
567 |
||
568 |
generality |
|
569 |
^ 150 |
|
570 |
! |
|
571 |
||
572 |
reduceGeneralityIfPossible |
|
573 |
"Answer the receiver transformed to a lower generality, if such a |
|
574 |
transformation is possible without losing information. |
|
575 |
If not, answer the receiver" |
|
576 |
||
577 |
imaginary isZero |
|
578 |
ifTrue: [^ real] |
|
579 |
ifFalse: [^ self] |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
580 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
581 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
582 |
!Complex methodsFor:'comparing'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
583 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
584 |
< aNumber |
7355 | 585 |
"raises an error - complex numbers are not well ordered" |
586 |
||
587 |
^ Number |
|
13548 | 588 |
raise: #unorderedSignal |
589 |
receiver: self |
|
590 |
selector: #< |
|
591 |
arg: aNumber |
|
592 |
errorString: 'Complex numbers are not well ordered' |
|
7355 | 593 |
|
594 |
" |
|
595 |
1 < (2 % 2) |
|
596 |
(2 % 2) < 1 |
|
597 |
" |
|
6500 | 598 |
! |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
599 |
|
24154 | 600 |
= anObject |
7355 | 601 |
"return true, if the argument represents the same numeric value |
7471 | 602 |
as the receiver, false otherwise." |
7355 | 603 |
|
24154 | 604 |
anObject class == self class ifTrue:[ |
605 |
^ (real = anObject real) and:[ (imaginary = anObject imaginary)] |
|
606 |
]. |
|
607 |
^ anObject equalFromComplex:self |
|
21798 | 608 |
|
609 |
" |
|
610 |
(Complex real:1.0 imaginary:2.0) = (Complex real:1.0 imaginary:2.0) |
|
611 |
(Complex real:1.0 imaginary:0) = 1.0 |
|
612 |
" |
|
613 |
||
614 |
"Modified (comment): / 12-06-2017 / 20:43:41 / cg" |
|
24154 | 615 |
"Modified: / 26-05-2019 / 10:04:33 / Claus Gittinger" |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
616 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
617 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
618 |
hash |
22729 | 619 |
"Hash is implemented because = is implemented." |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
620 |
|
7377 | 621 |
^ (real hash) bitXor:(imaginary hash bitShift:16) |
622 |
||
623 |
" |
|
13548 | 624 |
(1+0i) hash |
7377 | 625 |
(1+1i) hash |
626 |
" |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
627 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
628 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
629 |
!Complex methodsFor:'double dispatching'! |
265 | 630 |
|
9128 | 631 |
differenceFromComplex:aComplex |
632 |
"Return the difference of the argument, aComplex and the receiver." |
|
633 |
||
634 |
| r i | |
|
635 |
||
636 |
r := aComplex real - real. |
|
637 |
i := aComplex imaginary - imaginary. |
|
638 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 639 |
^ self class real:r imaginary:i. |
9128 | 640 |
! |
641 |
||
20740 | 642 |
differenceFromFixedPoint: aFixedPoint |
21110 | 643 |
"Return the difference of the argument, aFixedPoint and the receiver." |
644 |
||
20740 | 645 |
^ aFixedPoint asComplex - self |
646 |
! |
|
647 |
||
7355 | 648 |
differenceFromFloat:aFloat |
649 |
"Return the difference of the argument, aFloat and the receiver." |
|
650 |
||
651 |
"/ ^ aFloat asComplex - self |
|
652 |
||
653 |
| r | |
|
265 | 654 |
|
7355 | 655 |
r := aFloat - real. |
656 |
imaginary = 0 ifTrue:[ ^ r ]. |
|
16449 | 657 |
^ self class real:r imaginary:imaginary negated |
7355 | 658 |
|
659 |
" |
|
13548 | 660 |
(1 % 1) - 1.0 |
661 |
1.0 - (1 % 1) |
|
7355 | 662 |
" |
265 | 663 |
! |
664 |
||
7355 | 665 |
differenceFromFraction: aFraction |
21110 | 666 |
"Return the difference of the argument, aFraction and the receiver." |
667 |
||
7355 | 668 |
^ aFraction asComplex - self |
669 |
! |
|
670 |
||
671 |
differenceFromInteger: anInteger |
|
21110 | 672 |
"Return the difference of the argument, anInteger and the receiver." |
673 |
||
7355 | 674 |
^ anInteger asComplex - self |
265 | 675 |
! |
676 |
||
7355 | 677 |
equalFromComplex:aComplex |
21798 | 678 |
"return true if aComplex represents the same number as myself" |
679 |
||
7355 | 680 |
^ (aComplex real = real) and:[aComplex imaginary = imaginary] |
21798 | 681 |
|
682 |
"Modified (comment): / 12-06-2017 / 20:41:04 / cg" |
|
265 | 683 |
! |
684 |
||
7355 | 685 |
equalFromFloat:aFloat |
21798 | 686 |
"return true if aFloat represents the same number as myself" |
687 |
||
7355 | 688 |
imaginary = 0 ifFalse:[^ false]. |
689 |
^ real = aFloat |
|
21798 | 690 |
|
691 |
"Modified (comment): / 12-06-2017 / 20:41:00 / cg" |
|
265 | 692 |
! |
693 |
||
13548 | 694 |
productFromComplex:aComplex |
9128 | 695 |
"Return the product of the receiver and the argument, aComplex." |
696 |
||
697 |
| u v r i | |
|
698 |
||
699 |
u := aComplex real. |
|
700 |
v := aComplex imaginary. |
|
701 |
r := (real * u) - (imaginary * v). |
|
702 |
i := (real * v) + (imaginary * u). |
|
703 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 704 |
^ self class real:r imaginary:i |
24935 | 705 |
|
706 |
" |
|
707 |
(4 + 3i) * (2 + 5i) -> (-7+26i) |
|
708 |
(3 + 2i) * (2 + 1i) -> (4+7i) |
|
709 |
" |
|
9128 | 710 |
! |
711 |
||
20740 | 712 |
productFromFixedPoint: aFixedPoint |
21110 | 713 |
"Return the product of the receiver and the argument, aFixedPoint." |
714 |
||
20740 | 715 |
^ aFixedPoint asComplex * self |
716 |
! |
|
717 |
||
7355 | 718 |
productFromFloat: aFloat |
719 |
"Return the product of the receiver and the argument, aFloat." |
|
720 |
||
721 |
"/ ^ aFloat asComplex * self |
|
722 |
||
723 |
| u r i | |
|
724 |
||
725 |
u := aFloat. |
|
726 |
r := (real * aFloat). |
|
727 |
i := (imaginary * aFloat). |
|
728 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 729 |
^ self class real:r imaginary:i |
7355 | 730 |
|
731 |
" |
|
13548 | 732 |
(1 % 1) * 2.0 |
733 |
(1 % 1) * 0.0 |
|
7355 | 734 |
2.0 * (1 % 1) |
735 |
" |
|
265 | 736 |
! |
737 |
||
7355 | 738 |
productFromFraction: aFraction |
21110 | 739 |
"Return the product of the receiver and the argument, aFraction." |
740 |
||
7355 | 741 |
^ aFraction asComplex * self |
265 | 742 |
! |
743 |
||
7355 | 744 |
productFromInteger: anInteger |
21798 | 745 |
"sent when an integer does not know how to multiply the receiver, a complex. |
746 |
Return the product of the receiver and the argument, anInteger." |
|
21110 | 747 |
|
7355 | 748 |
^ anInteger asComplex * self |
21798 | 749 |
|
750 |
"Modified (comment): / 12-06-2017 / 20:30:20 / cg" |
|
7355 | 751 |
! |
752 |
||
13548 | 753 |
quotientFromComplex:aComplex |
9128 | 754 |
"Return the quotient of the argument, aComplex and the receiver." |
755 |
||
756 |
| denom nr ni r i | |
|
757 |
||
758 |
nr := aComplex real. |
|
759 |
ni := aComplex imaginary. |
|
760 |
denom := real * real + (imaginary * imaginary). |
|
16449 | 761 |
r := (real * nr + (imaginary * ni)) / denom. |
762 |
i := (real * ni - (imaginary * nr)) / denom. |
|
9128 | 763 |
i = 0 ifTrue:[ ^ r ]. |
16449 | 764 |
^ self class real:r imaginary:i |
9128 | 765 |
|
766 |
"/ is the stuff below better ? |
|
13548 | 767 |
"/ "Implement complex division (a + ib) / (c + id). |
9128 | 768 |
"/ Due to double dispatch, in this routine |
769 |
"/ self = (c + id) and aComplex = (a + ib)." |
|
770 |
"/ |
|
771 |
"/ | quotient denominator | |
|
772 |
"/ |
|
773 |
"/ self realPart abs >= (self imaginaryPart abs) |
|
13548 | 774 |
"/ ifTrue: [ |
9128 | 775 |
"/ quotient := self imaginaryPart / self realPart. |
776 |
"/ denominator := self realPart + (self imaginaryPart * quotient). |
|
13548 | 777 |
"/ ^ Complex |
9128 | 778 |
"/ real: (aComplex realPart + (aComplex imaginaryPart * quotient)) / denominator |
779 |
"/ imaginary: (aComplex imaginaryPart - (aComplex realPart * quotient)) / denominator ] |
|
13548 | 780 |
"/ ifFalse: [ |
9128 | 781 |
"/ quotient := self realPart / self imaginaryPart. |
782 |
"/ denominator := (self realPart * quotient) + self imaginaryPart. |
|
13548 | 783 |
"/ ^ Complex |
9128 | 784 |
"/ real: ((aComplex realPart * quotient) + aComplex imaginaryPart) / denominator |
785 |
"/ imaginary: ((aComplex imaginaryPart * quotient) - aComplex realPart) / denominator ] |
|
786 |
! |
|
787 |
||
20740 | 788 |
quotientFromFixedPoint:aFixedPoint |
789 |
"Return the quotient of the argument, aFixedPoint and the receiver." |
|
790 |
||
791 |
^ aFixedPoint asComplex / self |
|
792 |
! |
|
793 |
||
7442 | 794 |
quotientFromFloat:aFloat |
795 |
"Return the quotient of the argument, aFloat and the receiver." |
|
796 |
||
7355 | 797 |
^ aFloat asComplex / self |
265 | 798 |
! |
799 |
||
7442 | 800 |
quotientFromFraction:aFraction |
801 |
"Return the quotient of the argument, aFraction and the receiver." |
|
802 |
||
7355 | 803 |
^ aFraction asComplex / self |
804 |
! |
|
805 |
||
7442 | 806 |
quotientFromInteger:anInteger |
807 |
"Return the quotient of the argument, anInteger and the receiver." |
|
808 |
||
7355 | 809 |
^ anInteger asComplex / self |
265 | 810 |
! |
811 |
||
23644 | 812 |
raisedFromFloat:aNumber |
813 |
^ self raisedFromNumber:aNumber |
|
814 |
||
815 |
" |
|
816 |
2 raisedTo:(2 + 2i) |
|
817 |
2 ** (2 + 2i) |
|
818 |
||
819 |
2.0 raisedTo:(2 + 2i) |
|
820 |
2.0 ** (2 + 2i) |
|
821 |
" |
|
822 |
||
823 |
"Created: / 26-01-2019 / 10:56:06 / Claus Gittinger" |
|
824 |
! |
|
825 |
||
21933 | 826 |
raisedFromNumber:aNumber |
827 |
"see http://www.math.toronto.edu/mathnet/questionCorner/complexexp.html" |
|
828 |
||
829 |
"/ a ^ (b+i*c) = a^b * ( cos(c*ln(a)) + i*sin(c*ln(a)) ) |
|
830 |
||
831 |
|cLNa a_b| |
|
832 |
||
833 |
cLNa := imaginary * aNumber ln. |
|
834 |
a_b := aNumber raisedTo:real. |
|
835 |
||
836 |
^ Complex |
|
837 |
real:(a_b * cLNa cos) |
|
838 |
imaginary:(a_b * cLNa sin) |
|
839 |
||
21947 | 840 |
" |
841 |
2 raisedTo:(2 + 2i) |
|
842 |
2 ** (2 + 2i) |
|
843 |
" |
|
844 |
||
21933 | 845 |
"Created: / 01-07-2017 / 20:33:47 / cg" |
21947 | 846 |
"Modified (comment): / 03-07-2017 / 14:08:45 / cg" |
21933 | 847 |
! |
848 |
||
13548 | 849 |
sumFromComplex:aComplex |
9128 | 850 |
"Return the sum of the receiver and the argument, aComplex." |
851 |
||
852 |
| r i | |
|
853 |
||
854 |
r := aComplex real + real. |
|
855 |
i := aComplex imaginary + imaginary. |
|
856 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 857 |
^ self class real:r imaginary:i |
9128 | 858 |
! |
859 |
||
20740 | 860 |
sumFromFixedPoint: aFixedPoint |
21110 | 861 |
"Return the sum of the receiver and the argument, aFixedPoint." |
862 |
||
20740 | 863 |
^ aFixedPoint asComplex + self |
864 |
! |
|
865 |
||
7355 | 866 |
sumFromFloat: aFloat |
867 |
"Return the sum of the receiver and the argument, aFloat." |
|
868 |
||
869 |
"/ ^ aFloat asComplex + self |
|
870 |
||
871 |
| r | |
|
872 |
||
873 |
r := aFloat + real. |
|
874 |
imaginary = 0 ifTrue:[ ^ r ]. |
|
16449 | 875 |
^ self class real:r imaginary:imaginary |
7355 | 876 |
|
877 |
" |
|
13548 | 878 |
(1 % 1) + 1.0 |
879 |
1.0 + (1 % 1) |
|
7355 | 880 |
" |
265 | 881 |
! |
882 |
||
7355 | 883 |
sumFromFraction: aFraction |
21110 | 884 |
"Return the sum of the receiver and the argument, aFraction." |
885 |
||
7355 | 886 |
^ aFraction asComplex + self |
265 | 887 |
! |
888 |
||
7355 | 889 |
sumFromInteger: anInteger |
21110 | 890 |
"Return the sum of the receiver and the argument, anInteger." |
891 |
||
7355 | 892 |
^ anInteger asComplex + self |
265 | 893 |
! ! |
894 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
895 |
!Complex methodsFor:'mathematical functions'! |
265 | 896 |
|
897 |
angle |
|
7355 | 898 |
"Return the radian angle for this Complex number." |
265 | 899 |
|
7355 | 900 |
real < 0 ifTrue: [ |
13548 | 901 |
imaginary < 0 ifTrue: [ |
902 |
^ (imaginary / real) arcTan - Float pi |
|
903 |
]. |
|
904 |
^ Float pi + (imaginary / real) arcTan |
|
7355 | 905 |
]. |
906 |
^ (imaginary / real) arcTan |
|
907 |
||
908 |
" |
|
909 |
(1 % 1) angle radiansToDegrees |
|
910 |
" |
|
265 | 911 |
! |
912 |
||
22729 | 913 |
arg |
914 |
"Answer the argument of the receiver." |
|
915 |
||
916 |
self isZero ifTrue: [self error: 'zero has no argument.']. |
|
917 |
^ imaginary arcTan:real |
|
918 |
! |
|
919 |
||
265 | 920 |
exp |
7355 | 921 |
"Return the complex exponential of the receiver." |
265 | 922 |
|
22729 | 923 |
^ (Complex |
924 |
real:imaginary cos |
|
925 |
imaginary:imaginary sin) * real exp |
|
926 |
||
927 |
" |
|
928 |
(10+4i) exp -> (-14397.45885694595768-16669.6842763244522i) |
|
929 |
" |
|
930 |
! |
|
931 |
||
932 |
ln |
|
933 |
"Answer the natural log of the receiver." |
|
934 |
||
935 |
^ self abs ln + self arg i |
|
936 |
! |
|
937 |
||
938 |
log:base |
|
939 |
"Answer the log base aNumber of the receiver." |
|
940 |
||
941 |
^ self ln / base ln |
|
265 | 942 |
! |
943 |
||
944 |
sqrt |
|
7355 | 945 |
"Return the square root of the receiver" |
946 |
||
947 |
| w quotient absReal absImag | |
|
948 |
||
13548 | 949 |
((real = 0) and: [ imaginary = 0 ]) ifTrue: [ |
16449 | 950 |
^ self class zero |
7355 | 951 |
]. |
952 |
absReal := real abs. |
|
953 |
absImag := imaginary abs. |
|
954 |
||
13548 | 955 |
absReal >= absImag ifTrue:[ |
16449 | 956 |
quotient := imaginary / real. |
957 |
w := (absReal sqrt) * (((1 + (1 + (quotient * quotient)) sqrt) / 2) sqrt) |
|
13548 | 958 |
] ifFalse: [ |
16449 | 959 |
quotient := real / imaginary. |
960 |
w := (absImag sqrt) * (((quotient abs + (1 + (quotient * quotient)) sqrt) / 2) sqrt) |
|
7355 | 961 |
]. |
265 | 962 |
|
13548 | 963 |
real >= 0 ifTrue:[ |
16449 | 964 |
^ self class real: w imaginary: (imaginary / (2 * w)) |
7355 | 965 |
]. |
13548 | 966 |
imaginary >= 0 ifTrue: [ |
16449 | 967 |
^ self class real: absImag / (2 * w) imaginary: w |
7355 | 968 |
]. |
16449 | 969 |
^ self class real: absImag / (2 * w) imaginary: -1 * w |
7355 | 970 |
! |
971 |
||
972 |
sqrt_bad |
|
973 |
"Return the square root of the receiver" |
|
265 | 974 |
|
7355 | 975 |
| u v | |
976 |
||
977 |
(imaginary = 0 and: [real >= 0]) ifTrue: [^ real sqrt]. |
|
16449 | 978 |
v := ((self abs - real) / 2) sqrt. |
7355 | 979 |
u := imaginary / 2 / v. |
16449 | 980 |
^ self class real: u imaginary: v |
7355 | 981 |
|
982 |
" |
|
13548 | 983 |
-4 asComplex sqrt |
984 |
4 asComplex sqrt |
|
7355 | 985 |
" |
986 |
" |
|
13548 | 987 |
-4 asComplex sqrt squared |
7355 | 988 |
" |
22729 | 989 |
! |
990 |
||
991 |
squaredNorm |
|
992 |
"Answer the square of receiver's norm." |
|
993 |
||
994 |
^real * real + (imaginary * imaginary) |
|
265 | 995 |
! ! |
996 |
||
23643 | 997 |
!Complex methodsFor:'printing & storing'! |
265 | 998 |
|
16742 | 999 |
displayOn: aGCOrStream |
1000 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
|
21537 | 1001 |
"/ old ST80 means: draw-yourself on a GC. |
16742 | 1002 |
(aGCOrStream isStream) ifFalse:[ |
1003 |
^ super displayOn:aGCOrStream |
|
1004 |
]. |
|
21537 | 1005 |
|
16742 | 1006 |
aGCOrStream nextPut: $(. |
1007 |
self realPart printOn: aGCOrStream. |
|
7355 | 1008 |
self imaginaryPart >= 0 |
16742 | 1009 |
ifTrue: [ aGCOrStream nextPut: $+ ] |
1010 |
ifFalse: [ aGCOrStream nextPut: $- ]. |
|
1011 |
self imaginaryPart abs printOn: aGCOrStream. |
|
1012 |
aGCOrStream nextPutAll: 'i)' |
|
7355 | 1013 |
|
1014 |
" |
|
1015 |
Complex real:1 imaginary:1 |
|
23134 | 1016 |
(Complex real:1 imaginary:1) printString |
1017 |
(Complex real:1 imaginary:1) displayString |
|
7355 | 1018 |
" |
21537 | 1019 |
|
1020 |
"Modified (format): / 22-02-2017 / 17:01:02 / cg" |
|
23134 | 1021 |
"Modified (comment): / 26-06-2018 / 21:00:28 / Claus Gittinger" |
7355 | 1022 |
! |
1023 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1024 |
printOn: aStream |
7355 | 1025 |
aStream nextPut: $(. |
1026 |
real storeOn: aStream. |
|
1027 |
aStream nextPutAll: '%'. |
|
1028 |
imaginary storeOn: aStream. |
|
1029 |
aStream nextPut: $). |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1030 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1031 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1032 |
printString |
7355 | 1033 |
^ '(' , real printString, '%', imaginary printString, ')' |
265 | 1034 |
! |
1035 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1036 |
storeOn: aStream |
7355 | 1037 |
self printOn:aStream |
265 | 1038 |
! ! |
1039 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1040 |
!Complex methodsFor:'private'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1041 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1042 |
setReal: u setImaginary: v |
7355 | 1043 |
real := u. |
1044 |
imaginary := v. |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1045 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1046 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1047 |
!Complex methodsFor:'testing'! |
265 | 1048 |
|
1049 |
isComplex |
|
7355 | 1050 |
"Answer whether the receiver has an imaginary part |
1051 |
(i.e. if it is a complex number). Always true here." |
|
265 | 1052 |
|
7355 | 1053 |
^ true |
265 | 1054 |
! |
1055 |
||
1056 |
isReal |
|
7355 | 1057 |
"Return true if this Complex number has a zero imaginary part." |
1058 |
||
1059 |
^ imaginary = 0 |
|
265 | 1060 |
! |
1061 |
||
1062 |
isZero |
|
13548 | 1063 |
"Answer whether 'self = self class zero'. |
7355 | 1064 |
We can't use #= because #= is defined in terms of #isZero" |
265 | 1065 |
|
7355 | 1066 |
^real isZero and: [imaginary isZero] |
265 | 1067 |
! |
1068 |
||
1069 |
sign |
|
22729 | 1070 |
imaginary isZero ifTrue:[^ real sign]. |
1071 |
^ self / self abs |
|
1072 |
||
1073 |
"/ squeak does: |
|
1074 |
"/ ^ real sign |
|
1075 |
"/ |
|
1076 |
"/ the old code was: |
|
1077 |
"/ return a new complex, consisting of the signs of the real and imaginary parts. |
|
1078 |
"/ Q: is this a good thing to do ? |
|
1079 |
"/ cg: no |
|
1080 |
||
1081 |
"/ ^ self class real:(real sign) imaginary:(imaginary sign) |
|
1082 |
||
1083 |
" this is consistent with Wolfram's output: |
|
1084 |
||
1085 |
(Complex new setReal:1 setImaginary:0) sign -> 1 |
|
1086 |
(Complex new setReal:-1 setImaginary:0) sign -> -1 |
|
1087 |
(Complex new setReal:0 setImaginary:0) sign -> 0 |
|
1088 |
||
1089 |
(Complex new setReal:1 setImaginary:1) sign -> (0.707106781186547+0.707106781186547i) |
|
1090 |
(Complex new setReal:0 setImaginary:1) sign -> (0.0+1.0i) |
|
1091 |
(Complex new setReal:-1 setImaginary:1) sign -> (-0.707106781186547+0.707106781186547i) |
|
1092 |
||
1093 |
(Complex new setReal:1 setImaginary:-1) sign -> (0.707106781186547-0.707106781186547i) |
|
1094 |
(Complex new setReal:0 setImaginary:-1) sign -> (0.0-1.0i) |
|
1095 |
(Complex new setReal:-1 setImaginary:-1) sign -> (-0.707106781186547-0.707106781186547i) |
|
1096 |
" |
|
1097 |
! ! |
|
1098 |
||
1099 |
!Complex methodsFor:'trigonometric functions'! |
|
1100 |
||
1101 |
arcCos |
|
1102 |
"Answer the arc cosine of the receiver. |
|
1103 |
This is the inverse function of cos." |
|
1104 |
||
1105 |
| x y tmp sh2y shy delta ch2y chy | |
|
1106 |
||
1107 |
imaginary = 0 ifTrue: [ |
|
1108 |
real abs > 1 ifTrue:[ |
|
1109 |
x := real < 0 ifTrue: [Float pi] |
|
1110 |
ifFalse: [0]. |
|
1111 |
y := real copySignTo: real abs arcCosh. |
|
1112 |
^ self class real: x imaginary: y |
|
1113 |
] ifFalse: [ |
|
1114 |
^self class real: real arcCos imaginary: 0 |
|
1115 |
] |
|
1116 |
]. |
|
1117 |
tmp := self squaredNorm - 1 / 2. |
|
1118 |
delta := tmp squared + imaginary squared. |
|
1119 |
sh2y := tmp + delta sqrt. |
|
1120 |
shy := sh2y sqrt. |
|
1121 |
ch2y := 1 + sh2y. |
|
1122 |
chy := ch2y sqrt. |
|
1123 |
y := imaginary copySignTo: shy arSinh. |
|
1124 |
x := (real / chy) arcCos. |
|
1125 |
^ self class real: x imaginary: y negated |
|
1126 |
||
1127 |
" |
|
1128 |
(10+4i) cos (-22.91356068209214107-14.84629106966035727i) |
|
1129 |
(10+4i) cos arcCos |
|
1130 |
" |
|
1131 |
! |
|
1132 |
||
1133 |
arcCosh |
|
1134 |
"Answer the receiver's area hyperbolic cosine. |
|
1135 |
That is the inverse function of cosh. |
|
1136 |
Some possible implementations: |
|
1137 |
^imaginary > 0 |
|
1138 |
ifTrue: [(self + (self * self - 1) sqrt) ln] |
|
1139 |
ifFalse: [(self + (self * self - 1) sqrt) ln negated] |
|
1140 |
^self arcCos i |
|
1141 |
This implementation provides an answer with a positive real part. |
|
1142 |
It also avoids creating intermediate Complex." |
|
1143 |
||
1144 |
| x y tmp sh2x shx delta ch2x chx | |
|
1145 |
||
1146 |
imaginary = 0 ifTrue: [ |
|
1147 |
real abs > 1 ifTrue:[ |
|
1148 |
y := real < 0 ifTrue: [Float pi] |
|
1149 |
ifFalse: [0]. |
|
1150 |
x := real abs arcCosh. |
|
1151 |
^ self class real: x imaginary: y |
|
1152 |
] ifFalse: [ |
|
1153 |
^ self class real: 0 imaginary: real arcCos |
|
1154 |
] |
|
1155 |
]. |
|
1156 |
tmp := self squaredNorm - 1 / 2. |
|
1157 |
delta := tmp squared + imaginary squared. |
|
1158 |
sh2x := tmp + delta sqrt. |
|
1159 |
shx := sh2x sqrt. |
|
1160 |
ch2x := 1 + sh2x. |
|
1161 |
chx := ch2x sqrt. |
|
1162 |
x := shx arSinh. |
|
1163 |
y := imaginary copySignTo: (real / chx) arcCos. |
|
1164 |
^ self class real: x imaginary: y |
|
1165 |
||
1166 |
" |
|
1167 |
(10 + 4i) arcCosh |
|
1168 |
" |
|
1169 |
! |
|
1170 |
||
1171 |
arcSin |
|
1172 |
"Answer the arc sine of the receiver. |
|
1173 |
This is the inverse function of sin." |
|
1174 |
||
1175 |
| x y tmp delta sh2y shy ch2y chy | |
|
1176 |
||
1177 |
imaginary = 0 ifTrue:[ |
|
1178 |
real abs > 1 ifTrue:[ |
|
1179 |
x := Float pi / 2 * real sign. |
|
1180 |
y := (real copySignTo: real abs arcCosh) negated. |
|
1181 |
^ self class real: x imaginary: y |
|
1182 |
] ifFalse: [ |
|
1183 |
^ self class real: real arcSin imaginary: 0 |
|
1184 |
] |
|
1185 |
]. |
|
1186 |
tmp := (self squaredNorm - 1) / 2. |
|
1187 |
delta := tmp squared + imaginary squared. |
|
1188 |
sh2y := tmp + delta sqrt. |
|
1189 |
shy := sh2y sqrt. |
|
1190 |
ch2y := 1 + sh2y. |
|
1191 |
chy := ch2y sqrt. |
|
1192 |
y := imaginary copySignTo: shy arcSinh. |
|
1193 |
x := (real / chy) arcSin. |
|
1194 |
^self class real: x imaginary: y |
|
1195 |
||
1196 |
" |
|
1197 |
(10 + 4i) sin -> (-14.85625516387525498-22.89819255096375875i) |
|
1198 |
" |
|
1199 |
! |
|
1200 |
||
1201 |
arcSinh |
|
1202 |
"Answer receiver's area hyperbolic sine. |
|
1203 |
That is the inverse function of sinh." |
|
1204 |
||
1205 |
"Some possible implementation: |
|
1206 |
||
1207 |
^imaginary * real < 0 |
|
1208 |
ifTrue: [(self + (self * self + 1) sqrt) ln] |
|
1209 |
ifFalse: [(self - (self * self + 1) sqrt) ln]" |
|
1210 |
||
1211 |
^self i arcSin i negated |
|
1212 |
||
1213 |
" |
|
24936 | 1214 |
(10 + 4i) sinh -> (-7198.729413635291604-8334.842155341616165i) |
22729 | 1215 |
(10 + 4i) sinh arcSinh -> (-10.0-0.8584073464102067614i) |
1216 |
" |
|
1217 |
! |
|
265 | 1218 |
|
22729 | 1219 |
arcTan |
1220 |
"Answer the arc tangent of the receiver. |
|
1221 |
This is the inverse function of tan." |
|
1222 |
||
1223 |
| r2 | |
|
1224 |
||
1225 |
r2 := self squaredNorm. |
|
1226 |
^ self class |
|
1227 |
real: (real * 2 arcTan: 1 - r2) / 2 |
|
1228 |
imaginary: ((r2 + (imaginary * 2) + 1) / (r2 - (imaginary * 2) + 1)) ln / 4 |
|
1229 |
||
1230 |
" |
|
1231 |
(1+4i) tan arcTan -> (1.000000000000000043+4.000000000000004772i) |
|
1232 |
(10+4i) tan arcTan -> (0.5752220392306203171+4.00000000000000004i) |
|
1233 |
" |
|
1234 |
! |
|
1235 |
||
1236 |
arcTan:denominator |
|
1237 |
"Answer the four quadrants arc tangent of receiver over denominator." |
|
1238 |
||
1239 |
|res| |
|
1240 |
||
1241 |
denominator isZero ifTrue:[ |
|
1242 |
self isZero ifTrue:[ |
|
1243 |
"shouldn't it be an error ? ^DomainError signal: '0 arcTan: 0'" |
|
1244 |
^ self class real:0 imaginary:0 |
|
1245 |
] ifFalse:[ |
|
1246 |
^ self class real:Float pi / (real copySignTo:2) imaginary:0 |
|
1247 |
] |
|
1248 |
]. |
|
1249 |
||
1250 |
res := (self / denominator) arcTan. |
|
1251 |
denominator real < 0 ifTrue:[ |
|
1252 |
res := res + Float pi |
|
1253 |
]. |
|
1254 |
res real > Float pi ifTrue:[ |
|
1255 |
res := res - (Float pi * 2) |
|
1256 |
]. |
|
1257 |
^ res |
|
1258 |
! |
|
1259 |
||
1260 |
arcTanh |
|
1261 |
"Answer the receiver's area hyperbolic tangent. |
|
1262 |
That is the inverse function of tanh." |
|
1263 |
||
1264 |
"Some other possible implementation: |
|
1265 |
||
1266 |
^((1 + self) / (1 - self)) ln / 2" |
|
1267 |
||
1268 |
^ self i arcTan i negated |
|
1269 |
||
1270 |
" |
|
1271 |
(10+4i) tanh arcTanh |
|
1272 |
" |
|
1273 |
! |
|
1274 |
||
1275 |
cos |
|
1276 |
"Answer the receiver's cosine." |
|
1277 |
||
1278 |
"/ ^ self i cosh |
|
1279 |
^ self class |
|
1280 |
real:(real cos * imaginary cosh) |
|
1281 |
imaginary:(-1 * real sin * imaginary sinh) |
|
1282 |
||
1283 |
" |
|
1284 |
(10+4i) cos -> (-22.91356068209214107+14.84629106966035727i) |
|
1285 |
(10+4i) i cosh (-22.91356068209214107+14.84629106966035727i) |
|
1286 |
" |
|
1287 |
! |
|
1288 |
||
1289 |
cosh |
|
1290 |
"Answer the receiver's hyperbolic cosine. |
|
1291 |
Hyperbolic cosine is defined by same power series expansion as for real numbers, |
|
1292 |
that is in term of exponential: |
|
1293 |
^ (self exp + self negated exp) / 2. |
|
1294 |
This implementation avoids creating intermediate objects." |
|
1295 |
||
1296 |
^self class |
|
1297 |
real: real cosh * imaginary cos |
|
1298 |
imaginary: real sinh * imaginary sin |
|
1299 |
||
1300 |
" |
|
1301 |
(10+4i) cosh -> (-7198.729443310666079-8334.842120982836036i) |
|
1302 |
" |
|
1303 |
! |
|
1304 |
||
1305 |
sin |
|
1306 |
"Answer the receiver's sine." |
|
1307 |
||
1308 |
"/ alternative: |
|
1309 |
"/ ^ self i sinh i negated |
|
1310 |
||
1311 |
^ self class |
|
1312 |
real:(real sin * imaginary cosh) |
|
1313 |
imaginary:(real cos * imaginary sinh) |
|
1314 |
||
1315 |
" |
|
1316 |
(10+4i) sin -> (-14.85625516387525498-22.89819255096375875i) |
|
1317 |
" |
|
1318 |
! |
|
1319 |
||
1320 |
sinh |
|
1321 |
"Answer the receiver's hyperbolic sine. |
|
1322 |
Hyperbolic sine is defined by same power series expansion as for real numbers, |
|
1323 |
that is in term of exponential: |
|
1324 |
^ (self exp - self negated exp) / 2. |
|
1325 |
This implementation avoids creating intermediate objects." |
|
1326 |
||
1327 |
^ self class |
|
1328 |
real: real sinh * imaginary cos |
|
1329 |
imaginary: real cosh * imaginary sin |
|
1330 |
||
1331 |
" |
|
1332 |
10 sinh -> 11013.23287470339338 |
|
1333 |
(10+4i) sinh (-7198.729413635291604-8334.842155341616165i) |
|
1334 |
" |
|
1335 |
! |
|
1336 |
||
1337 |
tan |
|
22938 | 1338 |
"Answer the receiver's tangent." |
22729 | 1339 |
|
1340 |
^ self sin / self cos |
|
1341 |
||
1342 |
" |
|
1343 |
(10+4i) tan -> (0.0006123503000121616919+0.9997260574022127583i) |
|
1344 |
(10+4i) sin -> (-14.85625516387525498-22.89819255096375875i) |
|
1345 |
(10+4i) cos -> (-22.91356068209214107+14.84629106966035727i) |
|
1346 |
" |
|
1347 |
! |
|
1348 |
||
1349 |
tanh |
|
1350 |
"Answer the receiver's hyperbolic tangent." |
|
1351 |
||
1352 |
"Some possible implementations are: |
|
1353 |
^self sinh / self cosh |
|
1354 |
||
1355 |
| tr ti | |
|
1356 |
tr := real tanh. |
|
1357 |
ti := imaginary tan i. |
|
1358 |
^(tr + ti) / (tr * ti + 1) |
|
1359 |
" |
|
1360 |
||
1361 |
^ self i tan i negated |
|
1362 |
||
1363 |
" |
|
1364 |
(10+4i) tanh |
|
1365 |
" |
|
7355 | 1366 |
! ! |
1367 |
||
1368 |
!Complex methodsFor:'truncation & rounding'! |
|
1369 |
||
1370 |
ceiling |
|
1371 |
"blocked: complex numbers have no ceiling" |
|
1372 |
||
1373 |
^ self shouldNotImplement |
|
1374 |
! |
|
1375 |
||
1376 |
floor |
|
1377 |
"blocked: complex numbers have no floor" |
|
1378 |
||
1379 |
^ self shouldNotImplement |
|
265 | 1380 |
! ! |
1381 |
||
3637 | 1382 |
!Complex class methodsFor:'documentation'! |
1383 |
||
1384 |
version |
|
18836 | 1385 |
^ '$Header$' |
23644 | 1386 |
! |
1387 |
||
1388 |
version_CVS |
|
1389 |
^ '$Header$' |
|
3637 | 1390 |
! ! |
16449 | 1391 |