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