+% -----------------------------------------------------------------------------
+% $Id: Complex.lhs,v 1.5 2000/06/30 13:39:35 simonmar Exp $
%
-% (c) The AQUA Project, Glasgow University, 1994-1997
+% (c) The University of Glasgow, 1994-2000
%
\section[Complex]{Module @Complex@}
\begin{code}
-module Complex (
- Complex((:+)),
-
- realPart, imagPart, conjugate, mkPolar,
- cis, polar, magnitude, phase
- ) where
+module Complex
+ ( Complex((:+))
+
+ , realPart -- :: (RealFloat a) => Complex a -> a
+ , imagPart -- :: (RealFloat a) => Complex a -> a
+ , conjugate -- :: (RealFloat a) => Complex a -> Complex a
+ , 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
+
+ -- Complex instances:
+ --
+ -- (RealFloat a) => Eq (Complex a)
+ -- (RealFloat a) => Read (Complex a)
+ -- (RealFloat a) => Show (Complex a)
+ -- (RealFloat a) => Num (Complex a)
+ -- (RealFloat a) => Fractional (Complex a)
+ -- (RealFloat a) => Floating (Complex a)
+ --
+ -- Implementation checked wrt. Haskell 98 lib report, 1/99.
+
+ ) where
import Prelude
%*********************************************************
\begin{code}
-data (RealFloat a) => Complex a = !a :+ !a deriving (Eq,Read,Show)
+data (RealFloat a) => Complex a = !a :+ !a deriving (Eq, Read, Show)
\end{code}
\begin{code}
realPart, imagPart :: (RealFloat a) => Complex a -> a
-realPart (x:+y) = x
-imagPart (x:+y) = y
+realPart (x :+ _) = x
+imagPart (_ :+ y) = y
conjugate :: (RealFloat a) => Complex a -> Complex a
conjugate (x:+y) = x :+ (-y)
polar :: (RealFloat a) => Complex a -> (a,a)
polar z = (magnitude z, phase z)
-magnitude, phase :: (RealFloat a) => Complex a -> a
+magnitude :: (RealFloat a) => Complex a -> a
magnitude (x:+y) = scaleFloat k
- (sqrt ((scaleFloat mk x)^2 + (scaleFloat mk y)^2))
+ (sqrt ((scaleFloat mk x)^(2::Int) + (scaleFloat mk y)^(2::Int)))
where k = max (exponent x) (exponent y)
mk = - k
+phase :: (RealFloat a) => Complex a -> a
phase (0 :+ 0) = 0 -- SLPJ July 97 from John Peterson
-phase (x:+y) = atan2 y x
+phase (x:+y) = atan2 y x
\end{code}
\begin{code}
instance (RealFloat a) => Num (Complex a) where
+ {-# SPECIALISE instance Num (Complex Float) #-}
+ {-# SPECIALISE instance Num (Complex Double) #-}
(x:+y) + (x':+y') = (x+x') :+ (y+y')
(x:+y) - (x':+y') = (x-x') :+ (y-y')
(x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
fromInteger n = fromInteger n :+ 0
instance (RealFloat a) => Fractional (Complex a) where
+ {-# SPECIALISE instance Fractional (Complex Float) #-}
+ {-# SPECIALISE instance Fractional (Complex Double) #-}
(x:+y) / (x':+y') = (x*x''+y*y'') / d :+ (y*x''-x*y'') / d
where x'' = scaleFloat k x'
y'' = scaleFloat k y'
fromRational a = fromRational a :+ 0
instance (RealFloat a) => Floating (Complex a) where
+ {-# SPECIALISE instance Floating (Complex Float) #-}
+ {-# SPECIALISE instance Floating (Complex Double) #-}
pi = pi :+ 0
exp (x:+y) = expx * cos y :+ expx * sin y
where expx = exp x
asin z@(x:+y) = y':+(-x')
where (x':+y') = log (((-y):+x) + sqrt (1 - z*z))
- acos z@(x:+y) = y'':+(-x'')
+ acos z = y'':+(-x'')
where (x'':+y'') = log (z + ((-y'):+x'))
(x':+y') = sqrt (1 - z*z)
atan z@(x:+y) = y':+(-x')