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