{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RebindableSyntax #-}
{-# OPTIONS_GHC -Wall #-}

-- | Complex numbers.
module NumHask.Data.Complex
  ( Complex (..),
    realPart,
    imagPart,
  )
where

import Data.Data (Data)
import GHC.Generics (Generic, Generic1)
import NumHask.Algebra.Additive
import NumHask.Algebra.Field
import NumHask.Algebra.Lattice
import NumHask.Algebra.Metric
import NumHask.Algebra.Multiplicative
import NumHask.Algebra.Ring
import NumHask.Data.Integral
import Prelude hiding
  ( Num (..),
    atan,
    atan2,
    cos,
    exp,
    fromIntegral,
    log,
    negate,
    pi,
    recip,
    sin,
    sqrt,
    (/),
  )
import qualified Prelude as P (Ord (..), otherwise, (&&), (<), (<=), (==), (>))

-- -----------------------------------------------------------------------------
-- The Complex type
infix 6 :+

-- | Complex numbers have real and imaginary parts.
--
-- The 'Data.Foldable.Foldable' and 'Data.Traversable.Traversable' instances traverse the real part first.
data Complex a
  = -- | forms a complex number from its real and imaginary
    -- rectangular components.
    !a :+ !a
  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
/= :: 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
[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
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)
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
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,
      Typeable (Complex a)
DataType
Constr
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 -> DataType
Complex a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (Complex a))
(forall b. Data b => b -> b) -> Complex a -> Complex a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Complex a -> c (Complex a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Complex a)
forall a. Data a => Typeable (Complex a)
forall a. Data a => Complex a -> DataType
forall a. Data a => Complex a -> Constr
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))
$c:+ :: Constr
$tComplex :: DataType
gmapMo :: (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 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 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 :: 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 d. Data d => d -> u) -> Complex a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Complex a -> [u]
gmapQr :: (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 :: (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 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 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 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 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)
$cp1Data :: forall a. Data a => Typeable (Complex a)
Data,
      (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
$cto :: forall a x. Rep (Complex a) x -> Complex a
$cfrom :: forall a x. Complex a -> Rep (Complex a) x
Generic,
      (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
$cto1 :: forall a. Rep1 Complex a -> Complex a
$cfrom1 :: forall a. Complex a -> Rep1 Complex a
Generic1,
      a -> Complex b -> Complex a
(a -> b) -> Complex a -> Complex b
(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
<$ :: a -> Complex b -> Complex a
$c<$ :: forall a b. a -> Complex b -> Complex a
fmap :: (a -> b) -> Complex a -> Complex b
$cfmap :: forall a b. (a -> b) -> Complex a -> Complex b
Functor,
      Complex a -> Bool
(a -> m) -> Complex a -> m
(a -> b -> b) -> b -> Complex a -> b
(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
product :: Complex a -> a
$cproduct :: forall a. Num a => Complex a -> a
sum :: Complex a -> a
$csum :: forall a. Num a => Complex a -> a
minimum :: Complex a -> a
$cminimum :: forall a. Ord a => Complex a -> a
maximum :: Complex a -> a
$cmaximum :: forall a. Ord a => Complex a -> a
elem :: a -> Complex a -> Bool
$celem :: forall a. Eq a => a -> Complex a -> Bool
length :: Complex a -> Int
$clength :: forall a. Complex a -> Int
null :: Complex a -> Bool
$cnull :: forall a. Complex a -> Bool
toList :: Complex a -> [a]
$ctoList :: forall a. Complex a -> [a]
foldl1 :: (a -> a -> a) -> Complex a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Complex a -> a
foldr1 :: (a -> a -> a) -> Complex a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Complex a -> a
foldl' :: (b -> a -> b) -> b -> Complex a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Complex a -> b
foldl :: (b -> a -> b) -> b -> Complex a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Complex a -> b
foldr' :: (a -> b -> b) -> b -> Complex a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Complex a -> b
foldr :: (a -> b -> b) -> b -> Complex a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Complex a -> b
foldMap' :: (a -> m) -> Complex a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Complex a -> m
foldMap :: (a -> m) -> Complex a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Complex a -> m
fold :: Complex m -> m
$cfold :: forall m. Monoid m => Complex m -> m
Foldable,
      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
(a -> f b) -> Complex a -> f (Complex b)
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 :: Complex (m a) -> m (Complex a)
$csequence :: forall (m :: * -> *) a. Monad m => Complex (m a) -> m (Complex a)
mapM :: (a -> m b) -> Complex a -> m (Complex b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Complex a -> m (Complex b)
sequenceA :: Complex (f a) -> f (Complex a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Complex (f a) -> f (Complex a)
traverse :: (a -> f b) -> Complex a -> f (Complex b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Complex a -> f (Complex b)
$cp2Traversable :: Foldable Complex
$cp1Traversable :: Functor Complex
Traversable
    )

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

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

instance (Additive a) => Additive (Complex a) where
  (a
rx :+ a
ix) + :: Complex a -> Complex a -> Complex a
+ (a
ry :+ a
iy) = (a
rx a -> a -> a
forall a. Additive a => a -> a -> a
+ a
ry) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ix a -> a -> a
forall a. Additive a => a -> a -> a
+ a
iy)
  zero :: Complex a
zero = a
forall a. Additive a => a
zero a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. Additive a => a
zero

instance (Subtractive a) => Subtractive (Complex a) where
  negate :: Complex a -> Complex a
negate (a
rx :+ a
ix) = a -> a
forall a. Subtractive a => a -> a
negate a
rx a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Subtractive a => a -> a
negate a
ix

instance
  (Distributive a, Subtractive a) =>
  Distributive (Complex a)

instance
  (Subtractive a, Multiplicative a) =>
  Multiplicative (Complex a)
  where
  (a
rx :+ a
ix) * :: Complex a -> Complex a -> Complex a
* (a
ry :+ a
iy) =
    (a
rx a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
ry a -> a -> a
forall a. Subtractive a => a -> a -> a
- a
ix a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
iy) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ix a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
ry a -> a -> a
forall a. Additive a => a -> a -> a
+ a
iy a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
rx)
  one :: Complex a
one = a
forall a. Multiplicative a => a
one a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. Additive a => a
zero

instance
  (Subtractive a, Divisive a) =>
  Divisive (Complex a)
  where
  recip :: Complex a -> Complex a
recip (a
rx :+ a
ix) = (a
rx a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
d) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a -> a
forall a. Subtractive a => a -> a
negate a
ix a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
d)
    where
      d :: a
d = a -> a
forall a. Divisive a => a -> a
recip ((a
rx a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
rx) a -> a -> a
forall a. Additive a => a -> a -> a
+ (a
ix a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
ix))

instance
  (Additive a, FromIntegral a b) =>
  FromIntegral (Complex a) b
  where
  fromIntegral :: b -> Complex a
fromIntegral b
x = b -> a
forall a b. FromIntegral a b => b -> a
fromIntegral b
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. Additive a => a
zero

-- | A euclidean-style norm is strong convention for Complex.
instance
  (ExpField a) =>
  Norm (Complex a) a
  where
  norm :: Complex a -> a
norm (a
rx :+ a
ix) = a -> a
forall a. ExpField a => a -> a
sqrt (a
rx a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
rx a -> a -> a
forall a. Additive a => a -> a -> a
+ a
ix a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
ix)
  basis :: Complex a -> Complex a
basis x :: Complex a
x@(a
rx :+ a
ix) = a
rx a -> a -> a
forall a. Divisive a => a -> a -> a
/ Complex a -> a
forall a b. Norm a b => a -> b
norm Complex a
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
ix a -> a -> a
forall a. Divisive a => a -> a -> a
/ Complex a -> a
forall a b. Norm a b => a -> b
norm Complex a
x

instance (TrigField a) => Direction (Complex a) a where
  angle :: Complex a -> a
angle (a
x :+ a
y) = a -> a -> a
forall a. TrigField a => a -> a -> a
atan2 a
y a
x
  ray :: a -> Complex a
ray a
x = a -> a
forall a. TrigField a => a -> a
cos a
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. TrigField a => a -> a
sin a
x

instance
  (Ord a, Signed a, Subtractive a, Epsilon a) =>
  Epsilon (Complex a)
  where
  epsilon :: Complex a
epsilon = a
forall a. Epsilon a => a
epsilon a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. Epsilon a => a
epsilon
  nearZero :: Complex a -> Bool
nearZero (a
a :+ a
b) = a -> Bool
forall a. Epsilon a => a -> Bool
nearZero a
a Bool -> Bool -> Bool
&& a -> Bool
forall a. Epsilon a => a -> Bool
nearZero a
b

instance (Field a) => Field (Complex a)

instance (Ord a, TrigField a, ExpField a) => ExpField (Complex a) where
  exp :: Complex a -> Complex a
exp (a
rx :+ a
ix) = (a -> a
forall a. ExpField a => a -> a
exp a
rx a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a -> a
forall a. TrigField a => a -> a
cos a
ix) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a -> a
forall a. ExpField a => a -> a
exp a
rx a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a -> a
forall a. TrigField a => a -> a
sin a
ix)
  log :: Complex a -> Complex a
log (a
rx :+ a
ix) = a -> a
forall a. ExpField a => a -> a
log (a -> a
forall a. ExpField a => a -> a
sqrt (a
rx a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
rx a -> a -> a
forall a. Additive a => a -> a -> a
+ a
ix a -> a -> a
forall a. Multiplicative a => a -> a -> a
* a
ix)) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a -> a
forall a. (Ord a, TrigField a) => a -> a -> a
atan2' a
ix a
rx
    where
      atan2' :: a -> a -> a
atan2' a
y a
x
        | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
P.> a
forall a. Additive a => a
zero = a -> a
forall a. TrigField a => a -> a
atan (a
y a -> a -> a
forall a. Divisive a => a -> a -> a
/ a
x)
        | a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
P.== a
forall a. Additive a => a
zero Bool -> Bool -> Bool
P.&& a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
P.> a
forall a. Additive a => a
zero = a
forall a. TrigField a => a
pi a -> a -> a
forall a. Divisive a => a -> a -> a
/ (a
forall a. Multiplicative a => a
one a -> a -> a
forall a. Additive a => a -> a -> a
+ a
forall a. Multiplicative a => a
one)
        | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
P.< a
forall a. Multiplicative a => a
one Bool -> Bool -> Bool
P.&& a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
P.> a
forall a. Multiplicative a => a
one = a
forall a. TrigField a => a
pi a -> a -> a
forall a. Additive a => a -> a -> a
+ a -> a
forall a. TrigField a => a -> a
atan (a
y a -> a -> a
forall a. Divisive a => a -> a -> a
/ a
x)
        | (a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
P.<= a
forall a. Additive a => a
zero Bool -> Bool -> Bool
P.&& a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
P.< a
forall a. Additive a => a
zero) Bool -> Bool -> Bool
|| (a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
P.< a
forall a. Additive a => a
zero) =
          a -> a
forall a. Subtractive a => a -> a
negate (a -> a -> a
atan2' (a -> a
forall a. Subtractive a => a -> a
negate a
y) a
x)
        | a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
P.== a
forall a. Additive a => a
zero = a
forall a. TrigField a => a
pi -- must be after the previous test on zero y
        | a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
P.== a
forall a. Additive a => a
zero Bool -> Bool -> Bool
P.&& a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
P.== a
forall a. Additive a => a
zero = a
y -- must be after the other double zero tests
        | Bool
P.otherwise = a
x a -> a -> a
forall a. Additive a => a -> a -> a
+ a
y -- x or y is a NaN, return a NaN (via +)

instance (Distributive a, Subtractive a) => InvolutiveRing (Complex a) where
  adj :: Complex a -> Complex a
adj (a
a :+ a
b) = a
a a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. Subtractive a => a -> a
negate a
b

instance (UpperBoundedField a, Subtractive a) => UpperBoundedField (Complex a)

instance (LowerBoundedField a) => LowerBoundedField (Complex a)

instance (JoinSemiLattice a) => JoinSemiLattice (Complex a) where
  \/ :: Complex a -> Complex a -> Complex a
(\/) (a
ar :+ a
ai) (a
br :+ a
bi) = (a
ar a -> a -> a
forall a. JoinSemiLattice a => a -> a -> a
\/ a
br) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ai a -> a -> a
forall a. JoinSemiLattice a => a -> a -> a
\/ a
bi)

instance (MeetSemiLattice a) => MeetSemiLattice (Complex a) where
  /\ :: Complex a -> Complex a -> Complex a
(/\) (a
ar :+ a
ai) (a
br :+ a
bi) = (a
ar a -> a -> a
forall a. MeetSemiLattice a => a -> a -> a
/\ a
br) a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ai a -> a -> a
forall a. MeetSemiLattice a => a -> a -> a
/\ a
bi)

instance (BoundedJoinSemiLattice a) => BoundedJoinSemiLattice (Complex a) where
  bottom :: Complex a
bottom = a
forall a. BoundedJoinSemiLattice a => a
bottom a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. BoundedJoinSemiLattice a => a
bottom

instance (BoundedMeetSemiLattice a) => BoundedMeetSemiLattice (Complex a) where
  top :: Complex a
top = a
forall a. BoundedMeetSemiLattice a => a
top a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. BoundedMeetSemiLattice a => a
top