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