2b575fb4ed95648fd0c01c86d970433c0753d2a0
1 {-# LANGUAGE CPP, PackageImports #-}
3 {-# LANGUAGE Safe #-}
4 #endif
6 module Data.Complex (
7 -- * Rectangular form
8 Complex((:+))
10 , realPart -- :: (RealFloat a) => Complex a -> a
11 , imagPart -- :: (RealFloat a) => Complex a -> a
12 -- * Polar form
13 , mkPolar -- :: (RealFloat a) => a -> a -> Complex a
14 , cis -- :: (RealFloat a) => a -> Complex a
15 , polar -- :: (RealFloat a) => Complex a -> (a,a)
16 , magnitude -- :: (RealFloat a) => Complex a -> a
17 , phase -- :: (RealFloat a) => Complex a -> a
18 -- * Conjugate
19 , conjugate -- :: (RealFloat a) => Complex a -> Complex a
21 -- * Specification
23 -- \$code
24 ) where
25 import "base" Data.Complex
27 {- \$code
28 > module Data.Complex(Complex((:+)), realPart, imagPart, conjugate, mkPolar,
29 > cis, polar, magnitude, phase) where
30 >
31 > infix 6 :+
32 >
33 > data (RealFloat a) => Complex a = !a :+ !a deriving (Eq,Read,Show)
34 >
35 >
36 > realPart, imagPart :: (RealFloat a) => Complex a -> a
37 > realPart (x:+y) = x
38 > imagPart (x:+y) = y
39 >
40 > conjugate :: (RealFloat a) => Complex a -> Complex a
41 > conjugate (x:+y) = x :+ (-y)
42 >
43 > mkPolar :: (RealFloat a) => a -> a -> Complex a
44 > mkPolar r theta = r * cos theta :+ r * sin theta
45 >
46 > cis :: (RealFloat a) => a -> Complex a
47 > cis theta = cos theta :+ sin theta
48 >
49 > polar :: (RealFloat a) => Complex a -> (a,a)
50 > polar z = (magnitude z, phase z)
51 >
52 > magnitude :: (RealFloat a) => Complex a -> a
53 > magnitude (x:+y) = scaleFloat k
54 > (sqrt ((scaleFloat mk x)^2 + (scaleFloat mk y)^2))
55 > where k = max (exponent x) (exponent y)
56 > mk = - k
57 >
58 > phase :: (RealFloat a) => Complex a -> a
59 > phase (0 :+ 0) = 0
60 > phase (x :+ y) = atan2 y x
61 >
62 >
63 > instance (RealFloat a) => Num (Complex a) where
64 > (x:+y) + (x':+y') = (x+x') :+ (y+y')
65 > (x:+y) - (x':+y') = (x-x') :+ (y-y')
66 > (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
67 > negate (x:+y) = negate x :+ negate y
68 > abs z = magnitude z :+ 0
69 > signum 0 = 0
70 > signum z@(x:+y) = x/r :+ y/r where r = magnitude z
71 > fromInteger n = fromInteger n :+ 0
72 >
73 > instance (RealFloat a) => Fractional (Complex a) where
74 > (x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
75 > where x'' = scaleFloat k x'
76 > y'' = scaleFloat k y'
77 > k = - max (exponent x') (exponent y')
78 > d = x'*x'' + y'*y''
79 >
80 > fromRational a = fromRational a :+ 0
81 >
82 > instance (RealFloat a) => Floating (Complex a) where
83 > pi = pi :+ 0
84 > exp (x:+y) = expx * cos y :+ expx * sin y
85 > where expx = exp x
86 > log z = log (magnitude z) :+ phase z
87 >
88 > sqrt 0 = 0
89 > sqrt z@(x:+y) = u :+ (if y < 0 then -v else v)
90 > where (u,v) = if x < 0 then (v',u') else (u',v')
91 > v' = abs y / (u'*2)
92 > u' = sqrt ((magnitude z + abs x) / 2)
93 >
94 > sin (x:+y) = sin x * cosh y :+ cos x * sinh y
95 > cos (x:+y) = cos x * cosh y :+ (- sin x * sinh y)
96 > tan (x:+y) = (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
97 > where sinx = sin x
98 > cosx = cos x
99 > sinhy = sinh y
100 > coshy = cosh y
101 >
102 > sinh (x:+y) = cos y * sinh x :+ sin y * cosh x
103 > cosh (x:+y) = cos y * cosh x :+ sin y * sinh x
104 > tanh (x:+y) = (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
105 > where siny = sin y
106 > cosy = cos y
107 > sinhx = sinh x
108 > coshx = cosh x
109 >
110 > asin z@(x:+y) = y':+(-x')
111 > where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
112 > acos z@(x:+y) = y'':+(-x'')
113 > where (x'':+y'') = log (z + ((-y'):+x'))
114 > (x':+y') = sqrt (1 - z*z)
115 > atan z@(x:+y) = y':+(-x')
116 > where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
117 >
118 > asinh z = log (z + sqrt (1+z*z))
119 > acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
120 > atanh z = log ((1+z) / sqrt (1-z*z))
121 > -}