[project @ 2001-12-21 15:07:20 by simonmar]
[packages/old-time.git] / Data / Complex.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Module : Data.Complex
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
6 --
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
10 --
11 -- $Id: Complex.hs,v 1.2 2001/12/21 15:07:21 simonmar Exp $
12 --
13 -- Complex numbers.
14 --
15 -----------------------------------------------------------------------------
16
17 module Data.Complex
18 ( Complex((:+))
19
20 , realPart -- :: (RealFloat a) => Complex a -> a
21 , imagPart -- :: (RealFloat a) => Complex a -> a
22 , conjugate -- :: (RealFloat a) => Complex a -> Complex 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
28
29 -- Complex instances:
30 --
31 -- (RealFloat a) => Eq (Complex a)
32 -- (RealFloat a) => Read (Complex a)
33 -- (RealFloat a) => Show (Complex a)
34 -- (RealFloat a) => Num (Complex a)
35 -- (RealFloat a) => Fractional (Complex a)
36 -- (RealFloat a) => Floating (Complex a)
37 --
38 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
39
40 ) where
41
42 import Prelude
43
44 import Data.Dynamic
45
46 infix 6 :+
47
48 -- -----------------------------------------------------------------------------
49 -- The Complex type
50
51 data (RealFloat a) => Complex a = !a :+ !a deriving (Eq, Read, Show)
52
53
54 -- -----------------------------------------------------------------------------
55 -- Functions over Complex
56
57 realPart, imagPart :: (RealFloat a) => Complex a -> a
58 realPart (x :+ _) = x
59 imagPart (_ :+ y) = y
60
61 {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
62 conjugate :: (RealFloat a) => Complex a -> Complex a
63 conjugate (x:+y) = x :+ (-y)
64
65 {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
66 mkPolar :: (RealFloat a) => a -> a -> Complex a
67 mkPolar r theta = r * cos theta :+ r * sin theta
68
69 {-# SPECIALISE cis :: Double -> Complex Double #-}
70 cis :: (RealFloat a) => a -> Complex a
71 cis theta = cos theta :+ sin theta
72
73 {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
74 polar :: (RealFloat a) => Complex a -> (a,a)
75 polar z = (magnitude z, phase z)
76
77 {-# SPECIALISE magnitude :: Complex Double -> Double #-}
78 magnitude :: (RealFloat a) => Complex a -> a
79 magnitude (x:+y) = scaleFloat k
80 (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
81 where k = max (exponent x) (exponent y)
82 mk = - k
83
84 {-# SPECIALISE phase :: Complex Double -> Double #-}
85 phase :: (RealFloat a) => Complex a -> a
86 phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
87 phase (x:+y) = atan2 y x
88
89
90 -- -----------------------------------------------------------------------------
91 -- Instances of Complex
92
93 #include "Dynamic.h"
94 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
95
96 instance (RealFloat a) => Num (Complex a) where
97 {-# SPECIALISE instance Num (Complex Float) #-}
98 {-# SPECIALISE instance Num (Complex Double) #-}
99 (x:+y) + (x':+y') = (x+x') :+ (y+y')
100 (x:+y) - (x':+y') = (x-x') :+ (y-y')
101 (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
102 negate (x:+y) = negate x :+ negate y
103 abs z = magnitude z :+ 0
104 signum 0 = 0
105 signum z@(x:+y) = x/r :+ y/r where r = magnitude z
106 fromInteger n = fromInteger n :+ 0
107
108 instance (RealFloat a) => Fractional (Complex a) where
109 {-# SPECIALISE instance Fractional (Complex Float) #-}
110 {-# SPECIALISE instance Fractional (Complex Double) #-}
111 (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
112 where x'' = scaleFloat k x'
113 y'' = scaleFloat k y'
114 k = - max (exponent x') (exponent y')
115 d = x'*x'' + y'*y''
116
117 fromRational a = fromRational a :+ 0
118
119 instance (RealFloat a) => Floating (Complex a) where
120 {-# SPECIALISE instance Floating (Complex Float) #-}
121 {-# SPECIALISE instance Floating (Complex Double) #-}
122 pi = pi :+ 0
123 exp (x:+y) = expx * cos y :+ expx * sin y
124 where expx = exp x
125 log z = log (magnitude z) :+ phase z
126
127 sqrt 0 = 0
128 sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
129 where (u,v) = if x < 0 then (v',u') else (u',v')
130 v' = abs y / (u'*2)
131 u' = sqrt ((magnitude z + abs x) / 2)
132
133 sin (x:+y) = sin x * cosh y :+ cos x * sinh y
134 cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
135 tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
136 where sinx = sin x
137 cosx = cos x
138 sinhy = sinh y
139 coshy = cosh y
140
141 sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
142 cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
143 tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
144 where siny = sin y
145 cosy = cos y
146 sinhx = sinh x
147 coshx = cosh x
148
149 asin z@(x:+y) = y':+(-x')
150 where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
151 acos z = y'':+(-x'')
152 where (x'':+y'') = log (z + ((-y'):+x'))
153 (x':+y') = sqrt (1 - z*z)
154 atan z@(x:+y) = y':+(-x')
155 where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
156
157 asinh z = log (z + sqrt (1+z*z))
158 acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
159 atanh z = log ((1+z) / sqrt (1-z*z))