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.
47 import Hugs.Prelude(Num(fromInt), Fractional(fromDouble))
52 -- -----------------------------------------------------------------------------
55 data (RealFloat a) => Complex a = !a :+ !a deriving (Eq, Read, Show)
58 -- -----------------------------------------------------------------------------
59 -- Functions over Complex
61 realPart, imagPart :: (RealFloat a) => Complex a -> a
65 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
66 conjugate :: (RealFloat a) => Complex a -> Complex a
67 conjugate (x:+y) = x :+ (-y)
69 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
70 mkPolar :: (RealFloat a) => a -> a -> Complex a
71 mkPolar r theta = r * cos theta :+ r * sin theta
73 {-# SPECIALISE cis :: Double -> Complex Double #-}
74 cis :: (RealFloat a) => a -> Complex a
75 cis theta = cos theta :+ sin theta
77 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
78 polar :: (RealFloat a) => Complex a -> (a,a)
79 polar z = (magnitude z, phase z)
81 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
82 magnitude :: (RealFloat a) => Complex a -> a
83 magnitude (x:+y) = scaleFloat k
84 (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
85 where k = max (exponent x) (exponent y)
88 {-# SPECIALISE phase :: Complex Double -> Double #-}
89 phase :: (RealFloat a) => Complex a -> a
90 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
91 phase (x:+y) = atan2 y x
94 -- -----------------------------------------------------------------------------
95 -- Instances of Complex
99 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
102 instance (RealFloat a) => Num (Complex a) where
103 {-# SPECIALISE instance Num (Complex Float) #-}
104 {-# SPECIALISE instance Num (Complex Double) #-}
105 (x:+y) + (x':+y') = (x+x') :+ (y+y')
106 (x:+y) - (x':+y') = (x-x') :+ (y-y')
107 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
108 negate (x:+y) = negate x :+ negate y
109 abs z = magnitude z :+ 0
111 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
112 fromInteger n = fromInteger n :+ 0
114 fromInt n = fromInt n :+ 0
117 instance (RealFloat a) => Fractional (Complex a) where
118 {-# SPECIALISE instance Fractional (Complex Float) #-}
119 {-# SPECIALISE instance Fractional (Complex Double) #-}
120 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
121 where x'' = scaleFloat k x'
122 y'' = scaleFloat k y'
123 k = - max (exponent x') (exponent y')
126 fromRational a = fromRational a :+ 0
128 fromDouble a = fromDouble a :+ 0
131 instance (RealFloat a) => Floating (Complex a) where
132 {-# SPECIALISE instance Floating (Complex Float) #-}
133 {-# SPECIALISE instance Floating (Complex Double) #-}
135 exp (x:+y) = expx * cos y :+ expx * sin y
137 log z = log (magnitude z) :+ phase z
140 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
141 where (u,v) = if x < 0 then (v',u') else (u',v')
143 u' = sqrt ((magnitude z + abs x) / 2)
145 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
146 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
147 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
153 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
154 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
155 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
161 asin z@(x:+y) = y':+(-x')
162 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
164 where (x'':+y'') = log (z + ((-y'):+x'))
165 (x':+y') = sqrt (1 - z*z)
166 atan z@(x:+y) = y':+(-x')
167 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
169 asinh z = log (z + sqrt (1+z*z))
170 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
171 atanh z = log ((1+z) / sqrt (1-z*z))