Remove commented types in module export lists
[packages/base.git] / Data / Complex.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP, DeriveDataTypeable #-}
3 #ifdef __GLASGOW_HASKELL__
4 {-# LANGUAGE StandaloneDeriving #-}
5 #endif
6
7 -----------------------------------------------------------------------------
8 -- |
9 -- Module : Data.Complex
10 -- Copyright : (c) The University of Glasgow 2001
11 -- License : BSD-style (see the file libraries/base/LICENSE)
12 --
13 -- Maintainer : libraries@haskell.org
14 -- Stability : provisional
15 -- Portability : portable
16 --
17 -- Complex numbers.
18 --
19 -----------------------------------------------------------------------------
20
21 module Data.Complex
22 (
23 -- * Rectangular form
24 Complex((:+))
25
26 , realPart
27 , imagPart
28 -- * Polar form
29 , mkPolar
30 , cis
31 , polar
32 , magnitude
33 , phase
34 -- * Conjugate
35 , conjugate
36
37 ) where
38
39 import Prelude
40
41 import Data.Typeable
42 #ifdef __GLASGOW_HASKELL__
43 import Data.Data (Data)
44 #endif
45
46 #ifdef __HUGS__
47 import Hugs.Prelude(Num(fromInt), Fractional(fromDouble))
48 #endif
49
50 infix 6 :+
51
52 -- -----------------------------------------------------------------------------
53 -- The Complex type
54
55 -- | Complex numbers are an algebraic type.
56 --
57 -- For a complex number @z@, @'abs' z@ is a number with the magnitude of @z@,
58 -- but oriented in the positive real direction, whereas @'signum' z@
59 -- has the phase of @z@, but unit magnitude.
60 data Complex a
61 = !a :+ !a -- ^ forms a complex number from its real and imaginary
62 -- rectangular components.
63 # if __GLASGOW_HASKELL__
64 deriving (Eq, Show, Read, Data)
65 # else
66 deriving (Eq, Show, Read)
67 # endif
68
69 -- -----------------------------------------------------------------------------
70 -- Functions over Complex
71
72 -- | Extracts the real part of a complex number.
73 realPart :: (RealFloat a) => Complex a -> a
74 realPart (x :+ _) = x
75
76 -- | Extracts the imaginary part of a complex number.
77 imagPart :: (RealFloat a) => Complex a -> a
78 imagPart (_ :+ y) = y
79
80 -- | The conjugate of a complex number.
81 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
82 conjugate :: (RealFloat a) => Complex a -> Complex a
83 conjugate (x:+y) = x :+ (-y)
84
85 -- | Form a complex number from polar components of magnitude and phase.
86 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
87 mkPolar :: (RealFloat a) => a -> a -> Complex a
88 mkPolar r theta = r * cos theta :+ r * sin theta
89
90 -- | @'cis' t@ is a complex value with magnitude @1@
91 -- and phase @t@ (modulo @2*'pi'@).
92 {-# SPECIALISE cis :: Double -> Complex Double #-}
93 cis :: (RealFloat a) => a -> Complex a
94 cis theta = cos theta :+ sin theta
95
96 -- | The function 'polar' takes a complex number and
97 -- returns a (magnitude, phase) pair in canonical form:
98 -- the magnitude is nonnegative, and the phase in the range @(-'pi', 'pi']@;
99 -- if the magnitude is zero, then so is the phase.
100 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
101 polar :: (RealFloat a) => Complex a -> (a,a)
102 polar z = (magnitude z, phase z)
103
104 -- | The nonnegative magnitude of a complex number.
105 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
106 magnitude :: (RealFloat a) => Complex a -> a
107 magnitude (x:+y) = scaleFloat k
108 (sqrt (sqr (scaleFloat mk x) + sqr (scaleFloat mk y)))
109 where k = max (exponent x) (exponent y)
110 mk = - k
111 sqr z = z * z
112
113 -- | The phase of a complex number, in the range @(-'pi', 'pi']@.
114 -- If the magnitude is zero, then so is the phase.
115 {-# SPECIALISE phase :: Complex Double -> Double #-}
116 phase :: (RealFloat a) => Complex a -> a
117 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
118 phase (x:+y) = atan2 y x
119
120
121 -- -----------------------------------------------------------------------------
122 -- Instances of Complex
123
124 #include "Typeable.h"
125 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
126
127 instance (RealFloat a) => Num (Complex a) where
128 {-# SPECIALISE instance Num (Complex Float) #-}
129 {-# SPECIALISE instance Num (Complex Double) #-}
130 (x:+y) + (x':+y') = (x+x') :+ (y+y')
131 (x:+y) - (x':+y') = (x-x') :+ (y-y')
132 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
133 negate (x:+y) = negate x :+ negate y
134 abs z = magnitude z :+ 0
135 signum (0:+0) = 0
136 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
137 fromInteger n = fromInteger n :+ 0
138 #ifdef __HUGS__
139 fromInt n = fromInt n :+ 0
140 #endif
141
142 instance (RealFloat a) => Fractional (Complex a) where
143 {-# SPECIALISE instance Fractional (Complex Float) #-}
144 {-# SPECIALISE instance Fractional (Complex Double) #-}
145 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
146 where x'' = scaleFloat k x'
147 y'' = scaleFloat k y'
148 k = - max (exponent x') (exponent y')
149 d = x'*x'' + y'*y''
150
151 fromRational a = fromRational a :+ 0
152 #ifdef __HUGS__
153 fromDouble a = fromDouble a :+ 0
154 #endif
155
156 instance (RealFloat a) => Floating (Complex a) where
157 {-# SPECIALISE instance Floating (Complex Float) #-}
158 {-# SPECIALISE instance Floating (Complex Double) #-}
159 pi = pi :+ 0
160 exp (x:+y) = expx * cos y :+ expx * sin y
161 where expx = exp x
162 log z = log (magnitude z) :+ phase z
163
164 sqrt (0:+0) = 0
165 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
166 where (u,v) = if x < 0 then (v',u') else (u',v')
167 v' = abs y / (u'*2)
168 u' = sqrt ((magnitude z + abs x) / 2)
169
170 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
171 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
172 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
173 where sinx = sin x
174 cosx = cos x
175 sinhy = sinh y
176 coshy = cosh y
177
178 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
179 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
180 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
181 where siny = sin y
182 cosy = cos y
183 sinhx = sinh x
184 coshx = cosh x
185
186 asin z@(x:+y) = y':+(-x')
187 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
188 acos z = y'':+(-x'')
189 where (x'':+y'') = log (z + ((-y'):+x'))
190 (x':+y') = sqrt (1 - z*z)
191 atan z@(x:+y) = y':+(-x')
192 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
193
194 asinh z = log (z + sqrt (1+z*z))
195 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
196 atanh z = 0.5 * log ((1.0+z) / (1.0-z))
197