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