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