[project @ 2005-10-21 10:26:57 by ross]
[packages/old-time.git] / Data / Complex.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Data.Complex
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
6 --
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
10 --
11 -- Complex numbers.
12 --
13 -----------------------------------------------------------------------------
14
15 module Data.Complex
16 (
17 -- * Rectangular form
18 Complex((:+))
19
20 , realPart -- :: (RealFloat a) => Complex a -> a
21 , imagPart -- :: (RealFloat a) => Complex a -> a
22 -- * Polar form
23 , mkPolar -- :: (RealFloat a) => a -> a -> Complex a
24 , cis -- :: (RealFloat a) => a -> Complex a
25 , polar -- :: (RealFloat a) => Complex a -> (a,a)
26 , magnitude -- :: (RealFloat a) => Complex a -> a
27 , phase -- :: (RealFloat a) => Complex a -> a
28 -- * Conjugate
29 , conjugate -- :: (RealFloat a) => Complex a -> Complex a
30
31 -- Complex instances:
32 --
33 -- (RealFloat a) => Eq (Complex a)
34 -- (RealFloat a) => Read (Complex a)
35 -- (RealFloat a) => Show (Complex a)
36 -- (RealFloat a) => Num (Complex a)
37 -- (RealFloat a) => Fractional (Complex a)
38 -- (RealFloat a) => Floating (Complex a)
39 --
40 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
41
42 ) where
43
44 import Prelude
45
46 import Data.Typeable
47
48 #ifdef __HUGS__
49 import Hugs.Prelude(Num(fromInt), Fractional(fromDouble))
50 #endif
51
52 infix 6 :+
53
54 -- -----------------------------------------------------------------------------
55 -- The Complex type
56
57 -- | Complex numbers are an algebraic type.
58 --
59 -- For a complex number @z@, @'abs' z@ is a number with the magnitude of @z@,
60 -- but oriented in the positive real direction, whereas @'signum' z@
61 -- has the phase of @z@, but unit magnitude.
62 data (RealFloat a) => Complex a
63 = !a :+ !a -- ^ forms a complex number from its real and imaginary
64 -- rectangular components.
65 deriving (Eq, Read, Show)
66
67 -- -----------------------------------------------------------------------------
68 -- Functions over Complex
69
70 -- | Extracts the real part of a complex number.
71 realPart :: (RealFloat a) => Complex a -> a
72 realPart (x :+ _) = x
73
74 -- | Extracts the imaginary part of a complex number.
75 imagPart :: (RealFloat a) => Complex a -> a
76 imagPart (_ :+ y) = y
77
78 -- | The conjugate of a complex number.
79 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
80 conjugate :: (RealFloat a) => Complex a -> Complex a
81 conjugate (x:+y) = x :+ (-y)
82
83 -- | Form a complex number from polar components of magnitude and phase.
84 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
85 mkPolar :: (RealFloat a) => a -> a -> Complex a
86 mkPolar r theta = r * cos theta :+ r * sin theta
87
88 -- | @'cis' t@ is a complex value with magnitude @1@
89 -- and phase @t@ (modulo @2*'pi'@).
90 {-# SPECIALISE cis :: Double -> Complex Double #-}
91 cis :: (RealFloat a) => a -> Complex a
92 cis theta = cos theta :+ sin theta
93
94 -- | The function 'polar' takes a complex number and
95 -- returns a (magnitude, phase) pair in canonical form:
96 -- the magnitude is nonnegative, and the phase in the range @(-'pi', 'pi']@;
97 -- if the magnitude is zero, then so is the phase.
98 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
99 polar :: (RealFloat a) => Complex a -> (a,a)
100 polar z = (magnitude z, phase z)
101
102 -- | The nonnegative magnitude of a complex number.
103 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
104 magnitude :: (RealFloat a) => Complex a -> a
105 magnitude (x:+y) = scaleFloat k
106 (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
107 where k = max (exponent x) (exponent y)
108 mk = - k
109
110 -- | The phase of a complex number, in the range @(-'pi', 'pi']@.
111 -- If the magnitude is zero, then so is the phase.
112 {-# SPECIALISE phase :: Complex Double -> Double #-}
113 phase :: (RealFloat a) => Complex a -> a
114 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
115 phase (x:+y) = atan2 y x
116
117
118 -- -----------------------------------------------------------------------------
119 -- Instances of Complex
120
121 #include "Typeable.h"
122 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
123
124 instance (RealFloat a) => Num (Complex a) where
125 {-# SPECIALISE instance Num (Complex Float) #-}
126 {-# SPECIALISE instance Num (Complex Double) #-}
127 (x:+y) + (x':+y') = (x+x') :+ (y+y')
128 (x:+y) - (x':+y') = (x-x') :+ (y-y')
129 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
130 negate (x:+y) = negate x :+ negate y
131 abs z = magnitude z :+ 0
132 signum 0 = 0
133 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
134 fromInteger n = fromInteger n :+ 0
135 #ifdef __HUGS__
136 fromInt n = fromInt n :+ 0
137 #endif
138
139 instance (RealFloat a) => Fractional (Complex a) where
140 {-# SPECIALISE instance Fractional (Complex Float) #-}
141 {-# SPECIALISE instance Fractional (Complex Double) #-}
142 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
143 where x'' = scaleFloat k x'
144 y'' = scaleFloat k y'
145 k = - max (exponent x') (exponent y')
146 d = x'*x'' + y'*y''
147
148 fromRational a = fromRational a :+ 0
149 #ifdef __HUGS__
150 fromDouble a = fromDouble a :+ 0
151 #endif
152
153 instance (RealFloat a) => Floating (Complex a) where
154 {-# SPECIALISE instance Floating (Complex Float) #-}
155 {-# SPECIALISE instance Floating (Complex Double) #-}
156 pi = pi :+ 0
157 exp (x:+y) = expx * cos y :+ expx * sin y
158 where expx = exp x
159 log z = log (magnitude z) :+ phase z
160
161 sqrt 0 = 0
162 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
163 where (u,v) = if x < 0 then (v',u') else (u',v')
164 v' = abs y / (u'*2)
165 u' = sqrt ((magnitude z + abs x) / 2)
166
167 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
168 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
169 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
170 where sinx = sin x
171 cosx = cos x
172 sinhy = sinh y
173 coshy = cosh y
174
175 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
176 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
177 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
178 where siny = sin y
179 cosy = cos y
180 sinhx = sinh x
181 coshx = cosh x
182
183 asin z@(x:+y) = y':+(-x')
184 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
185 acos z = y'':+(-x'')
186 where (x'':+y'') = log (z + ((-y'):+x'))
187 (x':+y') = sqrt (1 - z*z)
188 atan z@(x:+y) = y':+(-x')
189 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
190
191 asinh z = log (z + sqrt (1+z*z))
192 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
193 atanh z = log ((1+z) / sqrt (1-z*z))