{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Complex
-- Copyright   :  (c) The University of Glasgow 2001
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  provisional
-- Portability :  portable
--
-- Complex numbers.
--
-----------------------------------------------------------------------------

module Data.Complex
        (
        -- * Rectangular form
          Complex((:+))

        , realPart
        , imagPart
        -- * Polar form
        , mkPolar
        , cis
        , polar
        , magnitude
        , phase
        -- * Conjugate
        , 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  :+

-- -----------------------------------------------------------------------------
-- The Complex type

-- | 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.
--
-- The 'Foldable' and 'Traversable' instances traverse the real part first.
--
-- Note that `Complex`'s instances inherit the deficiencies from the type
-- parameter's. For example, @Complex Float@'s 'Ord' instance has similar
-- problems to `Float`'s.
data Complex a
  = !a :+ !a    -- ^ forms a complex number from its real and imaginary
                -- rectangular components.
        deriving ( Eq          -- ^ @since 2.01
                 , Show        -- ^ @since 2.01
                 , Read        -- ^ @since 2.01
                 , Data        -- ^ @since 2.01
                 , Generic     -- ^ @since 4.9.0.0
                 , Generic1    -- ^ @since 4.9.0.0
                 , Functor     -- ^ @since 4.9.0.0
                 , Foldable    -- ^ @since 4.9.0.0
                 , Traversable -- ^ @since 4.9.0.0
                 )

-- -----------------------------------------------------------------------------
-- Functions over Complex

-- | Extracts the real part of a complex number.
realPart :: Complex a -> a
realPart :: Complex a -> a
realPart (x :: a
x :+ _) =  a
x

-- | Extracts the imaginary part of a complex number.
imagPart :: Complex a -> a
imagPart :: Complex a -> a
imagPart (_ :+ y :: a
y) =  a
y

-- | The conjugate of a complex number.
{-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
conjugate        :: Num a => Complex a -> Complex a
conjugate :: Complex a -> Complex a
conjugate (x :: a
x:+y :: a
y) =  a
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (-a
y)

-- | Form a complex number from polar components of magnitude and phase.
{-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
mkPolar          :: Floating a => a -> a -> Complex a
mkPolar :: a -> a -> Complex a
mkPolar r :: a
r theta :: 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

-- | @'cis' t@ is a complex value with magnitude @1@
-- and phase @t@ (modulo @2*'pi'@).
{-# SPECIALISE cis :: Double -> Complex Double #-}
cis              :: Floating a => a -> Complex a
cis :: a -> Complex a
cis theta :: 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

-- | 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 :: Complex a -> (a, a)
polar z :: 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)

-- | The nonnegative magnitude of a complex number.
{-# SPECIALISE magnitude :: Complex Double -> Double #-}
magnitude :: (RealFloat a) => Complex a -> a
magnitude :: Complex a -> a
magnitude (x :: a
x:+y :: 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 z :: a
z = a
z a -> a -> a
forall a. Num a => a -> a -> a
* a
z

-- | 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 :: Complex a -> a
phase (0 :+ 0)   = 0            -- SLPJ July 97 from John Peterson
phase (x :: a
x:+y :: a
y)     = a -> a -> a
forall a. RealFloat a => a -> a -> a
atan2 a
y a
x


-- -----------------------------------------------------------------------------
-- Instances of Complex

-- | @since 2.01
instance  (RealFloat a) => Num (Complex a)  where
    {-# SPECIALISE instance Num (Complex Float) #-}
    {-# SPECIALISE instance Num (Complex Double) #-}
    (x :: a
x:+y :: a
y) + :: Complex a -> Complex a -> Complex a
+ (x' :: a
x':+y' :: 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')
    (x :: a
x:+y :: a
y) - :: Complex a -> Complex a -> Complex a
- (x' :: a
x':+y' :: 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')
    (x :: a
x:+y :: a
y) * :: Complex a -> Complex a -> Complex a
* (x' :: a
x':+y' :: 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 (x :: a
x:+y :: 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 z :: 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
:+ 0
    signum :: Complex a -> Complex a
signum (0:+0)       =  0
    signum z :: Complex a
z@(x :: a
x:+y :: 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 n :: Integer
n       =  Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
n a -> a -> Complex a
forall a. a -> a -> Complex a
:+ 0

-- | @since 2.01
instance  (RealFloat a) => Fractional (Complex a)  where
    {-# SPECIALISE instance Fractional (Complex Float) #-}
    {-# SPECIALISE instance Fractional (Complex Double) #-}
    (x :: a
x:+y :: a
y) / :: Complex a -> Complex a -> Complex a
/ (x' :: a
x':+y' :: 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 a :: Rational
a      =  Rational -> a
forall a. Fractional a => Rational -> a
fromRational Rational
a a -> a -> Complex a
forall a. a -> a -> Complex a
:+ 0

-- | @since 2.01
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
:+ 0
    exp :: Complex a -> Complex a
exp (x :: a
x:+y :: 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 z :: 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

    x :: Complex a
x ** :: Complex a -> Complex a -> Complex a
** y :: Complex a
y = case (Complex a
x,Complex a
y) of
      (_ , (0:+0))  -> 1 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ 0
      ((0:+0), (exp_re :: a
exp_re:+_)) -> case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
exp_re 0 of
                 GT -> 0 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ 0
                 LT -> a
inf a -> a -> Complex a
forall a. a -> a -> Complex a
:+ 0
                 EQ -> a
nan a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
nan
      ((re :: a
re:+im :: a
im), (exp_re :: a
exp_re:+_))
        | (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 0 of
                 GT -> a
inf a -> a -> Complex a
forall a. a -> a -> Complex a
:+ 0
                 LT -> 0 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ 0
                 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 = 1a -> a -> a
forall a. Fractional a => a -> a -> a
/0
        nan :: a
nan = 0a -> a -> a
forall a. Fractional a => a -> a -> a
/0

    sqrt :: Complex a -> Complex a
sqrt (0:+0)    =  0
    sqrt z :: Complex a
z@(x :: a
x:+y :: 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
< 0 then -a
v else a
v)
                      where (u :: a
u,v :: a
v) = if a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< 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
*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
/ 2)

    sin :: Complex a -> Complex a
sin (x :: a
x:+y :: 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 (x :: a
x:+y :: 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 (x :: a
x:+y :: 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 (x :: a
x:+y :: 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 (x :: a
x:+y :: 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 (x :: a
x:+y :: 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@(x :: a
x:+y :: a
y)  =  a
y'a -> a -> Complex a
forall a. a -> a -> Complex a
:+(-a
x')
                      where  (x' :: a
x':+y' :: 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 (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 z :: Complex a
z         =  a
y''a -> a -> Complex a
forall a. a -> a -> Complex a
:+(-a
x'')
                      where (x'' :: a
x'':+y'' :: 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'))
                            (x' :: a
x':+y' :: a
y')   = Complex a -> Complex a
forall a. Floating a => a -> a
sqrt (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@(x :: a
x:+y :: a
y)  =  a
y'a -> a -> Complex a
forall a. a -> a -> Complex a
:+(-a
x')
                      where (x' :: a
x':+y' :: a
y') = Complex a -> Complex a
forall a. Floating a => a -> a
log (((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 (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 z :: 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 (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))
    -- Take care to allow (-1)::Complex, fixing #8532
    acosh :: Complex a -> Complex a
acosh z :: 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
+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
-1))
    atanh :: Complex a -> Complex a
atanh z :: Complex a
z        =  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 ((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
/ (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
b)
      | a -> a
forall a. Num a => a -> a
abs a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< 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
< 0.5
      , a
u <- 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
/(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
+1))) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a -> a
forall a. RealFloat a => a -> a -> a
atan2 (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 (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
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
< 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
/2)
      , a
w <- -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
+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
- 1
    {-# INLINE expm1 #-}

-- | @since 4.8.0.0
instance Storable a => Storable (Complex a) where
    sizeOf :: Complex a -> Int
sizeOf a :: Complex a
a       = 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 a :: 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 p :: 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 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 p :: Ptr (Complex a)
p (r :: a
r :+ i :: 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 1 a
i

-- | @since 4.9.0.0
instance Applicative Complex where
  pure :: a -> Complex a
pure a :: a
a = a
a a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
a
  f :: a -> b
f :+ g :: a -> b
g <*> :: Complex (a -> b) -> Complex a -> Complex b
<*> a :: a
a :+ b :: 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 f :: a -> b -> c
f (x :: a
x :+ y :: a
y) (a :: b
a :+ b :: 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

-- | @since 4.9.0.0
instance Monad Complex where
  a :: a
a :+ b :: a
b >>= :: Complex a -> (a -> Complex b) -> Complex b
>>= f :: 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 on Complex

{-# RULES

"realToFrac/a->Complex Double"
  realToFrac = \x -> realToFrac x :+ (0 :: Double)

"realToFrac/a->Complex Float"
  realToFrac = \x -> realToFrac x :+ (0 :: Float)

  #-}