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