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