{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# 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)
infix 6 :+
data Complex a
= !a :+ !a
deriving ( Eq
, Show
, Read
, Data
, Generic
, Generic1
, Functor
, Foldable
, Traversable
)
realPart :: Complex a -> a
realPart :: Complex a -> a
realPart (a
x :+ a
_) = a
x
imagPart :: Complex a -> a
imagPart :: Complex a -> a
imagPart (a
_ :+ a
y) = a
y
{-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
conjugate :: Num a => Complex a -> Complex a
conjugate :: Complex a -> Complex a
conjugate (a
x:+a
y) = a
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (-a
y)
{-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
mkPolar :: Floating a => a -> a -> Complex a
mkPolar :: a -> a -> Complex a
mkPolar a
r a
theta = a
r a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cos a
theta a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
r a -> a -> a
forall a. Num a => a -> 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 :: a -> Complex a
cis a
theta = a -> a
forall a. Floating a => a -> a
cos a
theta a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Floating a => a -> a
sin a
theta
{-# SPECIALISE polar :: Complex Double -> (Double,Double) #-}
polar :: (RealFloat a) => Complex a -> (a,a)
polar :: Complex a -> (a, a)
polar Complex a
z = (Complex a -> a
forall a. RealFloat a => Complex a -> a
magnitude Complex a
z, Complex a -> a
forall a. RealFloat a => Complex a -> a
phase Complex a
z)
{-# SPECIALISE magnitude :: Complex Double -> Double #-}
magnitude :: (RealFloat a) => Complex a -> a
magnitude :: Complex a -> a
magnitude (a
x:+a
y) = Int -> a -> a
forall a. RealFloat a => Int -> a -> a
scaleFloat Int
k
(a -> a
forall a. Floating a => a -> a
sqrt (a -> a
forall a. Num a => a -> a
sqr (Int -> a -> a
forall a. RealFloat a => Int -> a -> a
scaleFloat Int
mk a
x) a -> a -> a
forall a. Num a => a -> a -> a
+ a -> a
forall a. Num a => a -> a
sqr (Int -> a -> a
forall a. RealFloat a => Int -> a -> a
scaleFloat Int
mk a
y)))
where k :: Int
k = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (a -> Int
forall a. RealFloat a => a -> Int
exponent a
x) (a -> Int
forall a. RealFloat a => a -> Int
exponent a
y)
mk :: Int
mk = - Int
k
sqr :: a -> a
sqr a
z = a
z a -> a -> a
forall a. Num a => a -> a -> a
* a
z
{-# SPECIALISE phase :: Complex Double -> Double #-}
phase :: (RealFloat a) => Complex a -> a
phase :: Complex a -> a
phase (a
0 :+ a
0) = a
0
phase (a
x:+a
y) = a -> a -> a
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
xa -> a -> a
forall a. Num a => a -> a -> a
+a
x') a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ya -> a -> a
forall a. Num a => a -> a -> a
+a
y')
(a
x:+a
y) - :: Complex a -> Complex a -> Complex a
- (a
x':+a
y') = (a
xa -> a -> a
forall a. Num a => a -> a -> a
-a
x') a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ya -> a -> a
forall a. Num a => a -> a -> a
-a
y')
(a
x:+a
y) * :: Complex a -> Complex a -> Complex a
* (a
x':+a
y') = (a
xa -> a -> a
forall a. Num a => a -> a -> a
*a
x'a -> a -> a
forall a. Num a => a -> a -> a
-a
ya -> a -> a
forall a. Num a => a -> a -> a
*a
y') a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
xa -> a -> a
forall a. Num a => a -> a -> a
*a
y'a -> a -> a
forall a. Num a => a -> a -> a
+a
ya -> a -> a
forall a. Num a => a -> a -> a
*a
x')
negate :: Complex a -> Complex a
negate (a
x:+a
y) = a -> a
forall a. Num a => a -> a
negate a
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Num a => a -> a
negate a
y
abs :: Complex a -> Complex a
abs Complex a
z = Complex a -> a
forall a. RealFloat a => Complex a -> a
magnitude Complex a
z a -> a -> Complex a
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
xa -> a -> a
forall a. Fractional a => a -> a -> a
/a
r a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
ya -> a -> a
forall a. Fractional a => a -> a -> a
/a
r where r :: a
r = Complex a -> a
forall a. RealFloat a => Complex a -> a
magnitude Complex a
z
fromInteger :: Integer -> Complex a
fromInteger Integer
n = Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
n a -> a -> Complex a
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
xa -> a -> a
forall a. Num a => a -> a -> a
*a
x''a -> a -> a
forall a. Num a => a -> a -> a
+a
ya -> a -> a
forall a. Num a => a -> a -> a
*a
y'') a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
d a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ya -> a -> a
forall a. Num a => a -> a -> a
*a
x''a -> a -> a
forall a. Num a => a -> a -> a
-a
xa -> a -> a
forall a. Num a => a -> a -> a
*a
y'') a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
d
where x'' :: a
x'' = Int -> a -> a
forall a. RealFloat a => Int -> a -> a
scaleFloat Int
k a
x'
y'' :: a
y'' = Int -> a -> a
forall a. RealFloat a => Int -> a -> a
scaleFloat Int
k a
y'
k :: Int
k = - Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (a -> Int
forall a. RealFloat a => a -> Int
exponent a
x') (a -> Int
forall a. RealFloat a => a -> Int
exponent a
y')
d :: a
d = a
x'a -> a -> a
forall a. Num a => a -> a -> a
*a
x'' a -> a -> a
forall a. Num a => a -> a -> a
+ a
y'a -> a -> a
forall a. Num a => a -> a -> a
*a
y''
fromRational :: Rational -> Complex a
fromRational Rational
a = Rational -> a
forall a. Fractional a => Rational -> a
fromRational Rational
a a -> a -> Complex 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 = a
forall a. Floating a => a
pi a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
0
exp :: Complex a -> Complex a
exp (a
x:+a
y) = a
expx a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cos a
y a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
expx a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sin a
y
where expx :: a
expx = a -> a
forall a. Floating a => a -> a
exp a
x
log :: Complex a -> Complex a
log Complex a
z = a -> a
forall a. Floating a => a -> a
log (Complex a -> a
forall a. RealFloat a => Complex a -> a
magnitude Complex a
z) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ Complex a -> 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 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
0
((a
0:+a
0), (a
exp_re:+a
_)) -> case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
exp_re a
0 of
Ordering
GT -> a
0 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
0
Ordering
LT -> a
inf a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
0
Ordering
EQ -> a
nan a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
nan
((a
re:+a
im), (a
exp_re:+a
_))
| (a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
re Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
im) -> case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
exp_re a
0 of
Ordering
GT -> a
inf a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
0
Ordering
LT -> a
0 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
0
Ordering
EQ -> a
nan a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
nan
| Bool
otherwise -> Complex a -> Complex a
forall a. Floating a => a -> a
exp (Complex a -> Complex a
forall a. Floating a => a -> a
log Complex a
x Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
* Complex a
y)
where
inf :: a
inf = a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/a
0
nan :: a
nan = a
0a -> a -> a
forall 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 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (if a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 then -a
v else a
v)
where (a
u,a
v) = if a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 then (a
v',a
u') else (a
u',a
v')
v' :: a
v' = a -> a
forall a. Num a => a -> a
abs a
y a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
u'a -> a -> a
forall a. Num a => a -> a -> a
*a
2)
u' :: a
u' = a -> a
forall a. Floating a => a -> a
sqrt ((Complex a -> a
forall a. RealFloat a => Complex a -> a
magnitude Complex a
z a -> a -> a
forall a. Num a => a -> a -> a
+ a -> a
forall a. Num a => a -> a
abs a
x) a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2)
sin :: Complex a -> Complex a
sin (a
x:+a
y) = a -> a
forall a. Floating a => a -> a
sin a
x a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cosh a
y a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Floating a => a -> a
cos a
x a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sinh a
y
cos :: Complex a -> Complex a
cos (a
x:+a
y) = a -> a
forall a. Floating a => a -> a
cos a
x a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cosh a
y a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (- a -> a
forall a. Floating a => a -> a
sin a
x a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sinh a
y)
tan :: Complex a -> Complex a
tan (a
x:+a
y) = (a
sinxa -> a -> a
forall a. Num a => a -> a -> a
*a
coshya -> a -> Complex a
forall a. a -> a -> Complex a
:+a
cosxa -> a -> a
forall a. Num a => a -> a -> a
*a
sinhy)Complex a -> Complex a -> Complex a
forall a. Fractional a => a -> a -> a
/(a
cosxa -> a -> a
forall a. Num a => a -> a -> a
*a
coshya -> a -> Complex a
forall a. a -> a -> Complex a
:+(-a
sinxa -> a -> a
forall a. Num a => a -> a -> a
*a
sinhy))
where sinx :: a
sinx = a -> a
forall a. Floating a => a -> a
sin a
x
cosx :: a
cosx = a -> a
forall a. Floating a => a -> a
cos a
x
sinhy :: a
sinhy = a -> a
forall a. Floating a => a -> a
sinh a
y
coshy :: a
coshy = a -> a
forall a. Floating a => a -> a
cosh a
y
sinh :: Complex a -> Complex a
sinh (a
x:+a
y) = a -> a
forall a. Floating a => a -> a
cos a
y a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sinh a
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Floating a => a -> a
sin a
y a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cosh a
x
cosh :: Complex a -> Complex a
cosh (a
x:+a
y) = a -> a
forall a. Floating a => a -> a
cos a
y a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
cosh a
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Floating a => a -> a
sin a
y a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sinh a
x
tanh :: Complex a -> Complex a
tanh (a
x:+a
y) = (a
cosya -> a -> a
forall a. Num a => a -> a -> a
*a
sinhxa -> a -> Complex a
forall a. a -> a -> Complex a
:+a
sinya -> a -> a
forall a. Num a => a -> a -> a
*a
coshx)Complex a -> Complex a -> Complex a
forall a. Fractional a => a -> a -> a
/(a
cosya -> a -> a
forall a. Num a => a -> a -> a
*a
coshxa -> a -> Complex a
forall a. a -> a -> Complex a
:+a
sinya -> a -> a
forall a. Num a => a -> a -> a
*a
sinhx)
where siny :: a
siny = a -> a
forall a. Floating a => a -> a
sin a
y
cosy :: a
cosy = a -> a
forall a. Floating a => a -> a
cos a
y
sinhx :: a
sinhx = a -> a
forall a. Floating a => a -> a
sinh a
x
coshx :: a
coshx = a -> a
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'a -> a -> Complex a
forall a. a -> a -> Complex a
:+(-a
x')
where (a
x':+a
y') = Complex a -> Complex a
forall a. Floating a => a -> a
log (((-a
y)a -> a -> Complex a
forall a. a -> a -> Complex a
:+a
x) Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
+ Complex a -> Complex a
forall a. Floating a => a -> a
sqrt (Complex a
1 Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
- Complex a
zComplex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
*Complex a
z))
acos :: Complex a -> Complex a
acos Complex a
z = a
y''a -> a -> Complex a
forall a. a -> a -> Complex a
:+(-a
x'')
where (a
x'':+a
y'') = Complex a -> Complex a
forall a. Floating a => a -> a
log (Complex a
z Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
+ ((-a
y')a -> a -> Complex a
forall a. a -> a -> Complex a
:+a
x'))
(a
x':+a
y') = Complex a -> Complex a
forall a. Floating a => a -> a
sqrt (Complex a
1 Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
- Complex a
zComplex a -> Complex a -> Complex a
forall 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'a -> a -> Complex a
forall a. a -> a -> Complex a
:+(-a
x')
where (a
x':+a
y') = Complex a -> Complex a
forall a. Floating a => a -> a
log (((a
1a -> a -> a
forall a. Num a => a -> a -> a
-a
y)a -> a -> Complex a
forall a. a -> a -> Complex a
:+a
x) Complex a -> Complex a -> Complex a
forall a. Fractional a => a -> a -> a
/ Complex a -> Complex a
forall a. Floating a => a -> a
sqrt (Complex a
1Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
+Complex a
zComplex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
*Complex a
z))
asinh :: Complex a -> Complex a
asinh Complex a
z = Complex a -> Complex a
forall a. Floating a => a -> a
log (Complex a
z Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
+ Complex a -> Complex a
forall a. Floating a => a -> a
sqrt (Complex a
1Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
+Complex a
zComplex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
*Complex a
z))
acosh :: Complex a -> Complex a
acosh Complex a
z = Complex a -> Complex a
forall a. Floating a => a -> a
log (Complex a
z Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
+ (Complex a -> Complex a
forall a. Floating a => a -> a
sqrt (Complex a -> Complex a) -> Complex a -> Complex a
forall a b. (a -> b) -> a -> b
$ Complex a
zComplex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
+Complex a
1) Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
* (Complex a -> Complex a
forall a. Floating a => a -> a
sqrt (Complex a -> Complex a) -> Complex a -> Complex a
forall a b. (a -> b) -> a -> b
$ Complex a
zComplex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
-Complex a
1))
atanh :: Complex a -> Complex a
atanh Complex a
z = Complex a
0.5 Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
* Complex a -> Complex a
forall a. Floating a => a -> a
log ((Complex a
1.0Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
+Complex a
z) Complex a -> Complex a -> Complex a
forall a. Fractional a => a -> a -> a
/ (Complex a
1.0Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
-Complex a
z))
log1p :: Complex a -> Complex a
log1p x :: Complex a
x@(a
a :+ a
b)
| a -> a
forall a. Num a => a -> a
abs a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0.5 Bool -> Bool -> Bool
&& a -> a
forall a. Num a => a -> a
abs a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0.5
, a
u <- a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
a a -> a -> a
forall a. Num a => a -> a -> a
+ a
aa -> a -> a
forall a. Num a => a -> a -> a
*a
a a -> a -> a
forall a. Num a => a -> a -> a
+ a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
b = a -> a
forall a. Floating a => a -> a
log1p (a
ua -> a -> a
forall a. Fractional a => a -> a -> a
/(a
1 a -> a -> a
forall a. Num a => a -> a -> a
+ a -> a
forall a. Floating a => a -> a
sqrt(a
ua -> a -> a
forall a. Num a => a -> a -> a
+a
1))) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a -> a
forall a. RealFloat a => a -> a -> a
atan2 (a
1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
a) a
b
| Bool
otherwise = Complex a -> Complex a
forall a. Floating a => a -> a
log (Complex a
1 Complex a -> Complex a -> Complex a
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
aa -> a -> a
forall a. Num a => a -> a -> a
*a
a a -> a -> a
forall a. Num a => a -> a -> a
+ a
ba -> a -> a
forall a. Num a => a -> a -> a
*a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
1
, a
u <- a -> a
forall a. Floating a => a -> a
expm1 a
a
, a
v <- a -> a
forall a. Floating a => a -> a
sin (a
ba -> a -> a
forall a. Fractional a => a -> a -> a
/a
2)
, a
w <- -a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
va -> a -> a
forall a. Num a => a -> a -> a
*a
v = (a
ua -> a -> a
forall a. Num a => a -> a -> a
*a
w a -> a -> a
forall a. Num a => a -> a -> a
+ a
u a -> a -> a
forall a. Num a => a -> a -> a
+ a
w) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ua -> a -> a
forall a. Num a => a -> a -> a
+a
1)a -> a -> a
forall a. Num a => a -> a -> a
*a -> a
forall a. Floating a => a -> a
sin a
b
| Bool
otherwise = Complex a -> Complex a
forall a. Floating a => a -> a
exp Complex a
x Complex a -> Complex a -> Complex a
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
* a -> Int
forall a. Storable a => a -> Int
sizeOf (Complex a -> a
forall a. Complex a -> a
realPart Complex a
a)
alignment :: Complex a -> Int
alignment Complex a
a = a -> Int
forall a. Storable a => a -> Int
alignment (Complex a -> a
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 <- Ptr a -> IO (Ptr a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr a -> IO (Ptr a)) -> Ptr a -> IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ Ptr (Complex a) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr (Complex a)
p
a
r <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
q
a
i <- Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
q Int
1
Complex a -> IO (Complex a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
r a -> a -> Complex a
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 <-Ptr a -> IO (Ptr a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr a -> IO (Ptr a)) -> Ptr a -> IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ (Ptr (Complex a) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr (Complex a)
p)
Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
q a
r
Ptr a -> Int -> a -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr a
q Int
1 a
i
instance Applicative Complex where
pure :: a -> Complex a
pure a
a = a
a a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
a
a -> b
f :+ a -> b
g <*> :: Complex (a -> b) -> Complex a -> Complex b
<*> a
a :+ a
b = a -> b
f a
a b -> b -> Complex b
forall a. a -> a -> Complex a
:+ a -> b
g a
b
liftA2 :: (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 c -> c -> Complex c
forall a. a -> a -> Complex a
:+ a -> b -> c
f a
y b
b
instance Monad Complex where
a
a :+ a
b >>= :: Complex a -> (a -> Complex b) -> Complex b
>>= a -> Complex b
f = Complex b -> b
forall a. Complex a -> a
realPart (a -> Complex b
f a
a) b -> b -> Complex b
forall a. a -> a -> Complex a
:+ Complex b -> b
forall a. Complex a -> a
imagPart (a -> Complex b
f a
b)
{-# RULES
"realToFrac/a->Complex Double"
realToFrac = \x -> realToFrac x :+ (0 :: Double)
"realToFrac/a->Complex Float"
realToFrac = \x -> realToFrac x :+ (0 :: Float)
#-}