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