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 import Data.Generics.Basics( Data )
50 import Hugs.Prelude(Num(fromInt), Fractional(fromDouble))
55 -- -----------------------------------------------------------------------------
58 -- | Complex numbers are an algebraic type.
60 -- For a complex number @z@, @'abs' z@ is a number with the magnitude of @z@,
61 -- but oriented in the positive real direction, whereas @'signum' z@
62 -- has the phase of @z@, but unit magnitude.
63 data (RealFloat a) => Complex a
64 = !a :+ !a -- ^ forms a complex number from its real and imaginary
65 -- rectangular components.
66 # if __GLASGOW_HASKELL__
67 deriving (Eq, Show, Read, Data)
69 deriving (Eq, Show, Read)
72 -- -----------------------------------------------------------------------------
73 -- Functions over Complex
75 -- | Extracts the real part of a complex number.
76 realPart :: (RealFloat a) => Complex a -> a
79 -- | Extracts the imaginary part of a complex number.
80 imagPart :: (RealFloat a) => Complex a -> a
83 -- | The conjugate of a complex number.
84 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
85 conjugate :: (RealFloat a) => Complex a -> Complex a
86 conjugate (x:+y) = x :+ (-y)
88 -- | Form a complex number from polar components of magnitude and phase.
89 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
90 mkPolar :: (RealFloat a) => a -> a -> Complex a
91 mkPolar r theta = r * cos theta :+ r * sin theta
93 -- | @'cis' t@ is a complex value with magnitude @1@
94 -- and phase @t@ (modulo @2*'pi'@).
95 {-# SPECIALISE cis :: Double -> Complex Double #-}
96 cis :: (RealFloat a) => a -> Complex a
97 cis theta = cos theta :+ sin theta
99 -- | The function 'polar' takes a complex number and
100 -- returns a (magnitude, phase) pair in canonical form:
101 -- the magnitude is nonnegative, and the phase in the range @(-'pi', 'pi']@;
102 -- if the magnitude is zero, then so is the phase.
103 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
104 polar :: (RealFloat a) => Complex a -> (a,a)
105 polar z = (magnitude z, phase z)
107 -- | The nonnegative magnitude of a complex number.
108 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
109 magnitude :: (RealFloat a) => Complex a -> a
110 magnitude (x:+y) = scaleFloat k
111 (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
112 where k = max (exponent x) (exponent y)
115 -- | The phase of a complex number, in the range @(-'pi', 'pi']@.
116 -- If the magnitude is zero, then so is the phase.
117 {-# SPECIALISE phase :: Complex Double -> Double #-}
118 phase :: (RealFloat a) => Complex a -> a
119 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
120 phase (x:+y) = atan2 y x
123 -- -----------------------------------------------------------------------------
124 -- Instances of Complex
126 #include "Typeable.h"
127 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
129 instance (RealFloat a) => Num (Complex a) where
130 {-# SPECIALISE instance Num (Complex Float) #-}
131 {-# SPECIALISE instance Num (Complex Double) #-}
132 (x:+y) + (x':+y') = (x+x') :+ (y+y')
133 (x:+y) - (x':+y') = (x-x') :+ (y-y')
134 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
135 negate (x:+y) = negate x :+ negate y
136 abs z = magnitude z :+ 0
138 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
139 fromInteger n = fromInteger n :+ 0
141 fromInt n = fromInt n :+ 0
144 instance (RealFloat a) => Fractional (Complex a) where
145 {-# SPECIALISE instance Fractional (Complex Float) #-}
146 {-# SPECIALISE instance Fractional (Complex Double) #-}
147 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
148 where x'' = scaleFloat k x'
149 y'' = scaleFloat k y'
150 k = - max (exponent x') (exponent y')
153 fromRational a = fromRational a :+ 0
155 fromDouble a = fromDouble a :+ 0
158 instance (RealFloat a) => Floating (Complex a) where
159 {-# SPECIALISE instance Floating (Complex Float) #-}
160 {-# SPECIALISE instance Floating (Complex Double) #-}
162 exp (x:+y) = expx * cos y :+ expx * sin y
164 log z = log (magnitude z) :+ phase z
167 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
168 where (u,v) = if x < 0 then (v',u') else (u',v')
170 u' = sqrt ((magnitude z + abs x) / 2)
172 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
173 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
174 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
180 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
181 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
182 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
188 asin z@(x:+y) = y':+(-x')
189 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
191 where (x'':+y'') = log (z + ((-y'):+x'))
192 (x':+y') = sqrt (1 - z*z)
193 atan z@(x:+y) = y':+(-x')
194 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
196 asinh z = log (z + sqrt (1+z*z))
197 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
198 atanh z = log ((1+z) / sqrt (1-z*z))