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

-- | 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

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

instance (Subtractive a) => Subtractive (Complex a) where
  negate :: Complex a -> Complex a
negate (a
rx :+ a
ix) = forall a. Subtractive a => a -> a
negate a
rx forall a. a -> a -> Complex 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 forall a. Multiplicative a => a -> a -> a
* a
ry forall a. Subtractive a => a -> a -> a
- a
ix forall a. Multiplicative a => a -> a -> a
* a
iy) forall a. a -> a -> Complex a
:+ (a
ix forall a. Multiplicative a => a -> a -> a
* a
ry forall a. Additive a => a -> a -> a
+ a
iy forall a. Multiplicative a => a -> a -> a
* a
rx)
  one :: Complex a
one = forall a. Multiplicative a => a
one forall a. a -> a -> Complex 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 forall a. Multiplicative a => a -> a -> a
* a
d) forall a. a -> a -> Complex a
:+ (forall a. Subtractive a => a -> a
negate a
ix forall a. Multiplicative a => a -> a -> a
* a
d)
    where
      d :: a
d = forall a. Divisive a => a -> a
recip ((a
rx forall a. Multiplicative a => a -> a -> a
* a
rx) forall a. Additive a => a -> a -> a
+ (a
ix 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 = forall a b. FromIntegral a b => b -> a
fromIntegral b
x forall a. a -> a -> Complex 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) = forall a. ExpField a => a -> a
sqrt (a
rx forall a. Multiplicative a => a -> a -> a
* a
rx forall a. Additive a => a -> a -> a
+ a
ix 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 forall a. Divisive a => a -> a -> a
/ forall a b. Norm a b => a -> b
norm Complex a
x forall a. a -> a -> Complex a
:+ a
ix forall a. Divisive a => a -> 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) = forall a. TrigField a => a -> a -> a
atan2 a
y a
x
  ray :: a -> Complex a
ray a
x = forall a. TrigField a => a -> a
cos a
x forall a. a -> a -> Complex a
:+ forall a. TrigField a => a -> a
sin a
x

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

  nearZero :: Complex a -> Bool
nearZero (a
ar :+ a
ai) = a
ar forall a. Ord a => a -> a -> Bool
<= forall a. Epsilon a => a
epsilon Bool -> Bool -> Bool
&& a
ai forall a. Ord a => a -> a -> Bool
<= forall a. Epsilon a => a
epsilon

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

instance (JoinSemiLattice a) => JoinSemiLattice (Complex a) where
  \/ :: Complex a -> Complex a -> Complex a
(\/) (a
ar :+ a
ai) (a
br :+ a
bi) = (a
ar forall a. JoinSemiLattice a => a -> a -> a
\/ a
br) forall a. a -> a -> Complex a
:+ (a
ai 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 forall a. MeetSemiLattice a => a -> a -> a
/\ a
br) forall a. a -> a -> Complex a
:+ (a
ai forall a. MeetSemiLattice a => a -> a -> a
/\ a
bi)

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

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