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