1 -----------------------------------------------------------------------------
3 -- Module : Data.Complex
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
13 -----------------------------------------------------------------------------
18 , realPart -- :: (RealFloat a) => Complex a -> a
19 , imagPart -- :: (RealFloat a) => Complex a -> a
20 , conjugate -- :: (RealFloat a) => Complex a -> Complex a
21 , mkPolar -- :: (RealFloat a) => a -> a -> Complex a
22 , cis -- :: (RealFloat a) => a -> Complex a
23 , polar -- :: (RealFloat a) => Complex a -> (a,a)
24 , magnitude -- :: (RealFloat a) => Complex a -> a
25 , phase -- :: (RealFloat a) => Complex a -> a
29 -- (RealFloat a) => Eq (Complex a)
30 -- (RealFloat a) => Read (Complex a)
31 -- (RealFloat a) => Show (Complex a)
32 -- (RealFloat a) => Num (Complex a)
33 -- (RealFloat a) => Fractional (Complex a)
34 -- (RealFloat a) => Floating (Complex a)
36 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
48 -- -----------------------------------------------------------------------------
51 data (RealFloat a) => Complex a = !a :+ !a deriving (Eq, Read, Show)
54 -- -----------------------------------------------------------------------------
55 -- Functions over Complex
57 realPart, imagPart :: (RealFloat a) => Complex a -> a
61 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
62 conjugate :: (RealFloat a) => Complex a -> Complex a
63 conjugate (x:+y) = x :+ (-y)
65 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
66 mkPolar :: (RealFloat a) => a -> a -> Complex a
67 mkPolar r theta = r * cos theta :+ r * sin theta
69 {-# SPECIALISE cis :: Double -> Complex Double #-}
70 cis :: (RealFloat a) => a -> Complex a
71 cis theta = cos theta :+ sin theta
73 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
74 polar :: (RealFloat a) => Complex a -> (a,a)
75 polar z = (magnitude z, phase z)
77 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
78 magnitude :: (RealFloat a) => Complex a -> a
79 magnitude (x:+y) = scaleFloat k
80 (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
81 where k = max (exponent x) (exponent y)
84 {-# SPECIALISE phase :: Complex Double -> Double #-}
85 phase :: (RealFloat a) => Complex a -> a
86 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
87 phase (x:+y) = atan2 y x
90 -- -----------------------------------------------------------------------------
91 -- Instances of Complex
95 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
98 instance (RealFloat a) => Num (Complex a) where
99 {-# SPECIALISE instance Num (Complex Float) #-}
100 {-# SPECIALISE instance Num (Complex Double) #-}
101 (x:+y) + (x':+y') = (x+x') :+ (y+y')
102 (x:+y) - (x':+y') = (x-x') :+ (y-y')
103 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
104 negate (x:+y) = negate x :+ negate y
105 abs z = magnitude z :+ 0
107 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
108 fromInteger n = fromInteger n :+ 0
110 instance (RealFloat a) => Fractional (Complex a) where
111 {-# SPECIALISE instance Fractional (Complex Float) #-}
112 {-# SPECIALISE instance Fractional (Complex Double) #-}
113 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
114 where x'' = scaleFloat k x'
115 y'' = scaleFloat k y'
116 k = - max (exponent x') (exponent y')
119 fromRational a = fromRational a :+ 0
121 instance (RealFloat a) => Floating (Complex a) where
122 {-# SPECIALISE instance Floating (Complex Float) #-}
123 {-# SPECIALISE instance Floating (Complex Double) #-}
125 exp (x:+y) = expx * cos y :+ expx * sin y
127 log z = log (magnitude z) :+ phase z
130 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
131 where (u,v) = if x < 0 then (v',u') else (u',v')
133 u' = sqrt ((magnitude z + abs x) / 2)
135 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
136 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
137 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
143 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
144 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
145 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
151 asin z@(x:+y) = y':+(-x')
152 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
154 where (x'':+y'') = log (z + ((-y'):+x'))
155 (x':+y') = sqrt (1 - z*z)
156 atan z@(x:+y) = y':+(-x')
157 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
159 asinh z = log (z + sqrt (1+z*z))
160 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
161 atanh z = log ((1+z) / sqrt (1-z*z))