1 -----------------------------------------------------------------------------
3 -- Module : Data.Complex
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
11 -- $Id: Complex.hs,v 1.1 2001/06/28 14:15:02 simonmar Exp $
15 -----------------------------------------------------------------------------
20 , realPart -- :: (RealFloat a) => Complex a -> a
21 , imagPart -- :: (RealFloat a) => Complex a -> a
22 , conjugate -- :: (RealFloat a) => Complex a -> Complex a
23 , mkPolar -- :: (RealFloat a) => a -> a -> Complex a
24 , cis -- :: (RealFloat a) => a -> Complex a
25 , polar -- :: (RealFloat a) => Complex a -> (a,a)
26 , magnitude -- :: (RealFloat a) => Complex a -> a
27 , phase -- :: (RealFloat a) => Complex a -> a
31 -- (RealFloat a) => Eq (Complex a)
32 -- (RealFloat a) => Read (Complex a)
33 -- (RealFloat a) => Show (Complex a)
34 -- (RealFloat a) => Num (Complex a)
35 -- (RealFloat a) => Fractional (Complex a)
36 -- (RealFloat a) => Floating (Complex a)
38 -- 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 conjugate :: (RealFloat a) => Complex a -> Complex a
62 conjugate (x:+y) = x :+ (-y)
64 mkPolar :: (RealFloat a) => a -> a -> Complex a
65 mkPolar r theta = r * cos theta :+ r * sin theta
67 cis :: (RealFloat a) => a -> Complex a
68 cis theta = cos theta :+ sin theta
70 polar :: (RealFloat a) => Complex a -> (a,a)
71 polar z = (magnitude z, phase z)
73 magnitude :: (RealFloat a) => Complex a -> a
74 magnitude (x:+y) = scaleFloat k
75 (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
76 where k = max (exponent x) (exponent y)
79 phase :: (RealFloat a) => Complex a -> a
80 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
81 phase (x:+y) = atan2 y x
84 -- -----------------------------------------------------------------------------
85 -- Instances of Complex
88 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
90 instance (RealFloat a) => Num (Complex a) where
91 {-# SPECIALISE instance Num (Complex Float) #-}
92 {-# SPECIALISE instance Num (Complex Double) #-}
93 (x:+y) + (x':+y') = (x+x') :+ (y+y')
94 (x:+y) - (x':+y') = (x-x') :+ (y-y')
95 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
96 negate (x:+y) = negate x :+ negate y
97 abs z = magnitude z :+ 0
99 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
100 fromInteger n = fromInteger n :+ 0
102 instance (RealFloat a) => Fractional (Complex a) where
103 {-# SPECIALISE instance Fractional (Complex Float) #-}
104 {-# SPECIALISE instance Fractional (Complex Double) #-}
105 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
106 where x'' = scaleFloat k x'
107 y'' = scaleFloat k y'
108 k = - max (exponent x') (exponent y')
111 fromRational a = fromRational a :+ 0
113 instance (RealFloat a) => Floating (Complex a) where
114 {-# SPECIALISE instance Floating (Complex Float) #-}
115 {-# SPECIALISE instance Floating (Complex Double) #-}
117 exp (x:+y) = expx * cos y :+ expx * sin y
119 log z = log (magnitude z) :+ phase z
122 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
123 where (u,v) = if x < 0 then (v',u') else (u',v')
125 u' = sqrt ((magnitude z + abs x) / 2)
127 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
128 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
129 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
135 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
136 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
137 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
143 asin z@(x:+y) = y':+(-x')
144 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
146 where (x'':+y'') = log (z + ((-y'):+x'))
147 (x':+y') = sqrt (1 - z*z)
148 atan z@(x:+y) = y':+(-x')
149 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
151 asinh z = log (z + sqrt (1+z*z))
152 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
153 atanh z = log ((1+z) / sqrt (1-z*z))