1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Data.Complex
4 -- Copyright : (c) The University of Glasgow 2001
6 --
8 -- Stability : provisional
9 -- Portability : portable
10 --
11 -- Complex numbers.
12 --
13 -----------------------------------------------------------------------------
15 module Data.Complex
16 ( Complex((:+))
18 , realPart -- :: (RealFloat a) => Complex a -> a
19 , imagPart -- :: (RealFloat a) => Complex a -> a
20 , conjugate -- :: (RealFloat a) => Complex a -> Complex a
21 , mkPolar -- :: (RealFloat a) => a -> a -> Complex a
22 , cis -- :: (RealFloat a) => a -> Complex a
23 , polar -- :: (RealFloat a) => Complex a -> (a,a)
24 , magnitude -- :: (RealFloat a) => Complex a -> a
25 , phase -- :: (RealFloat a) => Complex a -> a
27 -- Complex instances:
28 --
29 -- (RealFloat a) => Eq (Complex a)
30 -- (RealFloat a) => Read (Complex a)
31 -- (RealFloat a) => Show (Complex a)
32 -- (RealFloat a) => Num (Complex a)
33 -- (RealFloat a) => Fractional (Complex a)
34 -- (RealFloat a) => Floating (Complex a)
35 --
36 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
38 ) where
40 import Prelude
42 import Data.Dynamic
44 infix 6 :+
46 -- -----------------------------------------------------------------------------
47 -- The Complex type
49 data (RealFloat a) => Complex a = !a :+ !a deriving (Eq, Read, Show)
52 -- -----------------------------------------------------------------------------
53 -- Functions over Complex
55 realPart, imagPart :: (RealFloat a) => Complex a -> a
56 realPart (x :+ _) = x
57 imagPart (_ :+ y) = y
59 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
60 conjugate :: (RealFloat a) => Complex a -> Complex a
61 conjugate (x:+y) = x :+ (-y)
63 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
64 mkPolar :: (RealFloat a) => a -> a -> Complex a
65 mkPolar r theta = r * cos theta :+ r * sin theta
67 {-# SPECIALISE cis :: Double -> Complex Double #-}
68 cis :: (RealFloat a) => a -> Complex a
69 cis theta = cos theta :+ sin theta
71 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
72 polar :: (RealFloat a) => Complex a -> (a,a)
73 polar z = (magnitude z, phase z)
75 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
76 magnitude :: (RealFloat a) => Complex a -> a
77 magnitude (x:+y) = scaleFloat k
78 (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
79 where k = max (exponent x) (exponent y)
80 mk = - k
82 {-# SPECIALISE phase :: Complex Double -> Double #-}
83 phase :: (RealFloat a) => Complex a -> a
84 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
85 phase (x:+y) = atan2 y x
88 -- -----------------------------------------------------------------------------
89 -- Instances of Complex
91 #include "Dynamic.h"
92 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
94 instance (RealFloat a) => Num (Complex a) where
95 {-# SPECIALISE instance Num (Complex Float) #-}
96 {-# SPECIALISE instance Num (Complex Double) #-}
97 (x:+y) + (x':+y') = (x+x') :+ (y+y')
98 (x:+y) - (x':+y') = (x-x') :+ (y-y')
99 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
100 negate (x:+y) = negate x :+ negate y
101 abs z = magnitude z :+ 0
102 signum 0 = 0
103 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
104 fromInteger n = fromInteger n :+ 0
106 instance (RealFloat a) => Fractional (Complex a) where
107 {-# SPECIALISE instance Fractional (Complex Float) #-}
108 {-# SPECIALISE instance Fractional (Complex Double) #-}
109 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
110 where x'' = scaleFloat k x'
111 y'' = scaleFloat k y'
112 k = - max (exponent x') (exponent y')
113 d = x'*x'' + y'*y''
115 fromRational a = fromRational a :+ 0
117 instance (RealFloat a) => Floating (Complex a) where
118 {-# SPECIALISE instance Floating (Complex Float) #-}
119 {-# SPECIALISE instance Floating (Complex Double) #-}
120 pi = pi :+ 0
121 exp (x:+y) = expx * cos y :+ expx * sin y
122 where expx = exp x
123 log z = log (magnitude z) :+ phase z
125 sqrt 0 = 0
126 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
127 where (u,v) = if x < 0 then (v',u') else (u',v')
128 v' = abs y / (u'*2)
129 u' = sqrt ((magnitude z + abs x) / 2)
131 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
132 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
133 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
134 where sinx = sin x
135 cosx = cos x
136 sinhy = sinh y
137 coshy = cosh y
139 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
140 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
141 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
142 where siny = sin y
143 cosy = cos y
144 sinhx = sinh x
145 coshx = cosh x
147 asin z@(x:+y) = y':+(-x')
148 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
149 acos z = y'':+(-x'')
150 where (x'':+y'') = log (z + ((-y'):+x'))
151 (x':+y') = sqrt (1 - z*z)
152 atan z@(x:+y) = y':+(-x')
153 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
155 asinh z = log (z + sqrt (1+z*z))
156 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
157 atanh z = log ((1+z) / sqrt (1-z*z))