Complex.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 24154 a118b07ac9e2
child 24935 396384936c4c
permissions -rw-r--r--
#REFACTORING by exept class: Smalltalk class changed: #recursiveInstallAutoloadedClassesFrom:rememberIn:maxLevels:noAutoload:packageTop:showSplashInLevels: Transcript showCR:(... bindWith:...) -> Transcript showCR:... 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
24154
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   599
= anObject
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
24154
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   603
    anObject class == self class ifTrue:[
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   604
        ^ (real = anObject real) and:[ (imaginary = anObject imaginary)]
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   605
    ].
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   606
    ^ anObject equalFromComplex:self
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   607
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
     (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
   610
     (Complex real:1.0 imaginary:0) = 1.0
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   611
    "
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   612
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   613
    "Modified (comment): / 12-06-2017 / 20:43:41 / cg"
24154
a118b07ac9e2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23644
diff changeset
   614
    "Modified: / 26-05-2019 / 10:04:33 / Claus Gittinger"
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   615
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   616
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   617
hash
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   618
    "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
   619
7377
b2f13d3b9f58 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
   620
    ^ (real hash) bitXor:(imaginary hash bitShift:16)
b2f13d3b9f58 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
   621
b2f13d3b9f58 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
   622
    "
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   623
     (1+0i) hash
7377
b2f13d3b9f58 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
   624
     (1+1i) hash
b2f13d3b9f58 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7355
diff changeset
   625
    "
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   626
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   627
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   628
!Complex methodsFor:'double dispatching'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   629
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   630
differenceFromComplex:aComplex
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   631
    "Return the difference of the argument, aComplex and the receiver."
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   632
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   633
    | r i |
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   634
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   635
    r := aComplex real - real.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   636
    i := aComplex imaginary - imaginary.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   637
    i = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   638
    ^ self class real:r imaginary:i.
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   639
!
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   640
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   641
differenceFromFixedPoint: aFixedPoint
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   642
    "Return the difference of the argument, aFixedPoint and the receiver."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   643
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   644
    ^ aFixedPoint asComplex - self
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   645
!
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   646
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   647
differenceFromFloat:aFloat
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   648
    "Return the difference of the argument, aFloat and the receiver."
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   649
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   650
    "/ ^ aFloat asComplex - self
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   651
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   652
    | r |
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   653
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   654
    r := aFloat - real.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   655
    imaginary = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   656
    ^ self class real:r imaginary:imaginary negated
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   657
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   658
    "
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   659
     (1 % 1) - 1.0
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   660
     1.0 - (1 % 1)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   661
    "
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   662
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   663
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   664
differenceFromFraction: aFraction
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   665
    "Return the difference of the argument, aFraction and the receiver."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   666
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   667
    ^ aFraction asComplex - self
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   668
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   669
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   670
differenceFromInteger: anInteger
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   671
    "Return the difference of the argument, anInteger and the receiver."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   672
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   673
    ^ anInteger asComplex - self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   674
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   675
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   676
equalFromComplex:aComplex
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   677
    "return true if aComplex represents the same number as myself"
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   678
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   679
    ^ (aComplex real = real) and:[aComplex imaginary = imaginary]
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   680
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   681
    "Modified (comment): / 12-06-2017 / 20:41:04 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   682
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   683
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   684
equalFromFloat:aFloat
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   685
    "return true if aFloat represents the same number as myself"
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   686
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   687
    imaginary = 0 ifFalse:[^ false].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   688
    ^ real = aFloat
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   689
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   690
    "Modified (comment): / 12-06-2017 / 20:41:00 / cg"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   691
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   692
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   693
productFromComplex:aComplex
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   694
    "Return the product of the receiver and the argument, aComplex."
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   695
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   696
    | u v r i |
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   697
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   698
    u := aComplex real.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   699
    v := aComplex imaginary.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   700
    r := (real * u) - (imaginary * v).
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   701
    i  := (real * v) + (imaginary * u).
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   702
    i = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   703
    ^ self class real:r imaginary:i
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   704
!
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   705
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   706
productFromFixedPoint: aFixedPoint
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   707
    "Return the product of the receiver and the argument, aFixedPoint."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   708
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   709
    ^ aFixedPoint asComplex * self
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   710
!
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   711
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   712
productFromFloat: aFloat
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   713
    "Return the product of the receiver and the argument, aFloat."
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
    "/  ^ aFloat asComplex * self
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   716
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   717
    | u r i |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   718
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   719
    u := aFloat.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   720
    r := (real * aFloat).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   721
    i  := (imaginary * aFloat).
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   722
    i = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   723
    ^ self class real:r imaginary:i
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   724
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   725
    "
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   726
     (1 % 1) * 2.0
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   727
     (1 % 1) * 0.0
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   728
     2.0 * (1 % 1)
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   729
    "
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   730
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   731
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   732
productFromFraction: aFraction
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   733
    "Return the product of the receiver and the argument, aFraction."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   734
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   735
    ^ aFraction asComplex * self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   736
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   737
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   738
productFromInteger: anInteger
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   739
    "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
   740
     Return the product of the receiver and the argument, anInteger."
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   741
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   742
    ^ anInteger asComplex * self
21798
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   743
eeac3e49a0af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21537
diff changeset
   744
    "Modified (comment): / 12-06-2017 / 20:30:20 / cg"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   745
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   746
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   747
quotientFromComplex:aComplex
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   748
    "Return the quotient of the argument, aComplex and the receiver."
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   749
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   750
    | denom nr ni r i |
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   751
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   752
    nr := aComplex real.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   753
    ni := aComplex imaginary.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   754
    denom := real * real + (imaginary * imaginary).
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   755
    r := (real * nr + (imaginary * ni)) / denom.
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   756
    i := (real * ni - (imaginary * nr)) / denom.
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   757
    i = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   758
    ^ self class real:r imaginary:i
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   759
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   760
"/ is the stuff below better ?
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   761
"/    "Implement complex division (a + ib) / (c + id).
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   762
"/     Due to double dispatch, in this routine
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   763
"/        self = (c + id) and aComplex = (a + ib)."
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   764
"/
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   765
"/    | quotient denominator |
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   766
"/
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   767
"/    self realPart abs >= (self imaginaryPart abs)
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   768
"/        ifTrue: [
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   769
"/            quotient := self imaginaryPart / self realPart.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   770
"/            denominator := self realPart + (self imaginaryPart * quotient).
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   771
"/            ^ Complex
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   772
"/                real: (aComplex realPart + (aComplex imaginaryPart * quotient)) / denominator
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   773
"/                imaginary: (aComplex imaginaryPart - (aComplex realPart * quotient)) / denominator ]
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   774
"/        ifFalse: [
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   775
"/            quotient := self realPart / self imaginaryPart.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   776
"/            denominator := (self realPart * quotient) + self imaginaryPart.
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   777
"/            ^ Complex
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   778
"/                real: ((aComplex realPart * quotient) + aComplex imaginaryPart) / denominator
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   779
"/                imaginary: ((aComplex imaginaryPart * quotient) - aComplex realPart) / denominator ]
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   780
!
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   781
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   782
quotientFromFixedPoint:aFixedPoint
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   783
    "Return the quotient of the argument, aFixedPoint and the receiver."
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   784
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   785
    ^ aFixedPoint asComplex / self
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   786
!
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   787
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   788
quotientFromFloat:aFloat
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   789
    "Return the quotient of the argument, aFloat and the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   790
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   791
    ^ aFloat asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   792
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   793
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   794
quotientFromFraction:aFraction
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   795
    "Return the quotient of the argument, aFraction and the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   796
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   797
    ^ aFraction asComplex / self
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   798
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   799
7442
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   800
quotientFromInteger:anInteger
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   801
    "Return the quotient of the argument, anInteger and the receiver."
1b0a20747a64 double dispatching
Claus Gittinger <cg@exept.de>
parents: 7404
diff changeset
   802
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   803
    ^ anInteger asComplex / self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   804
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   805
23644
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   806
raisedFromFloat:aNumber
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   807
    ^ self raisedFromNumber:aNumber
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
    "
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   810
     2 raisedTo:(2 + 2i)
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   811
     2 ** (2 + 2i)
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
     2.0 raisedTo:(2 + 2i)
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   814
     2.0 ** (2 + 2i)
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   815
    "
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   816
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   817
    "Created: / 26-01-2019 / 10:56:06 / Claus Gittinger"
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   818
!
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
   819
21933
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   820
raisedFromNumber:aNumber
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   821
    "see http://www.math.toronto.edu/mathnet/questionCorner/complexexp.html"
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
    "/ 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
   824
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   825
    |cLNa a_b|
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   826
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   827
    cLNa := imaginary * aNumber ln.
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   828
    a_b := aNumber raisedTo:real.
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   829
    
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   830
    ^ Complex
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   831
        real:(a_b * cLNa cos)
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   832
        imaginary:(a_b * cLNa sin)
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   833
21947
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   834
    "
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   835
     2 raisedTo:(2 + 2i)
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   836
     2 ** (2 + 2i)
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   837
    "
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   838
21933
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   839
    "Created: / 01-07-2017 / 20:33:47 / cg"
21947
40e2852e1422 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 21933
diff changeset
   840
    "Modified (comment): / 03-07-2017 / 14:08:45 / cg"
21933
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   841
!
56fbff93ec73 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21932
diff changeset
   842
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   843
sumFromComplex:aComplex
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   844
    "Return the sum of the receiver and the argument, aComplex."
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   845
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   846
    | r i |
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   847
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   848
    r := aComplex real + real.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   849
    i := aComplex imaginary + imaginary.
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   850
    i = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   851
    ^ self class real:r imaginary:i
9128
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   852
!
960eefbca77a differenceFromFloat bug fix
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
   853
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   854
sumFromFixedPoint: aFixedPoint
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   855
    "Return the sum of the receiver and the argument, aFixedPoint."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   856
20740
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   857
    ^ aFixedPoint asComplex + self
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   858
!
f6574113b828 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20408
diff changeset
   859
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   860
sumFromFloat: aFloat
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   861
    "Return the sum of the receiver and the argument, aFloat."
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
    "/ ^ aFloat asComplex + self
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   864
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   865
    | r |
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
    r := aFloat + real.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   868
    imaginary = 0 ifTrue:[ ^ r ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   869
    ^ self class real:r imaginary:imaginary
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   870
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   871
    "
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   872
     (1 % 1) + 1.0
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   873
     1.0 + (1 % 1)
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   874
    "
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   875
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   876
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   877
sumFromFraction: aFraction
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   878
    "Return the sum of the receiver and the argument, aFraction."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   879
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   880
    ^ aFraction asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   881
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   882
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   883
sumFromInteger: anInteger
21110
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   884
    "Return the sum of the receiver and the argument, anInteger."
fe4d99579623 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20740
diff changeset
   885
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   886
    ^ anInteger asComplex + self
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   887
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   888
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
   889
!Complex methodsFor:'mathematical functions'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   890
f014922e3b71 Initial revision
claus
parents:
diff changeset
   891
angle
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   892
    "Return the radian angle for this Complex number."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   893
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   894
    real < 0 ifTrue: [
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   895
	imaginary < 0 ifTrue: [
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   896
	    ^ (imaginary / real) arcTan - Float pi
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   897
	].
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   898
	^ Float pi + (imaginary / real) arcTan
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   899
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   900
    ^ (imaginary / real) arcTan
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   901
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   902
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   903
     (1 % 1) angle radiansToDegrees
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   904
    "
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   905
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   906
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   907
arg
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   908
    "Answer the argument of the receiver."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   909
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   910
    self isZero ifTrue: [self error: 'zero has no argument.'].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   911
    ^ imaginary arcTan:real
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   912
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   913
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   914
exp
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   915
    "Return the complex exponential of the receiver."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   916
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   917
    ^ (Complex 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   918
            real:imaginary cos 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   919
            imaginary:imaginary sin) * real exp
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
     (10+4i) exp -> (-14397.45885694595768-16669.6842763244522i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   923
    "
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
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   926
ln
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   927
    "Answer the natural log of the receiver."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   928
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   929
    ^ self abs ln + self arg i
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
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   932
log:base
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   933
    "Answer the log base aNumber of the receiver."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   934
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   935
    ^ self ln / base ln
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   936
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
   937
f014922e3b71 Initial revision
claus
parents:
diff changeset
   938
sqrt
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   939
    "Return the square root of the receiver"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   940
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   941
    | w quotient absReal absImag |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   942
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   943
    ((real = 0) and: [ imaginary = 0 ]) ifTrue: [
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   944
        ^ self class zero
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   945
    ].
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   946
    absReal := real abs.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   947
    absImag := imaginary abs.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   948
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   949
    absReal >= absImag ifTrue:[
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   950
        quotient := imaginary / real.
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   951
        w := (absReal sqrt) * (((1 + (1 + (quotient * quotient)) sqrt) / 2) sqrt)
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   952
    ] ifFalse: [
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   953
        quotient := real / imaginary.
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   954
        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
   955
    ].
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   956
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   957
    real >= 0 ifTrue:[
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   958
        ^ self class real: w imaginary: (imaginary / (2 * w))
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   959
    ].
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   960
    imaginary >= 0 ifTrue: [
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   961
        ^ self class real: absImag / (2 * w) imaginary: w
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   962
    ].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   963
    ^ self class real: absImag / (2 * w) imaginary: -1 * w
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   964
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   965
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   966
sqrt_bad
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   967
    "Return the square root of the receiver"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   968
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   969
    | u v |
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   970
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   971
    (imaginary = 0 and: [real >= 0]) ifTrue: [^ real sqrt].
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   972
    v := ((self abs - real) / 2) sqrt.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   973
    u := imaginary / 2 / v.
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
   974
    ^ self class real: u imaginary: v
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
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   978
     4 asComplex sqrt
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   979
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   980
    "
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
   981
     -4 asComplex sqrt squared
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
   982
    "
22729
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
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   985
squaredNorm
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   986
    "Answer the square of receiver's norm."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   987
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
   988
    ^real * real + (imaginary * imaginary)
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   989
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
   990
23643
22176167f5b8 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23167
diff changeset
   991
!Complex methodsFor:'printing & storing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
   992
16742
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   993
displayOn: aGCOrStream
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   994
    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
21537
81b78d558eaf #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21110
diff changeset
   995
    "/ old ST80 means: draw-yourself on a GC.
16742
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   996
    (aGCOrStream isStream) ifFalse:[
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   997
        ^ super displayOn:aGCOrStream
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
   998
    ].
21537
81b78d558eaf #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21110
diff changeset
   999
16742
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
  1000
    aGCOrStream nextPut: $(.
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
  1001
    self realPart printOn: aGCOrStream.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1002
    self imaginaryPart >= 0
16742
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
  1003
        ifTrue: [ aGCOrStream nextPut: $+ ]
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
  1004
        ifFalse: [ aGCOrStream nextPut: $- ].
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
  1005
    self imaginaryPart abs printOn: aGCOrStream.
4416b836c741 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16449
diff changeset
  1006
    aGCOrStream nextPutAll: 'i)'
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1007
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1008
    "
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1009
     Complex real:1 imaginary:1
23134
b6056d446cd8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22938
diff changeset
  1010
     (Complex real:1 imaginary:1) printString
b6056d446cd8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22938
diff changeset
  1011
     (Complex real:1 imaginary:1) displayString
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1012
    "
21537
81b78d558eaf #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21110
diff changeset
  1013
81b78d558eaf #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21110
diff changeset
  1014
    "Modified (format): / 22-02-2017 / 17:01:02 / cg"
23134
b6056d446cd8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22938
diff changeset
  1015
    "Modified (comment): / 26-06-2018 / 21:00:28 / Claus Gittinger"
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1016
!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1017
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1018
printOn: aStream
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1019
    aStream nextPut: $(.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1020
    real storeOn: aStream.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1021
    aStream nextPutAll: '%'.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1022
    imaginary storeOn: aStream.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1023
    aStream nextPut: $).
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1024
!
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1025
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1026
printString
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1027
    ^ '(' , real printString, '%', imaginary printString, ')'
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
storeOn: aStream
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1031
    self printOn:aStream
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1032
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1033
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1034
!Complex methodsFor:'private'!
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
setReal: u setImaginary: v
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1037
    real := u.
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1038
    imaginary := v.
3633
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1039
! !
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1040
89f1f31527f4 avoid a send if the result of an arithmetic op
Claus Gittinger <cg@exept.de>
parents: 1944
diff changeset
  1041
!Complex methodsFor:'testing'!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1042
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1043
isComplex
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1044
    "Answer whether the receiver has an imaginary part
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1045
     (i.e. if it is a complex number). Always true here."
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1046
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1047
    ^ true
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1048
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1049
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1050
isReal
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1051
    "Return true if this Complex number has a zero imaginary part."
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1052
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1053
    ^ imaginary = 0
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1054
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1055
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1056
isZero
13548
Claus Gittinger <cg@exept.de>
parents: 9128
diff changeset
  1057
    "Answer whether 'self = self class zero'.
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1058
     We can't use #= because #= is defined in terms of #isZero"
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1059
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1060
    ^real isZero and: [imaginary isZero]
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1061
!
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1062
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1063
sign
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1064
    imaginary isZero ifTrue:[^ real sign].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1065
    ^ self / self abs
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1066
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1067
    "/ squeak does:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1068
    "/      ^ real sign
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1069
    "/
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1070
    "/ the old code was:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1071
    "/ 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
  1072
    "/  Q: is this a good thing to do ?
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1073
    "/  cg: no
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
    "/ ^ self class real:(real sign) imaginary:(imaginary sign)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1076
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1077
    " this is consistent with Wolfram's output:
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:0) sign -> 1
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1080
     (Complex new setReal:-1 setImaginary:0) sign -> -1
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1081
     (Complex new setReal:0 setImaginary:0) sign -> 0
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1082
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1083
     (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
     (Complex new setReal:1 setImaginary:-1) sign -> (0.707106781186547-0.707106781186547i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1088
     (Complex new setReal:0 setImaginary:-1) sign -> (0.0-1.0i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1089
     (Complex new setReal:-1 setImaginary:-1) sign -> (-0.707106781186547-0.707106781186547i)
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
! !
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1092
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1093
!Complex methodsFor:'trigonometric functions'!
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
arcCos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1096
    "Answer the arc cosine of the receiver.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1097
     This is the inverse function of cos."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1098
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1099
    | x y tmp sh2y shy delta ch2y chy |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1100
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1101
    imaginary = 0 ifTrue: [
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1102
        real abs > 1 ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1103
            x := real < 0 ifTrue: [Float pi]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1104
                          ifFalse: [0].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1105
            y := real copySignTo: real abs arcCosh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1106
            ^ self class real: x imaginary: y
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1107
        ] ifFalse: [
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1108
            ^self class real: real arcCos imaginary: 0
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1109
        ]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1110
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1111
    tmp := self squaredNorm - 1 / 2.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1112
    delta := tmp squared + imaginary squared.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1113
    sh2y := tmp + delta sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1114
    shy := sh2y sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1115
    ch2y := 1 + sh2y.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1116
    chy := ch2y sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1117
    y := imaginary copySignTo: shy arSinh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1118
    x := (real / chy) arcCos.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1119
    ^ self class real: x imaginary: y negated
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
     (10+4i) cos (-22.91356068209214107-14.84629106966035727i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1123
     (10+4i) cos arcCos 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1124
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1125
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1126
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1127
arcCosh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1128
    "Answer the receiver's area hyperbolic cosine.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1129
     That is the inverse function of cosh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1130
     Some possible implementations:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1131
        ^imaginary > 0 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1132
            ifTrue: [(self + (self * self - 1) sqrt) ln]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1133
            ifFalse: [(self + (self * self - 1) sqrt) ln negated]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1134
        ^self arcCos i
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1135
     This implementation provides an answer with a positive real part.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1136
     It also avoids creating intermediate Complex."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1137
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1138
    | x y tmp sh2x shx delta ch2x chx |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1139
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1140
    imaginary = 0 ifTrue: [
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1141
        real abs > 1 ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1142
            y := real < 0 ifTrue: [Float pi]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1143
                          ifFalse: [0].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1144
            x := real abs arcCosh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1145
            ^ self class real: x imaginary: y
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1146
        ] ifFalse: [
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1147
            ^ self class real: 0 imaginary: real arcCos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1148
        ]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1149
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1150
    tmp := self squaredNorm - 1 / 2.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1151
    delta := tmp squared + imaginary squared.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1152
    sh2x := tmp + delta sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1153
    shx := sh2x sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1154
    ch2x := 1 + sh2x.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1155
    chx := ch2x sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1156
    x := shx arSinh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1157
    y := imaginary copySignTo: (real / chx) arcCos.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1158
    ^ self class real: x imaginary: y
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
     (10 + 4i) arcCosh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1162
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1163
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1164
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1165
arcSin
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1166
    "Answer the arc sine of the receiver.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1167
    This is the inverse function of sin."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1168
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1169
    | x y tmp delta sh2y shy ch2y chy |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1170
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1171
    imaginary = 0 ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1172
        real abs > 1 ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1173
            x := Float pi / 2 * real sign.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1174
            y := (real copySignTo: real abs arcCosh) negated.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1175
            ^ self class real: x imaginary: y
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1176
        ] ifFalse: [
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1177
            ^ self class real: real arcSin imaginary: 0
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1178
        ]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1179
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1180
    tmp := (self squaredNorm - 1) / 2.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1181
    delta := tmp squared + imaginary squared.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1182
    sh2y := tmp + delta sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1183
    shy := sh2y sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1184
    ch2y := 1 + sh2y.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1185
    chy := ch2y sqrt.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1186
    y := imaginary copySignTo: shy arcSinh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1187
    x := (real / chy) arcSin.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1188
    ^self class real: x imaginary: y
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
     (10 + 4i) sin -> (-14.85625516387525498-22.89819255096375875i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1192
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1193
!
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
arcSinh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1196
    "Answer receiver's area hyperbolic sine.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1197
     That is the inverse function of sinh."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1198
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1199
    "Some possible implementation:
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
    ^imaginary * real < 0 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1202
            ifTrue: [(self + (self * self + 1) sqrt) ln]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1203
            ifFalse: [(self - (self * self + 1) sqrt) ln]"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1204
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1205
    ^self i arcSin i negated
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1206
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1207
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1208
     (10 + 4i) sinh arcSinh -> (-10.0-0.8584073464102067614i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1209
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1210
!
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1211
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1212
arcTan
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1213
    "Answer the arc tangent of the receiver.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1214
     This is the inverse function of tan."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1215
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1216
    | r2 |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1217
    
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1218
    r2 := self squaredNorm.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1219
    ^ self class
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1220
        real: (real * 2 arcTan: 1 - r2) / 2
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1221
        imaginary: ((r2 + (imaginary * 2) + 1) / (r2 - (imaginary * 2) + 1)) ln / 4
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
     (1+4i) tan arcTan -> (1.000000000000000043+4.000000000000004772i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1225
     (10+4i) tan arcTan -> (0.5752220392306203171+4.00000000000000004i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1226
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1227
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1228
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1229
arcTan:denominator 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1230
    "Answer the  four quadrants arc tangent of receiver over denominator."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1231
    
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1232
    |res|
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1233
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1234
    denominator isZero ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1235
        self isZero ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1236
            "shouldn't it be an error ? ^DomainError signal: '0 arcTan: 0'"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1237
            ^ self class real:0 imaginary:0
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1238
        ] ifFalse:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1239
            ^ self class real:Float pi / (real copySignTo:2) imaginary:0
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1240
        ]
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1241
    ]. 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1242
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1243
    res := (self / denominator) arcTan.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1244
    denominator real < 0 ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1245
        res := res + Float pi
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1246
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1247
    res real > Float pi ifTrue:[
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1248
        res := res - (Float pi * 2)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1249
    ].
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1250
    ^ res
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1251
!
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
arcTanh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1254
    "Answer the receiver's area hyperbolic tangent.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1255
     That is the inverse function of tanh."
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
    "Some other possible implementation:
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
    ^((1 + self) / (1 - self)) ln / 2"
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1260
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1261
    ^ self i arcTan i negated
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
     (10+4i) tanh arcTanh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1265
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1266
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1267
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1268
cos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1269
    "Answer the receiver's cosine."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1270
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1271
    "/ ^ self i cosh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1272
    ^ self class 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1273
        real:(real cos * imaginary cosh)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1274
        imaginary:(-1 * real sin * imaginary sinh) 
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
     (10+4i) cos -> (-22.91356068209214107+14.84629106966035727i) 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1278
     (10+4i) i cosh (-22.91356068209214107+14.84629106966035727i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1279
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1280
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1281
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1282
cosh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1283
    "Answer the receiver's hyperbolic cosine.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1284
    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
  1285
    that is in term of exponential:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1286
        ^ (self exp + self negated exp) / 2.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1287
    This implementation avoids creating intermediate objects."
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
    ^self class
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1290
            real: real cosh * imaginary cos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1291
            imaginary: real sinh * imaginary sin
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
     (10+4i) cosh -> (-7198.729443310666079-8334.842120982836036i) 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1295
    "
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
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1298
sin
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1299
    "Answer the receiver's sine."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1300
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1301
    "/ alternative:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1302
    "/      ^ self i sinh i negated
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
    ^ self class 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1305
        real:(real sin * imaginary cosh)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1306
        imaginary:(real cos * imaginary sinh)
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
     (10+4i) sin -> (-14.85625516387525498-22.89819255096375875i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1310
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1311
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1312
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1313
sinh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1314
    "Answer the receiver's hyperbolic sine.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1315
    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
  1316
    that is in term of exponential:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1317
        ^ (self exp - self negated exp) / 2.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1318
    This implementation avoids creating intermediate objects."
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
    ^ self class
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1321
            real: real sinh * imaginary cos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1322
            imaginary: real cosh * imaginary sin
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
     10 sinh -> 11013.23287470339338
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1326
     (10+4i) sinh (-7198.729413635291604-8334.842155341616165i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1327
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1328
!
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1329
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1330
tan
22938
c3954250f7e7 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22729
diff changeset
  1331
    "Answer the receiver's tangent."
22729
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1332
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1333
    ^ self sin / self cos
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1334
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
     (10+4i) tan -> (0.0006123503000121616919+0.9997260574022127583i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1337
     (10+4i) sin -> (-14.85625516387525498-22.89819255096375875i) 
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1338
     (10+4i) cos -> (-22.91356068209214107+14.84629106966035727i)
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1339
    "
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
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1342
tanh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1343
    "Answer the receiver's hyperbolic tangent."
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1344
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1345
    "Some possible implementations are:
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1346
        ^self sinh / self cosh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1347
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1348
        | tr ti |
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1349
        tr := real tanh.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1350
        ti := imaginary tan i.
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1351
        ^(tr + ti) / (tr * ti + 1)
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
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1354
    ^ self i tan i negated
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1355
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1356
    "
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1357
     (10+4i) tanh
f2319f1ce645 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21987
diff changeset
  1358
    "
7355
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1359
! !
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1360
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1361
!Complex methodsFor:'truncation & rounding'!
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1362
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1363
ceiling
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1364
    "blocked: complex numbers have no ceiling"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1365
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1366
    ^ self shouldNotImplement
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
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1369
floor
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1370
    "blocked: complex numbers have no floor"
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1371
96f466eeddf5 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7221
diff changeset
  1372
    ^ self shouldNotImplement
265
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1373
! !
f014922e3b71 Initial revision
claus
parents:
diff changeset
  1374
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
  1375
!Complex class methodsFor:'documentation'!
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
  1376
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
  1377
version
18836
63832ff17b6c #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 16742
diff changeset
  1378
    ^ '$Header$'
23644
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
  1379
!
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
  1380
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
  1381
version_CVS
a1ff1b6e80dd #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 23643
diff changeset
  1382
    ^ '$Header$'
3637
2b4d733c9a01 legal stuff
Claus Gittinger <cg@exept.de>
parents: 3633
diff changeset
  1383
! !
16449
8d3bffc78311 class: Complex
Stefan Vogel <sv@exept.de>
parents: 13548
diff changeset
  1384