X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FComplex.hs;h=749ba91982b9d44a13340138010ca1fab5923639;hb=d71aee821935e29a3569bc4b401f695536209c85;hp=c9f83341595db00c6f5fe02f0d85184443c0bc51;hpb=d12c4f3de454ea031f2258627d388d3d994a3cfd;p=haskell-directory.git diff --git a/Data/Complex.hs b/Data/Complex.hs index c9f8334..749ba91 100644 --- a/Data/Complex.hs +++ b/Data/Complex.hs @@ -13,17 +13,21 @@ ----------------------------------------------------------------------------- module Data.Complex - ( Complex((:+)) - + ( + -- * Rectangular form + Complex((:+)) + , realPart -- :: (RealFloat a) => Complex a -> a , imagPart -- :: (RealFloat a) => Complex a -> a - , conjugate -- :: (RealFloat a) => Complex a -> Complex a + -- * Polar form , mkPolar -- :: (RealFloat a) => a -> a -> Complex a , cis -- :: (RealFloat a) => a -> Complex a , polar -- :: (RealFloat a) => Complex a -> (a,a) , magnitude -- :: (RealFloat a) => Complex a -> a , phase -- :: (RealFloat a) => Complex a -> a - + -- * Conjugate + , conjugate -- :: (RealFloat a) => Complex a -> Complex a + -- Complex instances: -- -- (RealFloat a) => Eq (Complex a) @@ -39,8 +43,10 @@ module Data.Complex import Prelude -#ifndef __NHC__ -import Data.Dynamic +import Data.Typeable + +#ifdef __HUGS__ +import Hugs.Prelude(Num(fromInt), Fractional(fromDouble)) #endif infix 6 :+ @@ -48,32 +54,52 @@ infix 6 :+ -- ----------------------------------------------------------------------------- -- The Complex type -data (RealFloat a) => Complex a = !a :+ !a deriving (Eq, Read, Show) - +-- | Complex numbers are an algebraic type. +-- +-- For a complex number @z@, @'abs' z@ is a number with the magnitude of @z@, +-- but oriented in the positive real direction, whereas @'signum' z@ +-- has the phase of @z@, but unit magnitude. +data (RealFloat a) => Complex a + = !a :+ !a -- ^ forms a complex number from its real and imaginary + -- rectangular components. + deriving (Eq, Read, Show) -- ----------------------------------------------------------------------------- -- Functions over Complex -realPart, imagPart :: (RealFloat a) => Complex a -> a +-- | Extracts the real part of a complex number. +realPart :: (RealFloat a) => Complex a -> a realPart (x :+ _) = x + +-- | Extracts the imaginary part of a complex number. +imagPart :: (RealFloat a) => Complex a -> a imagPart (_ :+ y) = y +-- | The conjugate of a complex number. {-# SPECIALISE conjugate :: Complex Double -> Complex Double #-} conjugate :: (RealFloat a) => Complex a -> Complex a conjugate (x:+y) = x :+ (-y) +-- | Form a complex number from polar components of magnitude and phase. {-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-} mkPolar :: (RealFloat a) => a -> a -> Complex a mkPolar r theta = r * cos theta :+ r * sin theta +-- | @'cis' t@ is a complex value with magnitude @1@ +-- and phase @t@ (modulo @2*'pi'@). {-# SPECIALISE cis :: Double -> Complex Double #-} cis :: (RealFloat a) => a -> Complex a cis theta = cos theta :+ sin theta +-- | The function 'polar' takes a complex number and +-- returns a (magnitude, phase) pair in canonical form: +-- the magnitude is nonnegative, and the phase in the range @(-'pi', 'pi']@; +-- if the magnitude is zero, then so is the phase. {-# SPECIALISE polar :: Complex Double -> (Double,Double) #-} polar :: (RealFloat a) => Complex a -> (a,a) polar z = (magnitude z, phase z) +-- | The nonnegative magnitude of a complex number. {-# SPECIALISE magnitude :: Complex Double -> Double #-} magnitude :: (RealFloat a) => Complex a -> a magnitude (x:+y) = scaleFloat k @@ -81,6 +107,8 @@ magnitude (x:+y) = scaleFloat k where k = max (exponent x) (exponent y) mk = - k +-- | The phase of a complex number, in the range @(-'pi', 'pi']@. +-- If the magnitude is zero, then so is the phase. {-# SPECIALISE phase :: Complex Double -> Double #-} phase :: (RealFloat a) => Complex a -> a phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson @@ -90,10 +118,8 @@ phase (x:+y) = atan2 y x -- ----------------------------------------------------------------------------- -- Instances of Complex -#ifndef __NHC__ -#include "Dynamic.h" +#include "Typeable.h" INSTANCE_TYPEABLE1(Complex,complexTc,"Complex") -#endif instance (RealFloat a) => Num (Complex a) where {-# SPECIALISE instance Num (Complex Float) #-} @@ -106,6 +132,9 @@ instance (RealFloat a) => Num (Complex a) where signum 0 = 0 signum z@(x:+y) = x/r :+ y/r where r = magnitude z fromInteger n = fromInteger n :+ 0 +#ifdef __HUGS__ + fromInt n = fromInt n :+ 0 +#endif instance (RealFloat a) => Fractional (Complex a) where {-# SPECIALISE instance Fractional (Complex Float) #-} @@ -117,6 +146,9 @@ instance (RealFloat a) => Fractional (Complex a) where d = x'*x'' + y'*y'' fromRational a = fromRational a :+ 0 +#ifdef __HUGS__ + fromDouble a = fromDouble a :+ 0 +#endif instance (RealFloat a) => Floating (Complex a) where {-# SPECIALISE instance Floating (Complex Float) #-}