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