Complex.st
author Claus Gittinger <cg@exept.de>
Mon, 27 Jan 2020 13:47:24 +0100
changeset 25204 b12f8693fe6f
parent 25064 a2de00e61eff
permissions -rw-r--r--
#BUGFIX by cg class: CharacterArray added: #asImmutableCollection #asImmutableString
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25062
1704e428aea4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25006
diff changeset
     1
"{ Encoding: utf8 }"
1704e428aea4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25006
diff changeset
     2
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     3
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     4
 This is a Manchester Goodie.  It is distributed freely on condition
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     5
 that you observe these conditions in respect of the whole Goodie, and on
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
     6
 any significant part of it which is separately transmitted or stored:
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
     7
	* You must ensure that every copy includes this notice, and that
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
     8
	  source and author(s) of the material are acknowledged.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
     9
	* These conditions must be imposed on anyone who receives a copy.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    10
	* The material shall not be used for commercial gain without the prior
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    11
	  written consent of the author(s).
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    12
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
    13
 For more information about the Manchester Goodies Library (from which
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    14
 this file was distributed) send e-mail:
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    15
	To: goodies-lib@cs.man.ac.uk
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
    16
	Subject: help
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    17
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    18
 This is an additional goody-class, which is NOT covered by the
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    19
 ST/X license. It has been packaged with the ST/X distribution to
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    20
 make your live easier instead. NO WARRANTY.
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    21
"
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
    22
"{ Package: 'stx:libbasic' }"
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    23
18836
63832ff17b6c #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 16742
diff changeset
    24
"{ NameSpace: Smalltalk }"
63832ff17b6c #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 16742
diff changeset
    25
23167
3d8ef4a8970d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 23134
diff changeset
    26
Number subclass:#Complex
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    27
	instanceVariableNames:'real imaginary'
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
    28
	classVariableNames:'ComplexOne ComplexZero'
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    29
	poolDictionaries:''
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
    30
	category:'Magnitude-Numbers'
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    31
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
    32
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    33
!Complex class methodsFor:'documentation'!
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
copyright
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    36
"
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    37
 This is a Manchester Goodie.  It is distributed freely on condition
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    38
 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
    39
 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
    40
	* 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
    41
	  source and author(s) of the material are acknowledged.
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    42
	* 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
    43
	* 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
    44
	  written consent of the author(s).
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    45
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
    46
 For more information about the Manchester Goodies Library (from which
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    47
 this file was distributed) send e-mail:
5287
b3b0d0e3ce98 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 3637
diff changeset
    48
	To: goodies-lib@cs.man.ac.uk
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
    49
	Subject: help
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    50
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    51
 This is an additional goody-class, which is NOT covered by the
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    52
 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
    53
 make your live easier instead. NO WARRANTY.
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
!
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    56
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    57
documentation
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    58
"
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
    59
    This class implements complex numbers.
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
    60
    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
    61
    in any numeric processing.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
    62
    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
    63
    can be used with one major exception of comparisons, since complex numbers cannot
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
    64
    be directly compared for size
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
    65
    (except through lengths of vectors (see absolute value)).
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
    66
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
    67
    [Instance variables:]
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
    68
       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
    69
       imaginary   <Number> the part of the number which, in terms of how the number behaves,
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    70
                            has been multiplied by 'i' (-1 sqrt)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    71
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    72
    [Constructors:]                         
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    73
        5 i
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    74
        6 + 7 i.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    75
        5.6 - 8 i.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    76
        Complex real: 10 imaginary: 5.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    77
        Complex abs: 5 arg: (Float pi / 4)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    78
25064
a2de00e61eff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25063
diff changeset
    79
    NOTE (from porter):
a2de00e61eff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25063
diff changeset
    80
        The original author's Complex class did not inherit from Number and had therefore to reimplement
a2de00e61eff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25063
diff changeset
    81
        a lot of useful code (reading, testing, mathematical functions, etc.)
a2de00e61eff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25063
diff changeset
    82
        Therefore we chose to place it under the number hierarchy, blocking some of the methods
a2de00e61eff #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25063
diff changeset
    83
        (eg. comparing, floor, ceiling, etc.)
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    84
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
    85
    [Author:]
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    86
        Kurt Hebel (hebel@uinova.cerl.uiuc.edu)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    87
        minor changes and double dispatching code by cg.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
    88
        additions (trigonometric) and fixes by cg.
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
    89
"
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    90
!
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    91
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
    92
examples
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
    93
"
21932
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
    94
    -25 sqrt                                  -> error
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
    95
    Complex trapImaginary:[ -25 sqrt ]        -> 5i (0.0+5.0i)
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
    96
    Complex trapImaginary:[ -25 integerSqrt ] -> 5i (0+5i)
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
    97
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
    98
    (Complex trapImaginary:[ -5397346292805549782720214077673687804022210808238353958670041357153884304 integerSqrt ])
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
    99
        squared
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   100
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   101
    1 + 3i
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   102
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   103
    Number i + 1
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   104
    1 + Number i
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   105
25063
bd3ba20222a4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25062
diff changeset
   106
    1 i + 1
bd3ba20222a4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25062
diff changeset
   107
    1 + 1 i
bd3ba20222a4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25062
diff changeset
   108
21932
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   109
    1i * 1i
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   110
    Number i * Number i
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   111
    
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
   112
    (5 % 7) real
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
   113
    (5 % 7) imaginary
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
   114
    (5 % 7) = 5
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   115
    (5 % 0) = 5
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   116
    (5.0 % 0) = 5
7221
b38093d749b3 documentation
Claus Gittinger <cg@exept.de>
parents: 6500
diff changeset
   117
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   118
    (1 % 0) + (2 % 0)
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   119
    (1 % 0) + (0 % 2)
7221
b38093d749b3 documentation
Claus Gittinger <cg@exept.de>
parents: 6500
diff changeset
   120
    (1 % 0) + (2 % 3)
b38093d749b3 documentation
Claus Gittinger <cg@exept.de>
parents: 6500
diff changeset
   121
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   122
    (1 % 0) * (2 % 0)
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   123
    (1 % 0) * (0 % 2)
7221
b38093d749b3 documentation
Claus Gittinger <cg@exept.de>
parents: 6500
diff changeset
   124
    (1 % 0) * (2 % 3)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   125
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   126
    (1 % 2) + 2
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   127
    (1 % 2) * 2
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   128
    2 + (1 % 2)
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   129
    2 * (1 % 2)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   130
21932
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   131
    (Number i raisedTo:-3) -> Number i
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   132
    (Number i raisedTo:-2) -> -1
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   133
    (Number i raisedTo:-1) -> Number i negated
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   134
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   135
    (Number i raisedTo:0) -> 1
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   136
    (Number i raisedTo:1) -> Number i
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   137
    (Number i raisedTo:2) -> -1
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   138
    (Number i raisedTo:3) -> Number i negated
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   139
    (Number i raisedTo:4) -> 1
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   140
    (Number i raisedTo:6) -> -1
21933
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   141
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   142
    3 raisedTo:Number i
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   143
    3 i raisedTo:Number i
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   144
21932
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   145
    5i * 5i     -> -25
c033a3755e36 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21873
diff changeset
   146
    5i squared  -> -25 
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
   147
"
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
   148
! !
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   149
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   150
!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
   151
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   152
abs:aNumber1 arg:aNumber2
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   153
    ^ self 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   154
        real:(aNumber1 * aNumber2 cos)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   155
        imaginary:(aNumber1 * aNumber2 sin).
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   156
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   157
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   158
     self abs:10 arg:2 -> (-4.16146836547142387+9.092974268256816954i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   159
    "
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   160
!
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   161
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   162
fromReal: aNumber
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   163
    "Create a new complex number from the given real number."
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   164
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   165
    ^ self basicNew 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   166
        setReal:aNumber setImaginary:0
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   167
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   168
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   169
     Complex fromReal:1.0 
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   170
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   171
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   172
    "Modified (comment): / 12-06-2017 / 20:42:56 / cg"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   173
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   174
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   175
imaginary:v
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   176
    "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
   177
     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
   178
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   179
    v = 0 ifTrue: [^ 0].
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   180
    ^ self basicNew setReal:0 setImaginary:v
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   181
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   182
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   183
     Complex imaginary:1.0 
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   184
     (0.0 % 1.0)
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   185
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   186
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   187
    "Modified (comment): / 12-06-2017 / 20:44:51 / cg"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   188
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   189
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   190
real:aNumber
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   191
    "Create a new complex number from the given real number."
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   192
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   193
    ^ self basicNew setReal:aNumber setImaginary:0
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   194
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   195
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   196
     Complex real:1.0 
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   197
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   198
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   199
    "Modified (comment): / 12-06-2017 / 20:42:14 / cg"
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   200
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   201
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   202
real:u imaginary:v
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   203
    "Create a new complex number with the given real and imaginary parts.  
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   204
     If the imaginary part is zero, return the real part of the number."
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   205
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   206
    v = 0 ifTrue: [^ u].
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   207
    ^ self basicNew setReal:u setImaginary:v
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   208
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   209
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   210
     Complex real:1.0 imaginary:2.0
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   211
     (1.0 % 2.0)
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   212
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   213
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   214
    "Modified (comment): / 12-06-2017 / 20:44:32 / cg"
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   215
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   216
21873
a2ed604af5c4 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21817
diff changeset
   217
!Complex class methodsFor:'coercing & converting'!
21817
5d251e1af13a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21798
diff changeset
   218
5d251e1af13a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21798
diff changeset
   219
coerce: aNumber
25006
c70ecfb14931 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24936
diff changeset
   220
    "convert the argument aNumber into an instance of the receiver (class) and return it."
21817
5d251e1af13a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21798
diff changeset
   221
5d251e1af13a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21798
diff changeset
   222
    ^ aNumber asComplex
5d251e1af13a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21798
diff changeset
   223
! !
5d251e1af13a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21798
diff changeset
   224
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   225
!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
   226
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   227
unity
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   228
    "Answer the value which allows, for any given arithmetic value, the following to be true:
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   229
        aNumber * aNumber class unity = aNumber
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   230
     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
   231
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   232
    ComplexOne isNil ifTrue:[
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   233
        ComplexOne := self basicNew setReal:1 setImaginary:0
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   234
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   235
    ^ ComplexOne
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   236
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   237
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   238
zero
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   239
    "Answer the value which allows, for any given arithmetic value, the following to be true:
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   240
        aNumber + aNumber class zero = aNumber
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   241
     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
   242
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   243
    ComplexZero isNil ifTrue:[
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   244
        ComplexZero := self basicNew setReal:0 setImaginary:0
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   245
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   246
    ^ ComplexZero
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   247
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   248
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   249
!Complex methodsFor:'accessing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   250
f014922e3b71 Initial revision
claus
parents:
diff changeset
   251
imaginary
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   252
    "Return the imaginary part of the complex number."
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   253
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   254
    ^ imaginary
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   255
!
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
imaginaryPart
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   258
    "Return the imaginary part of the complex number.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   259
     An alias for imaginary (for compatibility with other complex implementations)"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   260
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   261
    ^ imaginary
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   262
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   263
f014922e3b71 Initial revision
claus
parents:
diff changeset
   264
real
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   265
    "Return the real part of the complex number."
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   266
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   267
    ^ real
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   268
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   269
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   270
realPart
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   271
    "Return the real part of the complex number.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   272
     An alias for real (for compatibility with other complex implementations)"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   273
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   274
    ^ real
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   275
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   276
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   277
!Complex methodsFor:'arithmetic'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   278
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   279
* aNumber
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   280
    "Return the product of the receiver and the argument."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   281
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   282
"/  calling productFromComplex:self 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   283
"/  is just as fast than an isComplex call
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   284
"/  (unless the VM cheats)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   285
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   286
"/    | u v r i |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   287
"/
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   288
"/    aNumber isComplex ifTrue:[
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   289
"/        u := aNumber real.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   290
"/        v := aNumber imaginary.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   291
"/        r := (real * u) - (imaginary * v).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   292
"/        i  := (real * v) + (imaginary * u).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   293
"/        i = 0 ifTrue:[ ^ r ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   294
"/        ^ Complex real:r imaginary:i
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   295
"/    ].
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   296
    ^ aNumber productFromComplex:self.
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   297
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   298
    "Modified: / 8.7.1998 / 12:12:37 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   299
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   300
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   301
+ aNumber
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   302
    "Return the sum of the receiver and the argument."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   303
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   304
"/  calling sumFromComplex:self 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   305
"/  is just as fast than an isComplex call
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   306
"/  (unless the VM cheats)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   307
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   308
"/    | r i |
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
"/    aNumber isComplex ifTrue: [
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   311
"/        r := aNumber real + real.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   312
"/        i := aNumber imaginary + imaginary.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   313
"/        i = 0 ifTrue:[ ^ r ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   314
"/        ^ Complex real:r imaginary:i
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   315
"/    ].
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   316
    ^ aNumber sumFromComplex:self.
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   317
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   318
    "Modified: / 8.7.1998 / 12:15:42 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   319
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   320
f014922e3b71 Initial revision
claus
parents:
diff changeset
   321
- aNumber
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   322
    "Return the difference of the receiver and the argument."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   323
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   324
"/  calling differenceFromComplex:self 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   325
"/  is just as fast than an isComplex call
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   326
"/  (unless the VM cheats)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   327
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   328
"/    | r i |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   329
"/
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   330
"/    aNumber isComplex ifTrue: [
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   331
"/        r := real - aNumber real.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   332
"/        i := imaginary - aNumber imaginary.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   333
"/        i = 0 ifTrue:[ ^ r ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   334
"/        ^ Complex real:r imaginary:i.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   335
"/    ].
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   336
    ^ aNumber differenceFromComplex:self.
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   337
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   338
    "Modified: / 8.7.1998 / 12:15:38 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   339
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   340
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   341
/ aNumber
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   342
    "Return the quotient of the receiver and the argument."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   343
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   344
"/  calling quotientFromComplex:self 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   345
"/  is just as fast than an isComplex call
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   346
"/  (unless the VM cheats)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   347
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   348
"/    | denom u v r i |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   349
"/
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   350
"/    aNumber isComplex ifTrue:[
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   351
"/        u := aNumber real.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   352
"/        v := aNumber imaginary.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   353
"/        denom := u * u + (v * v).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   354
"/        r := u * real + (v * imaginary) / denom.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   355
"/        i := u * imaginary - (v * real) / denom.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   356
"/        i = 0 ifTrue:[ ^ r ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   357
"/        ^ Complex real:r imaginary:i
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   358
"/    ].
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   359
    ^ aNumber quotientFromComplex:self.
3633
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
    "Modified: / 8.7.1998 / 12:15:34 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   362
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   363
f014922e3b71 Initial revision
claus
parents:
diff changeset
   364
abs
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   365
    "Return the magnitude (or absolute value) of the complex number
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   366
     that's the distance from zero (the origin in the complex plane)."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   367
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   368
    ^ (real * real + (imaginary * imaginary)) sqrt
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   369
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   370
    "
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   371
     (1 + 1i) abs
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   372
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   373
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   374
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   375
absSecure
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   376
    "Answer the distance of the receiver from zero (0 + 0 i).
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   377
     Try avoiding overflow and/or underflow"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   378
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   379
    | scale |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   380
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   381
    scale := real abs max: imaginary abs.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   382
    scale isZero ifTrue: [^ scale].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   383
    ^ (self class 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   384
            real: real / scale 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   385
            imaginary: imaginary / scale)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   386
         squaredNorm sqrt * scale
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   387
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   388
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   389
     (10 + 4i) abs          -> 10.770329614269
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   390
     (10 + 4i) absSecure    -> 10.770329614269
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   391
    "
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   392
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   393
f014922e3b71 Initial revision
claus
parents:
diff changeset
   394
conjugated
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   395
    "Return the complex conjugate of this complex number
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   396
     (i.e. with imaginary part negated)."
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   397
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   398
    ^ self class
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   399
        real: real
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   400
        imaginary: imaginary negated
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   401
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   402
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   403
divideFastAndSecureBy: anObject
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   404
    "Answer the result of dividing receiver by aNumber"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   405
    " Both operands are scaled to avoid arithmetic overflow. 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   406
      This algorithm works for a wide range of values, and it needs only three divisions.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   407
      Note: #reciprocal uses #/ for devision "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   408
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   409
    | r d newReal newImaginary |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   410
    
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   411
    anObject isComplex ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   412
        (anObject real abs) > (anObject imaginary abs) ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   413
            r := anObject imaginary / anObject real.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   414
            d := r*anObject imaginary + anObject real.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   415
            newReal := r*imaginary + real/d.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   416
            newImaginary := r negated * real + imaginary/d.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   417
        ] ifFalse:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   418
            r := anObject real / anObject imaginary.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   419
            d := r*anObject real + anObject imaginary.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   420
            newReal := r*real + imaginary/d.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   421
            newImaginary := r*imaginary - real/d.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   422
        ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   423
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   424
        ^ Complex real: newReal imaginary: newImaginary
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   425
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   426
    ^ anObject adaptToComplex: self andSend: #/.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   427
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   428
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   429
divideSecureBy:anObject
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   430
    "Answer the result of dividing receiver by aNumber"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   431
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   432
    "Both operands are scaled to avoid arithmetic overflow. 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   433
     This algorithm works for a wide range of values, 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   434
     but it requires six divisions.  
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   435
     #divideFastAndSecureBy:  is also quite good, 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   436
     but it uses only 3 divisions.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   437
     Note: #reciprocal uses #/ for devision"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   438
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   439
    | s ars ais brs bis newReal newImaginary |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   440
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   441
    anObject isComplex ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   442
        s := anObject real abs + anObject imaginary abs.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   443
        ars := self real / s.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   444
        ais := self imaginary / s.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   445
        brs := anObject real / s.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   446
        bis := anObject imaginary / s.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   447
        s := brs squared + bis squared.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   448
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   449
        newReal := ars*brs + (ais*bis) /s.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   450
        newImaginary := ais*brs - (ars*bis)/s.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   451
        ^ Complex real:newReal imaginary:newImaginary
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   452
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   453
    ^ anObject adaptToComplex:self andSend:#/.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   454
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   455
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   456
i
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   457
    "Answer the result of multiplying the receiver with pure imaginary.
25062
1704e428aea4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25006
diff changeset
   458
        ^ self * 1 i
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   459
     This is an obvious extension of method i implemented in Number."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   460
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   461
    ^ self class 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   462
        real: imaginary negated 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   463
        imaginary: real
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   464
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   465
    "
24936
eaca1260a80c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24935
diff changeset
   466
     (10+4i)i -> (-4+10i)
eaca1260a80c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24935
diff changeset
   467
     1i * 1i  -> -1
eaca1260a80c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24935
diff changeset
   468
     1i i     -> -1
eaca1260a80c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24935
diff changeset
   469
     (3+4i) raisedTo: 2
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   470
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   471
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   472
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   473
modulus
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   474
    | absReal absImag multiplicand quotient |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   475
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   476
    absReal := real abs.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   477
    absImag := imaginary abs.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   478
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   479
    absReal >= absImag ifTrue: [
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   480
	multiplicand := absReal.
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   481
	quotient := imaginary / real
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   482
    ] ifFalse: [
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   483
	multiplicand := absImag.
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   484
	quotient := real / imaginary
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   485
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   486
    ^ multiplicand * ((1 + (quotient * quotient)) sqrt)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   487
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   488
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   489
negated
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   490
    "return a new complex with both real and imaginary parts negated"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   491
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   492
    ^ self class
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   493
        real: real negated
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   494
        imaginary: imaginary negated
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   495
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   496
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   497
!Complex methodsFor:'coercing & converting'!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   498
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   499
asComplex
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   500
    "I am a complex - so return the receiver"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   501
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   502
    ^ self
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   503
!
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   504
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   505
asFloat
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   506
    imaginary = 0 ifTrue: [^ real asFloat].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   507
    ^ Number
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   508
	    raise: #coercionErrorSignal
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   509
	    receiver: self
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   510
	    selector: #asFloat
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   511
	    errorString: 'Can''t coerce an instance of Complex to a Float'
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   512
!
21873
a2ed604af5c4 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21817
diff changeset
   513
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   514
asInteger
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   515
    imaginary = 0 ifTrue: [^real asInteger].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   516
    ^ Number
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   517
	raise: #coercionErrorSignal
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   518
	receiver: self
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   519
	selector: #asInteger
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   520
	errorString: 'Can''t coerce an instance of Complex to an Integer'
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   521
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   522
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   523
asPoint
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   524
    "Return the complex number as a point."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   525
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   526
    ^ real @ imaginary
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   527
!
21873
a2ed604af5c4 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 21817
diff changeset
   528
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   529
coerce: aNumber
18836
63832ff17b6c #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 16742
diff changeset
   530
    "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
   531
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   532
    ^ aNumber asComplex
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   533
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   534
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   535
generality
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   536
    ^ 150
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   537
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   538
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   539
reduceGeneralityIfPossible
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   540
    "Answer the receiver transformed to a lower generality, if such a
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   541
     transformation is possible without losing information.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   542
     If not, answer the receiver"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   543
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   544
    imaginary isZero
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   545
	ifTrue: [^ real]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   546
	ifFalse: [^ self]
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   547
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   548
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   549
!Complex methodsFor:'comparing'!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   550
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   551
< aNumber
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   552
    "raises an error - complex numbers are not well ordered"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   553
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   554
    ^ Number
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   555
	raise: #unorderedSignal
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   556
	receiver: self
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   557
	selector: #<
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   558
	arg: aNumber
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   559
	errorString: 'Complex numbers are not well ordered'
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   560
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   561
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   562
     1 < (2 % 2)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   563
     (2 % 2) < 1
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   564
    "
6500
d2ce5b0a1a78 comments
Claus Gittinger <cg@exept.de>
parents: 5287
diff changeset
   565
!
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   566
24154
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   567
= anObject
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   568
    "return true, if the argument represents the same numeric value
7471
c5d4bd612d9f comments
Claus Gittinger <cg@exept.de>
parents: 7442
diff changeset
   569
     as the receiver, false otherwise."
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   570
24154
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   571
    anObject class == self class ifTrue:[
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   572
        ^ (real = anObject real) and:[ (imaginary = anObject imaginary)]
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   573
    ].
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   574
    ^ anObject equalFromComplex:self
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   575
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   576
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   577
     (Complex real:1.0 imaginary:2.0) = (Complex real:1.0 imaginary:2.0)
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   578
     (Complex real:1.0 imaginary:0) = 1.0
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   579
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   580
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   581
    "Modified (comment): / 12-06-2017 / 20:43:41 / cg"
24154
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   582
    "Modified: / 26-05-2019 / 10:04:33 / Claus Gittinger"
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   583
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   584
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   585
hash
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   586
    "Hash is implemented because = is implemented."
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   587
7377
b2f13d3b9f58 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
   588
    ^ (real hash) bitXor:(imaginary hash bitShift:16)
b2f13d3b9f58 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
   589
b2f13d3b9f58 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
   590
    "
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   591
     (1+0i) hash
7377
b2f13d3b9f58 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
   592
     (1+1i) hash
b2f13d3b9f58 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
   593
    "
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   594
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   595
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   596
!Complex methodsFor:'double dispatching'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   597
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   598
differenceFromComplex:aComplex
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   599
    "Return the difference of the argument, aComplex and the receiver."
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   600
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   601
    | r i |
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   602
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   603
    r := aComplex real - real.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   604
    i := aComplex imaginary - imaginary.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   605
    i = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   606
    ^ self class real:r imaginary:i.
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   607
!
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   608
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   609
differenceFromFixedPoint: aFixedPoint
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   610
    "Return the difference of the argument, aFixedPoint and the receiver."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   611
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   612
    ^ aFixedPoint asComplex - self
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   613
!
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   614
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   615
differenceFromFloat:aFloat
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   616
    "Return the difference of the argument, aFloat and the receiver."
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
    "/ ^ aFloat asComplex - self
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   619
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   620
    | r |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   621
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   622
    r := aFloat - real.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   623
    imaginary = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   624
    ^ self class real:r imaginary:imaginary negated
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   625
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   626
    "
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   627
     (1 % 1) - 1.0
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   628
     1.0 - (1 % 1)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   629
    "
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   630
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   631
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   632
differenceFromFraction: aFraction
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   633
    "Return the difference of the argument, aFraction and the receiver."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   634
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   635
    ^ aFraction asComplex - self
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   636
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   637
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   638
differenceFromInteger: anInteger
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   639
    "Return the difference of the argument, anInteger and the receiver."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   640
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   641
    ^ anInteger asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   642
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   643
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   644
equalFromComplex:aComplex
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   645
    "return true if aComplex represents the same number as myself"
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   646
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   647
    ^ (aComplex real = real) and:[aComplex imaginary = imaginary]
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   648
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   649
    "Modified (comment): / 12-06-2017 / 20:41:04 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   650
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   651
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   652
equalFromFloat:aFloat
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   653
    "return true if aFloat represents the same number as myself"
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   654
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   655
    imaginary = 0 ifFalse:[^ false].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   656
    ^ real = aFloat
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   657
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   658
    "Modified (comment): / 12-06-2017 / 20:41:00 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   659
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   660
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   661
productFromComplex:aComplex
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   662
    "Return the product of the receiver and the argument, aComplex."
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   663
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   664
    | u v r i |
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   665
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   666
    u := aComplex real.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   667
    v := aComplex imaginary.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   668
    r := (real * u) - (imaginary * v).
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   669
    i  := (real * v) + (imaginary * u).
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   670
    i = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   671
    ^ self class real:r imaginary:i
24935
396384936c4c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24154
diff changeset
   672
396384936c4c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24154
diff changeset
   673
    "
396384936c4c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24154
diff changeset
   674
     (4 + 3i) * (2 + 5i) -> (-7+26i)
396384936c4c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24154
diff changeset
   675
     (3 + 2i) * (2 + 1i) -> (4+7i)
396384936c4c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24154
diff changeset
   676
    "
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   677
!
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   678
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   679
productFromFixedPoint: aFixedPoint
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   680
    "Return the product of the receiver and the argument, aFixedPoint."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   681
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   682
    ^ aFixedPoint asComplex * self
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   683
!
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   684
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   685
productFromFloat: aFloat
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   686
    "Return the product of the receiver and the argument, aFloat."
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   687
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   688
    "/  ^ aFloat asComplex * self
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   689
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   690
    | u r i |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   691
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   692
    u := aFloat.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   693
    r := (real * aFloat).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   694
    i  := (imaginary * aFloat).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   695
    i = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   696
    ^ self class real:r imaginary:i
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
     (1 % 1) * 2.0
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   700
     (1 % 1) * 0.0
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   701
     2.0 * (1 % 1)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   702
    "
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   703
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   704
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   705
productFromFraction: aFraction
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   706
    "Return the product of the receiver and the argument, aFraction."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   707
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   708
    ^ aFraction asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   709
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   710
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   711
productFromInteger: anInteger
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   712
    "sent when an integer does not know how to multiply the receiver, a complex.
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   713
     Return the product of the receiver and the argument, anInteger."
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   714
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   715
    ^ anInteger asComplex * self
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   716
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   717
    "Modified (comment): / 12-06-2017 / 20:30:20 / cg"
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
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   720
quotientFromComplex:aComplex
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   721
    "Return the quotient of the argument, aComplex and the receiver."
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   722
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   723
    | denom nr ni r i |
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   724
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   725
    nr := aComplex real.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   726
    ni := aComplex imaginary.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   727
    denom := real * real + (imaginary * imaginary).
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   728
    r := (real * nr + (imaginary * ni)) / denom.
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   729
    i := (real * ni - (imaginary * nr)) / denom.
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   730
    i = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   731
    ^ self class real:r imaginary:i
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   732
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   733
"/ is the stuff below better ?
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   734
"/    "Implement complex division (a + ib) / (c + id).
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   735
"/     Due to double dispatch, in this routine
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   736
"/        self = (c + id) and aComplex = (a + ib)."
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   737
"/
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   738
"/    | quotient denominator |
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   739
"/
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   740
"/    self realPart abs >= (self imaginaryPart abs)
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   741
"/        ifTrue: [
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   742
"/            quotient := self imaginaryPart / self realPart.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   743
"/            denominator := self realPart + (self imaginaryPart * quotient).
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   744
"/            ^ Complex
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   745
"/                real: (aComplex realPart + (aComplex imaginaryPart * quotient)) / denominator
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   746
"/                imaginary: (aComplex imaginaryPart - (aComplex realPart * quotient)) / denominator ]
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   747
"/        ifFalse: [
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   748
"/            quotient := self realPart / self imaginaryPart.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   749
"/            denominator := (self realPart * quotient) + self imaginaryPart.
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   750
"/            ^ Complex
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   751
"/                real: ((aComplex realPart * quotient) + aComplex imaginaryPart) / denominator
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   752
"/                imaginary: ((aComplex imaginaryPart * quotient) - aComplex realPart) / denominator ]
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   753
!
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   754
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   755
quotientFromFixedPoint:aFixedPoint
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   756
    "Return the quotient of the argument, aFixedPoint and the receiver."
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   757
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   758
    ^ aFixedPoint asComplex / self
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   759
!
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   760
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   761
quotientFromFloat:aFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   762
    "Return the quotient of the argument, aFloat and the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   763
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   764
    ^ aFloat asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   765
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   766
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   767
quotientFromFraction:aFraction
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   768
    "Return the quotient of the argument, aFraction and the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   769
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   770
    ^ aFraction asComplex / self
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   771
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   772
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   773
quotientFromInteger:anInteger
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   774
    "Return the quotient of the argument, anInteger and the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   775
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   776
    ^ anInteger asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   777
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   778
23644
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   779
raisedFromFloat:aNumber
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   780
    ^ self raisedFromNumber:aNumber
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   781
    
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   782
    "
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   783
     2 raisedTo:(2 + 2i)
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   784
     2 ** (2 + 2i)
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   785
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   786
     2.0 raisedTo:(2 + 2i)
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   787
     2.0 ** (2 + 2i)
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   788
    "
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   789
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   790
    "Created: / 26-01-2019 / 10:56:06 / Claus Gittinger"
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   791
!
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   792
21933
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   793
raisedFromNumber:aNumber
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   794
    "see http://www.math.toronto.edu/mathnet/questionCorner/complexexp.html"
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   795
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   796
    "/ a ^ (b+i*c) = a^b * ( cos(c*ln(a)) + i*sin(c*ln(a)) )
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   797
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   798
    |cLNa a_b|
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   799
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   800
    cLNa := imaginary * aNumber ln.
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   801
    a_b := aNumber raisedTo:real.
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   802
    
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   803
    ^ Complex
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   804
        real:(a_b * cLNa cos)
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   805
        imaginary:(a_b * cLNa sin)
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   806
21947
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   807
    "
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   808
     2 raisedTo:(2 + 2i)
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   809
     2 ** (2 + 2i)
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   810
    "
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   811
21933
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   812
    "Created: / 01-07-2017 / 20:33:47 / cg"
21947
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   813
    "Modified (comment): / 03-07-2017 / 14:08:45 / cg"
21933
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   814
!
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   815
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   816
sumFromComplex:aComplex
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   817
    "Return the sum of the receiver and the argument, aComplex."
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   818
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   819
    | r i |
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   820
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   821
    r := aComplex real + real.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   822
    i := aComplex imaginary + imaginary.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   823
    i = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   824
    ^ self class real:r imaginary:i
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   825
!
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   826
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   827
sumFromFixedPoint: aFixedPoint
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   828
    "Return the sum of the receiver and the argument, aFixedPoint."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   829
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   830
    ^ aFixedPoint asComplex + self
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   831
!
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   832
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   833
sumFromFloat: aFloat
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   834
    "Return the sum of the receiver and the argument, aFloat."
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   835
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   836
    "/ ^ aFloat asComplex + self
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   837
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   838
    | r |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   839
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   840
    r := aFloat + real.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   841
    imaginary = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   842
    ^ self class real:r imaginary:imaginary
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   843
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   844
    "
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   845
     (1 % 1) + 1.0
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   846
     1.0 + (1 % 1)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   847
    "
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   848
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   849
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   850
sumFromFraction: aFraction
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   851
    "Return the sum of the receiver and the argument, aFraction."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   852
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   853
    ^ aFraction asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   854
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   855
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   856
sumFromInteger: anInteger
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   857
    "Return the sum of the receiver and the argument, anInteger."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   858
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   859
    ^ anInteger asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   860
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   861
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   862
!Complex methodsFor:'mathematical functions'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   863
f014922e3b71 Initial revision
claus
parents:
diff changeset
   864
angle
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   865
    "Return the radian angle for this Complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   866
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   867
    real < 0 ifTrue: [
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   868
	imaginary < 0 ifTrue: [
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   869
	    ^ (imaginary / real) arcTan - Float pi
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   870
	].
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   871
	^ Float pi + (imaginary / real) arcTan
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   872
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   873
    ^ (imaginary / real) arcTan
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   874
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   875
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   876
     (1 % 1) angle radiansToDegrees
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   877
    "
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   878
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   879
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   880
arg
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   881
    "Answer the argument of the receiver."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   882
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   883
    self isZero ifTrue: [self error: 'zero has no argument.'].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   884
    ^ imaginary arcTan:real
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   885
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   886
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   887
exp
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   888
    "Return the complex exponential of the receiver."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   889
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   890
    ^ (Complex 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   891
            real:imaginary cos 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   892
            imaginary:imaginary sin) * real exp
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   893
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   894
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   895
     (10+4i) exp -> (-14397.45885694595768-16669.6842763244522i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   896
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   897
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   898
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   899
ln
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   900
    "Answer the natural log of the receiver."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   901
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   902
    ^ self abs ln + self arg i
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   903
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   904
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   905
log:base
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   906
    "Answer the log base aNumber of the receiver."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   907
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   908
    ^ self ln / base ln
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   909
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   910
f014922e3b71 Initial revision
claus
parents:
diff changeset
   911
sqrt
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   912
    "Return the square root of the receiver"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   913
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   914
    | w quotient absReal absImag |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   915
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   916
    ((real = 0) and: [ imaginary = 0 ]) ifTrue: [
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   917
        ^ self class zero
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   918
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   919
    absReal := real abs.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   920
    absImag := imaginary abs.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   921
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   922
    absReal >= absImag ifTrue:[
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   923
        quotient := imaginary / real.
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   924
        w := (absReal sqrt) * (((1 + (1 + (quotient * quotient)) sqrt) / 2) sqrt)
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   925
    ] ifFalse: [
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   926
        quotient := real / imaginary.
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   927
        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
   928
    ].
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   929
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   930
    real >= 0 ifTrue:[
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   931
        ^ self class real: w imaginary: (imaginary / (2 * w))
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   932
    ].
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   933
    imaginary >= 0 ifTrue: [
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   934
        ^ self class real: absImag / (2 * w) imaginary: w
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   935
    ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   936
    ^ self class real: absImag / (2 * w) imaginary: -1 * w
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   937
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   938
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   939
sqrt_bad
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   940
    "Return the square root of the receiver"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   941
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   942
    | u v |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   943
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   944
    (imaginary = 0 and: [real >= 0]) ifTrue: [^ real sqrt].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   945
    v := ((self abs - real) / 2) sqrt.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   946
    u := imaginary / 2 / v.
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   947
    ^ self class real: u imaginary: v
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   948
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   949
    "
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   950
     -4 asComplex sqrt
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   951
     4 asComplex sqrt
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   952
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   953
    "
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   954
     -4 asComplex sqrt squared
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   955
    "
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   956
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   957
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   958
squaredNorm
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   959
    "Answer the square of receiver's norm."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   960
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   961
    ^real * real + (imaginary * imaginary)
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   962
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   963
23643
22176167f5b8 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23167
diff changeset
   964
!Complex methodsFor:'printing & storing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   965
16742
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   966
displayOn: aGCOrStream
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   967
    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
21537
81b78d558eaf #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21110
diff changeset
   968
    "/ old ST80 means: draw-yourself on a GC.
16742
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   969
    (aGCOrStream isStream) ifFalse:[
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   970
        ^ super displayOn:aGCOrStream
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   971
    ].
21537
81b78d558eaf #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21110
diff changeset
   972
16742
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   973
    aGCOrStream nextPut: $(.
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   974
    self realPart printOn: aGCOrStream.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   975
    self imaginaryPart >= 0
16742
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   976
        ifTrue: [ aGCOrStream nextPut: $+ ]
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   977
        ifFalse: [ aGCOrStream nextPut: $- ].
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   978
    self imaginaryPart abs printOn: aGCOrStream.
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   979
    aGCOrStream nextPutAll: 'i)'
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   980
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   981
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   982
     Complex real:1 imaginary:1
23134
b6056d446cd8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22938
diff changeset
   983
     (Complex real:1 imaginary:1) printString
b6056d446cd8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22938
diff changeset
   984
     (Complex real:1 imaginary:1) displayString
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   985
    "
21537
81b78d558eaf #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21110
diff changeset
   986
81b78d558eaf #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21110
diff changeset
   987
    "Modified (format): / 22-02-2017 / 17:01:02 / cg"
23134
b6056d446cd8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22938
diff changeset
   988
    "Modified (comment): / 26-06-2018 / 21:00:28 / Claus Gittinger"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   989
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   990
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   991
printOn: aStream
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   992
    aStream nextPut: $(.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   993
    real storeOn: aStream.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   994
    aStream nextPutAll: '%'.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   995
    imaginary storeOn: aStream.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   996
    aStream nextPut: $).
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   997
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   998
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   999
printString
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1000
    ^ '(' , real printString, '%', imaginary printString, ')'
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1001
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1002
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1003
storeOn: aStream
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1004
    self printOn:aStream
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1005
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1006
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1007
!Complex methodsFor:'private'!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1008
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1009
setReal: u setImaginary: v
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1010
    real := u.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1011
    imaginary := v.
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1012
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1013
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1014
!Complex methodsFor:'testing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1015
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1016
isComplex
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1017
    "Answer whether the receiver has an imaginary part
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1018
     (i.e. if it is a complex number). Always true here."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1019
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1020
    ^ true
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1021
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1022
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1023
isReal
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1024
    "Return true if this Complex number has a zero imaginary part."
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1025
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1026
    ^ imaginary = 0
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1027
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1028
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1029
isZero
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
  1030
    "Answer whether 'self = self class zero'.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1031
     We can't use #= because #= is defined in terms of #isZero"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1032
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1033
    ^real isZero and: [imaginary isZero]
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1034
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1035
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1036
sign
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1037
    imaginary isZero ifTrue:[^ real sign].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1038
    ^ self / self abs
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1039
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1040
    "/ squeak does:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1041
    "/      ^ real sign
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1042
    "/
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1043
    "/ the old code was:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1044
    "/ return a new complex, consisting of the signs of the real and imaginary parts.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1045
    "/  Q: is this a good thing to do ?
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1046
    "/  cg: no
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1047
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1048
    "/ ^ self class real:(real sign) imaginary:(imaginary sign)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1049
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1050
    " this is consistent with Wolfram's output:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1051
    
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1052
     (Complex new setReal:1 setImaginary:0) sign -> 1
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1053
     (Complex new setReal:-1 setImaginary:0) sign -> -1
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1054
     (Complex new setReal:0 setImaginary:0) sign -> 0
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1055
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1056
     (Complex new setReal:1 setImaginary:1) sign -> (0.707106781186547+0.707106781186547i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1057
     (Complex new setReal:0 setImaginary:1) sign -> (0.0+1.0i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1058
     (Complex new setReal:-1 setImaginary:1) sign -> (-0.707106781186547+0.707106781186547i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1059
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1060
     (Complex new setReal:1 setImaginary:-1) sign -> (0.707106781186547-0.707106781186547i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1061
     (Complex new setReal:0 setImaginary:-1) sign -> (0.0-1.0i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1062
     (Complex new setReal:-1 setImaginary:-1) sign -> (-0.707106781186547-0.707106781186547i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1063
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1064
! !
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1065
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1066
!Complex methodsFor:'trigonometric functions'!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1067
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1068
arcCos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1069
    "Answer the arc cosine of the receiver.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1070
     This is the inverse function of cos."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1071
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1072
    | x y tmp sh2y shy delta ch2y chy |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1073
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1074
    imaginary = 0 ifTrue: [
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1075
        real abs > 1 ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1076
            x := real < 0 ifTrue: [Float pi]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1077
                          ifFalse: [0].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1078
            y := real copySignTo: real abs arcCosh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1079
            ^ self class real: x imaginary: y
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1080
        ] ifFalse: [
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1081
            ^self class real: real arcCos imaginary: 0
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1082
        ]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1083
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1084
    tmp := self squaredNorm - 1 / 2.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1085
    delta := tmp squared + imaginary squared.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1086
    sh2y := tmp + delta sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1087
    shy := sh2y sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1088
    ch2y := 1 + sh2y.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1089
    chy := ch2y sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1090
    y := imaginary copySignTo: shy arSinh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1091
    x := (real / chy) arcCos.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1092
    ^ self class real: x imaginary: y negated
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1093
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1094
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1095
     (10+4i) cos (-22.91356068209214107-14.84629106966035727i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1096
     (10+4i) cos arcCos 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1097
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1098
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1099
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1100
arcCosh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1101
    "Answer the receiver's area hyperbolic cosine.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1102
     That is the inverse function of cosh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1103
     Some possible implementations:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1104
        ^imaginary > 0 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1105
            ifTrue: [(self + (self * self - 1) sqrt) ln]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1106
            ifFalse: [(self + (self * self - 1) sqrt) ln negated]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1107
        ^self arcCos i
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1108
     This implementation provides an answer with a positive real part.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1109
     It also avoids creating intermediate Complex."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1110
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1111
    | x y tmp sh2x shx delta ch2x chx |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1112
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1113
    imaginary = 0 ifTrue: [
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1114
        real abs > 1 ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1115
            y := real < 0 ifTrue: [Float pi]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1116
                          ifFalse: [0].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1117
            x := real abs arcCosh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1118
            ^ self class real: x imaginary: y
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1119
        ] ifFalse: [
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1120
            ^ self class real: 0 imaginary: real arcCos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1121
        ]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1122
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1123
    tmp := self squaredNorm - 1 / 2.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1124
    delta := tmp squared + imaginary squared.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1125
    sh2x := tmp + delta sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1126
    shx := sh2x sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1127
    ch2x := 1 + sh2x.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1128
    chx := ch2x sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1129
    x := shx arSinh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1130
    y := imaginary copySignTo: (real / chx) arcCos.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1131
    ^ self class real: x imaginary: y
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1132
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1133
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1134
     (10 + 4i) arcCosh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1135
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1136
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1137
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1138
arcSin
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1139
    "Answer the arc sine of the receiver.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1140
    This is the inverse function of sin."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1141
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1142
    | x y tmp delta sh2y shy ch2y chy |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1143
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1144
    imaginary = 0 ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1145
        real abs > 1 ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1146
            x := Float pi / 2 * real sign.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1147
            y := (real copySignTo: real abs arcCosh) negated.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1148
            ^ self class real: x imaginary: y
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1149
        ] ifFalse: [
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1150
            ^ self class real: real arcSin imaginary: 0
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1151
        ]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1152
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1153
    tmp := (self squaredNorm - 1) / 2.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1154
    delta := tmp squared + imaginary squared.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1155
    sh2y := tmp + delta sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1156
    shy := sh2y sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1157
    ch2y := 1 + sh2y.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1158
    chy := ch2y sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1159
    y := imaginary copySignTo: shy arcSinh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1160
    x := (real / chy) arcSin.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1161
    ^self class real: x imaginary: y
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1162
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1163
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1164
     (10 + 4i) sin -> (-14.85625516387525498-22.89819255096375875i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1165
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1166
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1167
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1168
arcSinh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1169
    "Answer receiver's area hyperbolic sine.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1170
     That is the inverse function of sinh."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1171
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1172
    "Some possible implementation:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1173
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1174
    ^imaginary * real < 0 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1175
            ifTrue: [(self + (self * self + 1) sqrt) ln]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1176
            ifFalse: [(self - (self * self + 1) sqrt) ln]"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1177
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1178
    ^self i arcSin i negated
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1179
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1180
    "
24936
eaca1260a80c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24935
diff changeset
  1181
     (10 + 4i) sinh         -> (-7198.729413635291604-8334.842155341616165i)
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1182
     (10 + 4i) sinh arcSinh -> (-10.0-0.8584073464102067614i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1183
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1184
!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1185
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1186
arcTan
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1187
    "Answer the arc tangent of the receiver.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1188
     This is the inverse function of tan."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1189
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1190
    | r2 |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1191
    
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1192
    r2 := self squaredNorm.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1193
    ^ self class
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1194
        real: (real * 2 arcTan: 1 - r2) / 2
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1195
        imaginary: ((r2 + (imaginary * 2) + 1) / (r2 - (imaginary * 2) + 1)) ln / 4
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1196
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1197
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1198
     (1+4i) tan arcTan -> (1.000000000000000043+4.000000000000004772i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1199
     (10+4i) tan arcTan -> (0.5752220392306203171+4.00000000000000004i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1200
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1201
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1202
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1203
arcTan:denominator 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1204
    "Answer the  four quadrants arc tangent of receiver over denominator."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1205
    
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1206
    |res|
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1207
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1208
    denominator isZero ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1209
        self isZero ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1210
            "shouldn't it be an error ? ^DomainError signal: '0 arcTan: 0'"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1211
            ^ self class real:0 imaginary:0
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1212
        ] ifFalse:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1213
            ^ self class real:Float pi / (real copySignTo:2) imaginary:0
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1214
        ]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1215
    ]. 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1216
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1217
    res := (self / denominator) arcTan.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1218
    denominator real < 0 ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1219
        res := res + Float pi
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1220
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1221
    res real > Float pi ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1222
        res := res - (Float pi * 2)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1223
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1224
    ^ res
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1225
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1226
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1227
arcTanh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1228
    "Answer the receiver's area hyperbolic tangent.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1229
     That is the inverse function of tanh."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1230
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1231
    "Some other possible implementation:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1232
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1233
    ^((1 + self) / (1 - self)) ln / 2"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1234
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1235
    ^ self i arcTan i negated
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1236
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1237
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1238
     (10+4i) tanh arcTanh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1239
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1240
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1241
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1242
cos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1243
    "Answer the receiver's cosine."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1244
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1245
    "/ ^ self i cosh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1246
    ^ self class 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1247
        real:(real cos * imaginary cosh)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1248
        imaginary:(-1 * real sin * imaginary sinh) 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1249
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1250
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1251
     (10+4i) cos -> (-22.91356068209214107+14.84629106966035727i) 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1252
     (10+4i) i cosh (-22.91356068209214107+14.84629106966035727i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1253
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1254
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1255
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1256
cosh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1257
    "Answer the receiver's hyperbolic cosine.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1258
    Hyperbolic cosine is defined by same power series expansion as for real numbers, 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1259
    that is in term of exponential:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1260
        ^ (self exp + self negated exp) / 2.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1261
    This implementation avoids creating intermediate objects."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1262
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1263
    ^self class
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1264
            real: real cosh * imaginary cos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1265
            imaginary: real sinh * imaginary sin
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1266
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1267
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1268
     (10+4i) cosh -> (-7198.729443310666079-8334.842120982836036i) 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1269
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1270
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1271
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1272
sin
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1273
    "Answer the receiver's sine."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1274
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1275
    "/ alternative:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1276
    "/      ^ self i sinh i negated
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1277
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1278
    ^ self class 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1279
        real:(real sin * imaginary cosh)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1280
        imaginary:(real cos * imaginary sinh)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1281
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1282
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1283
     (10+4i) sin -> (-14.85625516387525498-22.89819255096375875i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1284
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1285
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1286
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1287
sinh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1288
    "Answer the receiver's hyperbolic sine.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1289
    Hyperbolic sine is defined by same power series expansion as for real numbers, 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1290
    that is in term of exponential:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1291
        ^ (self exp - self negated exp) / 2.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1292
    This implementation avoids creating intermediate objects."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1293
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1294
    ^ self class
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1295
            real: real sinh * imaginary cos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1296
            imaginary: real cosh * imaginary sin
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1297
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1298
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1299
     10 sinh -> 11013.23287470339338
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1300
     (10+4i) sinh (-7198.729413635291604-8334.842155341616165i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1301
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1302
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1303
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1304
tan
22938
c3954250f7e7 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22729
diff changeset
  1305
    "Answer the receiver's tangent."
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1306
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1307
    ^ self sin / self cos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1308
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1309
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1310
     (10+4i) tan -> (0.0006123503000121616919+0.9997260574022127583i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1311
     (10+4i) sin -> (-14.85625516387525498-22.89819255096375875i) 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1312
     (10+4i) cos -> (-22.91356068209214107+14.84629106966035727i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1313
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1314
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1315
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1316
tanh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1317
    "Answer the receiver's hyperbolic tangent."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1318
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1319
    "Some possible implementations are:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1320
        ^self sinh / self cosh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1321
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1322
        | tr ti |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1323
        tr := real tanh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1324
        ti := imaginary tan i.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1325
        ^(tr + ti) / (tr * ti + 1)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1326
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1327
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1328
    ^ self i tan i negated
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1329
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1330
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1331
     (10+4i) tanh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1332
    "
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1333
! !
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1334
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1335
!Complex methodsFor:'truncation & rounding'!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1336
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1337
ceiling
25062
1704e428aea4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25006
diff changeset
  1338
    "blocked: complex numbers have no ceiling.
1704e428aea4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25006
diff changeset
  1339
     Reports an error"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1340
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1341
    ^ self shouldNotImplement
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1342
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1343
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1344
floor
25062
1704e428aea4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25006
diff changeset
  1345
    "blocked: complex numbers have no floor.
1704e428aea4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 25006
diff changeset
  1346
     Reports an error"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1347
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1348
    ^ self shouldNotImplement
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1349
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1350
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
  1351
!Complex class methodsFor:'documentation'!
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
  1352
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
  1353
version
18836
63832ff17b6c #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 16742
diff changeset
  1354
    ^ '$Header$'
23644
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
  1355
!
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
  1356
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
  1357
version_CVS
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
  1358
    ^ '$Header$'
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
  1359
! !
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
  1360