1 {-# LANGUAGE CPP, DeriveDataTypeable #-}
3 -----------------------------------------------------------------------------
5 -- Module : Data.Complex
6 -- Copyright : (c) The University of Glasgow 2001
7 -- License : BSD-style (see the file libraries/base/LICENSE)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : provisional
11 -- Portability : portable
15 -----------------------------------------------------------------------------
22 , realPart -- :: (RealFloat a) => Complex a -> a
23 , imagPart -- :: (RealFloat a) => Complex a -> a
25 , mkPolar -- :: (RealFloat a) => a -> a -> Complex a
26 , cis -- :: (RealFloat a) => a -> Complex a
27 , polar -- :: (RealFloat a) => Complex a -> (a,a)
28 , magnitude -- :: (RealFloat a) => Complex a -> a
29 , phase -- :: (RealFloat a) => Complex a -> a
31 , conjugate -- :: (RealFloat a) => Complex a -> Complex a
35 -- (RealFloat a) => Eq (Complex a)
36 -- (RealFloat a) => Read (Complex a)
37 -- (RealFloat a) => Show (Complex a)
38 -- (RealFloat a) => Num (Complex a)
39 -- (RealFloat a) => Fractional (Complex a)
40 -- (RealFloat a) => Floating (Complex a)
42 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
49 #ifdef __GLASGOW_HASKELL__
50 import Data.Data (Data)
54 import Hugs.Prelude(Num(fromInt), Fractional(fromDouble))
59 -- -----------------------------------------------------------------------------
62 -- | Complex numbers are an algebraic type.
64 -- For a complex number @z@, @'abs' z@ is a number with the magnitude of @z@,
65 -- but oriented in the positive real direction, whereas @'signum' z@
66 -- has the phase of @z@, but unit magnitude.
67 data (RealFloat a) => Complex a
68 = !a :+ !a -- ^ forms a complex number from its real and imaginary
69 -- rectangular components.
70 # if __GLASGOW_HASKELL__
71 deriving (Eq, Show, Read, Data)
73 deriving (Eq, Show, Read)
76 -- -----------------------------------------------------------------------------
77 -- Functions over Complex
79 -- | Extracts the real part of a complex number.
80 realPart :: (RealFloat a) => Complex a -> a
83 -- | Extracts the imaginary part of a complex number.
84 imagPart :: (RealFloat a) => Complex a -> a
87 -- | The conjugate of a complex number.
88 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
89 conjugate :: (RealFloat a) => Complex a -> Complex a
90 conjugate (x:+y) = x :+ (-y)
92 -- | Form a complex number from polar components of magnitude and phase.
93 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
94 mkPolar :: (RealFloat a) => a -> a -> Complex a
95 mkPolar r theta = r * cos theta :+ r * sin theta
97 -- | @'cis' t@ is a complex value with magnitude @1@
98 -- and phase @t@ (modulo @2*'pi'@).
99 {-# SPECIALISE cis :: Double -> Complex Double #-}
100 cis :: (RealFloat a) => a -> Complex a
101 cis theta = cos theta :+ sin theta
103 -- | The function 'polar' takes a complex number and
104 -- returns a (magnitude, phase) pair in canonical form:
105 -- the magnitude is nonnegative, and the phase in the range @(-'pi', 'pi']@;
106 -- if the magnitude is zero, then so is the phase.
107 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
108 polar :: (RealFloat a) => Complex a -> (a,a)
109 polar z = (magnitude z, phase z)
111 -- | The nonnegative magnitude of a complex number.
112 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
113 magnitude :: (RealFloat a) => Complex a -> a
114 magnitude (x:+y) = scaleFloat k
115 (sqrt (sqr (scaleFloat mk x) + sqr (scaleFloat mk y)))
116 where k = max (exponent x) (exponent y)
120 -- | The phase of a complex number, in the range @(-'pi', 'pi']@.
121 -- If the magnitude is zero, then so is the phase.
122 {-# SPECIALISE phase :: Complex Double -> Double #-}
123 phase :: (RealFloat a) => Complex a -> a
124 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
125 phase (x:+y) = atan2 y x
128 -- -----------------------------------------------------------------------------
129 -- Instances of Complex
131 #include "Typeable.h"
132 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
134 instance (RealFloat a) => Num (Complex a) where
135 {-# SPECIALISE instance Num (Complex Float) #-}
136 {-# SPECIALISE instance Num (Complex Double) #-}
137 (x:+y) + (x':+y') = (x+x') :+ (y+y')
138 (x:+y) - (x':+y') = (x-x') :+ (y-y')
139 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
140 negate (x:+y) = negate x :+ negate y
141 abs z = magnitude z :+ 0
143 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
144 fromInteger n = fromInteger n :+ 0
146 fromInt n = fromInt n :+ 0
149 instance (RealFloat a) => Fractional (Complex a) where
150 {-# SPECIALISE instance Fractional (Complex Float) #-}
151 {-# SPECIALISE instance Fractional (Complex Double) #-}
152 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
153 where x'' = scaleFloat k x'
154 y'' = scaleFloat k y'
155 k = - max (exponent x') (exponent y')
158 fromRational a = fromRational a :+ 0
160 fromDouble a = fromDouble a :+ 0
163 instance (RealFloat a) => Floating (Complex a) where
164 {-# SPECIALISE instance Floating (Complex Float) #-}
165 {-# SPECIALISE instance Floating (Complex Double) #-}
167 exp (x:+y) = expx * cos y :+ expx * sin y
169 log z = log (magnitude z) :+ phase z
172 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
173 where (u,v) = if x < 0 then (v',u') else (u',v')
175 u' = sqrt ((magnitude z + abs x) / 2)
177 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
178 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
179 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
185 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
186 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
187 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
193 asin z@(x:+y) = y':+(-x')
194 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
196 where (x'':+y'') = log (z + ((-y'):+x'))
197 (x':+y') = sqrt (1 - z*z)
198 atan z@(x:+y) = y':+(-x')
199 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
201 asinh z = log (z + sqrt (1+z*z))
202 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
203 atanh z = 0.5 * log ((1.0+z) / (1.0-z))