remove instances for types/classes that are not part of Haskell
[haskell-report.git] / report / libs / Data-Complex.tex
1 \haddockmoduleheading{Data.Complex}
2 \label{module:Data.Complex}
3 \haddockbeginheader
4 {\haddockverb\begin{verbatim}
5 module Data.Complex (
6 Complex(:+), realPart, imagPart, mkPolar, cis, polar, magnitude,
7 phase, conjugate
8 ) where\end{verbatim}}
9 \haddockendheader
10
11 \section{Rectangular form
12 }
13 \begin{haddockdesc}
14 \item[\begin{tabular}{@{}l}
15 data\ RealFloat\ a\ =>\ Complex\ a
16 \end{tabular}]\haddockbegindoc
17 \haddockbeginconstrs
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
24 For a complex number \haddocktt{z}, \haddocktt{abs\ z} is a number with the magnitude of \haddocktt{z},
25 but oriented in the positive real direction, whereas \haddocktt{signum\ z}
26 has the phase of \haddocktt{z}, but unit magnitude.
27 \par
28
29 \end{haddockdesc}
30 \begin{haddockdesc}
31 \item[\begin{tabular}{@{}l}
32 instance\ RealFloat\ a\ =>\ Eq\ (Complex\ a)\\instance\ RealFloat\ a\ =>\ Floating\ (Complex\ a)\\instance\ RealFloat\ a\ =>\ Fractional\ (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}]
34 \end{haddockdesc}
35 \begin{haddockdesc}
36 \item[\begin{tabular}{@{}l}
37 realPart\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ a
38 \end{tabular}]\haddockbegindoc
39 Extracts the real part of a complex number.
40 \par
41
42 \end{haddockdesc}
43 \begin{haddockdesc}
44 \item[\begin{tabular}{@{}l}
45 imagPart\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ a
46 \end{tabular}]\haddockbegindoc
47 Extracts the imaginary part of a complex number.
48 \par
49
50 \end{haddockdesc}
51 \section{Polar form
52 }
53 \begin{haddockdesc}
54 \item[\begin{tabular}{@{}l}
55 mkPolar\ ::\ RealFloat\ a\ =>\ a\ ->\ a\ ->\ Complex\ a
56 \end{tabular}]\haddockbegindoc
57 Form a complex number from polar components of magnitude and phase.
58 \par
59
60 \end{haddockdesc}
61 \begin{haddockdesc}
62 \item[\begin{tabular}{@{}l}
63 cis\ ::\ RealFloat\ a\ =>\ a\ ->\ Complex\ a
64 \end{tabular}]\haddockbegindoc
65 \haddocktt{cis\ t} is a complex value with magnitude \haddocktt{1}
66 and phase \haddocktt{t} (modulo \haddocktt{2*pi}).
67 \par
68
69 \end{haddockdesc}
70 \begin{haddockdesc}
71 \item[\begin{tabular}{@{}l}
72 polar\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ (a,\ a)
73 \end{tabular}]\haddockbegindoc
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
79
80 \end{haddockdesc}
81 \begin{haddockdesc}
82 \item[\begin{tabular}{@{}l}
83 magnitude\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ a
84 \end{tabular}]\haddockbegindoc
85 The nonnegative magnitude of a complex number.
86 \par
87
88 \end{haddockdesc}
89 \begin{haddockdesc}
90 \item[\begin{tabular}{@{}l}
91 phase\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ a
92 \end{tabular}]\haddockbegindoc
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
96
97 \end{haddockdesc}
98 \section{Conjugate
99 }
100 \begin{haddockdesc}
101 \item[\begin{tabular}{@{}l}
102 conjugate\ ::\ RealFloat\ a\ =>\ Complex\ a\ ->\ Complex\ a
103 \end{tabular}]\haddockbegindoc
104 The conjugate of a complex number.
105 \par
106
107 \end{haddockdesc}
108 \section{Specification
109 }
110 \begin{quote}
111 {\haddockverb\begin{verbatim}
112 module Complex(Complex((:+)), realPart, imagPart, conjugate, mkPolar,
113 cis, polar, magnitude, phase) where
114
115 infix 6 :+
116
117 data (RealFloat a) => Complex a = !a :+ !a deriving (Eq,Read,Show)
118
119
120 realPart, imagPart :: (RealFloat a) => Complex a -> a
121 realPart (x:+y) = x
122 imagPart (x:+y) = y
123
124 conjugate :: (RealFloat a) => Complex a -> Complex a
125 conjugate (x:+y) = x :+ (-y)
126
127 mkPolar :: (RealFloat a) => a -> a -> Complex a
128 mkPolar r theta = r * cos theta :+ r * sin theta
129
130 cis :: (RealFloat a) => a -> Complex a
131 cis theta = cos theta :+ sin theta
132
133 polar :: (RealFloat a) => Complex a -> (a,a)
134 polar z = (magnitude z, phase z)
135
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
141
142 phase :: (RealFloat a) => Complex a -> a
143 phase (0 :+ 0) = 0
144 phase (x :+ y) = atan2 y x
145
146
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
156
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''
163
164 fromRational a = fromRational a :+ 0
165
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
171
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)
177
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
185
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
193
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))
201
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))
205
206 \end{verbatim}}
207 \end{quote}