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 -----------------------------------------------------------------------------
20 , realPart -- :: (RealFloat a) => Complex a -> a
21 , imagPart -- :: (RealFloat a) => Complex a -> 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
29 , conjugate -- :: (RealFloat a) => Complex a -> Complex a
33 -- (RealFloat a) => Eq (Complex a)
34 -- (RealFloat a) => Read (Complex a)
35 -- (RealFloat a) => Show (Complex a)
36 -- (RealFloat a) => Num (Complex a)
37 -- (RealFloat a) => Fractional (Complex a)
38 -- (RealFloat a) => Floating (Complex a)
40 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
47 #ifdef __GLASGOW_HASKELL__
48 import Data.Data (Data)
52 import Hugs.Prelude(Num(fromInt), Fractional(fromDouble))
57 -- -----------------------------------------------------------------------------
60 -- | Complex numbers are an algebraic type.
62 -- For a complex number @z@, @'abs' z@ is a number with the magnitude of @z@,
63 -- but oriented in the positive real direction, whereas @'signum' z@
64 -- has the phase of @z@, but unit magnitude.
65 data (RealFloat a) => Complex a
66 = !a :+ !a -- ^ forms a complex number from its real and imaginary
67 -- rectangular components.
68 deriving (Eq, Show, Read)
70 -- -----------------------------------------------------------------------------
71 -- Functions over Complex
73 -- | Extracts the real part of a complex number.
74 realPart :: (RealFloat a) => Complex a -> a
77 -- | Extracts the imaginary part of a complex number.
78 imagPart :: (RealFloat a) => Complex a -> a
81 -- | The conjugate of a complex number.
82 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
83 conjugate :: (RealFloat a) => Complex a -> Complex a
84 conjugate (x:+y) = x :+ (-y)
86 -- | Form a complex number from polar components of magnitude and phase.
87 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
88 mkPolar :: (RealFloat a) => a -> a -> Complex a
89 mkPolar r theta = r * cos theta :+ r * sin theta
91 -- | @'cis' t@ is a complex value with magnitude @1@
92 -- and phase @t@ (modulo @2*'pi'@).
93 {-# SPECIALISE cis :: Double -> Complex Double #-}
94 cis :: (RealFloat a) => a -> Complex a
95 cis theta = cos theta :+ sin theta
97 -- | The function 'polar' takes a complex number and
98 -- returns a (magnitude, phase) pair in canonical form:
99 -- the magnitude is nonnegative, and the phase in the range @(-'pi', 'pi']@;
100 -- if the magnitude is zero, then so is the phase.
101 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
102 polar :: (RealFloat a) => Complex a -> (a,a)
103 polar z = (magnitude z, phase z)
105 -- | The nonnegative magnitude of a complex number.
106 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
107 magnitude :: (RealFloat a) => Complex a -> a
108 magnitude (x:+y) = scaleFloat k
109 (sqrt (sqr (scaleFloat mk x) + sqr (scaleFloat mk y)))
110 where k = max (exponent x) (exponent y)
114 -- | The phase of a complex number, in the range @(-'pi', 'pi']@.
115 -- If the magnitude is zero, then so is the phase.
116 {-# SPECIALISE phase :: Complex Double -> Double #-}
117 phase :: (RealFloat a) => Complex a -> a
118 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
119 phase (x:+y) = atan2 y x
122 -- -----------------------------------------------------------------------------
123 -- Instances of Complex
125 #include "Typeable.h"
126 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
128 instance (RealFloat a) => Num (Complex a) where
129 {-# SPECIALISE instance Num (Complex Float) #-}
130 {-# SPECIALISE instance Num (Complex Double) #-}
131 (x:+y) + (x':+y') = (x+x') :+ (y+y')
132 (x:+y) - (x':+y') = (x-x') :+ (y-y')
133 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
134 negate (x:+y) = negate x :+ negate y
135 abs z = magnitude z :+ 0
137 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
138 fromInteger n = fromInteger n :+ 0
140 fromInt n = fromInt n :+ 0
143 instance (RealFloat a) => Fractional (Complex a) where
144 {-# SPECIALISE instance Fractional (Complex Float) #-}
145 {-# SPECIALISE instance Fractional (Complex Double) #-}
146 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
147 where x'' = scaleFloat k x'
148 y'' = scaleFloat k y'
149 k = - max (exponent x') (exponent y')
152 fromRational a = fromRational a :+ 0
154 fromDouble a = fromDouble a :+ 0
157 instance (RealFloat a) => Floating (Complex a) where
158 {-# SPECIALISE instance Floating (Complex Float) #-}
159 {-# SPECIALISE instance Floating (Complex Double) #-}
161 exp (x:+y) = expx * cos y :+ expx * sin y
163 log z = log (magnitude z) :+ phase z
166 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
167 where (u,v) = if x < 0 then (v',u') else (u',v')
169 u' = sqrt ((magnitude z + abs x) / 2)
171 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
172 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
173 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
179 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
180 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
181 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
187 asin z@(x:+y) = y':+(-x')
188 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
190 where (x'':+y'') = log (z + ((-y'):+x'))
191 (x':+y') = sqrt (1 - z*z)
192 atan z@(x:+y) = y':+(-x')
193 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
195 asinh z = log (z + sqrt (1+z*z))
196 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
197 atanh z = log ((1+z) / sqrt (1-z*z))