2a5d1a06e26f8f0eca37d568bca482708976324c
2 \label{module:Data.Complex}
5 module Data.Complex (
6 Complex(:+), realPart, imagPart, mkPolar, cis, polar, magnitude,
7 phase, conjugate
8 ) where\end{verbatim}}
11 \section{Rectangular form
12 }
14 \item[\begin{tabular}{@{}l}
15 data\ RealFloat\ a\ =>\ Complex\ a
18 \haddockdecltt{=} & \haddockdecltt{!a :+ !a} & forms a complex number from its real and imaginary
19 rectangular components.
20 \\
21 \end{tabulary}\par
22 Complex numbers are an algebraic type.
23 \par
25 but oriented in the positive real direction, whereas \haddocktt{signum\ z}
26 has the phase of \haddocktt{z}, but unit magnitude.
27 \par
31 \item[\begin{tabular}{@{}l}
32 instance\ Typeable1\ Complex\\instance\ RealFloat\ a\ =>\ Eq\ (Complex\ a)\\instance\ RealFloat\ a\ =>\ Floating\ (Complex\ a)\\instance\ RealFloat\ a\ =>\ Fractional\ (Complex\ a)\\instance\ (Data\ a,\ RealFloat\ a)\ =>\ Data\ (Complex\ a)\\instance\ RealFloat\ a\ =>\ Num\ (Complex\ a)\\instance\ (Read\ a,\ RealFloat\ a)\ =>\ Read\ (Complex\ a)\\instance\ RealFloat\ a\ =>\ Show\ (Complex\ a)
33 \end{tabular}]
36 \item[\begin{tabular}{@{}l}
37 realPart\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ a
39 Extracts the real part of a complex number.
40 \par
44 \item[\begin{tabular}{@{}l}
45 imagPart\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ a
47 Extracts the imaginary part of a complex number.
48 \par
51 \section{Polar form
52 }
54 \item[\begin{tabular}{@{}l}
55 mkPolar\ ::\ RealFloat\ a\ =>\ a\ ->\ a\ ->\ Complex\ a
57 Form a complex number from polar components of magnitude and phase.
58 \par
62 \item[\begin{tabular}{@{}l}
63 cis\ ::\ RealFloat\ a\ =>\ a\ ->\ Complex\ a
67 \par
71 \item[\begin{tabular}{@{}l}
72 polar\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ (a,\ a)
74 The function \haddockid{polar} takes a complex number and
75 returns a (magnitude, phase) pair in canonical form:
76 the magnitude is nonnegative, and the phase in the range \haddocktt{(-pi,\ pi{\char 93}};
77 if the magnitude is zero, then so is the phase.
78 \par
82 \item[\begin{tabular}{@{}l}
83 magnitude\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ a
85 The nonnegative magnitude of a complex number.
86 \par
90 \item[\begin{tabular}{@{}l}
91 phase\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ a
93 The phase of a complex number, in the range \haddocktt{(-pi,\ pi{\char 93}}.
94 If the magnitude is zero, then so is the phase.
95 \par
98 \section{Conjugate
99 }
101 \item[\begin{tabular}{@{}l}
102 conjugate\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ Complex\ a
104 The conjugate of a complex number.
105 \par
108 \section{Specification
109 }
110 \begin{quote}
112 module Complex(Complex((:+)), realPart, imagPart, conjugate, mkPolar,
113 cis, polar, magnitude, phase) where
115 infix 6 :+
117 data (RealFloat a) => Complex a = !a :+ !a deriving (Eq,Read,Show)
120 realPart, imagPart :: (RealFloat a) => Complex a -> a
121 realPart (x:+y) = x
122 imagPart (x:+y) = y
124 conjugate :: (RealFloat a) => Complex a -> Complex a
125 conjugate (x:+y) = x :+ (-y)
127 mkPolar :: (RealFloat a) => a -> a -> Complex a
128 mkPolar r theta = r * cos theta :+ r * sin theta
130 cis :: (RealFloat a) => a -> Complex a
131 cis theta = cos theta :+ sin theta
133 polar :: (RealFloat a) => Complex a -> (a,a)
134 polar z = (magnitude z, phase z)
136 magnitude :: (RealFloat a) => Complex a -> a
137 magnitude (x:+y) = scaleFloat k
138 (sqrt ((scaleFloat mk x)^2 + (scaleFloat mk y)^2))
139 where k = max (exponent x) (exponent y)
140 mk = - k
142 phase :: (RealFloat a) => Complex a -> a
143 phase (0 :+ 0) = 0
144 phase (x :+ y) = atan2 y x
147 instance (RealFloat a) => Num (Complex a) where
148 (x:+y) + (x':+y') = (x+x') :+ (y+y')
149 (x:+y) - (x':+y') = (x-x') :+ (y-y')
150 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
151 negate (x:+y) = negate x :+ negate y
152 abs z = magnitude z :+ 0
153 signum 0 = 0
154 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
155 fromInteger n = fromInteger n :+ 0
157 instance (RealFloat a) => Fractional (Complex a) where
158 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
159 where x'' = scaleFloat k x'
160 y'' = scaleFloat k y'
161 k = - max (exponent x') (exponent y')
162 d = x'*x'' + y'*y''
164 fromRational a = fromRational a :+ 0
166 instance (RealFloat a) => Floating (Complex a) where
167 pi = pi :+ 0
168 exp (x:+y) = expx * cos y :+ expx * sin y
169 where expx = exp x
170 log z = log (magnitude z) :+ phase z
172 sqrt 0 = 0
173 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
174 where (u,v) = if x < 0 then (v',u') else (u',v')
175 v' = abs y / (u'*2)
176 u' = sqrt ((magnitude z + abs x) / 2)
178 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
179 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
180 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
181 where sinx = sin x
182 cosx = cos x
183 sinhy = sinh y
184 coshy = cosh y
186 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
187 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
188 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
189 where siny = sin y
190 cosy = cos y
191 sinhx = sinh x
192 coshx = cosh x
194 asin z@(x:+y) = y':+(-x')
195 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
196 acos z@(x:+y) = y'':+(-x'')
197 where (x'':+y'') = log (z + ((-y'):+x'))
198 (x':+y') = sqrt (1 - z*z)
199 atan z@(x:+y) = y':+(-x')
200 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
202 asinh z = log (z + sqrt (1+z*z))
203 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
204 atanh z = log ((1+z) / sqrt (1-z*z))
206 \end{verbatim}}
207 \end{quote}