[project @ 1999-02-03 16:53:16 by simonm]
[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     (x:+y) + (x':+y')   =  (x+x') :+ (y+y')
93     (x:+y) - (x':+y')   =  (x-x') :+ (y-y')
94     (x:+y) * (x':+y')   =  (x*x'-y*y') :+ (x*y'+y*x')
95     negate (x:+y)       =  negate x :+ negate y
96     abs z               =  magnitude z :+ 0
97     signum 0            =  0
98     signum z@(x:+y)     =  x/r :+ y/r  where r = magnitude z
99     fromInteger n       =  fromInteger n :+ 0
100
101 instance  (RealFloat a) => Fractional (Complex a)  where
102     (x:+y) / (x':+y')   =  (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
103                            where x'' = scaleFloat k x'
104                                  y'' = scaleFloat k y'
105                                  k   = - max (exponent x') (exponent y')
106                                  d   = x'*x'' + y'*y''
107
108     fromRational a      =  fromRational a :+ 0
109
110 instance  (RealFloat a) => Floating (Complex a) where
111     pi             =  pi :+ 0
112     exp (x:+y)     =  expx * cos y :+ expx * sin y
113                       where expx = exp x
114     log z          =  log (magnitude z) :+ phase z
115
116     sqrt 0         =  0
117     sqrt z@(x:+y)  =  u :+ (if y < 0 then -v else v)
118                       where (u,v) = if x < 0 then (v',u') else (u',v')
119                             v'    = abs y / (u'*2)
120                             u'    = sqrt ((magnitude z + abs x) / 2)
121
122     sin (x:+y)     =  sin x * cosh y :+ cos x * sinh y
123     cos (x:+y)     =  cos x * cosh y :+ (- sin x * sinh y)
124     tan (x:+y)     =  (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
125                       where sinx  = sin x
126                             cosx  = cos x
127                             sinhy = sinh y
128                             coshy = cosh y
129
130     sinh (x:+y)    =  cos y * sinh x :+ sin  y * cosh x
131     cosh (x:+y)    =  cos y * cosh x :+ sin y * sinh x
132     tanh (x:+y)    =  (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
133                       where siny  = sin y
134                             cosy  = cos y
135                             sinhx = sinh x
136                             coshx = cosh x
137
138     asin z@(x:+y)  =  y':+(-x')
139                       where  (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
140     acos z         =  y'':+(-x'')
141                       where (x'':+y'') = log (z + ((-y'):+x'))
142                             (x':+y')   = sqrt (1 - z*z)
143     atan z@(x:+y)  =  y':+(-x')
144                       where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
145
146     asinh z        =  log (z + sqrt (1+z*z))
147     acosh z        =  log (z + (z+1) * sqrt ((z-1)/(z+1)))
148     atanh z        =  log ((1+z) / sqrt (1-z*z))
149 \end{code}