{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Crypto.Lol.Types.Unsafe.Complex
( Complex(..)
, roundComplex
, cis, real, imag, fromReal
) where
import Algebra.Additive as Additive (C)
import Algebra.Field as Field (C)
import Algebra.IntegralDomain as IntegralDomain
import Algebra.Ring as Ring (C)
import Algebra.ZeroTestable as ZeroTestable (C)
import qualified Number.Complex as C hiding (exp, signum)
import Crypto.Lol.Types.Numeric as LP
import Control.DeepSeq
import System.Random
newtype Complex a = Complex' (C.T a)
deriving (Additive.C, Ring.C, ZeroTestable.C, Field.C, Eq, Show)
instance (Field a) => IntegralDomain.C (Complex a) where
(Complex' a) `divMod` (Complex' b) = (Complex' $ a / b, LP.zero)
instance (NFData a) => NFData (Complex a) where
rnf (Complex' x) = let r = C.real x
i = C.imag x
in rnf r `seq` rnf i `seq` ()
instance (Random a) => Random (Complex a) where
random g = let (a,g') = random g
(b,g'') = random g'
in (Complex' $ a C.+: b, g'')
randomR = error "randomR not defined for (Complex t)"
roundComplex :: (RealRing a, ToInteger b) => Complex a -> (b,b)
roundComplex (Complex' x) = (round $ C.real x, round $ C.imag x)
cis :: Transcendental a => a -> Complex a
cis = Complex' . C.cis
real :: Complex a -> a
real (Complex' a) = C.real a
imag :: Complex a -> a
imag (Complex' a) = C.imag a
fromReal :: Additive a => a -> Complex a
fromReal = Complex' . C.fromReal