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