[project @ 2001-08-04 06:19:54 by ken]
[ghc-hetmet.git] / ghc / lib / std / Complex.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: Complex.lhs,v 1.5 2000/06/30 13:39:35 simonmar Exp $
3 %
4 % (c) The University of Glasgow, 1994-2000
5 %
6
7 \section[Complex]{Module @Complex@}
8
9 \begin{code}
10 module Complex
11         ( Complex((:+))
12         
13         , realPart      -- :: (RealFloat a) => Complex a -> a
14         , imagPart      -- :: (RealFloat a) => Complex a -> a
15         , conjugate     -- :: (RealFloat a) => Complex a -> Complex a
16         , mkPolar       -- :: (RealFloat a) => a -> a -> Complex a
17         , cis           -- :: (RealFloat a) => a -> Complex a
18         , polar         -- :: (RealFloat a) => Complex a -> (a,a)
19         , magnitude     -- :: (RealFloat a) => Complex a -> a
20         , phase         -- :: (RealFloat a) => Complex a -> a
21         
22         -- Complex instances:
23         --
24         --  (RealFloat a) => Eq         (Complex a)
25         --  (RealFloat a) => Read       (Complex a)
26         --  (RealFloat a) => Show       (Complex a)
27         --  (RealFloat a) => Num        (Complex a)
28         --  (RealFloat a) => Fractional (Complex a)
29         --  (RealFloat a) => Floating   (Complex a)
30         -- 
31         -- Implementation checked wrt. Haskell 98 lib report, 1/99.
32
33         )  where
34
35 import Prelude
36
37 infix  6  :+
38 \end{code}
39
40 %*********************************************************
41 %*                                                      *
42 \subsection{The @Complex@ type}
43 %*                                                      *
44 %*********************************************************
45
46 \begin{code}
47 data  (RealFloat a)     => Complex a = !a :+ !a  deriving (Eq, Read, Show)
48 \end{code}
49
50
51 %*********************************************************
52 %*                                                      *
53 \subsection{Functions over @Complex@}
54 %*                                                      *
55 %*********************************************************
56
57 \begin{code}
58 realPart, imagPart :: (RealFloat a) => Complex a -> a
59 realPart (x :+ _) =  x
60 imagPart (_ :+ y) =  y
61
62 conjugate        :: (RealFloat a) => Complex a -> Complex a
63 conjugate (x:+y) =  x :+ (-y)
64
65 mkPolar          :: (RealFloat a) => a -> a -> Complex a
66 mkPolar r theta  =  r * cos theta :+ r * sin theta
67
68 cis              :: (RealFloat a) => a -> Complex a
69 cis theta        =  cos theta :+ sin theta
70
71 polar            :: (RealFloat a) => Complex a -> (a,a)
72 polar z          =  (magnitude z, phase z)
73
74 magnitude :: (RealFloat a) => Complex a -> a
75 magnitude (x:+y) =  scaleFloat k
76                      (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
77                     where k  = max (exponent x) (exponent y)
78                           mk = - k
79
80 phase :: (RealFloat a) => Complex a -> a
81 phase (0 :+ 0)   = 0            -- SLPJ July 97 from John Peterson
82 phase (x:+y)     = atan2 y x
83 \end{code}
84
85
86 %*********************************************************
87 %*                                                      *
88 \subsection{Instances of @Complex@}
89 %*                                                      *
90 %*********************************************************
91
92 \begin{code}
93 instance  (RealFloat a) => Num (Complex a)  where
94     {-# SPECIALISE instance Num (Complex Float) #-}
95     {-# SPECIALISE instance Num (Complex Double) #-}
96     (x:+y) + (x':+y')   =  (x+x') :+ (y+y')
97     (x:+y) - (x':+y')   =  (x-x') :+ (y-y')
98     (x:+y) * (x':+y')   =  (x*x'-y*y') :+ (x*y'+y*x')
99     negate (x:+y)       =  negate x :+ negate y
100     abs z               =  magnitude z :+ 0
101     signum 0            =  0
102     signum z@(x:+y)     =  x/r :+ y/r  where r = magnitude z
103     fromInteger n       =  fromInteger n :+ 0
104
105 instance  (RealFloat a) => Fractional (Complex a)  where
106     {-# SPECIALISE instance Fractional (Complex Float) #-}
107     {-# SPECIALISE instance Fractional (Complex Double) #-}
108     (x:+y) / (x':+y')   =  (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
109                            where x'' = scaleFloat k x'
110                                  y'' = scaleFloat k y'
111                                  k   = - max (exponent x') (exponent y')
112                                  d   = x'*x'' + y'*y''
113
114     fromRational a      =  fromRational a :+ 0
115
116 instance  (RealFloat a) => Floating (Complex a) where
117     {-# SPECIALISE instance Floating (Complex Float) #-}
118     {-# SPECIALISE instance Floating (Complex Double) #-}
119     pi             =  pi :+ 0
120     exp (x:+y)     =  expx * cos y :+ expx * sin y
121                       where expx = exp x
122     log z          =  log (magnitude z) :+ phase z
123
124     sqrt 0         =  0
125     sqrt z@(x:+y)  =  u :+ (if y < 0 then -v else v)
126                       where (u,v) = if x < 0 then (v',u') else (u',v')
127                             v'    = abs y / (u'*2)
128                             u'    = sqrt ((magnitude z + abs x) / 2)
129
130     sin (x:+y)     =  sin x * cosh y :+ cos x * sinh y
131     cos (x:+y)     =  cos x * cosh y :+ (- sin x * sinh y)
132     tan (x:+y)     =  (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
133                       where sinx  = sin x
134                             cosx  = cos x
135                             sinhy = sinh y
136                             coshy = cosh y
137
138     sinh (x:+y)    =  cos y * sinh x :+ sin  y * cosh x
139     cosh (x:+y)    =  cos y * cosh x :+ sin y * sinh x
140     tanh (x:+y)    =  (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
141                       where siny  = sin y
142                             cosy  = cos y
143                             sinhx = sinh x
144                             coshx = cosh x
145
146     asin z@(x:+y)  =  y':+(-x')
147                       where  (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
148     acos z         =  y'':+(-x'')
149                       where (x'':+y'') = log (z + ((-y'):+x'))
150                             (x':+y')   = sqrt (1 - z*z)
151     atan z@(x:+y)  =  y':+(-x')
152                       where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
153
154     asinh z        =  log (z + sqrt (1+z*z))
155     acosh z        =  log (z + (z+1) * sqrt ((z-1)/(z+1)))
156     atanh z        =  log ((1+z) / sqrt (1-z*z))
157 \end{code}