{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

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

import Data.Data (Data)
import GHC.Generics
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,
    ceiling,
    cos,
    exp,
    floor,
    fromIntegral,
    log,
    negate,
    pi,
    properFraction,
    recip,
    round,
    sin,
    sqrt,
    truncate,
    (/),
  )

-- | The underlying representation is a newtype-wrapped tuple, compared with the base datatype. This was chosen to facilitate the use of DerivingVia.
newtype Complex a = Complex {forall a. Complex a -> (a, a)
complexPair :: (a, a)}
  deriving stock
    ( 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 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
    )
  deriving
    ( Complex a
Complex a -> Complex a -> Complex a
forall a. Additive a => Complex a
forall a. Additive a => Complex a -> Complex a -> Complex a
forall a. (a -> a -> a) -> a -> Additive a
zero :: Complex a
$czero :: forall a. Additive a => Complex a
+ :: Complex a -> Complex a -> Complex a
$c+ :: forall a. Additive a => Complex a -> Complex a -> Complex a
Additive,
      Complex a -> Complex a
Complex a -> Complex a -> Complex a
forall {a}. Subtractive a => Additive (Complex a)
forall a. Subtractive a => Complex a -> Complex a
forall a. Subtractive a => Complex a -> Complex a -> Complex a
forall a. Additive a -> (a -> a) -> (a -> a -> a) -> Subtractive a
- :: Complex a -> Complex a -> Complex a
$c- :: forall a. Subtractive a => Complex a -> Complex a -> Complex a
negate :: Complex a -> Complex a
$cnegate :: forall a. Subtractive a => Complex a -> Complex a
Subtractive,
      Complex a -> Mag (Complex a)
Complex a -> Base (Complex a)
forall a.
Distributive (Mag a) -> (a -> Mag a) -> (a -> Base a) -> Basis a
forall {a}. (ExpField a, Eq a) => Distributive (Mag (Complex a))
forall a. (ExpField a, Eq a) => Complex a -> Mag (Complex a)
forall a. (ExpField a, Eq a) => Complex a -> Base (Complex a)
basis :: Complex a -> Base (Complex a)
$cbasis :: forall a. (ExpField a, Eq a) => Complex a -> Base (Complex a)
magnitude :: Complex a -> Mag (Complex a)
$cmagnitude :: forall a. (ExpField a, Eq a) => Complex a -> Mag (Complex a)
Basis,
      Dir (Complex a) -> Complex a
Complex a -> Dir (Complex a)
forall coord.
Distributive coord
-> Distributive (Dir coord)
-> (coord -> Dir coord)
-> (Dir coord -> coord)
-> Direction coord
forall {a}. TrigField a => Distributive (Dir (Complex a))
forall {a}. TrigField a => Distributive (Complex a)
forall a. TrigField a => Dir (Complex a) -> Complex a
forall a. TrigField a => Complex a -> Dir (Complex a)
ray :: Dir (Complex a) -> Complex a
$cray :: forall a. TrigField a => Dir (Complex a) -> Complex a
angle :: Complex a -> Dir (Complex a)
$cangle :: forall a. TrigField a => Complex a -> Dir (Complex a)
Direction,
      Complex a
forall a. Eq a -> Additive a -> a -> Epsilon a
forall {a}. Epsilon a => Eq (Complex a)
forall {a}. Epsilon a => Additive (Complex a)
forall a. Epsilon a => Complex a
epsilon :: Complex a
$cepsilon :: forall a. Epsilon a => Complex a
Epsilon,
      Complex a -> Complex a -> Complex a
forall a. Eq a -> (a -> a -> a) -> JoinSemiLattice a
forall {a}. JoinSemiLattice a => Eq (Complex a)
forall a. JoinSemiLattice a => Complex a -> Complex a -> Complex a
\/ :: Complex a -> Complex a -> Complex a
$c\/ :: forall a. JoinSemiLattice a => Complex a -> Complex a -> Complex a
JoinSemiLattice,
      Complex a -> Complex a -> Complex a
forall a. Eq a -> (a -> a -> a) -> MeetSemiLattice a
forall {a}. MeetSemiLattice a => Eq (Complex a)
forall a. MeetSemiLattice a => Complex a -> Complex a -> Complex a
/\ :: Complex a -> Complex a -> Complex a
$c/\ :: forall a. MeetSemiLattice a => Complex a -> Complex a -> Complex a
MeetSemiLattice,
      Complex a
forall {a}. BoundedJoinSemiLattice a => JoinSemiLattice (Complex a)
forall a. BoundedJoinSemiLattice a => Complex a
forall a. JoinSemiLattice a -> a -> BoundedJoinSemiLattice a
bottom :: Complex a
$cbottom :: forall a. BoundedJoinSemiLattice a => Complex a
BoundedJoinSemiLattice,
      Complex a
forall {a}. BoundedMeetSemiLattice a => MeetSemiLattice (Complex a)
forall a. BoundedMeetSemiLattice a => Complex a
forall a. MeetSemiLattice a -> a -> BoundedMeetSemiLattice a
top :: Complex a
$ctop :: forall a. BoundedMeetSemiLattice a => Complex a
BoundedMeetSemiLattice,
      Complex a -> Complex a
Complex a -> Complex a -> Complex a
forall {a}. (Ord a, TrigField a, ExpField a) => Field (Complex a)
forall a.
(Ord a, TrigField a, ExpField a) =>
Complex a -> Complex a
forall a.
(Ord a, TrigField a, ExpField a) =>
Complex a -> Complex a -> Complex a
forall a.
Field a
-> (a -> a)
-> (a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> ExpField a
sqrt :: Complex a -> Complex a
$csqrt :: forall a.
(Ord a, TrigField a, ExpField a) =>
Complex a -> Complex a
logBase :: Complex a -> Complex a -> Complex a
$clogBase :: forall a.
(Ord a, TrigField a, ExpField a) =>
Complex a -> Complex a -> Complex a
** :: Complex a -> Complex a -> Complex a
$c** :: forall a.
(Ord a, TrigField a, ExpField a) =>
Complex a -> Complex a -> Complex a
log :: Complex a -> Complex a
$clog :: forall a.
(Ord a, TrigField a, ExpField a) =>
Complex a -> Complex a
exp :: Complex a -> Complex a
$cexp :: forall a.
(Ord a, TrigField a, ExpField a) =>
Complex a -> Complex a
ExpField
    )
    via (EuclideanPair a)

infixl 6 +:

-- | Complex number constructor.
(+:) :: a -> a -> Complex a
+: :: forall a. a -> a -> Complex a
(+:) a
r a
i = forall a. (a, a) -> Complex a
Complex (a
r, a
i)

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

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

instance
  (Subtractive a, Multiplicative a) =>
  Multiplicative (Complex a)
  where
  (Complex (a
r, a
i)) * :: Complex a -> Complex a -> Complex a
* (Complex (a
r', a
i')) =
    forall a. (a, a) -> Complex a
Complex (a
r forall a. Multiplicative a => a -> a -> a
* a
r' forall a. Subtractive a => a -> a -> a
- a
i forall a. Multiplicative a => a -> a -> a
* a
i', a
i forall a. Multiplicative a => a -> a -> a
* a
r' forall a. Additive a => a -> a -> a
+ a
i' forall a. Multiplicative a => a -> a -> a
* a
r)
  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 (Complex (a
r, a
i)) = (a
r forall a. Multiplicative a => a -> a -> a
* a
d) forall a. a -> a -> Complex a
+: (forall a. Subtractive a => a -> a
negate a
i forall a. Multiplicative a => a -> a -> a
* a
d)
    where
      d :: a
d = forall a. Divisive a => a -> a
recip ((a
r forall a. Multiplicative a => a -> a -> a
* a
r) forall a. Additive a => a -> a -> a
+ (a
i forall a. Multiplicative a => a -> a -> a
* a
i))

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

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

-- Can't use DerivingVia due to extra Whole constraints
instance (Eq (Whole a), Ring (Whole a), QuotientField a) => QuotientField (Complex a) where
  type Whole (Complex a) = Complex (Whole a)

  properFraction :: Complex a -> (Whole (Complex a), Complex a)
properFraction (Complex (a
x, a
y)) =
    (forall a. (a, a) -> Complex a
Complex (Whole a
xwhole, Whole a
ywhole), forall a. (a, a) -> Complex a
Complex (a
xfrac, a
yfrac))
    where
      (Whole a
xwhole, a
xfrac) = forall a. QuotientField a => a -> (Whole a, a)
properFraction a
x
      (Whole a
ywhole, a
yfrac) = forall a. QuotientField a => a -> (Whole a, a)
properFraction a
y

  round :: (Eq (Whole (Complex a)), Ring (Whole (Complex a))) =>
Complex a -> Whole (Complex a)
round (Complex (a
x, a
y)) = forall a. (a, a) -> Complex a
Complex (forall a.
(QuotientField a, Eq (Whole a), Ring (Whole a)) =>
a -> Whole a
round a
x, forall a.
(QuotientField a, Eq (Whole a), Ring (Whole a)) =>
a -> Whole a
round a
y)
  ceiling :: Distributive (Whole (Complex a)) => Complex a -> Whole (Complex a)
ceiling (Complex (a
x, a
y)) = forall a. (a, a) -> Complex a
Complex (forall a. (QuotientField a, Distributive (Whole a)) => a -> Whole a
ceiling a
x, forall a. (QuotientField a, Distributive (Whole a)) => a -> Whole a
ceiling a
y)
  floor :: Ring (Whole (Complex a)) => Complex a -> Whole (Complex a)
floor (Complex (a
x, a
y)) = forall a. (a, a) -> Complex a
Complex (forall a. (QuotientField a, Ring (Whole a)) => a -> Whole a
floor a
x, forall a. (QuotientField a, Ring (Whole a)) => a -> Whole a
floor a
y)
  truncate :: Ring (Whole (Complex a)) => Complex a -> Whole (Complex a)
truncate (Complex (a
x, a
y)) = forall a. (a, a) -> Complex a
Complex (forall a. (QuotientField a, Ring (Whole a)) => a -> Whole a
truncate a
x, forall a. (QuotientField a, Ring (Whole a)) => a -> Whole a
truncate a
y)