Complex.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Feb 2003 11:15:21 +0100
changeset 7052 207cc9c62982
parent 6500 d2ce5b0a1a78
child 7221 b38093d749b3
permissions -rw-r--r--
prep for 0-based stream position
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     1
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     2
 This is a Manchester Goodie.  It is distributed freely on condition
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     3
 that you observe these conditions in respect of the whole Goodie, and on
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     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
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    10
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    11
 For more information about the Manchester Goodies Library (from which 
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    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
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    14
	Subject: help 
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    15
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    16
 This is an additional goody-class, which is NOT covered by the
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    17
 ST/X license. It has been packaged with the ST/X distribution to
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    18
 make your live easier instead. NO WARRANTY.
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    19
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    20
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    21
"{ Package: 'stx:goodies' }"
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    22
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    23
ArithmeticValue subclass:#Complex
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    24
	instanceVariableNames:'real imaginary'
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    25
	classVariableNames:''
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    26
	poolDictionaries:''
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    27
	category:'Magnitude-Numbers'
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    28
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
    29
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    30
!Complex class methodsFor:'documentation'!
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    31
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    32
copyright
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    33
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    34
 This is a Manchester Goodie.  It is distributed freely on condition
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    35
 that you observe these conditions in respect of the whole Goodie, and on
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    36
 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
    37
	* 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
    38
	  source and author(s) of the material are acknowledged.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    39
	* 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
    40
	* 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
    41
	  written consent of the author(s).
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    42
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    43
 For more information about the Manchester Goodies Library (from which 
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    44
 this file was distributed) send e-mail:
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    45
	To: goodies-lib@cs.man.ac.uk
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    46
	Subject: help 
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    47
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    48
 This is an additional goody-class, which is NOT covered by the
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    49
 ST/X license. It has been packaged with the ST/X distribution to
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    50
 make your live easier instead. NO WARRANTY.
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    51
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    52
!
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    53
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    54
documentation
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    55
"
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    56
 This is an implementation of complex numbers.  A complex number has real and
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    57
 imaginary parts which must be manipulated simultaneously in any numeric processing.
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    58
 Complex numbers can be used in many of the same places that regular numbers
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    59
 can be used with one major exception of comparisons, since complex numbers cannot
f014922e3b71 Initial revision
claus
parents:
diff changeset
    60
 be directly compared for size (except through lengths of vectors (see absolute
f014922e3b71 Initial revision
claus
parents:
diff changeset
    61
 value)).
f014922e3b71 Initial revision
claus
parents:
diff changeset
    62
f014922e3b71 Initial revision
claus
parents:
diff changeset
    63
Instance variables:
f014922e3b71 Initial revision
claus
parents:
diff changeset
    64
    real        <Number> the part of the number which can be expressed as a Real number
f014922e3b71 Initial revision
claus
parents:
diff changeset
    65
    imaginary   <Number> the part of the number which, in terms of how the number behaves,
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    66
			 has been multiplied by 'i' (-1 sqrt)
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    67
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    68
Author: Kurt Hebel (hebel@uinova.cerl.uiuc.edu)
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    69
"
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    70
!
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    71
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    72
example
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    73
"
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    74
    (5 % 7) real
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    75
    (5 % 7) imaginary
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    76
    (5 % 7) = 5
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    77
    (5 % 0) = 5     
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    78
    (5.0 % 0) = 5       
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    79
"
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    80
! !
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    81
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    82
!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
    83
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    84
fromReal: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    85
	"Create a new complex number from the given real number."
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    86
	^ self basicNew setReal: aNumber setImaginary: 0
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    87
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    88
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    89
real: u imaginary: v
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    90
	"Create a new complex number with the given real and imaginary parts.  If the
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    91
	 imaginary part is zero, return the real part of the number."
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    92
	^v = 0 ifTrue: [u]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    93
	       ifFalse: [self basicNew setReal: u setImaginary: v]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    94
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    95
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    96
!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
    97
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    98
unity
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    99
	"Answer the value which allows, for any given arithmetic value, the following to be true
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   100
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   101
	 aNumber * aNumber class unity = aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   102
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   103
	This must be true regardless of how a given subclass chooses to define #*"
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   104
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   105
	^self fromReal: 1
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   106
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   107
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   108
zero
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   109
	"Answer the value which allows, for any given arithmetic value, the following to be true
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   110
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   111
		aNumber + aNumber class zero = aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   112
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   113
	This must be true regardless of how a given subclass chooses to define #+"
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   114
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   115
	^self fromReal: 0
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   116
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   117
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   118
!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
   119
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   120
trapImaginary: aBlock
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   121
	"Complex trapImaginary: [-27 sqrt]"
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   122
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   123
	| send |
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   124
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   125
	^Number domainErrorSignal handle: [ :ex |
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   126
	    send := ex parameter.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   127
	    (send selector = #sqrt or: [send selector = #sqrtTruncated]) ifTrue: [
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   128
		send receiver: send receiver asComplex.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   129
		ex proceedWith: send value
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   130
	    ] ifFalse: [
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   131
		ex reject
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   132
	    ]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   133
	] do: aBlock
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   134
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   135
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   136
!Complex methodsFor:'accessing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   137
f014922e3b71 Initial revision
claus
parents:
diff changeset
   138
imaginary
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   139
	"Return the imaginary part of the complex number."
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   140
	^ imaginary
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   141
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   142
f014922e3b71 Initial revision
claus
parents:
diff changeset
   143
real
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   144
	"Return the real part of the complex number."
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   145
	^ real
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   146
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   147
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   148
!Complex methodsFor:'arithmetic'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   149
f014922e3b71 Initial revision
claus
parents:
diff changeset
   150
* aNumber 
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   151
	"Return the product of the receiver and the argument."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   152
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   153
	| u v r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   154
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   155
	aNumber isComplex ifTrue:[
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   156
	    u := aNumber real.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   157
	    v := aNumber imaginary.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   158
	    r := (real * u) - (imaginary * v).
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   159
	    i  := (real * v) + (imaginary * u).
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   160
	    i = 0 ifTrue:[ ^ r ].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   161
	    ^ Complex real:r imaginary:i
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   162
	].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   163
	^ self retry: #* coercing: aNumber
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   164
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   165
    "Modified: / 8.7.1998 / 12:12:37 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   166
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   167
f014922e3b71 Initial revision
claus
parents:
diff changeset
   168
+ aNumber 
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   169
	"Return the sum of the receiver and the argument."
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   170
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   171
	| r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   172
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   173
	aNumber isComplex ifTrue: [
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   174
	    r := aNumber real + real.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   175
	    i := aNumber imaginary + imaginary.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   176
	    i = 0 ifTrue:[ ^ r ].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   177
	    ^ Complex real:r imaginary:i
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   178
	].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   179
	^ self retry: #+ coercing: aNumber
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   180
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   181
    "Modified: / 8.7.1998 / 12:15:42 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   182
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   183
f014922e3b71 Initial revision
claus
parents:
diff changeset
   184
- aNumber
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   185
	"Return the difference of the receiver and the argument."
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   186
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   187
	| r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   188
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   189
	aNumber isComplex ifTrue: [
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   190
	    r := real - aNumber real.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   191
	    i := imaginary - aNumber imaginary.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   192
	    i = 0 ifTrue:[ ^ r ].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   193
	    ^ Complex real:r imaginary:i.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   194
	].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   195
	^ self retry: #- coercing: aNumber
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   196
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   197
    "Modified: / 8.7.1998 / 12:15:38 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   198
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   199
f014922e3b71 Initial revision
claus
parents:
diff changeset
   200
/ aNumber 
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   201
	"Return the quotient of the receiver and the argument."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   202
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   203
	| denom u v r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   204
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   205
	aNumber isComplex ifTrue:[ 
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   206
	    u := aNumber real.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   207
	    v := aNumber imaginary.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   208
	    denom := u * u + (v * v).
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   209
	    r := u * real + (v * imaginary) / denom.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   210
	    i := u * imaginary - (v * real) / denom.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   211
	    i = 0 ifTrue:[ ^ r ].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   212
	    ^ Complex real:r imaginary:i
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   213
	].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   214
	^ self retry: #/ coercing: aNumber
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   215
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   216
    "Modified: / 8.7.1998 / 12:15:34 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   217
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   218
f014922e3b71 Initial revision
claus
parents:
diff changeset
   219
abs
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   220
	"Return the magnitude (or absolute value) of the complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   221
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   222
	^ (real * real + (imaginary * imaginary)) sqrt
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   223
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   224
f014922e3b71 Initial revision
claus
parents:
diff changeset
   225
conjugated
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   226
	"Return the complex conjugate of this complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   227
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   228
	^ Complex real: real imaginary: imaginary negated
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   229
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   230
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   231
!Complex methodsFor:'coercing'!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   232
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   233
coerce: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   234
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   235
	^aNumber asComplex
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   236
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   237
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   238
generality
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   239
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   240
	^150
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   241
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   242
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   243
!Complex methodsFor:'comparing'!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   244
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   245
< aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   246
	^Number
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   247
		raise: #unorderedSignal
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   248
		receiver: self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   249
		selector: #<
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   250
		arg: aNumber
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
   251
		errorString: 'Complex numbers are not well ordered'
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
   252
!
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
= aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   255
	^ (aNumber real = real) and:[aNumber imaginary = imaginary]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   256
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   257
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   258
hash
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   259
	"Hash is implemented because equals is implemented."
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   260
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   261
	^ real hash
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   262
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   263
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   264
!Complex methodsFor:'converting'!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   265
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   266
asComplex
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   267
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   268
	^self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   269
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   270
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   271
asFloat
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   272
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   273
	imaginary = 0 ifTrue: [^real asFloat].
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   274
	^Number
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   275
		raise: #coercionErrorSignal
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   276
		receiver: self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   277
		selector: #asFloat
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   278
		errorString: 'Can''t coerce an instance of Complex to a Float'
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
asInteger
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   282
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   283
	imaginary = 0 ifTrue: [^real asInteger].
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   284
	^Number
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   285
		raise: #coercionErrorSignal
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   286
		receiver: self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   287
		selector: #asInteger
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   288
		errorString: 'Can''t coerce an instance of Complex to an Integer'
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   289
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   290
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   291
asPoint
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   292
	"Return the complex number as a point."
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   293
	^ real @ imaginary
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   294
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   295
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   296
reduceGeneralityIfPossible
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   297
	"Answer the receiver transformed to a lower generality, if such a 
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   298
	transformation is possible without losing information. If not, answer 
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   299
	the receiver"
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   300
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   301
	imaginary isZero
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   302
		ifTrue: [^real]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   303
		ifFalse: [^self]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   304
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   305
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   306
!Complex methodsFor:'double dispatching'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   307
f014922e3b71 Initial revision
claus
parents:
diff changeset
   308
differenceFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   309
	^ argument asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   310
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   311
f014922e3b71 Initial revision
claus
parents:
diff changeset
   312
differenceFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   313
	^ argument asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   314
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   315
f014922e3b71 Initial revision
claus
parents:
diff changeset
   316
differenceFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   317
	^ argument asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   318
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   319
f014922e3b71 Initial revision
claus
parents:
diff changeset
   320
productFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   321
	^ argument asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   322
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   323
f014922e3b71 Initial revision
claus
parents:
diff changeset
   324
productFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   325
	^ argument asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   326
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   327
f014922e3b71 Initial revision
claus
parents:
diff changeset
   328
productFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   329
	^ argument asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   330
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   331
f014922e3b71 Initial revision
claus
parents:
diff changeset
   332
quotientFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   333
	^ argument asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   334
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   335
f014922e3b71 Initial revision
claus
parents:
diff changeset
   336
quotientFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   337
	^ argument asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   338
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   339
f014922e3b71 Initial revision
claus
parents:
diff changeset
   340
quotientFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   341
	^ argument asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   342
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   343
f014922e3b71 Initial revision
claus
parents:
diff changeset
   344
sumFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   345
	^ argument asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   346
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   347
f014922e3b71 Initial revision
claus
parents:
diff changeset
   348
sumFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   349
	^ argument asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   350
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   351
f014922e3b71 Initial revision
claus
parents:
diff changeset
   352
sumFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   353
	^ argument asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   354
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   355
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   356
!Complex methodsFor:'mathematical functions'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   357
f014922e3b71 Initial revision
claus
parents:
diff changeset
   358
angle
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   359
	"Return the radian angle for this Complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   360
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   361
	real < 0 ifTrue: [
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   362
	    imaginary < 0 ifTrue: [
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   363
		^ (imaginary / real) arcTan - Float pi
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   364
	    ].
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   365
	    ^ Float pi + (imaginary / real) arcTan
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   366
	].
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   367
	^ (imaginary / real) arcTan
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   368
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   369
f014922e3b71 Initial revision
claus
parents:
diff changeset
   370
exp
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   371
	"Return the complex exponential of the receiver."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   372
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   373
	^ imaginary cos % imaginary sin * real exp
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   374
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   375
f014922e3b71 Initial revision
claus
parents:
diff changeset
   376
sqrt
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   377
	"Return the square root of the receiver"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   378
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   379
	| u v |
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   380
	(imaginary = 0 and: [real >= 0]) ifTrue: [^real sqrt].
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   381
	v := (self abs - real / 2) sqrt.
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   382
	u := imaginary / 2 / v.
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   383
	^Complex real: u imaginary: v
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   384
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   385
	"-4 asComplex sqrt"
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   386
	"-4 asComplex sqrt squared"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   387
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   388
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   389
!Complex methodsFor:'printing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   390
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   391
printOn: aStream
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   392
	aStream nextPut: $(.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   393
	real storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   394
	aStream nextPutAll: '%'.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   395
	imaginary storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   396
	aStream nextPut: $).
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   397
!
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
printString
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   400
	^ '(' , real printString, '%', imaginary printString, ')'
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   401
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   402
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   403
storeOn: aStream
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   404
	aStream nextPut: $(.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   405
	real storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   406
	aStream nextPutAll: '%'.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   407
	imaginary storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   408
	aStream nextPut: $).
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   409
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   410
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   411
!Complex methodsFor:'private'!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   412
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   413
setReal: u setImaginary: v
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   414
	real := u.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   415
	imaginary := v.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   416
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   417
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   418
!Complex methodsFor:'testing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   419
f014922e3b71 Initial revision
claus
parents:
diff changeset
   420
isComplex
f014922e3b71 Initial revision
claus
parents:
diff changeset
   421
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   422
	^true
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   423
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   424
f014922e3b71 Initial revision
claus
parents:
diff changeset
   425
isReal
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   426
	"Return true if this Complex number has a zero imaginary part."
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   427
	^ imaginary = 0
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   428
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   429
f014922e3b71 Initial revision
claus
parents:
diff changeset
   430
isZero
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   431
	"Answer whether 'self = self class zero'.  We can't use #= because
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   432
	#= is defined in terms of #isZero"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   433
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   434
	^real isZero and: [imaginary isZero]
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   435
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   436
f014922e3b71 Initial revision
claus
parents:
diff changeset
   437
sign
f014922e3b71 Initial revision
claus
parents:
diff changeset
   438
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   439
	^Complex real: real sign imaginary: imaginary sign
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   440
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   441
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   442
!Complex class methodsFor:'documentation'!
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   443
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   444
version
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
   445
    ^ '$Header: /cvs/stx/stx/libbasic/Complex.st,v 1.6 2002-04-11 14:03:42 cg Exp $'
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   446
! !