Complex.st
author Claus Gittinger <cg@exept.de>
Thu, 12 Aug 1999 10:59:29 +0200
changeset 4595 24446fd5d3e1
parent 3637 2b4d733c9a01
child 5287 b3b0d0e3ce98
permissions -rw-r--r--
checkin from browser
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:
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     5
        * You must ensure that every copy includes this notice, and that
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     6
          source and author(s) of the material are acknowledged.
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     7
        * These conditions must be imposed on anyone who receives a copy.
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     8
        * The material shall not be used for commercial gain without the prior
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     9
          written consent of the author(s).
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:
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    13
        To: goodies-lib@cs.man.ac.uk
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    14
        Subject: help 
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
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    21
ArithmeticValue subclass:#Complex
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    22
	instanceVariableNames:'real imaginary'
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    23
	classVariableNames:''
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    24
	poolDictionaries:''
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    25
	category:'Magnitude-Numbers'
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    26
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
    27
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    28
!Complex class methodsFor:'documentation'!
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    29
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    30
copyright
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
 This is a Manchester Goodie.  It is distributed freely on condition
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    33
 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
    34
 any significant part of it which is separately transmitted or stored:
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    35
        * You must ensure that every copy includes this notice, and that
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    36
          source and author(s) of the material are acknowledged.
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    37
        * These conditions must be imposed on anyone who receives a copy.
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    38
        * The material shall not be used for commercial gain without the prior
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    39
          written consent of the author(s).
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    40
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    41
 For more information about the Manchester Goodies Library (from which 
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    42
 this file was distributed) send e-mail:
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    43
        To: goodies-lib@cs.man.ac.uk
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    44
        Subject: help 
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    45
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    46
 This is an additional goody-class, which is NOT covered by the
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    47
 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
    48
 make your live easier instead. NO WARRANTY.
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    49
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    50
!
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
documentation
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
This is an implementation of complex numbers.  A complex number has real and
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    55
 imaginary parts which must be manipulated simultaneously in any numeric processing.
f014922e3b71 Initial revision
claus
parents:
diff changeset
    56
  Complex numbers can be used in many of the same places that regular numbers
f014922e3b71 Initial revision
claus
parents:
diff changeset
    57
 can be used with one major exception of comparisons, since complex numbers cannot
f014922e3b71 Initial revision
claus
parents:
diff changeset
    58
 be directly compared for size (except through lengths of vectors (see absolute
f014922e3b71 Initial revision
claus
parents:
diff changeset
    59
 value)).
f014922e3b71 Initial revision
claus
parents:
diff changeset
    60
f014922e3b71 Initial revision
claus
parents:
diff changeset
    61
Instance variables:
f014922e3b71 Initial revision
claus
parents:
diff changeset
    62
    real        <Number> the part of the number which can be expressed as a Real number
f014922e3b71 Initial revision
claus
parents:
diff changeset
    63
    imaginary   <Number> the part of the number which, in terms of how the number behaves,
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    64
                         has been multiplied by 'i' (-1 sqrt)
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    65
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    66
Author: Kurt Hebel (hebel@uinova.cerl.uiuc.edu)
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    67
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    68
! !
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    69
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    70
!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
    71
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    72
fromReal: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    73
	"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
    74
	^ 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
    75
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    76
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    77
real: u imaginary: v
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    78
	"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
    79
	 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
    80
	^v = 0 ifTrue: [u]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    81
	       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
    82
! !
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
!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
    85
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    86
unity
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    87
	"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
    88
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    89
	 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
    90
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    91
	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
    92
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    93
	^self fromReal: 1
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    94
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
    95
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    96
zero
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    97
	"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
    98
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    99
		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
   100
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   101
	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
   102
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   103
	^self fromReal: 0
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
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   106
!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
   107
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   108
trapImaginary: aBlock
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   109
	"Complex trapImaginary: [-27 sqrt]"
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
	| send |
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
	^Number domainErrorSignal handle: [ :ex |
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   114
	    send := ex parameter.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   115
	    (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
   116
		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
   117
		ex proceedWith: send value
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   118
	    ] ifFalse: [
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   119
		ex reject
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   120
	    ]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   121
	] do: aBlock
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
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   124
!Complex methodsFor:'accessing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   125
f014922e3b71 Initial revision
claus
parents:
diff changeset
   126
imaginary
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   127
	"Return the imaginary part of the complex number."
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   128
	^ imaginary
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   129
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   130
f014922e3b71 Initial revision
claus
parents:
diff changeset
   131
real
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   132
	"Return the real part of the complex number."
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   133
	^ real
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   134
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   135
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   136
!Complex methodsFor:'arithmetic'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   137
f014922e3b71 Initial revision
claus
parents:
diff changeset
   138
* aNumber 
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   139
        "Return the product of the receiver and the argument."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   140
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   141
        | u v r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   142
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   143
        aNumber isComplex ifTrue:[
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   144
            u := aNumber real.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   145
            v := aNumber imaginary.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   146
            r := (real * u) - (imaginary * v).
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   147
            i  := (real * v) + (imaginary * u).
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   148
            i = 0 ifTrue:[ ^ r ].
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   149
            ^ Complex real:r imaginary:i
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   150
        ].
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   151
        ^ self retry: #* coercing: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   152
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   153
    "Modified: / 8.7.1998 / 12:12:37 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   154
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   155
f014922e3b71 Initial revision
claus
parents:
diff changeset
   156
+ aNumber 
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   157
        "Return the sum of the receiver and the argument."
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   158
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   159
        | r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   160
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   161
        aNumber isComplex ifTrue: [
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   162
            r := aNumber real + real.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   163
            i := aNumber imaginary + imaginary.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   164
            i = 0 ifTrue:[ ^ r ].
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   165
            ^ Complex real:r imaginary:i
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   166
        ].
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   167
        ^ self retry: #+ coercing: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   168
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   169
    "Modified: / 8.7.1998 / 12:15:42 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   170
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   171
f014922e3b71 Initial revision
claus
parents:
diff changeset
   172
- aNumber
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   173
        "Return the difference of the receiver and the argument."
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   174
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   175
        | r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   176
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   177
        aNumber isComplex ifTrue: [
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   178
            r := real - aNumber real.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   179
            i := imaginary - aNumber imaginary.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   180
            i = 0 ifTrue:[ ^ r ].
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   181
            ^ Complex real:r imaginary:i.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   182
        ].
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   183
        ^ self retry: #- coercing: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   184
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   185
    "Modified: / 8.7.1998 / 12:15:38 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   186
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   187
f014922e3b71 Initial revision
claus
parents:
diff changeset
   188
/ aNumber 
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   189
        "Return the quotient of the receiver and the argument."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   190
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   191
        | denom u v r i |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   192
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   193
        aNumber isComplex ifTrue:[ 
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   194
            u := aNumber real.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   195
            v := aNumber imaginary.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   196
            denom := u * u + (v * v).
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   197
            r := u * real + (v * imaginary) / denom.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   198
            i := u * imaginary - (v * real) / denom.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   199
            i = 0 ifTrue:[ ^ r ].
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   200
            ^ Complex real:r imaginary:i
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   201
        ].
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   202
        ^ self retry: #/ coercing: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   203
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   204
    "Modified: / 8.7.1998 / 12:15:34 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   205
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   206
f014922e3b71 Initial revision
claus
parents:
diff changeset
   207
abs
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   208
	"Return the magnitude (or absolute value) of the complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   209
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   210
	^ (real * real + (imaginary * imaginary)) sqrt
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   211
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   212
f014922e3b71 Initial revision
claus
parents:
diff changeset
   213
conjugated
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   214
	"Return the complex conjugate of this complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   215
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   216
	^ Complex real: real imaginary: imaginary negated
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   217
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   218
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   219
!Complex methodsFor:'coercing'!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   220
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   221
coerce: aNumber
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
	^aNumber asComplex
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
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   226
generality
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
	^150
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
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   231
!Complex methodsFor:'comparing'!
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
< aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   234
	^Number
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   235
		raise: #unorderedSignal
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   236
		receiver: self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   237
		selector: #<
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   238
		arg: aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   239
		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
   240
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   241
= aNumber
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   242
	^ (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
   243
!
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
hash
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   246
	"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
   247
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   248
	^ real hash
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
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   251
!Complex methodsFor:'converting'!
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
asComplex
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
	^self
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
asFloat
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
	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
   261
	^Number
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   262
		raise: #coercionErrorSignal
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   263
		receiver: self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   264
		selector: #asFloat
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   265
		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
   266
!
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
asInteger
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
	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
   271
	^Number
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   272
		raise: #coercionErrorSignal
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   273
		receiver: self
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   274
		selector: #asInteger
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   275
		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
   276
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   277
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   278
asPoint
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   279
	"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
   280
	^ real @ imaginary
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   281
!
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
reduceGeneralityIfPossible
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   284
	"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
   285
	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
   286
	the receiver"
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   287
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   288
	imaginary isZero
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   289
		ifTrue: [^real]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   290
		ifFalse: [^self]
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   291
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   292
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   293
!Complex methodsFor:'double dispatching'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   294
f014922e3b71 Initial revision
claus
parents:
diff changeset
   295
differenceFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   296
	^ argument asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   297
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   298
f014922e3b71 Initial revision
claus
parents:
diff changeset
   299
differenceFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   300
	^ argument asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   301
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   302
f014922e3b71 Initial revision
claus
parents:
diff changeset
   303
differenceFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   304
	^ argument asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   305
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   306
f014922e3b71 Initial revision
claus
parents:
diff changeset
   307
productFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   308
	^ argument asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   309
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   310
f014922e3b71 Initial revision
claus
parents:
diff changeset
   311
productFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   312
	^ argument asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   313
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   314
f014922e3b71 Initial revision
claus
parents:
diff changeset
   315
productFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   316
	^ argument asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   317
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   318
f014922e3b71 Initial revision
claus
parents:
diff changeset
   319
quotientFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   320
	^ argument asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   321
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   322
f014922e3b71 Initial revision
claus
parents:
diff changeset
   323
quotientFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   324
	^ argument asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   325
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   326
f014922e3b71 Initial revision
claus
parents:
diff changeset
   327
quotientFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   328
	^ argument asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   329
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   330
f014922e3b71 Initial revision
claus
parents:
diff changeset
   331
sumFromFloat: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   332
	^ argument asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   333
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   334
f014922e3b71 Initial revision
claus
parents:
diff changeset
   335
sumFromFraction: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   336
	^ argument asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   337
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   338
f014922e3b71 Initial revision
claus
parents:
diff changeset
   339
sumFromInteger: argument
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   340
	^ argument asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   341
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   342
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   343
!Complex methodsFor:'mathematical functions'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   344
f014922e3b71 Initial revision
claus
parents:
diff changeset
   345
angle
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   346
	"Return the radian angle for this Complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   347
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   348
	real < 0 ifTrue: [
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   349
	    imaginary < 0 ifTrue: [
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   350
		^ (imaginary / real) arcTan - Float pi
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   351
	    ].
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   352
	    ^ Float pi + (imaginary / real) arcTan
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   353
	].
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   354
	^ (imaginary / real) arcTan
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   355
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   356
f014922e3b71 Initial revision
claus
parents:
diff changeset
   357
exp
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   358
	"Return the complex exponential of the receiver."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   359
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   360
	^ imaginary cos % imaginary sin * real exp
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   361
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   362
f014922e3b71 Initial revision
claus
parents:
diff changeset
   363
sqrt
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   364
	"Return the square root of the receiver"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   365
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   366
	| u v |
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   367
	(imaginary = 0 and: [real >= 0]) ifTrue: [^real sqrt].
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   368
	v := (self abs - real / 2) sqrt.
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   369
	u := imaginary / 2 / v.
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   370
	^Complex real: u imaginary: v
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   371
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   372
	"-4 asComplex sqrt"
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   373
	"-4 asComplex sqrt squared"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   374
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   375
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   376
!Complex methodsFor:'printing'!
265
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
printOn: aStream
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   379
	aStream nextPut: $(.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   380
	real storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   381
	aStream nextPutAll: '%'.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   382
	imaginary storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   383
	aStream nextPut: $).
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   384
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   385
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   386
printString
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   387
	^ '(' , real printString, '%', imaginary printString, ')'
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   388
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   389
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   390
storeOn: aStream
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   391
	aStream nextPut: $(.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   392
	real storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   393
	aStream nextPutAll: '%'.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   394
	imaginary storeOn: aStream.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   395
	aStream nextPut: $).
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   396
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   397
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   398
!Complex methodsFor:'private'!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   399
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   400
setReal: u setImaginary: v
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   401
	real := u.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   402
	imaginary := v.
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   403
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   404
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   405
!Complex methodsFor:'testing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   406
f014922e3b71 Initial revision
claus
parents:
diff changeset
   407
isComplex
f014922e3b71 Initial revision
claus
parents:
diff changeset
   408
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   409
	^true
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   410
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   411
f014922e3b71 Initial revision
claus
parents:
diff changeset
   412
isReal
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   413
	"Return true if this Complex number has a zero imaginary part."
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   414
	^ imaginary = 0
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   415
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   416
f014922e3b71 Initial revision
claus
parents:
diff changeset
   417
isZero
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   418
	"Answer whether 'self = self class zero'.  We can't use #= because
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   419
	#= is defined in terms of #isZero"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   420
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   421
	^real isZero and: [imaginary isZero]
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   422
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   423
f014922e3b71 Initial revision
claus
parents:
diff changeset
   424
sign
f014922e3b71 Initial revision
claus
parents:
diff changeset
   425
1944
9fb0b642d2ca packages
Claus Gittinger <cg@exept.de>
parents: 265
diff changeset
   426
	^Complex real: real sign imaginary: imaginary sign
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   427
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   428
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   429
!Complex class methodsFor:'documentation'!
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   430
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   431
version
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   432
    ^ '$Header: /cvs/stx/stx/libbasic/Complex.st,v 1.4 1998-07-09 08:25:14 cg Exp $'
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   433
! !