{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE Trustworthy #-}
{-# 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 Prelude hiding (Applicative(..))
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  :+

-- -----------------------------------------------------------------------------
-- 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 ( Complex a -> Complex a -> Bool
(Complex a -> Complex a -> Bool)
-> (Complex a -> Complex a -> Bool) -> Eq (Complex a)
forall a. Eq a => Complex a -> Complex a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$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
/= :: Complex a -> Complex a -> Bool
Eq          -- ^ @since 2.01
                 , Int -> Complex a -> ShowS
[Complex a] -> ShowS
Complex a -> String
(Int -> Complex a -> ShowS)
-> (Complex a -> String)
-> ([Complex a] -> ShowS)
-> Show (Complex a)
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
$cshowsPrec :: forall a. Show a => Int -> Complex a -> ShowS
showsPrec :: Int -> Complex a -> ShowS
$cshow :: forall a. Show a => Complex a -> String
show :: Complex a -> String
$cshowList :: forall a. Show a => [Complex a] -> ShowS
showList :: [Complex a] -> ShowS
Show        -- ^ @since 2.01
                 , ReadPrec [Complex a]
ReadPrec (Complex a)
Int -> ReadS (Complex a)
ReadS [Complex a]
(Int -> ReadS (Complex a))
-> ReadS [Complex a]
-> ReadPrec (Complex a)
-> ReadPrec [Complex a]
-> Read (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
$creadsPrec :: forall a. Read a => Int -> ReadS (Complex a)
readsPrec :: Int -> ReadS (Complex a)
$creadList :: forall a. Read a => ReadS [Complex a]
readList :: ReadS [Complex a]
$creadPrec :: forall a. Read a => ReadPrec (Complex a)
readPrec :: ReadPrec (Complex a)
$creadListPrec :: forall a. Read a => ReadPrec [Complex a]
readListPrec :: ReadPrec [Complex a]
Read        -- ^ @since 2.01
                 , Typeable (Complex a)
Typeable (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 (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Complex a))
-> (Complex a -> Constr)
-> (Complex a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Complex a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Complex a)))
-> ((forall b. Data b => b -> b) -> Complex a -> Complex a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Complex a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Complex a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Complex a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> Complex a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Complex a -> m (Complex a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Complex a -> m (Complex a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Complex a -> m (Complex a))
-> Data (Complex a)
Complex a -> Constr
Complex a -> DataType
(forall b. Data b => b -> b) -> Complex a -> Complex a
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 u. Int -> (forall d. Data d => d -> u) -> Complex a -> u
forall u. (forall d. Data d => d -> u) -> Complex a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Complex a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Complex a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex 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))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (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)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Complex a -> 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)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Complex a)
$ctoConstr :: forall a. Data a => Complex a -> Constr
toConstr :: Complex a -> Constr
$cdataTypeOf :: forall a. Data a => Complex a -> DataType
dataTypeOf :: Complex a -> DataType
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Complex a))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> 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))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Complex a))
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Complex a -> Complex a
gmapT :: (forall b. Data b => b -> b) -> Complex a -> Complex a
$cgmapQl :: 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
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Complex a -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Complex a -> r
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Complex a -> [u]
gmapQ :: forall u. (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
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Complex a -> u
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad 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)
$cgmapMp :: 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)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Complex a -> m (Complex a)
Data        -- ^ @since 2.01
                 , (forall x. Complex a -> Rep (Complex a) x)
-> (forall x. Rep (Complex a) x -> Complex a)
-> Generic (Complex a)
forall x. Rep (Complex a) x -> Complex a
forall x. Complex a -> Rep (Complex a) x
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
$cfrom :: forall a x. Complex a -> Rep (Complex a) x
from :: forall x. Complex a -> Rep (Complex a) x
$cto :: forall a x. Rep (Complex a) x -> Complex a
to :: forall x. Rep (Complex a) x -> Complex a
Generic     -- ^ @since 4.9.0.0
                 , (forall a. Complex a -> Rep1 Complex a)
-> (forall a. Rep1 Complex a -> Complex a) -> Generic1 Complex
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
$cfrom1 :: forall a. Complex a -> Rep1 Complex a
from1 :: forall a. Complex a -> Rep1 Complex a
$cto1 :: forall a. Rep1 Complex a -> Complex a
to1 :: forall a. Rep1 Complex a -> Complex a
Generic1    -- ^ @since 4.9.0.0
                 , (forall a b. (a -> b) -> Complex a -> Complex b)
-> (forall a b. a -> Complex b -> Complex a) -> Functor Complex
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
$cfmap :: forall a b. (a -> b) -> Complex a -> Complex b
fmap :: forall a b. (a -> b) -> Complex a -> Complex b
$c<$ :: forall a b. a -> Complex b -> Complex a
<$ :: forall a b. a -> Complex b -> Complex a
Functor     -- ^ @since 4.9.0.0
                 , (forall m. Monoid m => Complex m -> m)
-> (forall m a. Monoid m => (a -> m) -> Complex a -> m)
-> (forall m a. Monoid m => (a -> m) -> Complex a -> m)
-> (forall a b. (a -> b -> b) -> b -> Complex a -> b)
-> (forall a b. (a -> b -> b) -> b -> Complex a -> b)
-> (forall b a. (b -> a -> b) -> b -> Complex a -> b)
-> (forall b a. (b -> a -> b) -> b -> Complex a -> b)
-> (forall a. (a -> a -> a) -> Complex a -> a)
-> (forall a. (a -> a -> a) -> Complex a -> a)
-> (forall a. Complex a -> [a])
-> (forall a. Complex a -> Bool)
-> (forall a. Complex a -> Int)
-> (forall a. Eq a => a -> Complex a -> Bool)
-> (forall a. Ord a => Complex a -> a)
-> (forall a. Ord a => Complex a -> a)
-> (forall a. Num a => Complex a -> a)
-> (forall a. Num a => Complex a -> a)
-> Foldable Complex
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
$cfold :: forall m. Monoid m => Complex m -> m
fold :: forall m. Monoid m => Complex m -> 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
foldMap' :: forall m a. Monoid m => (a -> m) -> Complex a -> m
$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
foldr' :: forall a b. (a -> b -> 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
foldl' :: forall b a. (b -> a -> b) -> b -> Complex a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Complex a -> a
foldr1 :: forall a. (a -> a -> a) -> Complex a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Complex a -> a
foldl1 :: forall a. (a -> a -> a) -> Complex a -> a
$ctoList :: forall a. Complex a -> [a]
toList :: forall a. Complex a -> [a]
$cnull :: forall a. Complex a -> Bool
null :: forall a. Complex a -> Bool
$clength :: forall a. Complex a -> Int
length :: forall a. Complex a -> Int
$celem :: forall a. Eq a => a -> Complex a -> Bool
elem :: forall a. Eq a => a -> Complex a -> Bool
$cmaximum :: forall a. Ord a => Complex a -> a
maximum :: forall a. Ord a => Complex a -> a
$cminimum :: forall a. Ord a => Complex a -> a
minimum :: forall a. Ord a => Complex a -> a
$csum :: forall a. Num a => Complex a -> a
sum :: forall a. Num a => Complex a -> a
$cproduct :: forall a. Num a => Complex a -> a
product :: forall a. Num a => Complex a -> a
Foldable    -- ^ @since 4.9.0.0
                 , Functor Complex
Foldable Complex
(Functor Complex, Foldable Complex) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Complex a -> f (Complex b))
-> (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 (m :: * -> *) a.
    Monad m =>
    Complex (m a) -> m (Complex a))
-> Traversable 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)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Complex a -> f (Complex b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Complex a -> f (Complex b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Complex (f a) -> f (Complex a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Complex (f a) -> f (Complex a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Complex a -> m (Complex b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Complex a -> m (Complex b)
$csequence :: forall (m :: * -> *) a. Monad m => Complex (m a) -> m (Complex a)
sequence :: forall (m :: * -> *) a. Monad m => Complex (m a) -> m (Complex a)
Traversable -- ^ @since 4.9.0.0
                 )

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

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

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

-- | The conjugate of a complex number.
{-# 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 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 :: forall a. Floating a => 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

-- | @'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 :: forall a. Floating a => 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

-- | The function 'polar' takes a complex number and
-- returns a (magnitude, phase) pair in canonical form:
-- the magnitude is non-negative, 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 :: forall a. RealFloat a => 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)

-- | The non-negative magnitude of a complex number.
{-# SPECIALISE magnitude :: Complex Double -> Double #-}
magnitude :: (RealFloat a) => Complex a -> a
magnitude :: forall a. RealFloat a => 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

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

-- | @since 2.01
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

-- | @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
:+ 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))
    -- Take care to allow (-1)::Complex, fixing #8532
    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 #-}

-- | @since 4.8.0.0
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 a. a -> IO 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 a. a -> IO 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 a. a -> IO 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

-- | @since 4.9.0.0
instance Applicative Complex where
  pure :: forall a. 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 <*> :: forall a b. 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 :: 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 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 >>= :: forall 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)

-- | @since 4.15.0.0
instance MonadZip Complex where
  mzipWith :: forall a b c. (a -> b -> c) -> Complex a -> Complex b -> Complex c
mzipWith = (a -> b -> c) -> Complex a -> Complex b -> Complex c
forall a b c. (a -> b -> c) -> Complex a -> Complex b -> Complex c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2

-- | @since 4.15.0.0
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) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (let a
_ :+ a
a = a -> Complex a
f a
a in a
a)

-- -----------------------------------------------------------------------------
-- Rules on Complex

{-# RULES

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

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

  #-}