2 % (c) The AQUA Project, Glasgow University, 1994-1997
5 \section[Complex]{Module @Complex@}
11 realPart, imagPart, conjugate, mkPolar,
12 cis, polar, magnitude, phase
20 %*********************************************************
22 \subsection{The @Complex@ type}
24 %*********************************************************
27 data (RealFloat a) => Complex a = !a :+ !a deriving (Eq,Read,Show)
31 %*********************************************************
33 \subsection{Functions over @Complex@}
35 %*********************************************************
38 realPart, imagPart :: (RealFloat a) => Complex a -> a
42 conjugate :: (RealFloat a) => Complex a -> Complex a
43 conjugate (x:+y) = x :+ (-y)
45 mkPolar :: (RealFloat a) => a -> a -> Complex a
46 mkPolar r theta = r * cos theta :+ r * sin theta
48 cis :: (RealFloat a) => a -> Complex a
49 cis theta = cos theta :+ sin theta
51 polar :: (RealFloat a) => Complex a -> (a,a)
52 polar z = (magnitude z, phase z)
54 magnitude, phase :: (RealFloat a) => Complex a -> a
55 magnitude (x:+y) = scaleFloat k
56 (sqrt ((scaleFloat mk x)^2 + (scaleFloat mk y)^2))
57 where k = max (exponent x) (exponent y)
60 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
61 phase (x:+y) = atan2 y x
65 %*********************************************************
67 \subsection{Instances of @Complex@}
69 %*********************************************************
72 instance (RealFloat a) => Num (Complex a) where
73 (x:+y) + (x':+y') = (x+x') :+ (y+y')
74 (x:+y) - (x':+y') = (x-x') :+ (y-y')
75 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
76 negate (x:+y) = negate x :+ negate y
77 abs z = magnitude z :+ 0
79 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
80 fromInteger n = fromInteger n :+ 0
82 instance (RealFloat a) => Fractional (Complex a) where
83 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
84 where x'' = scaleFloat k x'
86 k = - max (exponent x') (exponent y')
89 fromRational a = fromRational a :+ 0
91 instance (RealFloat a) => Floating (Complex a) where
93 exp (x:+y) = expx * cos y :+ expx * sin y
95 log z = log (magnitude z) :+ phase z
98 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
99 where (u,v) = if x < 0 then (v',u') else (u',v')
101 u' = sqrt ((magnitude z + abs x) / 2)
103 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
104 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
105 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
111 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
112 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
113 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
119 asin z@(x:+y) = y':+(-x')
120 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
121 acos z@(x:+y) = y'':+(-x'')
122 where (x'':+y'') = log (z + ((-y'):+x'))
123 (x':+y') = sqrt (1 - z*z)
124 atan z@(x:+y) = y':+(-x')
125 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
127 asinh z = log (z + sqrt (1+z*z))
128 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
129 atanh z = log ((1+z) / sqrt (1-z*z))