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.
49 import Hugs.Prelude(Num(fromInt), Fractional(fromDouble))
54 -- -----------------------------------------------------------------------------
57 -- | Complex numbers are an algebraic type.
59 -- For a complex number @z@, @'abs' z@ is a number with the magnitude of @z@,
60 -- but oriented in the positive real direction, whereas @'signum' z@
61 -- has the phase of @z@, but unit magnitude.
62 data (RealFloat a) => Complex a
63 = !a :+ !a -- ^ forms a complex number from its real and imaginary
64 -- rectangular components.
65 deriving (Eq, Show, Read)
67 -- -----------------------------------------------------------------------------
68 -- Functions over Complex
70 -- | Extracts the real part of a complex number.
71 realPart :: (RealFloat a) => Complex a -> a
74 -- | Extracts the imaginary part of a complex number.
75 imagPart :: (RealFloat a) => Complex a -> a
78 -- | The conjugate of a complex number.
79 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
80 conjugate :: (RealFloat a) => Complex a -> Complex a
81 conjugate (x:+y) = x :+ (-y)
83 -- | Form a complex number from polar components of magnitude and phase.
84 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
85 mkPolar :: (RealFloat a) => a -> a -> Complex a
86 mkPolar r theta = r * cos theta :+ r * sin theta
88 -- | @'cis' t@ is a complex value with magnitude @1@
89 -- and phase @t@ (modulo @2*'pi'@).
90 {-# SPECIALISE cis :: Double -> Complex Double #-}
91 cis :: (RealFloat a) => a -> Complex a
92 cis theta = cos theta :+ sin theta
94 -- | The function 'polar' takes a complex number and
95 -- returns a (magnitude, phase) pair in canonical form:
96 -- the magnitude is nonnegative, and the phase in the range @(-'pi', 'pi']@;
97 -- if the magnitude is zero, then so is the phase.
98 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
99 polar :: (RealFloat a) => Complex a -> (a,a)
100 polar z = (magnitude z, phase z)
102 -- | The nonnegative magnitude of a complex number.
103 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
104 magnitude :: (RealFloat a) => Complex a -> a
105 magnitude (x:+y) = scaleFloat k
106 (sqrt (sqr (scaleFloat mk x) + sqr (scaleFloat mk y)))
107 where k = max (exponent x) (exponent y)
111 -- | The phase of a complex number, in the range @(-'pi', 'pi']@.
112 -- If the magnitude is zero, then so is the phase.
113 {-# SPECIALISE phase :: Complex Double -> Double #-}
114 phase :: (RealFloat a) => Complex a -> a
115 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
116 phase (x:+y) = atan2 y x
119 -- -----------------------------------------------------------------------------
120 -- Instances of Complex
122 #include "Typeable.h"
123 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
125 instance (RealFloat a) => Num (Complex a) where
126 {-# SPECIALISE instance Num (Complex Float) #-}
127 {-# SPECIALISE instance Num (Complex Double) #-}
128 (x:+y) + (x':+y') = (x+x') :+ (y+y')
129 (x:+y) - (x':+y') = (x-x') :+ (y-y')
130 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
131 negate (x:+y) = negate x :+ negate y
132 abs z = magnitude z :+ 0
134 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
135 fromInteger n = fromInteger n :+ 0
137 fromInt n = fromInt n :+ 0
140 instance (RealFloat a) => Fractional (Complex a) where
141 {-# SPECIALISE instance Fractional (Complex Float) #-}
142 {-# SPECIALISE instance Fractional (Complex Double) #-}
143 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
144 where x'' = scaleFloat k x'
145 y'' = scaleFloat k y'
146 k = - max (exponent x') (exponent y')
149 fromRational a = fromRational a :+ 0
151 fromDouble a = fromDouble a :+ 0
154 instance (RealFloat a) => Floating (Complex a) where
155 {-# SPECIALISE instance Floating (Complex Float) #-}
156 {-# SPECIALISE instance Floating (Complex Double) #-}
158 exp (x:+y) = expx * cos y :+ expx * sin y
160 log z = log (magnitude z) :+ phase z
163 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
164 where (u,v) = if x < 0 then (v',u') else (u',v')
166 u' = sqrt ((magnitude z + abs x) / 2)
168 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
169 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
170 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
176 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
177 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
178 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
184 asin z@(x:+y) = y':+(-x')
185 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
187 where (x'':+y'') = log (z + ((-y'):+x'))
188 (x':+y') = sqrt (1 - z*z)
189 atan z@(x:+y) = y':+(-x')
190 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
192 asinh z = log (z + sqrt (1+z*z))
193 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
194 atanh z = log ((1+z) / sqrt (1-z*z))