6b9999b19c3533ca43791a96c58dcf3a0accd7aa
[ghc-base.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 #ifndef __NHC__
47 import Data.Typeable
48 #endif
49
50 #ifdef __HUGS__
51 import Hugs.Prelude(Num(fromInt), Fractional(fromDouble))
52 #endif
53
54 infix  6  :+
55
56 -- -----------------------------------------------------------------------------
57 -- The Complex type
58
59 -- | Complex numbers are an algebraic type.
60 --
61 -- For a complex number @z@, @'abs' z@ is a number with the magnitude of @z@,
62 -- but oriented in the positive real direction, whereas @'signum' z@
63 -- has the phase of @z@, but unit magnitude.
64 data (RealFloat a) => Complex a
65   = !a :+ !a    -- ^ forms a complex number from its real and imaginary
66                 -- rectangular components.
67   deriving (Eq, Read, Show)
68
69 -- -----------------------------------------------------------------------------
70 -- Functions over Complex
71
72 -- | Extracts the real part of a complex number.
73 realPart :: (RealFloat a) => Complex a -> a
74 realPart (x :+ _) =  x
75
76 -- | Extracts the imaginary part of a complex number.
77 imagPart :: (RealFloat a) => Complex a -> a
78 imagPart (_ :+ y) =  y
79
80 -- | The conjugate of a complex number.
81 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
82 conjugate        :: (RealFloat a) => Complex a -> Complex a
83 conjugate (x:+y) =  x :+ (-y)
84
85 -- | Form a complex number from polar components of magnitude and phase.
86 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
87 mkPolar          :: (RealFloat a) => a -> a -> Complex a
88 mkPolar r theta  =  r * cos theta :+ r * sin theta
89
90 -- | @'cis' t@ is a complex value with magnitude @1@
91 -- and phase @t@ (modulo @2*'pi'@).
92 {-# SPECIALISE cis :: Double -> Complex Double #-}
93 cis              :: (RealFloat a) => a -> Complex a
94 cis theta        =  cos theta :+ sin theta
95
96 -- | The function 'polar' takes a complex number and
97 -- returns a (magnitude, phase) pair in canonical form:
98 -- the magnitude is nonnegative, and the phase in the range @(-'pi', 'pi']@;
99 -- if the magnitude is zero, then so is the phase.
100 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
101 polar            :: (RealFloat a) => Complex a -> (a,a)
102 polar z          =  (magnitude z, phase z)
103
104 -- | The nonnegative magnitude of a complex number.
105 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
106 magnitude :: (RealFloat a) => Complex a -> a
107 magnitude (x:+y) =  scaleFloat k
108                      (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
109                     where k  = max (exponent x) (exponent y)
110                           mk = - k
111
112 -- | The phase of a complex number, in the range @(-'pi', 'pi']@.
113 -- If the magnitude is zero, then so is the phase.
114 {-# SPECIALISE phase :: Complex Double -> Double #-}
115 phase :: (RealFloat a) => Complex a -> a
116 phase (0 :+ 0)   = 0            -- SLPJ July 97 from John Peterson
117 phase (x:+y)     = atan2 y x
118
119
120 -- -----------------------------------------------------------------------------
121 -- Instances of Complex
122
123 #include "Typeable.h"
124 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
125
126 instance  (RealFloat a) => Num (Complex a)  where
127     {-# SPECIALISE instance Num (Complex Float) #-}
128     {-# SPECIALISE instance Num (Complex Double) #-}
129     (x:+y) + (x':+y')   =  (x+x') :+ (y+y')
130     (x:+y) - (x':+y')   =  (x-x') :+ (y-y')
131     (x:+y) * (x':+y')   =  (x*x'-y*y') :+ (x*y'+y*x')
132     negate (x:+y)       =  negate x :+ negate y
133     abs z               =  magnitude z :+ 0
134     signum 0            =  0
135     signum z@(x:+y)     =  x/r :+ y/r  where r = magnitude z
136     fromInteger n       =  fromInteger n :+ 0
137 #ifdef __HUGS__
138     fromInt n           =  fromInt n :+ 0
139 #endif
140
141 instance  (RealFloat a) => Fractional (Complex a)  where
142     {-# SPECIALISE instance Fractional (Complex Float) #-}
143     {-# SPECIALISE instance Fractional (Complex Double) #-}
144     (x:+y) / (x':+y')   =  (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
145                            where x'' = scaleFloat k x'
146                                  y'' = scaleFloat k y'
147                                  k   = - max (exponent x') (exponent y')
148                                  d   = x'*x'' + y'*y''
149
150     fromRational a      =  fromRational a :+ 0
151 #ifdef __HUGS__
152     fromDouble a        =  fromDouble a :+ 0
153 #endif
154
155 instance  (RealFloat a) => Floating (Complex a) where
156     {-# SPECIALISE instance Floating (Complex Float) #-}
157     {-# SPECIALISE instance Floating (Complex Double) #-}
158     pi             =  pi :+ 0
159     exp (x:+y)     =  expx * cos y :+ expx * sin y
160                       where expx = exp x
161     log z          =  log (magnitude z) :+ phase z
162
163     sqrt 0         =  0
164     sqrt z@(x:+y)  =  u :+ (if y < 0 then -v else v)
165                       where (u,v) = if x < 0 then (v',u') else (u',v')
166                             v'    = abs y / (u'*2)
167                             u'    = sqrt ((magnitude z + abs x) / 2)
168
169     sin (x:+y)     =  sin x * cosh y :+ cos x * sinh y
170     cos (x:+y)     =  cos x * cosh y :+ (- sin x * sinh y)
171     tan (x:+y)     =  (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
172                       where sinx  = sin x
173                             cosx  = cos x
174                             sinhy = sinh y
175                             coshy = cosh y
176
177     sinh (x:+y)    =  cos y * sinh x :+ sin  y * cosh x
178     cosh (x:+y)    =  cos y * cosh x :+ sin y * sinh x
179     tanh (x:+y)    =  (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
180                       where siny  = sin y
181                             cosy  = cos y
182                             sinhx = sinh x
183                             coshx = cosh x
184
185     asin z@(x:+y)  =  y':+(-x')
186                       where  (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
187     acos z         =  y'':+(-x'')
188                       where (x'':+y'') = log (z + ((-y'):+x'))
189                             (x':+y')   = sqrt (1 - z*z)
190     atan z@(x:+y)  =  y':+(-x')
191                       where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
192
193     asinh z        =  log (z + sqrt (1+z*z))
194     acosh z        =  log (z + (z+1) * sqrt ((z-1)/(z+1)))
195     atanh z        =  log ((1+z) / sqrt (1-z*z))