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