[project @ 2001-06-28 14:15:04 by simonmar]
[ghc-base.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.1 2001/06/28 14:15:02 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 conjugate        :: (RealFloat a) => Complex a -> Complex a
62 conjugate (x:+y) =  x :+ (-y)
63
64 mkPolar          :: (RealFloat a) => a -> a -> Complex a
65 mkPolar r theta  =  r * cos theta :+ r * sin theta
66
67 cis              :: (RealFloat a) => a -> Complex a
68 cis theta        =  cos theta :+ sin theta
69
70 polar            :: (RealFloat a) => Complex a -> (a,a)
71 polar z          =  (magnitude z, phase z)
72
73 magnitude :: (RealFloat a) => Complex a -> a
74 magnitude (x:+y) =  scaleFloat k
75                      (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
76                     where k  = max (exponent x) (exponent y)
77                           mk = - k
78
79 phase :: (RealFloat a) => Complex a -> a
80 phase (0 :+ 0)   = 0            -- SLPJ July 97 from John Peterson
81 phase (x:+y)     = atan2 y x
82
83
84 -- -----------------------------------------------------------------------------
85 -- Instances of Complex
86
87 #include "Dynamic.h"
88 INSTANCE_TYPEABLE1(Complex,complexTc,"Complex")
89
90 instance  (RealFloat a) => Num (Complex a)  where
91     {-# SPECIALISE instance Num (Complex Float) #-}
92     {-# SPECIALISE instance Num (Complex Double) #-}
93     (x:+y) + (x':+y')   =  (x+x') :+ (y+y')
94     (x:+y) - (x':+y')   =  (x-x') :+ (y-y')
95     (x:+y) * (x':+y')   =  (x*x'-y*y') :+ (x*y'+y*x')
96     negate (x:+y)       =  negate x :+ negate y
97     abs z               =  magnitude z :+ 0
98     signum 0            =  0
99     signum z@(x:+y)     =  x/r :+ y/r  where r = magnitude z
100     fromInteger n       =  fromInteger n :+ 0
101
102 instance  (RealFloat a) => Fractional (Complex a)  where
103     {-# SPECIALISE instance Fractional (Complex Float) #-}
104     {-# SPECIALISE instance Fractional (Complex Double) #-}
105     (x:+y) / (x':+y')   =  (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
106                            where x'' = scaleFloat k x'
107                                  y'' = scaleFloat k y'
108                                  k   = - max (exponent x') (exponent y')
109                                  d   = x'*x'' + y'*y''
110
111     fromRational a      =  fromRational a :+ 0
112
113 instance  (RealFloat a) => Floating (Complex a) where
114     {-# SPECIALISE instance Floating (Complex Float) #-}
115     {-# SPECIALISE instance Floating (Complex Double) #-}
116     pi             =  pi :+ 0
117     exp (x:+y)     =  expx * cos y :+ expx * sin y
118                       where expx = exp x
119     log z          =  log (magnitude z) :+ phase z
120
121     sqrt 0         =  0
122     sqrt z@(x:+y)  =  u :+ (if y < 0 then -v else v)
123                       where (u,v) = if x < 0 then (v',u') else (u',v')
124                             v'    = abs y / (u'*2)
125                             u'    = sqrt ((magnitude z + abs x) / 2)
126
127     sin (x:+y)     =  sin x * cosh y :+ cos x * sinh y
128     cos (x:+y)     =  cos x * cosh y :+ (- sin x * sinh y)
129     tan (x:+y)     =  (sinx*coshy:+cosx*sinhy)/(cosx*coshy:+(-sinx*sinhy))
130                       where sinx  = sin x
131                             cosx  = cos x
132                             sinhy = sinh y
133                             coshy = cosh y
134
135     sinh (x:+y)    =  cos y * sinh x :+ sin  y * cosh x
136     cosh (x:+y)    =  cos y * cosh x :+ sin y * sinh x
137     tanh (x:+y)    =  (cosy*sinhx:+siny*coshx)/(cosy*coshx:+siny*sinhx)
138                       where siny  = sin y
139                             cosy  = cos y
140                             sinhx = sinh x
141                             coshx = cosh x
142
143     asin z@(x:+y)  =  y':+(-x')
144                       where  (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
145     acos z         =  y'':+(-x'')
146                       where (x'':+y'') = log (z + ((-y'):+x'))
147                             (x':+y')   = sqrt (1 - z*z)
148     atan z@(x:+y)  =  y':+(-x')
149                       where (x':+y') = log (((1-y):+x) / sqrt (1+z*z))
150
151     asinh z        =  log (z + sqrt (1+z*z))
152     acosh z        =  log (z + (z+1) * sqrt ((z-1)/(z+1)))
153     atanh z        =  log ((1+z) / sqrt (1-z*z))