author | Claus Gittinger <cg@exept.de> |
Sat, 01 Jul 2017 20:17:49 +0200 | |
changeset 21932 | c033a3755e36 |
parent 21873 | a2ed604af5c4 |
child 21933 | 56fbff93ec73 |
permissions | -rw-r--r-- |
3637 | 1 |
" |
2 |
This is a Manchester Goodie. It is distributed freely on condition |
|
3 |
that you observe these conditions in respect of the whole Goodie, and on |
|
4 |
any significant part of it which is separately transmitted or stored: |
|
5287
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
5 |
* You must ensure that every copy includes this notice, and that |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
6 |
source and author(s) of the material are acknowledged. |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
7 |
* These conditions must be imposed on anyone who receives a copy. |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
8 |
* The material shall not be used for commercial gain without the prior |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
9 |
written consent of the author(s). |
3637 | 10 |
|
13548 | 11 |
For more information about the Manchester Goodies Library (from which |
3637 | 12 |
this file was distributed) send e-mail: |
5287
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
13 |
To: goodies-lib@cs.man.ac.uk |
13548 | 14 |
Subject: help |
3637 | 15 |
|
16 |
This is an additional goody-class, which is NOT covered by the |
|
17 |
ST/X license. It has been packaged with the ST/X distribution to |
|
18 |
make your live easier instead. NO WARRANTY. |
|
19 |
" |
|
13548 | 20 |
"{ Package: 'stx:libbasic' }" |
6500 | 21 |
|
18836 | 22 |
"{ NameSpace: Smalltalk }" |
23 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
24 |
ArithmeticValue subclass:#Complex |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
25 |
instanceVariableNames:'real imaginary' |
7355 | 26 |
classVariableNames:'ComplexOne ComplexZero' |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
27 |
poolDictionaries:'' |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
28 |
category:'Magnitude-Numbers' |
265 | 29 |
! |
30 |
||
3637 | 31 |
!Complex class methodsFor:'documentation'! |
32 |
||
33 |
copyright |
|
34 |
" |
|
35 |
This is a Manchester Goodie. It is distributed freely on condition |
|
36 |
that you observe these conditions in respect of the whole Goodie, and on |
|
37 |
any significant part of it which is separately transmitted or stored: |
|
5287
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
38 |
* You must ensure that every copy includes this notice, and that |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
39 |
source and author(s) of the material are acknowledged. |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
40 |
* These conditions must be imposed on anyone who receives a copy. |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
41 |
* The material shall not be used for commercial gain without the prior |
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
42 |
written consent of the author(s). |
3637 | 43 |
|
13548 | 44 |
For more information about the Manchester Goodies Library (from which |
3637 | 45 |
this file was distributed) send e-mail: |
5287
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
3637
diff
changeset
|
46 |
To: goodies-lib@cs.man.ac.uk |
13548 | 47 |
Subject: help |
3637 | 48 |
|
49 |
This is an additional goody-class, which is NOT covered by the |
|
50 |
ST/X license. It has been packaged with the ST/X distribution to |
|
51 |
make your live easier instead. NO WARRANTY. |
|
52 |
" |
|
53 |
! |
|
54 |
||
55 |
documentation |
|
56 |
" |
|
13548 | 57 |
This class implements complex numbers. |
58 |
A complex number has real and imaginary parts which must be manipulated simultaneously |
|
7355 | 59 |
in any numeric processing. |
60 |
Complex numbers can be used in many of the same places that regular numbers |
|
61 |
can be used with one major exception of comparisons, since complex numbers cannot |
|
13548 | 62 |
be directly compared for size |
7355 | 63 |
(except through lengths of vectors (see absolute value)). |
265 | 64 |
|
7355 | 65 |
[Instance variables:] |
66 |
real <Number> the part of the number which can be expressed as a Real number |
|
67 |
imaginary <Number> the part of the number which, in terms of how the number behaves, |
|
13548 | 68 |
has been multiplied by 'i' (-1 sqrt) |
265 | 69 |
|
7355 | 70 |
[Author:] |
13548 | 71 |
Kurt Hebel (hebel@uinova.cerl.uiuc.edu) |
72 |
minor changes and double dispatching code by cg. |
|
3637 | 73 |
" |
6500 | 74 |
! |
75 |
||
7355 | 76 |
examples |
6500 | 77 |
" |
21932 | 78 |
-25 sqrt -> error |
79 |
Complex trapImaginary:[ -25 sqrt ] -> 5i (0.0+5.0i) |
|
80 |
Complex trapImaginary:[ -25 integerSqrt ] -> 5i (0+5i) |
|
81 |
||
82 |
(Complex trapImaginary:[ -5397346292805549782720214077673687804022210808238353958670041357153884304 integerSqrt ]) |
|
83 |
squared |
|
84 |
||
85 |
1 + 3i |
|
86 |
||
87 |
Number i + 1 |
|
88 |
1 + Number i |
|
89 |
||
90 |
1i * 1i |
|
91 |
Number i * Number i |
|
92 |
||
6500 | 93 |
(5 % 7) real |
94 |
(5 % 7) imaginary |
|
95 |
(5 % 7) = 5 |
|
13548 | 96 |
(5 % 0) = 5 |
97 |
(5.0 % 0) = 5 |
|
7221 | 98 |
|
13548 | 99 |
(1 % 0) + (2 % 0) |
100 |
(1 % 0) + (0 % 2) |
|
7221 | 101 |
(1 % 0) + (2 % 3) |
102 |
||
13548 | 103 |
(1 % 0) * (2 % 0) |
104 |
(1 % 0) * (0 % 2) |
|
7221 | 105 |
(1 % 0) * (2 % 3) |
7355 | 106 |
|
107 |
(1 % 2) + 2 |
|
108 |
(1 % 2) * 2 |
|
13548 | 109 |
2 + (1 % 2) |
110 |
2 * (1 % 2) |
|
7355 | 111 |
|
21932 | 112 |
(Number i raisedTo:-3) -> Number i |
113 |
(Number i raisedTo:-2) -> -1 |
|
114 |
(Number i raisedTo:-1) -> Number i negated |
|
115 |
||
116 |
(Number i raisedTo:0) -> 1 |
|
117 |
(Number i raisedTo:1) -> Number i |
|
118 |
(Number i raisedTo:2) -> -1 |
|
119 |
(Number i raisedTo:3) -> Number i negated |
|
120 |
(Number i raisedTo:4) -> 1 |
|
121 |
(Number i raisedTo:6) -> -1 |
|
122 |
||
123 |
5i * 5i -> -25 |
|
124 |
5i squared -> -25 |
|
6500 | 125 |
" |
3637 | 126 |
! ! |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
127 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
128 |
!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
|
129 |
|
9128 | 130 |
abs:aNumber1 arg:aNumber2 |
131 |
|real imaginary| |
|
132 |
||
133 |
real := aNumber1 * aNumber2 cos. |
|
134 |
imaginary := aNumber1 * aNumber2 sin. |
|
135 |
^ real + imaginary i |
|
136 |
! |
|
137 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
138 |
fromReal: aNumber |
7355 | 139 |
"Create a new complex number from the given real number." |
140 |
||
141 |
^ self basicNew setReal: aNumber setImaginary: 0 |
|
21798 | 142 |
|
143 |
" |
|
144 |
Complex fromReal:1.0 |
|
145 |
" |
|
146 |
||
147 |
"Modified (comment): / 12-06-2017 / 20:42:56 / cg" |
|
7355 | 148 |
! |
149 |
||
150 |
imaginary: v |
|
151 |
"Create a new complex number with 0 as real and given imaginary parts. |
|
152 |
If the imaginary part is zero, return the real part of the number." |
|
153 |
||
154 |
v = 0 ifTrue: [^ 0]. |
|
155 |
^ self basicNew setReal: 0 setImaginary: v |
|
21798 | 156 |
|
157 |
" |
|
158 |
Complex imaginary:1.0 |
|
159 |
(0.0 % 1.0) |
|
160 |
" |
|
161 |
||
162 |
"Modified (comment): / 12-06-2017 / 20:44:51 / cg" |
|
7355 | 163 |
! |
164 |
||
165 |
real: aNumber |
|
166 |
"Create a new complex number from the given real number." |
|
167 |
||
168 |
^ self basicNew setReal: aNumber setImaginary: 0 |
|
21798 | 169 |
|
170 |
" |
|
171 |
Complex real:1.0 |
|
172 |
" |
|
173 |
||
174 |
"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
|
175 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
176 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
177 |
real: u imaginary: v |
7355 | 178 |
"Create a new complex number with the given real and imaginary parts. If the |
179 |
imaginary part is zero, return the real part of the number." |
|
180 |
||
181 |
v = 0 ifTrue: [^ u]. |
|
182 |
^ self basicNew setReal: u setImaginary: v |
|
21798 | 183 |
|
184 |
" |
|
185 |
Complex real:1.0 imaginary:2.0 |
|
186 |
(1.0 % 2.0) |
|
187 |
" |
|
188 |
||
189 |
"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
|
190 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
191 |
|
21873 | 192 |
!Complex class methodsFor:'coercing & converting'! |
21817 | 193 |
|
194 |
coerce: aNumber |
|
195 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
|
196 |
||
197 |
^ aNumber asComplex |
|
198 |
! ! |
|
199 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
200 |
!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
|
201 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
202 |
unity |
7355 | 203 |
"Answer the value which allows, for any given arithmetic value, the following to be true: |
13548 | 204 |
aNumber * aNumber class unity = aNumber |
7355 | 205 |
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
|
206 |
|
7355 | 207 |
ComplexOne isNil ifTrue:[ |
13548 | 208 |
ComplexOne := self fromReal: 1 |
7355 | 209 |
]. |
210 |
^ ComplexOne |
|
265 | 211 |
! |
212 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
213 |
zero |
7355 | 214 |
"Answer the value which allows, for any given arithmetic value, the following to be true: |
13548 | 215 |
aNumber + aNumber class zero = aNumber |
7355 | 216 |
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
|
217 |
|
7355 | 218 |
ComplexZero isNil ifTrue:[ |
13548 | 219 |
ComplexZero := self fromReal: 0 |
7355 | 220 |
]. |
221 |
^ ComplexZero |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
222 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
223 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
224 |
!Complex class methodsFor:'exception handling'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
225 |
|
7404 | 226 |
trapImaginary:aBlock |
227 |
"evaluate aBlock; if any DomainError occurs inside, with respect to square roots, |
|
228 |
convert the root to a complex root and proceed. |
|
229 |
This allows for regular (failing) code to transparently convert to complex." |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
230 |
|
21932 | 231 |
^ ImaginaryResultError |
232 |
handle: [:ex | |
|
233 |
|msgSend selector rcvr| |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
234 |
|
21932 | 235 |
msgSend := ex parameter. |
236 |
selector := msgSend selector. |
|
237 |
rcvr := msgSend receiver. |
|
238 |
(selector = #sqrt or: [selector = #sqrtTruncated]) ifTrue: [ |
|
239 |
(rcvr isNumber and:[rcvr isReal]) ifTrue:[ |
|
240 |
ex proceedWith:(rcvr abs perform:selector) i |
|
241 |
] ifFalse:[ |
|
242 |
msgSend receiver: rcvr asComplex. |
|
243 |
ex proceedWith: msgSend value |
|
244 |
]. |
|
245 |
] ifFalse: [ |
|
246 |
(selector = #integerSqrt) ifTrue: [ |
|
247 |
(rcvr isInteger) ifTrue:[ |
|
248 |
ex proceedWith:(rcvr abs integerSqrt) i |
|
249 |
] ifFalse:[ |
|
250 |
ex proceedWith:(rcvr abs asComplex sqrt floor) i |
|
251 |
]. |
|
252 |
] ifFalse: [ |
|
253 |
ex reject |
|
254 |
] |
|
255 |
] |
|
256 |
] do: |
|
257 |
aBlock |
|
7404 | 258 |
|
259 |
" |
|
13548 | 260 |
Complex trapImaginary: [-2 sqrt] |
7404 | 261 |
" |
262 |
||
263 |
"failing code: |
|
21932 | 264 |
|a| |
7404 | 265 |
|
21932 | 266 |
a := -2. |
267 |
(a sqrt + 5) * 17. |
|
7404 | 268 |
" |
269 |
"complex code: |
|
21932 | 270 |
|a| |
7404 | 271 |
|
21932 | 272 |
Complex trapImaginary:[ |
273 |
a := -2. |
|
274 |
(a sqrt + 5) * 2. |
|
275 |
] |
|
7404 | 276 |
" |
21932 | 277 |
|
278 |
"Modified: / 01-07-2017 / 20:17:15 / cg" |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
279 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
280 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
281 |
!Complex methodsFor:'accessing'! |
265 | 282 |
|
283 |
imaginary |
|
7355 | 284 |
"Return the imaginary part of the complex number." |
285 |
||
286 |
^ imaginary |
|
287 |
! |
|
288 |
||
289 |
imaginaryPart |
|
290 |
"Return the imaginary part of the complex number. |
|
291 |
An alias for imaginary (for compatibility with other complex implementations)" |
|
292 |
||
293 |
^ imaginary |
|
265 | 294 |
! |
295 |
||
296 |
real |
|
7355 | 297 |
"Return the real part of the complex number." |
298 |
||
299 |
^ real |
|
300 |
! |
|
301 |
||
302 |
realPart |
|
303 |
"Return the real part of the complex number. |
|
304 |
An alias for real (for compatibility with other complex implementations)" |
|
305 |
||
306 |
^ real |
|
265 | 307 |
! ! |
308 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
309 |
!Complex methodsFor:'arithmetic'! |
265 | 310 |
|
13548 | 311 |
* aNumber |
7355 | 312 |
"Return the product of the receiver and the argument." |
265 | 313 |
|
7355 | 314 |
"/ | u v r i | |
315 |
"/ |
|
316 |
"/ aNumber isComplex ifTrue:[ |
|
317 |
"/ u := aNumber real. |
|
318 |
"/ v := aNumber imaginary. |
|
319 |
"/ r := (real * u) - (imaginary * v). |
|
320 |
"/ i := (real * v) + (imaginary * u). |
|
321 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
322 |
"/ ^ Complex real:r imaginary:i |
|
323 |
"/ ]. |
|
13548 | 324 |
^ aNumber productFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
325 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
326 |
"Modified: / 8.7.1998 / 12:12:37 / cg" |
265 | 327 |
! |
328 |
||
13548 | 329 |
+ aNumber |
7355 | 330 |
"Return the sum of the receiver and the argument." |
265 | 331 |
|
7355 | 332 |
"/ | r i | |
333 |
"/ |
|
334 |
"/ aNumber isComplex ifTrue: [ |
|
335 |
"/ r := aNumber real + real. |
|
336 |
"/ i := aNumber imaginary + imaginary. |
|
337 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
338 |
"/ ^ Complex real:r imaginary:i |
|
339 |
"/ ]. |
|
13548 | 340 |
^ aNumber sumFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
341 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
342 |
"Modified: / 8.7.1998 / 12:15:42 / cg" |
265 | 343 |
! |
344 |
||
345 |
- aNumber |
|
7355 | 346 |
"Return the difference of the receiver and the argument." |
265 | 347 |
|
7355 | 348 |
"/ | r i | |
349 |
"/ |
|
350 |
"/ aNumber isComplex ifTrue: [ |
|
351 |
"/ r := real - aNumber real. |
|
352 |
"/ i := imaginary - aNumber imaginary. |
|
353 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
354 |
"/ ^ Complex real:r imaginary:i. |
|
355 |
"/ ]. |
|
13548 | 356 |
^ aNumber differenceFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
357 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
358 |
"Modified: / 8.7.1998 / 12:15:38 / cg" |
265 | 359 |
! |
360 |
||
13548 | 361 |
/ aNumber |
7355 | 362 |
"Return the quotient of the receiver and the argument." |
265 | 363 |
|
7355 | 364 |
"/ | denom u v r i | |
365 |
"/ |
|
13548 | 366 |
"/ aNumber isComplex ifTrue:[ |
7355 | 367 |
"/ u := aNumber real. |
368 |
"/ v := aNumber imaginary. |
|
369 |
"/ denom := u * u + (v * v). |
|
370 |
"/ r := u * real + (v * imaginary) / denom. |
|
371 |
"/ i := u * imaginary - (v * real) / denom. |
|
372 |
"/ i = 0 ifTrue:[ ^ r ]. |
|
373 |
"/ ^ Complex real:r imaginary:i |
|
374 |
"/ ]. |
|
13548 | 375 |
^ aNumber quotientFromComplex:self. |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
376 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
377 |
"Modified: / 8.7.1998 / 12:15:34 / cg" |
265 | 378 |
! |
379 |
||
380 |
abs |
|
7355 | 381 |
"Return the magnitude (or absolute value) of the complex number |
20408 | 382 |
(that's the distance from the origin in the complex plane)." |
265 | 383 |
|
7355 | 384 |
^ (real * real + (imaginary * imaginary)) sqrt |
385 |
||
386 |
" |
|
387 |
(1 % 1) abs |
|
388 |
" |
|
265 | 389 |
! |
390 |
||
391 |
conjugated |
|
7355 | 392 |
"Return the complex conjugate of this complex number |
393 |
(i.e. with imaginary part negated)." |
|
394 |
||
16449 | 395 |
^ self class |
396 |
real: real |
|
397 |
imaginary: imaginary negated |
|
7355 | 398 |
! |
399 |
||
400 |
modulus |
|
401 |
| absReal absImag multiplicand quotient | |
|
402 |
||
403 |
absReal := real abs. |
|
404 |
absImag := imaginary abs. |
|
405 |
||
13548 | 406 |
absReal >= absImag ifTrue: [ |
407 |
multiplicand := absReal. |
|
408 |
quotient := imaginary / real |
|
409 |
] ifFalse: [ |
|
410 |
multiplicand := absImag. |
|
411 |
quotient := real / imaginary |
|
7355 | 412 |
]. |
413 |
^ multiplicand * ((1 + (quotient * quotient)) sqrt) |
|
414 |
! |
|
415 |
||
416 |
negated |
|
417 |
"return a new complex with both real and imaginary parts negated" |
|
418 |
||
16449 | 419 |
^ self class |
420 |
real: real negated |
|
421 |
imaginary: imaginary negated |
|
265 | 422 |
! ! |
423 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
424 |
!Complex methodsFor:'coercing'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
425 |
|
21873 | 426 |
generality |
427 |
^ 150 |
|
428 |
! ! |
|
429 |
||
430 |
!Complex methodsFor:'coercing & converting'! |
|
431 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
432 |
coerce: aNumber |
18836 | 433 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
434 |
||
7355 | 435 |
^ aNumber asComplex |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
436 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
437 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
438 |
!Complex methodsFor:'comparing'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
439 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
440 |
< aNumber |
7355 | 441 |
"raises an error - complex numbers are not well ordered" |
442 |
||
443 |
^ Number |
|
13548 | 444 |
raise: #unorderedSignal |
445 |
receiver: self |
|
446 |
selector: #< |
|
447 |
arg: aNumber |
|
448 |
errorString: 'Complex numbers are not well ordered' |
|
7355 | 449 |
|
450 |
" |
|
451 |
1 < (2 % 2) |
|
452 |
(2 % 2) < 1 |
|
453 |
" |
|
6500 | 454 |
! |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
455 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
456 |
= aNumber |
7355 | 457 |
"return true, if the argument represents the same numeric value |
7471 | 458 |
as the receiver, false otherwise." |
7355 | 459 |
|
13548 | 460 |
^ aNumber equalFromComplex:self |
21798 | 461 |
|
462 |
" |
|
463 |
(Complex real:1.0 imaginary:2.0) = (Complex real:1.0 imaginary:2.0) |
|
464 |
(Complex real:1.0 imaginary:0) = 1.0 |
|
465 |
" |
|
466 |
||
467 |
"Modified (comment): / 12-06-2017 / 20:43:41 / cg" |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
468 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
469 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
470 |
hash |
7355 | 471 |
"Hash is implemented because equals is implemented." |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
472 |
|
7377 | 473 |
^ (real hash) bitXor:(imaginary hash bitShift:16) |
474 |
||
475 |
" |
|
13548 | 476 |
(1+0i) hash |
7377 | 477 |
(1+1i) hash |
478 |
" |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
479 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
480 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
481 |
!Complex methodsFor:'converting'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
482 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
483 |
asComplex |
7355 | 484 |
"I am a complex - so return the receiver" |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
485 |
|
7355 | 486 |
^ self |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
487 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
488 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
489 |
asFloat |
7355 | 490 |
imaginary = 0 ifTrue: [^ real asFloat]. |
491 |
^ Number |
|
13548 | 492 |
raise: #coercionErrorSignal |
493 |
receiver: self |
|
494 |
selector: #asFloat |
|
495 |
errorString: 'Can''t coerce an instance of Complex to a Float' |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
496 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
497 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
498 |
asInteger |
7355 | 499 |
imaginary = 0 ifTrue: [^real asInteger]. |
500 |
^ Number |
|
13548 | 501 |
raise: #coercionErrorSignal |
502 |
receiver: self |
|
503 |
selector: #asInteger |
|
504 |
errorString: 'Can''t coerce an instance of Complex to an Integer' |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
505 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
506 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
507 |
asPoint |
7355 | 508 |
"Return the complex number as a point." |
509 |
||
510 |
^ real @ imaginary |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
511 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
512 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
513 |
reduceGeneralityIfPossible |
13548 | 514 |
"Answer the receiver transformed to a lower generality, if such a |
515 |
transformation is possible without losing information. |
|
7355 | 516 |
If not, answer the receiver" |
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
517 |
|
7355 | 518 |
imaginary isZero |
13548 | 519 |
ifTrue: [^ real] |
520 |
ifFalse: [^ self] |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
521 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
522 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
523 |
!Complex methodsFor:'double dispatching'! |
265 | 524 |
|
9128 | 525 |
differenceFromComplex:aComplex |
526 |
"Return the difference of the argument, aComplex and the receiver." |
|
527 |
||
528 |
| r i | |
|
529 |
||
530 |
r := aComplex real - real. |
|
531 |
i := aComplex imaginary - imaginary. |
|
532 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 533 |
^ self class real:r imaginary:i. |
9128 | 534 |
! |
535 |
||
20740 | 536 |
differenceFromFixedPoint: aFixedPoint |
21110 | 537 |
"Return the difference of the argument, aFixedPoint and the receiver." |
538 |
||
20740 | 539 |
^ aFixedPoint asComplex - self |
540 |
! |
|
541 |
||
7355 | 542 |
differenceFromFloat:aFloat |
543 |
"Return the difference of the argument, aFloat and the receiver." |
|
544 |
||
545 |
"/ ^ aFloat asComplex - self |
|
546 |
||
547 |
| r | |
|
265 | 548 |
|
7355 | 549 |
r := aFloat - real. |
550 |
imaginary = 0 ifTrue:[ ^ r ]. |
|
16449 | 551 |
^ self class real:r imaginary:imaginary negated |
7355 | 552 |
|
553 |
" |
|
13548 | 554 |
(1 % 1) - 1.0 |
555 |
1.0 - (1 % 1) |
|
7355 | 556 |
" |
265 | 557 |
! |
558 |
||
7355 | 559 |
differenceFromFraction: aFraction |
21110 | 560 |
"Return the difference of the argument, aFraction and the receiver." |
561 |
||
7355 | 562 |
^ aFraction asComplex - self |
563 |
! |
|
564 |
||
565 |
differenceFromInteger: anInteger |
|
21110 | 566 |
"Return the difference of the argument, anInteger and the receiver." |
567 |
||
7355 | 568 |
^ anInteger asComplex - self |
265 | 569 |
! |
570 |
||
7355 | 571 |
equalFromComplex:aComplex |
21798 | 572 |
"return true if aComplex represents the same number as myself" |
573 |
||
7355 | 574 |
^ (aComplex real = real) and:[aComplex imaginary = imaginary] |
21798 | 575 |
|
576 |
"Modified (comment): / 12-06-2017 / 20:41:04 / cg" |
|
265 | 577 |
! |
578 |
||
7355 | 579 |
equalFromFloat:aFloat |
21798 | 580 |
"return true if aFloat represents the same number as myself" |
581 |
||
7355 | 582 |
imaginary = 0 ifFalse:[^ false]. |
583 |
^ real = aFloat |
|
21798 | 584 |
|
585 |
"Modified (comment): / 12-06-2017 / 20:41:00 / cg" |
|
265 | 586 |
! |
587 |
||
13548 | 588 |
productFromComplex:aComplex |
9128 | 589 |
"Return the product of the receiver and the argument, aComplex." |
590 |
||
591 |
| u v r i | |
|
592 |
||
593 |
u := aComplex real. |
|
594 |
v := aComplex imaginary. |
|
595 |
r := (real * u) - (imaginary * v). |
|
596 |
i := (real * v) + (imaginary * u). |
|
597 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 598 |
^ self class real:r imaginary:i |
9128 | 599 |
! |
600 |
||
20740 | 601 |
productFromFixedPoint: aFixedPoint |
21110 | 602 |
"Return the product of the receiver and the argument, aFixedPoint." |
603 |
||
20740 | 604 |
^ aFixedPoint asComplex * self |
605 |
! |
|
606 |
||
7355 | 607 |
productFromFloat: aFloat |
608 |
"Return the product of the receiver and the argument, aFloat." |
|
609 |
||
610 |
"/ ^ aFloat asComplex * self |
|
611 |
||
612 |
| u r i | |
|
613 |
||
614 |
u := aFloat. |
|
615 |
r := (real * aFloat). |
|
616 |
i := (imaginary * aFloat). |
|
617 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 618 |
^ self class real:r imaginary:i |
7355 | 619 |
|
620 |
" |
|
13548 | 621 |
(1 % 1) * 2.0 |
622 |
(1 % 1) * 0.0 |
|
7355 | 623 |
2.0 * (1 % 1) |
624 |
" |
|
265 | 625 |
! |
626 |
||
7355 | 627 |
productFromFraction: aFraction |
21110 | 628 |
"Return the product of the receiver and the argument, aFraction." |
629 |
||
7355 | 630 |
^ aFraction asComplex * self |
265 | 631 |
! |
632 |
||
7355 | 633 |
productFromInteger: anInteger |
21798 | 634 |
"sent when an integer does not know how to multiply the receiver, a complex. |
635 |
Return the product of the receiver and the argument, anInteger." |
|
21110 | 636 |
|
7355 | 637 |
^ anInteger asComplex * self |
21798 | 638 |
|
639 |
"Modified (comment): / 12-06-2017 / 20:30:20 / cg" |
|
7355 | 640 |
! |
641 |
||
13548 | 642 |
quotientFromComplex:aComplex |
9128 | 643 |
"Return the quotient of the argument, aComplex and the receiver." |
644 |
||
645 |
| denom nr ni r i | |
|
646 |
||
647 |
nr := aComplex real. |
|
648 |
ni := aComplex imaginary. |
|
649 |
denom := real * real + (imaginary * imaginary). |
|
16449 | 650 |
r := (real * nr + (imaginary * ni)) / denom. |
651 |
i := (real * ni - (imaginary * nr)) / denom. |
|
9128 | 652 |
i = 0 ifTrue:[ ^ r ]. |
16449 | 653 |
^ self class real:r imaginary:i |
9128 | 654 |
|
655 |
"/ is the stuff below better ? |
|
13548 | 656 |
"/ "Implement complex division (a + ib) / (c + id). |
9128 | 657 |
"/ Due to double dispatch, in this routine |
658 |
"/ self = (c + id) and aComplex = (a + ib)." |
|
659 |
"/ |
|
660 |
"/ | quotient denominator | |
|
661 |
"/ |
|
662 |
"/ self realPart abs >= (self imaginaryPart abs) |
|
13548 | 663 |
"/ ifTrue: [ |
9128 | 664 |
"/ quotient := self imaginaryPart / self realPart. |
665 |
"/ denominator := self realPart + (self imaginaryPart * quotient). |
|
13548 | 666 |
"/ ^ Complex |
9128 | 667 |
"/ real: (aComplex realPart + (aComplex imaginaryPart * quotient)) / denominator |
668 |
"/ imaginary: (aComplex imaginaryPart - (aComplex realPart * quotient)) / denominator ] |
|
13548 | 669 |
"/ ifFalse: [ |
9128 | 670 |
"/ quotient := self realPart / self imaginaryPart. |
671 |
"/ denominator := (self realPart * quotient) + self imaginaryPart. |
|
13548 | 672 |
"/ ^ Complex |
9128 | 673 |
"/ real: ((aComplex realPart * quotient) + aComplex imaginaryPart) / denominator |
674 |
"/ imaginary: ((aComplex imaginaryPart * quotient) - aComplex realPart) / denominator ] |
|
675 |
! |
|
676 |
||
20740 | 677 |
quotientFromFixedPoint:aFixedPoint |
678 |
"Return the quotient of the argument, aFixedPoint and the receiver." |
|
679 |
||
680 |
^ aFixedPoint asComplex / self |
|
681 |
! |
|
682 |
||
7442 | 683 |
quotientFromFloat:aFloat |
684 |
"Return the quotient of the argument, aFloat and the receiver." |
|
685 |
||
7355 | 686 |
^ aFloat asComplex / self |
265 | 687 |
! |
688 |
||
7442 | 689 |
quotientFromFraction:aFraction |
690 |
"Return the quotient of the argument, aFraction and the receiver." |
|
691 |
||
7355 | 692 |
^ aFraction asComplex / self |
693 |
! |
|
694 |
||
7442 | 695 |
quotientFromInteger:anInteger |
696 |
"Return the quotient of the argument, anInteger and the receiver." |
|
697 |
||
7355 | 698 |
^ anInteger asComplex / self |
265 | 699 |
! |
700 |
||
13548 | 701 |
sumFromComplex:aComplex |
9128 | 702 |
"Return the sum of the receiver and the argument, aComplex." |
703 |
||
704 |
| r i | |
|
705 |
||
706 |
r := aComplex real + real. |
|
707 |
i := aComplex imaginary + imaginary. |
|
708 |
i = 0 ifTrue:[ ^ r ]. |
|
16449 | 709 |
^ self class real:r imaginary:i |
9128 | 710 |
! |
711 |
||
20740 | 712 |
sumFromFixedPoint: aFixedPoint |
21110 | 713 |
"Return the sum of the receiver and the argument, aFixedPoint." |
714 |
||
20740 | 715 |
^ aFixedPoint asComplex + self |
716 |
! |
|
717 |
||
7355 | 718 |
sumFromFloat: aFloat |
719 |
"Return the sum of the receiver and the argument, aFloat." |
|
720 |
||
721 |
"/ ^ aFloat asComplex + self |
|
722 |
||
723 |
| r | |
|
724 |
||
725 |
r := aFloat + real. |
|
726 |
imaginary = 0 ifTrue:[ ^ r ]. |
|
16449 | 727 |
^ self class real:r imaginary:imaginary |
7355 | 728 |
|
729 |
" |
|
13548 | 730 |
(1 % 1) + 1.0 |
731 |
1.0 + (1 % 1) |
|
7355 | 732 |
" |
265 | 733 |
! |
734 |
||
7355 | 735 |
sumFromFraction: aFraction |
21110 | 736 |
"Return the sum of the receiver and the argument, aFraction." |
737 |
||
7355 | 738 |
^ aFraction asComplex + self |
265 | 739 |
! |
740 |
||
7355 | 741 |
sumFromInteger: anInteger |
21110 | 742 |
"Return the sum of the receiver and the argument, anInteger." |
743 |
||
7355 | 744 |
^ anInteger asComplex + self |
265 | 745 |
! ! |
746 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
747 |
!Complex methodsFor:'mathematical functions'! |
265 | 748 |
|
749 |
angle |
|
7355 | 750 |
"Return the radian angle for this Complex number." |
265 | 751 |
|
7355 | 752 |
real < 0 ifTrue: [ |
13548 | 753 |
imaginary < 0 ifTrue: [ |
754 |
^ (imaginary / real) arcTan - Float pi |
|
755 |
]. |
|
756 |
^ Float pi + (imaginary / real) arcTan |
|
7355 | 757 |
]. |
758 |
^ (imaginary / real) arcTan |
|
759 |
||
760 |
" |
|
761 |
(1 % 1) angle radiansToDegrees |
|
762 |
" |
|
265 | 763 |
! |
764 |
||
765 |
exp |
|
7355 | 766 |
"Return the complex exponential of the receiver." |
265 | 767 |
|
7355 | 768 |
^ (imaginary cos % imaginary sin) * real exp |
265 | 769 |
! |
770 |
||
771 |
sqrt |
|
7355 | 772 |
"Return the square root of the receiver" |
773 |
||
774 |
| w quotient absReal absImag | |
|
775 |
||
13548 | 776 |
((real = 0) and: [ imaginary = 0 ]) ifTrue: [ |
16449 | 777 |
^ self class zero |
7355 | 778 |
]. |
779 |
absReal := real abs. |
|
780 |
absImag := imaginary abs. |
|
781 |
||
13548 | 782 |
absReal >= absImag ifTrue:[ |
16449 | 783 |
quotient := imaginary / real. |
784 |
w := (absReal sqrt) * (((1 + (1 + (quotient * quotient)) sqrt) / 2) sqrt) |
|
13548 | 785 |
] ifFalse: [ |
16449 | 786 |
quotient := real / imaginary. |
787 |
w := (absImag sqrt) * (((quotient abs + (1 + (quotient * quotient)) sqrt) / 2) sqrt) |
|
7355 | 788 |
]. |
265 | 789 |
|
13548 | 790 |
real >= 0 ifTrue:[ |
16449 | 791 |
^ self class real: w imaginary: (imaginary / (2 * w)) |
7355 | 792 |
]. |
13548 | 793 |
imaginary >= 0 ifTrue: [ |
16449 | 794 |
^ self class real: absImag / (2 * w) imaginary: w |
7355 | 795 |
]. |
16449 | 796 |
^ self class real: absImag / (2 * w) imaginary: -1 * w |
7355 | 797 |
! |
798 |
||
799 |
sqrt_bad |
|
800 |
"Return the square root of the receiver" |
|
265 | 801 |
|
7355 | 802 |
| u v | |
803 |
||
804 |
(imaginary = 0 and: [real >= 0]) ifTrue: [^ real sqrt]. |
|
16449 | 805 |
v := ((self abs - real) / 2) sqrt. |
7355 | 806 |
u := imaginary / 2 / v. |
16449 | 807 |
^ self class real: u imaginary: v |
7355 | 808 |
|
809 |
" |
|
13548 | 810 |
-4 asComplex sqrt |
811 |
4 asComplex sqrt |
|
7355 | 812 |
" |
813 |
" |
|
13548 | 814 |
-4 asComplex sqrt squared |
7355 | 815 |
" |
265 | 816 |
! ! |
817 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
818 |
!Complex methodsFor:'printing'! |
265 | 819 |
|
16742 | 820 |
displayOn: aGCOrStream |
821 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
|
21537 | 822 |
"/ old ST80 means: draw-yourself on a GC. |
16742 | 823 |
(aGCOrStream isStream) ifFalse:[ |
824 |
^ super displayOn:aGCOrStream |
|
825 |
]. |
|
21537 | 826 |
|
16742 | 827 |
aGCOrStream nextPut: $(. |
828 |
self realPart printOn: aGCOrStream. |
|
7355 | 829 |
self imaginaryPart >= 0 |
16742 | 830 |
ifTrue: [ aGCOrStream nextPut: $+ ] |
831 |
ifFalse: [ aGCOrStream nextPut: $- ]. |
|
832 |
self imaginaryPart abs printOn: aGCOrStream. |
|
833 |
aGCOrStream nextPutAll: 'i)' |
|
7355 | 834 |
|
835 |
" |
|
836 |
Complex real:1 imaginary:1 |
|
837 |
" |
|
21537 | 838 |
|
839 |
"Modified (format): / 22-02-2017 / 17:01:02 / cg" |
|
7355 | 840 |
! |
841 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
842 |
printOn: aStream |
7355 | 843 |
aStream nextPut: $(. |
844 |
real storeOn: aStream. |
|
845 |
aStream nextPutAll: '%'. |
|
846 |
imaginary storeOn: aStream. |
|
847 |
aStream nextPut: $). |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
848 |
! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
849 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
850 |
printString |
7355 | 851 |
^ '(' , real printString, '%', imaginary printString, ')' |
265 | 852 |
! |
853 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
854 |
storeOn: aStream |
7355 | 855 |
self printOn:aStream |
265 | 856 |
! ! |
857 |
||
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
858 |
!Complex methodsFor:'private'! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
859 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
860 |
setReal: u setImaginary: v |
7355 | 861 |
real := u. |
862 |
imaginary := v. |
|
3633
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
863 |
! ! |
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
864 |
|
89f1f31527f4
avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
865 |
!Complex methodsFor:'testing'! |
265 | 866 |
|
867 |
isComplex |
|
7355 | 868 |
"Answer whether the receiver has an imaginary part |
869 |
(i.e. if it is a complex number). Always true here." |
|
265 | 870 |
|
7355 | 871 |
^ true |
265 | 872 |
! |
873 |
||
874 |
isReal |
|
7355 | 875 |
"Return true if this Complex number has a zero imaginary part." |
876 |
||
877 |
^ imaginary = 0 |
|
265 | 878 |
! |
879 |
||
880 |
isZero |
|
13548 | 881 |
"Answer whether 'self = self class zero'. |
7355 | 882 |
We can't use #= because #= is defined in terms of #isZero" |
265 | 883 |
|
7355 | 884 |
^real isZero and: [imaginary isZero] |
265 | 885 |
! |
886 |
||
887 |
sign |
|
7471 | 888 |
"return a new complex, consisting of the signs of the real and imaginary parts. |
889 |
Q: is this a good thing to do ?" |
|
265 | 890 |
|
16449 | 891 |
^ self class real: real sign imaginary: imaginary sign |
7355 | 892 |
! ! |
893 |
||
894 |
!Complex methodsFor:'truncation & rounding'! |
|
895 |
||
896 |
ceiling |
|
897 |
"blocked: complex numbers have no ceiling" |
|
898 |
||
899 |
^ self shouldNotImplement |
|
900 |
! |
|
901 |
||
902 |
floor |
|
903 |
"blocked: complex numbers have no floor" |
|
904 |
||
905 |
^ self shouldNotImplement |
|
265 | 906 |
! ! |
907 |
||
3637 | 908 |
!Complex class methodsFor:'documentation'! |
909 |
||
910 |
version |
|
18836 | 911 |
^ '$Header$' |
3637 | 912 |
! ! |
16449 | 913 |