{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
module Data.Complex
(
Complex((:+))
, realPart
, imagPart
, mkPolar
, cis
, polar
, magnitude
, phase
, conjugate
) where
import GHC.Base (Applicative (..))
import GHC.Generics (Generic, Generic1)
import GHC.Float (Floating(..))
import Data.Data (Data)
import Foreign (Storable, castPtr, peek, poke, pokeElemOff, peekElemOff, sizeOf,
alignment)
import Control.Monad.Fix (MonadFix(..))
import Control.Monad.Zip (MonadZip(..))
infix 6 :+
data Complex a
= !a :+ !a
deriving ( Complex a -> Complex a -> Bool
forall a. Eq a => Complex a -> Complex a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Complex a -> Complex a -> Bool
$c/= :: forall a. Eq a => Complex a -> Complex a -> Bool
== :: Complex a -> Complex a -> Bool
$c== :: forall a. Eq a => Complex a -> Complex a -> Bool
Eq
, Int -> Complex a -> ShowS
forall a. Show a => Int -> Complex a -> ShowS
forall a. Show a => [Complex a] -> ShowS
forall a. Show a => Complex a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Complex a] -> ShowS
$cshowList :: forall a. Show a => [Complex a] -> ShowS
show :: Complex a -> String
$cshow :: forall a. Show a => Complex a -> String
showsPrec :: Int -> Complex a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Complex a -> ShowS
Show
, ReadPrec [Complex a]
ReadPrec (Complex a)
ReadS [Complex a]
forall a. Read a => ReadPrec [Complex a]
forall a. Read a => ReadPrec (Complex a)
forall a. Read a => Int -> ReadS (Complex a)
forall a. Read a => ReadS [Complex a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Complex a]
$creadListPrec :: forall a. Read a => ReadPrec [Complex a]
readPrec :: ReadPrec (Complex a)
$creadPrec :: forall a. Read a => ReadPrec (Complex a)
readList :: ReadS [Complex a]
$creadList :: forall a. Read a => ReadS [Complex a]
readsPrec :: Int -> ReadS (Complex a)
$creadsPrec :: forall a. Read a => Int -> ReadS (Complex a)
Read
, Complex a -> Constr
Complex a -> DataType
forall {a}. Data a => Typeable (Complex a)
forall a. Data a => Complex a -> Constr
forall a. Data a => Complex a -> DataType
forall a.
Data a =>
(forall b. Data b => b -> b) -> Complex a -> Complex a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Complex a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> Complex a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Complex a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Complex a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Complex a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Complex a -> c (Complex a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Complex a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Complex a))
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Complex a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Complex a -> c (Complex a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Complex a))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Complex a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Complex a -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Complex a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Complex a -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Complex a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Complex a -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Complex a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Complex a -> r
gmapT :: (forall b. Data b => b -> b) -> Complex a -> Complex a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Complex a -> Complex a
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Complex a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Complex a))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Complex a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Complex a))
dataTypeOf :: Complex a -> DataType
$cdataTypeOf :: forall a. Data a => Complex a -> DataType
toConstr :: Complex a -> Constr
$ctoConstr :: forall a. Data a => Complex a -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Complex a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Complex a)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Complex a -> c (Complex a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Complex a -> c (Complex a)
Data
, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Complex a) x -> Complex a
forall a x. Complex a -> Rep (Complex a) x
$cto :: forall a x. Rep (Complex a) x -> Complex a
$cfrom :: forall a x. Complex a -> Rep (Complex a) x
Generic
, forall a. Rep1 Complex a -> Complex a
forall a. Complex a -> Rep1 Complex a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall a. Rep1 Complex a -> Complex a
$cfrom1 :: forall a. Complex a -> Rep1 Complex a
Generic1
, forall a b. a -> Complex b -> Complex a
forall a b. (a -> b) -> Complex a -> Complex b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Complex b -> Complex a
$c<$ :: forall a b. a -> Complex b -> Complex a
fmap :: forall a b. (a -> b) -> Complex a -> Complex b
$cfmap :: forall a b. (a -> b) -> Complex a -> Complex b
Functor
, forall a. Eq a => a -> Complex a -> Bool
forall a. Num a => Complex a -> a
forall a. Ord a => Complex a -> a
forall m. Monoid m => Complex m -> m
forall a. Complex a -> Bool
forall a. Complex a -> Int
forall a. Complex a -> [a]
forall a. (a -> a -> a) -> Complex a -> a
forall m a. Monoid m => (a -> m) -> Complex a -> m
forall b a. (b -> a -> b) -> b -> Complex a -> b
forall a b. (a -> b -> b) -> b -> Complex a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Complex a -> a
$cproduct :: forall a. Num a => Complex a -> a
sum :: forall a. Num a => Complex a -> a
$csum :: forall a. Num a => Complex a -> a
minimum :: forall a. Ord a => Complex a -> a
$cminimum :: forall a. Ord a => Complex a -> a
maximum :: forall a. Ord a => Complex a -> a
$cmaximum :: forall a. Ord a => Complex a -> a
elem :: forall a. Eq a => a -> Complex a -> Bool
$celem :: forall a. Eq a => a -> Complex a -> Bool
length :: forall a. Complex a -> Int
$clength :: forall a. Complex a -> Int
null :: forall a. Complex a -> Bool
$cnull :: forall a. Complex a -> Bool
toList :: forall a. Complex a -> [a]
$ctoList :: forall a. Complex a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Complex a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Complex a -> a
foldr1 :: forall a. (a -> a -> a) -> Complex a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Complex a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Complex a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Complex a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Complex a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Complex a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Complex a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Complex a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Complex a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Complex a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Complex a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Complex a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Complex a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Complex a -> m
fold :: forall m. Monoid m => Complex m -> m
$cfold :: forall m. Monoid m => Complex m -> m
Foldable
, Functor Complex
Foldable Complex
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Complex (m a) -> m (Complex a)
forall (f :: * -> *) a.
Applicative f =>
Complex (f a) -> f (Complex a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Complex a -> m (Complex b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Complex a -> f (Complex b)
sequence :: forall (m :: * -> *) a. Monad m => Complex (m a) -> m (Complex a)
$csequence :: forall (m :: * -> *) a. Monad m => Complex (m a) -> m (Complex a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Complex a -> m (Complex b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Complex a -> m (Complex b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Complex (f a) -> f (Complex a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Complex (f a) -> f (Complex a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Complex a -> f (Complex b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Complex a -> f (Complex b)
Traversable
)
realPart :: Complex a -> a
realPart :: forall a. Complex a -> a
realPart (a
x :+ a
_) = a
x
imagPart :: Complex a -> a
imagPart :: forall a. Complex a -> a
imagPart (a
_ :+ a
y) = a
y
{-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
conjugate :: Num a => Complex a -> Complex a
conjugate :: forall a. Num a => Complex a -> Complex a
conjugate (a
x:+a
y) = a
x forall a. a -> a -> Complex a
:+ (-a
y)
{-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
mkPolar :: Floating a => a -> a -> Complex a
mkPolar :: forall a. Floating a => a -> a -> Complex a
mkPolar a
r a
theta = a
r forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos a
theta forall a. a -> a -> Complex a
:+ a
r forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin a
theta
{-# SPECIALISE cis :: Double -> Complex Double #-}
cis :: Floating a => a -> Complex a
cis :: forall a. Floating a => a -> Complex a
cis a
theta = forall a. Floating a => a -> a
cos a
theta forall a. a -> a -> Complex a
:+ forall a. Floating a => a -> a
sin a
theta
{-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
polar :: (RealFloat a) => Complex a -> (a,a)
polar :: forall a. RealFloat a => Complex a -> (a, a)
polar Complex a
z = (forall a. RealFloat a => Complex a -> a
magnitude Complex a
z, forall a. RealFloat a => Complex a -> a
phase Complex a
z)
{-# SPECIALISE magnitude :: Complex Double -> Double #-}
magnitude :: (RealFloat a) => Complex a -> a
magnitude :: forall a. RealFloat a => Complex a -> a
magnitude (a
x:+a
y) = forall a. RealFloat a => Int -> a -> a
scaleFloat Int
k
(forall a. Floating a => a -> a
sqrt (forall {a}. Num a => a -> a
sqr (forall a. RealFloat a => Int -> a -> a
scaleFloat Int
mk a
x) forall a. Num a => a -> a -> a
+ forall {a}. Num a => a -> a
sqr (forall a. RealFloat a => Int -> a -> a
scaleFloat Int
mk a
y)))
where k :: Int
k = forall a. Ord a => a -> a -> a
max (forall a. RealFloat a => a -> Int
exponent a
x) (forall a. RealFloat a => a -> Int
exponent a
y)
mk :: Int
mk = - Int
k
sqr :: a -> a
sqr a
z = a
z forall a. Num a => a -> a -> a
* a
z
{-# SPECIALISE phase :: Complex Double -> Double #-}
phase :: (RealFloat a) => Complex a -> a
phase :: forall a. RealFloat a => Complex a -> a
phase (a
0 :+ a
0) = a
0
phase (a
x:+a
y) = forall a. RealFloat a => a -> a -> a
atan2 a
y a
x
instance (RealFloat a) => Num (Complex a) where
{-# SPECIALISE instance Num (Complex Float) #-}
{-# SPECIALISE instance Num (Complex Double) #-}
(a
x:+a
y) + :: Complex a -> Complex a -> Complex a
+ (a
x':+a
y') = (a
xforall a. Num a => a -> a -> a
+a
x') forall a. a -> a -> Complex a
:+ (a
yforall a. Num a => a -> a -> a
+a
y')
(a
x:+a
y) - :: Complex a -> Complex a -> Complex a
- (a
x':+a
y') = (a
xforall a. Num a => a -> a -> a
-a
x') forall a. a -> a -> Complex a
:+ (a
yforall a. Num a => a -> a -> a
-a
y')
(a
x:+a
y) * :: Complex a -> Complex a -> Complex a
* (a
x':+a
y') = (a
xforall a. Num a => a -> a -> a
*a
x'forall a. Num a => a -> a -> a
-a
yforall a. Num a => a -> a -> a
*a
y') forall a. a -> a -> Complex a
:+ (a
xforall a. Num a => a -> a -> a
*a
y'forall a. Num a => a -> a -> a
+a
yforall a. Num a => a -> a -> a
*a
x')
negate :: Complex a -> Complex a
negate (a
x:+a
y) = forall {a}. Num a => a -> a
negate a
x forall a. a -> a -> Complex a
:+ forall {a}. Num a => a -> a
negate a
y
abs :: Complex a -> Complex a
abs Complex a
z = forall a. RealFloat a => Complex a -> a
magnitude Complex a
z forall a. a -> a -> Complex a
:+ a
0
signum :: Complex a -> Complex a
signum (a
0:+a
0) = Complex a
0
signum z :: Complex a
z@(a
x:+a
y) = a
xforall a. Fractional a => a -> a -> a
/a
r forall a. a -> a -> Complex a
:+ a
yforall a. Fractional a => a -> a -> a
/a
r where r :: a
r = forall a. RealFloat a => Complex a -> a
magnitude Complex a
z
fromInteger :: Integer -> Complex a
fromInteger Integer
n = forall a. Num a => Integer -> a
fromInteger Integer
n forall a. a -> a -> Complex a
:+ a
0
instance (RealFloat a) => Fractional (Complex a) where
{-# SPECIALISE instance Fractional (Complex Float) #-}
{-# SPECIALISE instance Fractional (Complex Double) #-}
(a
x:+a
y) / :: Complex a -> Complex a -> Complex a
/ (a
x':+a
y') = (a
xforall a. Num a => a -> a -> a
*a
x''forall a. Num a => a -> a -> a
+a
yforall a. Num a => a -> a -> a
*a
y'') forall a. Fractional a => a -> a -> a
/ a
d forall a. a -> a -> Complex a
:+ (a
yforall a. Num a => a -> a -> a
*a
x''forall a. Num a => a -> a -> a
-a
xforall a. Num a => a -> a -> a
*a
y'') forall a. Fractional a => a -> a -> a
/ a
d
where x'' :: a
x'' = forall a. RealFloat a => Int -> a -> a
scaleFloat Int
k a
x'
y'' :: a
y'' = forall a. RealFloat a => Int -> a -> a
scaleFloat Int
k a
y'
k :: Int
k = - forall a. Ord a => a -> a -> a
max (forall a. RealFloat a => a -> Int
exponent a
x') (forall a. RealFloat a => a -> Int
exponent a
y')
d :: a
d = a
x'forall a. Num a => a -> a -> a
*a
x'' forall a. Num a => a -> a -> a
+ a
y'forall a. Num a => a -> a -> a
*a
y''
fromRational :: Rational -> Complex a
fromRational Rational
a = forall a. Fractional a => Rational -> a
fromRational Rational
a forall a. a -> a -> Complex a
:+ a
0
instance (RealFloat a) => Floating (Complex a) where
{-# SPECIALISE instance Floating (Complex Float) #-}
{-# SPECIALISE instance Floating (Complex Double) #-}
pi :: Complex a
pi = forall a. Floating a => a
pi forall a. a -> a -> Complex a
:+ a
0
exp :: Complex a -> Complex a
exp (a
x:+a
y) = a
expx forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos a
y forall a. a -> a -> Complex a
:+ a
expx forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin a
y
where expx :: a
expx = forall a. Floating a => a -> a
exp a
x
log :: Complex a -> Complex a
log Complex a
z = forall a. Floating a => a -> a
log (forall a. RealFloat a => Complex a -> a
magnitude Complex a
z) forall a. a -> a -> Complex a
:+ forall a. RealFloat a => Complex a -> a
phase Complex a
z
Complex a
x ** :: Complex a -> Complex a -> Complex a
** Complex a
y = case (Complex a
x,Complex a
y) of
(Complex a
_ , (a
0:+a
0)) -> a
1 forall a. a -> a -> Complex a
:+ a
0
((a
0:+a
0), (a
exp_re:+a
_)) -> case forall a. Ord a => a -> a -> Ordering
compare a
exp_re a
0 of
Ordering
GT -> a
0 forall a. a -> a -> Complex a
:+ a
0
Ordering
LT -> a
inf forall a. a -> a -> Complex a
:+ a
0
Ordering
EQ -> a
nan forall a. a -> a -> Complex a
:+ a
nan
((a
re:+a
im), (a
exp_re:+a
_))
| (forall a. RealFloat a => a -> Bool
isInfinite a
re Bool -> Bool -> Bool
|| forall a. RealFloat a => a -> Bool
isInfinite a
im) -> case forall a. Ord a => a -> a -> Ordering
compare a
exp_re a
0 of
Ordering
GT -> a
inf forall a. a -> a -> Complex a
:+ a
0
Ordering
LT -> a
0 forall a. a -> a -> Complex a
:+ a
0
Ordering
EQ -> a
nan forall a. a -> a -> Complex a
:+ a
nan
| Bool
otherwise -> forall a. Floating a => a -> a
exp (forall a. Floating a => a -> a
log Complex a
x forall a. Num a => a -> a -> a
* Complex a
y)
where
inf :: a
inf = a
1forall a. Fractional a => a -> a -> a
/a
0
nan :: a
nan = a
0forall a. Fractional a => a -> a -> a
/a
0
sqrt :: Complex a -> Complex a
sqrt (a
0:+a
0) = Complex a
0
sqrt z :: Complex a
z@(a
x:+a
y) = a
u forall a. a -> a -> Complex a
:+ (if a
y forall a. Ord a => a -> a -> Bool
< a
0 then -a
v else a
v)
where (a
u,a
v) = if a
x forall a. Ord a => a -> a -> Bool
< a
0 then (a
v',a
u') else (a
u',a
v')
v' :: a
v' = forall {a}. Num a => a -> a
abs a
y forall a. Fractional a => a -> a -> a
/ (a
u'forall a. Num a => a -> a -> a
*a
2)
u' :: a
u' = forall a. Floating a => a -> a
sqrt ((forall a. RealFloat a => Complex a -> a
magnitude Complex a
z forall a. Num a => a -> a -> a
+ forall {a}. Num a => a -> a
abs a
x) forall a. Fractional a => a -> a -> a
/ a
2)
sin :: Complex a -> Complex a
sin (a
x:+a
y) = forall a. Floating a => a -> a
sin a
x forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cosh a
y forall a. a -> a -> Complex a
:+ forall a. Floating a => a -> a
cos a
x forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sinh a
y
cos :: Complex a -> Complex a
cos (a
x:+a
y) = forall a. Floating a => a -> a
cos a
x forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cosh a
y forall a. a -> a -> Complex a
:+ (- forall a. Floating a => a -> a
sin a
x forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sinh a
y)
tan :: Complex a -> Complex a
tan (a
x:+a
y) = (a
sinxforall a. Num a => a -> a -> a
*a
coshyforall a. a -> a -> Complex a
:+a
cosxforall a. Num a => a -> a -> a
*a
sinhy)forall a. Fractional a => a -> a -> a
/(a
cosxforall a. Num a => a -> a -> a
*a
coshyforall a. a -> a -> Complex a
:+(-a
sinxforall a. Num a => a -> a -> a
*a
sinhy))
where sinx :: a
sinx = forall a. Floating a => a -> a
sin a
x
cosx :: a
cosx = forall a. Floating a => a -> a
cos a
x
sinhy :: a
sinhy = forall a. Floating a => a -> a
sinh a
y
coshy :: a
coshy = forall a. Floating a => a -> a
cosh a
y
sinh :: Complex a -> Complex a
sinh (a
x:+a
y) = forall a. Floating a => a -> a
cos a
y forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sinh a
x forall a. a -> a -> Complex a
:+ forall a. Floating a => a -> a
sin a
y forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cosh a
x
cosh :: Complex a -> Complex a
cosh (a
x:+a
y) = forall a. Floating a => a -> a
cos a
y forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cosh a
x forall a. a -> a -> Complex a
:+ forall a. Floating a => a -> a
sin a
y forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sinh a
x
tanh :: Complex a -> Complex a
tanh (a
x:+a
y) = (a
cosyforall a. Num a => a -> a -> a
*a
sinhxforall a. a -> a -> Complex a
:+a
sinyforall a. Num a => a -> a -> a
*a
coshx)forall a. Fractional a => a -> a -> a
/(a
cosyforall a. Num a => a -> a -> a
*a
coshxforall a. a -> a -> Complex a
:+a
sinyforall a. Num a => a -> a -> a
*a
sinhx)
where siny :: a
siny = forall a. Floating a => a -> a
sin a
y
cosy :: a
cosy = forall a. Floating a => a -> a
cos a
y
sinhx :: a
sinhx = forall a. Floating a => a -> a
sinh a
x
coshx :: a
coshx = forall a. Floating a => a -> a
cosh a
x
asin :: Complex a -> Complex a
asin z :: Complex a
z@(a
x:+a
y) = a
y'forall a. a -> a -> Complex a
:+(-a
x')
where (a
x':+a
y') = forall a. Floating a => a -> a
log (((-a
y)forall a. a -> a -> Complex a
:+a
x) forall a. Num a => a -> a -> a
+ forall a. Floating a => a -> a
sqrt (Complex a
1 forall a. Num a => a -> a -> a
- Complex a
zforall a. Num a => a -> a -> a
*Complex a
z))
acos :: Complex a -> Complex a
acos Complex a
z = a
y''forall a. a -> a -> Complex a
:+(-a
x'')
where (a
x'':+a
y'') = forall a. Floating a => a -> a
log (Complex a
z forall a. Num a => a -> a -> a
+ ((-a
y')forall a. a -> a -> Complex a
:+a
x'))
(a
x':+a
y') = forall a. Floating a => a -> a
sqrt (Complex a
1 forall a. Num a => a -> a -> a
- Complex a
zforall a. Num a => a -> a -> a
*Complex a
z)
atan :: Complex a -> Complex a
atan z :: Complex a
z@(a
x:+a
y) = a
y'forall a. a -> a -> Complex a
:+(-a
x')
where (a
x':+a
y') = forall a. Floating a => a -> a
log (((a
1forall a. Num a => a -> a -> a
-a
y)forall a. a -> a -> Complex a
:+a
x) forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a -> a
sqrt (Complex a
1forall a. Num a => a -> a -> a
+Complex a
zforall a. Num a => a -> a -> a
*Complex a
z))
asinh :: Complex a -> Complex a
asinh Complex a
z = forall a. Floating a => a -> a
log (Complex a
z forall a. Num a => a -> a -> a
+ forall a. Floating a => a -> a
sqrt (Complex a
1forall a. Num a => a -> a -> a
+Complex a
zforall a. Num a => a -> a -> a
*Complex a
z))
acosh :: Complex a -> Complex a
acosh Complex a
z = forall a. Floating a => a -> a
log (Complex a
z forall a. Num a => a -> a -> a
+ (forall a. Floating a => a -> a
sqrt forall a b. (a -> b) -> a -> b
$ Complex a
zforall a. Num a => a -> a -> a
+Complex a
1) forall a. Num a => a -> a -> a
* (forall a. Floating a => a -> a
sqrt forall a b. (a -> b) -> a -> b
$ Complex a
zforall a. Num a => a -> a -> a
-Complex a
1))
atanh :: Complex a -> Complex a
atanh Complex a
z = Complex a
0.5 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
log ((Complex a
1.0forall a. Num a => a -> a -> a
+Complex a
z) forall a. Fractional a => a -> a -> a
/ (Complex a
1.0forall a. Num a => a -> a -> a
-Complex a
z))
log1p :: Complex a -> Complex a
log1p x :: Complex a
x@(a
a :+ a
b)
| forall {a}. Num a => a -> a
abs a
a forall a. Ord a => a -> a -> Bool
< a
0.5 Bool -> Bool -> Bool
&& forall {a}. Num a => a -> a
abs a
b forall a. Ord a => a -> a -> Bool
< a
0.5
, a
u <- a
2forall a. Num a => a -> a -> a
*a
a forall a. Num a => a -> a -> a
+ a
aforall a. Num a => a -> a -> a
*a
a forall a. Num a => a -> a -> a
+ a
bforall a. Num a => a -> a -> a
*a
b = forall a. Floating a => a -> a
log1p (a
uforall a. Fractional a => a -> a -> a
/(a
1 forall a. Num a => a -> a -> a
+ forall a. Floating a => a -> a
sqrt(a
uforall a. Num a => a -> a -> a
+a
1))) forall a. a -> a -> Complex a
:+ forall a. RealFloat a => a -> a -> a
atan2 (a
1 forall a. Num a => a -> a -> a
+ a
a) a
b
| Bool
otherwise = forall a. Floating a => a -> a
log (Complex a
1 forall a. Num a => a -> a -> a
+ Complex a
x)
{-# INLINE log1p #-}
expm1 :: Complex a -> Complex a
expm1 x :: Complex a
x@(a
a :+ a
b)
| a
aforall a. Num a => a -> a -> a
*a
a forall a. Num a => a -> a -> a
+ a
bforall a. Num a => a -> a -> a
*a
b forall a. Ord a => a -> a -> Bool
< a
1
, a
u <- forall a. Floating a => a -> a
expm1 a
a
, a
v <- forall a. Floating a => a -> a
sin (a
bforall a. Fractional a => a -> a -> a
/a
2)
, a
w <- -a
2forall a. Num a => a -> a -> a
*a
vforall a. Num a => a -> a -> a
*a
v = (a
uforall a. Num a => a -> a -> a
*a
w forall a. Num a => a -> a -> a
+ a
u forall a. Num a => a -> a -> a
+ a
w) forall a. a -> a -> Complex a
:+ (a
uforall a. Num a => a -> a -> a
+a
1)forall a. Num a => a -> a -> a
*forall a. Floating a => a -> a
sin a
b
| Bool
otherwise = forall a. Floating a => a -> a
exp Complex a
x forall a. Num a => a -> a -> a
- Complex a
1
{-# INLINE expm1 #-}
instance Storable a => Storable (Complex a) where
sizeOf :: Complex a -> Int
sizeOf Complex a
a = Int
2 forall a. Num a => a -> a -> a
* forall a. Storable a => a -> Int
sizeOf (forall a. Complex a -> a
realPart Complex a
a)
alignment :: Complex a -> Int
alignment Complex a
a = forall a. Storable a => a -> Int
alignment (forall a. Complex a -> a
realPart Complex a
a)
peek :: Ptr (Complex a) -> IO (Complex a)
peek Ptr (Complex a)
p = do
Ptr a
q <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. Ptr a -> Ptr b
castPtr Ptr (Complex a)
p
a
r <- forall a. Storable a => Ptr a -> IO a
peek Ptr a
q
a
i <- forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
q Int
1
forall (m :: * -> *) a. Monad m => a -> m a
return (a
r forall a. a -> a -> Complex a
:+ a
i)
poke :: Ptr (Complex a) -> Complex a -> IO ()
poke Ptr (Complex a)
p (a
r :+ a
i) = do
Ptr a
q <-forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (forall a b. Ptr a -> Ptr b
castPtr Ptr (Complex a)
p)
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
q a
r
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr a
q Int
1 a
i
instance Applicative Complex where
pure :: forall a. a -> Complex a
pure a
a = a
a forall a. a -> a -> Complex a
:+ a
a
a -> b
f :+ a -> b
g <*> :: forall a b. Complex (a -> b) -> Complex a -> Complex b
<*> a
a :+ a
b = a -> b
f a
a forall a. a -> a -> Complex a
:+ a -> b
g a
b
liftA2 :: forall a b c. (a -> b -> c) -> Complex a -> Complex b -> Complex c
liftA2 a -> b -> c
f (a
x :+ a
y) (b
a :+ b
b) = a -> b -> c
f a
x b
a forall a. a -> a -> Complex a
:+ a -> b -> c
f a
y b
b
instance Monad Complex where
a
a :+ a
b >>= :: forall a b. Complex a -> (a -> Complex b) -> Complex b
>>= a -> Complex b
f = forall a. Complex a -> a
realPart (a -> Complex b
f a
a) forall a. a -> a -> Complex a
:+ forall a. Complex a -> a
imagPart (a -> Complex b
f a
b)
instance MonadZip Complex where
mzipWith :: forall a b c. (a -> b -> c) -> Complex a -> Complex b -> Complex c
mzipWith = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
instance MonadFix Complex where
mfix :: forall a. (a -> Complex a) -> Complex a
mfix a -> Complex a
f = (let a
a :+ a
_ = a -> Complex a
f a
a in a
a) forall a. a -> a -> Complex a
:+ (let a
_ :+ a
a = a -> Complex a
f a
a in a
a)
{-# RULES
"realToFrac/a->Complex Double"
realToFrac = \x -> realToFrac x :+ (0 :: Double)
"realToFrac/a->Complex Float"
realToFrac = \x -> realToFrac x :+ (0 :: Float)
#-}