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