Complex.st
author Claus Gittinger <cg@exept.de>
Thu, 02 Mar 2000 15:14:57 +0100
changeset 5287 b3b0d0e3ce98
parent 3637 2b4d733c9a01
child 6500 d2ce5b0a1a78
permissions -rw-r--r--
package-definitions fixed/updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
     1
"{ Package: 'stx:goodies' }"
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
     2
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     3
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     4
 This is a Manchester Goodie.  It is distributed freely on condition
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     5
 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
     6
 any significant part of it which is separately transmitted or stored:
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
     7
	* You must ensure that every copy includes this notice, and that
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
     8
	  source and author(s) of the material are acknowledged.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
     9
	* These conditions must be imposed on anyone who receives a copy.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    10
	* The material shall not be used for commercial gain without the prior
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    11
	  written consent of the author(s).
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    12
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    13
 For more information about the Manchester Goodies Library (from which 
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    14
 this file was distributed) send e-mail:
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    15
	To: goodies-lib@cs.man.ac.uk
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    16
	Subject: help 
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    17
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    18
 This is an additional goody-class, which is NOT covered by the
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    19
 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
    20
 make your live easier instead. NO WARRANTY.
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    21
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
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
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
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.
f014922e3b71 Initial revision
claus
parents:
diff changeset
    58
  Complex numbers can be used in many of the same places that regular numbers
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
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    70
! !
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    71
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    72
!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
    73
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    74
fromReal: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    75
	"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
    76
	^ 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
    77
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    78
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    79
real: u imaginary: v
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    80
	"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
    81
	 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
    82
	^v = 0 ifTrue: [u]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    83
	       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
    84
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    85
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    86
!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
    87
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    88
unity
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    89
	"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
    90
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    91
	 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
    92
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    93
	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
    94
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    95
	^self fromReal: 1
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    96
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
    97
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    98
zero
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 zero = 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: 0
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   106
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   107
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   108
!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
   109
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   110
trapImaginary: aBlock
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   111
	"Complex trapImaginary: [-27 sqrt]"
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
	| send |
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
	^Number domainErrorSignal handle: [ :ex |
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   116
	    send := ex parameter.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   117
	    (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
   118
		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
   119
		ex proceedWith: send value
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   120
	    ] ifFalse: [
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   121
		ex reject
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
	] do: aBlock
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
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   126
!Complex methodsFor:'accessing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   127
f014922e3b71 Initial revision
claus
parents:
diff changeset
   128
imaginary
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   129
	"Return the imaginary part of the complex number."
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   130
	^ imaginary
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   131
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   132
f014922e3b71 Initial revision
claus
parents:
diff changeset
   133
real
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   134
	"Return the real part of the complex number."
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   135
	^ real
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   136
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   137
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   138
!Complex methodsFor:'arithmetic'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   139
f014922e3b71 Initial revision
claus
parents:
diff changeset
   140
* aNumber 
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   141
	"Return the product of the receiver and the argument."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   142
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   143
	| u v r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   144
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   145
	aNumber isComplex ifTrue:[
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   146
	    u := aNumber real.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   147
	    v := aNumber imaginary.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   148
	    r := (real * u) - (imaginary * v).
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   149
	    i  := (real * v) + (imaginary * u).
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   150
	    i = 0 ifTrue:[ ^ r ].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   151
	    ^ Complex real:r imaginary:i
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   152
	].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   153
	^ 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
   154
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   155
    "Modified: / 8.7.1998 / 12:12:37 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   156
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   157
f014922e3b71 Initial revision
claus
parents:
diff changeset
   158
+ aNumber 
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   159
	"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
   160
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   161
	| r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   162
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   163
	aNumber isComplex ifTrue: [
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   164
	    r := aNumber real + real.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   165
	    i := aNumber imaginary + imaginary.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   166
	    i = 0 ifTrue:[ ^ r ].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   167
	    ^ Complex real:r imaginary:i
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   168
	].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   169
	^ 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
   170
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   171
    "Modified: / 8.7.1998 / 12:15:42 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   172
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   173
f014922e3b71 Initial revision
claus
parents:
diff changeset
   174
- aNumber
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   175
	"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
   176
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   177
	| r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   178
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   179
	aNumber isComplex ifTrue: [
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   180
	    r := real - aNumber real.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   181
	    i := imaginary - aNumber imaginary.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   182
	    i = 0 ifTrue:[ ^ r ].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   183
	    ^ Complex real:r imaginary:i.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   184
	].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   185
	^ 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
   186
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   187
    "Modified: / 8.7.1998 / 12:15:38 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   188
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   189
f014922e3b71 Initial revision
claus
parents:
diff changeset
   190
/ aNumber 
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   191
	"Return the quotient of the receiver and the argument."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   192
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   193
	| denom u v r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   194
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   195
	aNumber isComplex ifTrue:[ 
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   196
	    u := aNumber real.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   197
	    v := aNumber imaginary.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   198
	    denom := u * u + (v * v).
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   199
	    r := u * real + (v * imaginary) / denom.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   200
	    i := u * imaginary - (v * real) / denom.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   201
	    i = 0 ifTrue:[ ^ r ].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   202
	    ^ Complex real:r imaginary:i
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   203
	].
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   204
	^ 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
   205
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   206
    "Modified: / 8.7.1998 / 12:15:34 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   207
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   208
f014922e3b71 Initial revision
claus
parents:
diff changeset
   209
abs
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   210
	"Return the magnitude (or absolute value) of the complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   211
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   212
	^ (real * real + (imaginary * imaginary)) sqrt
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   213
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   214
f014922e3b71 Initial revision
claus
parents:
diff changeset
   215
conjugated
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   216
	"Return the complex conjugate of this complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   217
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   218
	^ Complex real: real imaginary: imaginary negated
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   219
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   220
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   221
!Complex methodsFor:'coercing'!
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
coerce: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   224
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   225
	^aNumber asComplex
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   226
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   227
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   228
generality
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   229
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   230
	^150
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   231
! !
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
!Complex methodsFor:'comparing'!
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
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   236
	^Number
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   237
		raise: #unorderedSignal
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   238
		receiver: self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   239
		selector: #<
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   240
		arg: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   241
		errorString: 'Complex numbers are not well ordered'!
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
= aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   244
	^ (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
   245
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   246
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   247
hash
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   248
	"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
   249
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   250
	^ real hash
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   251
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   252
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   253
!Complex methodsFor:'converting'!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   254
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   255
asComplex
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
	^self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   258
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   259
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   260
asFloat
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   261
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   262
	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
   263
	^Number
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   264
		raise: #coercionErrorSignal
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   265
		receiver: self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   266
		selector: #asFloat
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   267
		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
   268
!
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
asInteger
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   271
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   272
	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
   273
	^Number
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   274
		raise: #coercionErrorSignal
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   275
		receiver: self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   276
		selector: #asInteger
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   277
		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
   278
!
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
asPoint
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   281
	"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
   282
	^ real @ imaginary
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   283
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   284
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   285
reduceGeneralityIfPossible
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   286
	"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
   287
	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
   288
	the receiver"
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
	imaginary isZero
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   291
		ifTrue: [^real]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   292
		ifFalse: [^self]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   293
! !
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
!Complex methodsFor:'double dispatching'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   296
f014922e3b71 Initial revision
claus
parents:
diff changeset
   297
differenceFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   298
	^ argument asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   299
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   300
f014922e3b71 Initial revision
claus
parents:
diff changeset
   301
differenceFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   302
	^ argument asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   303
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   304
f014922e3b71 Initial revision
claus
parents:
diff changeset
   305
differenceFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   306
	^ argument asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   307
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   308
f014922e3b71 Initial revision
claus
parents:
diff changeset
   309
productFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   310
	^ argument asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   311
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   312
f014922e3b71 Initial revision
claus
parents:
diff changeset
   313
productFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   314
	^ argument asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   315
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   316
f014922e3b71 Initial revision
claus
parents:
diff changeset
   317
productFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   318
	^ argument asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   319
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   320
f014922e3b71 Initial revision
claus
parents:
diff changeset
   321
quotientFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   322
	^ argument asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   323
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   324
f014922e3b71 Initial revision
claus
parents:
diff changeset
   325
quotientFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   326
	^ argument asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   327
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   328
f014922e3b71 Initial revision
claus
parents:
diff changeset
   329
quotientFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   330
	^ argument asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   331
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   332
f014922e3b71 Initial revision
claus
parents:
diff changeset
   333
sumFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   334
	^ argument asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   335
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   336
f014922e3b71 Initial revision
claus
parents:
diff changeset
   337
sumFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   338
	^ argument asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   339
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   340
f014922e3b71 Initial revision
claus
parents:
diff changeset
   341
sumFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   342
	^ argument asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   343
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   344
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   345
!Complex methodsFor:'mathematical functions'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   346
f014922e3b71 Initial revision
claus
parents:
diff changeset
   347
angle
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   348
	"Return the radian angle for this Complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   349
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   350
	real < 0 ifTrue: [
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   351
	    imaginary < 0 ifTrue: [
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   352
		^ (imaginary / real) arcTan - Float pi
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   353
	    ].
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   354
	    ^ Float pi + (imaginary / real) arcTan
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   355
	].
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   356
	^ (imaginary / real) arcTan
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   357
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   358
f014922e3b71 Initial revision
claus
parents:
diff changeset
   359
exp
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   360
	"Return the complex exponential of the receiver."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   361
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   362
	^ imaginary cos % imaginary sin * real exp
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   363
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   364
f014922e3b71 Initial revision
claus
parents:
diff changeset
   365
sqrt
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   366
	"Return the square root of the receiver"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   367
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   368
	| u v |
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   369
	(imaginary = 0 and: [real >= 0]) ifTrue: [^real sqrt].
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   370
	v := (self abs - real / 2) sqrt.
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   371
	u := imaginary / 2 / v.
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   372
	^Complex real: u imaginary: v
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   373
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   374
	"-4 asComplex sqrt"
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   375
	"-4 asComplex sqrt squared"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   376
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   377
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   378
!Complex methodsFor:'printing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   379
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   380
printOn: aStream
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   381
	aStream nextPut: $(.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   382
	real storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   383
	aStream nextPutAll: '%'.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   384
	imaginary storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   385
	aStream nextPut: $).
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   386
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   387
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   388
printString
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   389
	^ '(' , real printString, '%', imaginary printString, ')'
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   390
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   391
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   392
storeOn: aStream
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   393
	aStream nextPut: $(.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   394
	real storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   395
	aStream nextPutAll: '%'.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   396
	imaginary storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   397
	aStream nextPut: $).
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   398
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   399
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   400
!Complex methodsFor:'private'!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   401
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   402
setReal: u setImaginary: v
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   403
	real := u.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   404
	imaginary := v.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   405
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   406
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   407
!Complex methodsFor:'testing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   408
f014922e3b71 Initial revision
claus
parents:
diff changeset
   409
isComplex
f014922e3b71 Initial revision
claus
parents:
diff changeset
   410
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   411
	^true
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   412
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   413
f014922e3b71 Initial revision
claus
parents:
diff changeset
   414
isReal
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   415
	"Return true if this Complex number has a zero imaginary part."
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   416
	^ imaginary = 0
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   417
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   418
f014922e3b71 Initial revision
claus
parents:
diff changeset
   419
isZero
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   420
	"Answer whether 'self = self class zero'.  We can't use #= because
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   421
	#= is defined in terms of #isZero"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   422
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   423
	^real isZero and: [imaginary isZero]
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   424
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   425
f014922e3b71 Initial revision
claus
parents:
diff changeset
   426
sign
f014922e3b71 Initial revision
claus
parents:
diff changeset
   427
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   428
	^Complex real: real sign imaginary: imaginary sign
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   429
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   430
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   431
!Complex class methodsFor:'documentation'!
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   432
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   433
version
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
   434
    ^ '$Header: /cvs/stx/stx/libbasic/Complex.st,v 1.5 2000-03-02 14:14:56 cg Exp $'
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   435
! !