{-# LANGUAGE RebindableSyntax #-}
module Data.YAP.Algebra.Internal (
module Data.YAP.Algebra.Internal,
Ratio(..)
) where
import Prelude hiding (
Num(..), Real(..), Integral(..), Fractional(..),
Floating(..), RealFrac(..), RealFloat(..),
even, odd, gcd, lcm, (^), (^^), fromIntegral, realToFrac)
import qualified Prelude
import GHC.Real (Ratio(..))
import Data.Complex (Complex((:+)))
import Numeric.Natural (Natural)
import GHC.Num.Integer (integerToNatural)
infixr 8 ^, ^^, **
infixl 7 *, /, `quot`, `rem`, `div`, `mod`
infixl 6 +, -
infixl 7 %
class AdditiveMonoid a where
(+) :: a -> a -> a
zero :: a
atimes :: (ToInteger b) => b -> a -> a
atimes b
n a
x
| b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
<= b
forall a. AdditiveMonoid a => a
zero = a
forall a. AdditiveMonoid a => a
zero
| b -> Bool
forall a. ToInteger a => a -> Bool
even b
n = a
ya -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+a
y
| Bool
otherwise = a
ya -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+a
ya -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+a
x
where
y :: a
y = b -> a -> a
forall b. ToInteger b => b -> a -> a
forall a b. (AdditiveMonoid a, ToInteger b) => b -> a -> a
atimes (b
n b -> b -> b
forall a. Euclidean a => a -> a -> a
`div` b
two) a
x
two :: b
two = b
forall a. Semiring a => a
one b -> b -> b
forall a. AdditiveMonoid a => a -> a -> a
+ b
forall a. Semiring a => a
one
class (AdditiveMonoid a) => AbelianGroup a where
(-) :: a -> a -> a
negate :: a -> a
gtimes :: (AbelianGroup b, ToInteger b) => b -> a -> a
gtimes b
n a
x
| b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< b
forall a. AdditiveMonoid a => a
zero = a -> a
forall a. AbelianGroup a => a -> a
negate (b -> a -> a
forall b. ToInteger b => b -> a -> a
forall a b. (AdditiveMonoid a, ToInteger b) => b -> a -> a
atimes (b -> b
forall a. AbelianGroup a => a -> a
negate b
n) a
x)
| Bool
otherwise = b -> a -> a
forall b. ToInteger b => b -> a -> a
forall a b. (AdditiveMonoid a, ToInteger b) => b -> a -> a
atimes (b -> b
forall a. AbelianGroup a => a -> a
negate b
n) a
x
a
x - a
y = a
x a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a -> a
forall a. AbelianGroup a => a -> a
negate a
y
negate a
x = a
forall a. AdditiveMonoid a => a
zero a -> a -> a
forall a. AbelianGroup a => a -> a -> a
- a
x
{-# MINIMAL (-) | negate #-}
class (AdditiveMonoid a) => Semiring a where
(*) :: a -> a -> a
one :: a
one = Natural -> a
forall a. Semiring a => Natural -> a
fromNatural (Integer -> Natural
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
1)
fromNatural :: Natural -> a
fromNatural Natural
n = Natural -> a -> a
forall b. ToInteger b => b -> a -> a
forall a b. (AdditiveMonoid a, ToInteger b) => b -> a -> a
atimes Natural
n a
forall a. Semiring a => a
one
rescale :: a -> a -> (a, a, a -> a)
rescale a
x a
y = (a
x, a
y, a -> a
forall a. a -> a
id)
{-# MINIMAL (*), (one | fromNatural) #-}
class (AbelianGroup a, Semiring a) => Ring a where
fromInteger :: Integer -> a
fromInteger Integer
n = Integer -> a -> a
forall b. (AbelianGroup b, ToInteger b) => b -> a -> a
forall a b.
(AbelianGroup a, AbelianGroup b, ToInteger b) =>
b -> a -> a
gtimes Integer
n a
forall a. Semiring a => a
one
class (Semiring a) => StandardAssociate a where
stdAssociate :: a -> a
stdUnit :: a -> a
stdRecip :: a -> a
stdAssociate a
x = a
x a -> a -> a
forall a. Semiring a => a -> a -> a
* a -> a
forall a. StandardAssociate a => a -> a
stdRecip a
x
class (Semiring a) => Euclidean a where
div :: a -> a -> a
mod :: a -> a -> a
divMod :: a -> a -> (a,a)
a
n `divMod` a
d = (a
n a -> a -> a
forall a. Euclidean a => a -> a -> a
`div` a
d, a
n a -> a -> a
forall a. Euclidean a => a -> a -> a
`mod` a
d)
a
n `div` a
d = a
q where (a
q,a
_) = a -> a -> (a, a)
forall a. Euclidean a => a -> a -> (a, a)
divMod a
n a
d
a
n `mod` a
d = a
r where (a
_,a
r) = a -> a -> (a, a)
forall a. Euclidean a => a -> a -> (a, a)
divMod a
n a
d
euclideanNorm :: a -> Natural
{-# MINIMAL ((divMod | (div, mod)), euclideanNorm) #-}
class (Ord a, Semiring a) => ToRational a where
toRational :: a -> Rational
class (StandardAssociate a, Euclidean a, ToRational a) => ToInteger a where
toInteger :: a -> Integer
class (Semiring a) => DivisionSemiring a where
recip :: a -> a
class (Ring a, DivisionSemiring a) => DivisionRing a
class (DivisionSemiring a) => Semifield a where
(/) :: a -> a -> a
a
x / a
y = a
x a -> a -> a
forall a. Semiring a => a -> a -> a
* a -> a
forall a. DivisionSemiring a => a -> a
recip a
y
{-# MINIMAL #-}
class (DivisionRing a, Semifield a) => Field a
class (Ring a) => FromRational a where
fromRational :: Rational -> a
instance AdditiveMonoid Int where
+ :: Int -> Int -> Int
(+) = Int -> Int -> Int
forall a. Num a => a -> a -> a
(Prelude.+)
zero :: Int
zero = Int
0
instance AbelianGroup Int where
(-) = Int -> Int -> Int
forall a. Num a => a -> a -> a
(Prelude.-)
negate :: Int -> Int
negate = Int -> Int
forall a. Num a => a -> a
Prelude.negate
gtimes :: forall b. (AbelianGroup b, ToInteger b) => b -> Int -> Int
gtimes b
n Int
m = b -> Int
forall a b. (ToInteger a, Ring b) => a -> b
fromIntegral b
n Int -> Int -> Int
forall a. Semiring a => a -> a -> a
* Int
m
instance Semiring Int where
* :: Int -> Int -> Int
(*) = Int -> Int -> Int
forall a. Num a => a -> a -> a
(Prelude.*)
one :: Int
one = Int
1
instance Ring Int where
fromInteger :: Integer -> Int
fromInteger = Integer -> Int
forall a. Num a => Integer -> a
Prelude.fromInteger
instance StandardAssociate Int where
stdAssociate :: Int -> Int
stdAssociate Int
x = if Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 then -Int
x else Int
x
stdUnit :: Int -> Int
stdUnit Int
x = if Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 then -Int
1 else Int
1
stdRecip :: Int -> Int
stdRecip = Int -> Int
forall a. StandardAssociate a => a -> a
stdUnit
instance Euclidean Int where
div :: Int -> Int -> Int
div = Int -> Int -> Int
forall a. Integral a => a -> a -> a
Prelude.div
mod :: Int -> Int -> Int
mod = Int -> Int -> Int
forall a. Integral a => a -> a -> a
Prelude.mod
euclideanNorm :: Int -> Natural
euclideanNorm = Integer -> Natural
integerToNatural (Integer -> Natural) -> (Int -> Integer) -> Int -> Natural
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a. ToInteger a => a -> Integer
toInteger
instance ToRational Int where
toRational :: Int -> Rational
toRational Int
x = Int -> Integer
forall a. ToInteger a => a -> Integer
toInteger Int
x Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
1
instance ToInteger Int where
toInteger :: Int -> Integer
toInteger = Int -> Integer
forall a. Integral a => a -> Integer
Prelude.toInteger
instance AdditiveMonoid Word where
+ :: Word -> Word -> Word
(+) = Word -> Word -> Word
forall a. Num a => a -> a -> a
(Prelude.+)
zero :: Word
zero = Word
0
instance AbelianGroup Word where
(-) = Word -> Word -> Word
forall a. Num a => a -> a -> a
(Prelude.-)
negate :: Word -> Word
negate = Word -> Word
forall a. Num a => a -> a
Prelude.negate
gtimes :: forall b. (AbelianGroup b, ToInteger b) => b -> Word -> Word
gtimes b
n Word
m = b -> Word
forall a b. (ToInteger a, Ring b) => a -> b
fromIntegral b
n Word -> Word -> Word
forall a. Semiring a => a -> a -> a
* Word
m
instance Semiring Word where
* :: Word -> Word -> Word
(*) = Word -> Word -> Word
forall a. Num a => a -> a -> a
(Prelude.*)
one :: Word
one = Word
1
instance Ring Word where
fromInteger :: Integer -> Word
fromInteger = Integer -> Word
forall a. Num a => Integer -> a
Prelude.fromInteger
instance StandardAssociate Word where
stdAssociate :: Word -> Word
stdAssociate Word
x = Word
x
stdUnit :: Word -> Word
stdUnit Word
_ = Word
1
stdRecip :: Word -> Word
stdRecip Word
_ = Word
1
instance Euclidean Word where
div :: Word -> Word -> Word
div = Word -> Word -> Word
forall a. Integral a => a -> a -> a
Prelude.div
mod :: Word -> Word -> Word
mod = Word -> Word -> Word
forall a. Integral a => a -> a -> a
Prelude.mod
euclideanNorm :: Word -> Natural
euclideanNorm = Integer -> Natural
integerToNatural (Integer -> Natural) -> (Word -> Integer) -> Word -> Natural
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Integer
forall a. ToInteger a => a -> Integer
toInteger
instance ToRational Word where
toRational :: Word -> Rational
toRational Word
x = Word -> Integer
forall a. ToInteger a => a -> Integer
toInteger Word
x Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
1
instance ToInteger Word where
toInteger :: Word -> Integer
toInteger = Word -> Integer
forall a. Integral a => a -> Integer
Prelude.toInteger
instance AdditiveMonoid Natural where
+ :: Natural -> Natural -> Natural
(+) = Natural -> Natural -> Natural
forall a. Num a => a -> a -> a
(Prelude.+)
zero :: Natural
zero = Integer -> Natural
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
0
instance Semiring Natural where
* :: Natural -> Natural -> Natural
(*) = Natural -> Natural -> Natural
forall a. Num a => a -> a -> a
(Prelude.*)
one :: Natural
one = Integer -> Natural
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
1
fromNatural :: Natural -> Natural
fromNatural = Natural -> Natural
forall a. a -> a
id
instance StandardAssociate Natural where
stdAssociate :: Natural -> Natural
stdAssociate Natural
x = Natural
x
stdUnit :: Natural -> Natural
stdUnit Natural
_ = Natural
forall a. Semiring a => a
one
stdRecip :: Natural -> Natural
stdRecip Natural
_ = Natural
forall a. Semiring a => a
one
instance Euclidean Natural where
div :: Natural -> Natural -> Natural
div = Natural -> Natural -> Natural
forall a. Integral a => a -> a -> a
Prelude.div
mod :: Natural -> Natural -> Natural
mod = Natural -> Natural -> Natural
forall a. Integral a => a -> a -> a
Prelude.mod
euclideanNorm :: Natural -> Natural
euclideanNorm = Natural -> Natural
forall a. a -> a
id
instance ToRational Natural where
toRational :: Natural -> Rational
toRational Natural
x = Natural -> Integer
forall a. ToInteger a => a -> Integer
toInteger Natural
x Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
1
instance ToInteger Natural where
toInteger :: Natural -> Integer
toInteger = Natural -> Integer
forall a. Integral a => a -> Integer
Prelude.toInteger
instance AdditiveMonoid Integer where
+ :: Integer -> Integer -> Integer
(+) = Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(Prelude.+)
zero :: Integer
zero = Integer
0
instance AbelianGroup Integer where
(-) = Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(Prelude.-)
negate :: Integer -> Integer
negate = Integer -> Integer
forall a. Num a => a -> a
Prelude.negate
gtimes :: forall b. (AbelianGroup b, ToInteger b) => b -> Integer -> Integer
gtimes b
n Integer
m = b -> Integer
forall a. ToInteger a => a -> Integer
toInteger b
n Integer -> Integer -> Integer
forall a. Semiring a => a -> a -> a
* Integer
m
instance Semiring Integer where
* :: Integer -> Integer -> Integer
(*) = Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(Prelude.*)
one :: Integer
one = Integer
1
instance Ring Integer where
fromInteger :: Integer -> Integer
fromInteger = Integer -> Integer
forall a. a -> a
id
instance StandardAssociate Integer where
stdAssociate :: Integer -> Integer
stdAssociate Integer
x = if Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 then -Integer
x else Integer
x
stdUnit :: Integer -> Integer
stdUnit Integer
x = if Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 then -Integer
1 else Integer
1
stdRecip :: Integer -> Integer
stdRecip = Integer -> Integer
forall a. StandardAssociate a => a -> a
stdUnit
instance Euclidean Integer where
div :: Integer -> Integer -> Integer
div = Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
Prelude.div
mod :: Integer -> Integer -> Integer
mod = Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
Prelude.mod
euclideanNorm :: Integer -> Natural
euclideanNorm = Integer -> Natural
integerToNatural
instance ToRational Integer where
toRational :: Integer -> Rational
toRational Integer
x = Integer
x Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
1
instance ToInteger Integer where
toInteger :: Integer -> Integer
toInteger = Integer -> Integer
forall a. a -> a
id
instance AdditiveMonoid Float where
+ :: Float -> Float -> Float
(+) = Float -> Float -> Float
forall a. Num a => a -> a -> a
(Prelude.+)
zero :: Float
zero = Float
0
instance AbelianGroup Float where
(-) = Float -> Float -> Float
forall a. Num a => a -> a -> a
(Prelude.-)
negate :: Float -> Float
negate = Float -> Float
forall a. Num a => a -> a
Prelude.negate
gtimes :: forall b. (AbelianGroup b, ToInteger b) => b -> Float -> Float
gtimes b
n Float
m = b -> Float
forall a b. (ToInteger a, Ring b) => a -> b
fromIntegral b
n Float -> Float -> Float
forall a. Semiring a => a -> a -> a
* Float
m
instance Semiring Float where
* :: Float -> Float -> Float
(*) = Float -> Float -> Float
forall a. Num a => a -> a -> a
(Prelude.*)
one :: Float
one = Float
1
rescale :: Float -> Float -> (Float, Float, Float -> Float)
rescale = Float -> Float -> (Float, Float, Float -> Float)
forall a. RealFloat a => a -> a -> (a, a, a -> a)
rescaleRealFloat
instance Ring Float where
fromInteger :: Integer -> Float
fromInteger = Integer -> Float
forall a. Num a => Integer -> a
Prelude.fromInteger
instance DivisionSemiring Float where
recip :: Float -> Float
recip = (Float -> Float
forall a. Fractional a => a -> a
Prelude.recip)
instance Semifield Float where
/ :: Float -> Float -> Float
(/) = Float -> Float -> Float
forall a. Fractional a => a -> a -> a
(Prelude./)
instance DivisionRing Float
instance Field Float
instance FromRational Float where
fromRational :: Rational -> Float
fromRational = Rational -> Float
forall a. Fractional a => Rational -> a
Prelude.fromRational
instance ToRational Float where
toRational :: Float -> Rational
toRational Float
x = (Integer
mInteger -> Integer -> Rational
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
%Integer
1)Rational -> Rational -> Rational
forall a. Semiring a => a -> a -> a
*(Integer
bInteger -> Integer -> Rational
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
%Integer
1)Rational -> Int -> Rational
forall a b.
(DivisionSemiring a, AbelianGroup b, ToInteger b) =>
a -> b -> a
^^Int
n
where (Integer
m,Int
n) = Float -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
Prelude.decodeFloat Float
x
b :: Integer
b = Float -> Integer
forall a. RealFloat a => a -> Integer
Prelude.floatRadix Float
x
instance AdditiveMonoid Double where
+ :: Double -> Double -> Double
(+) = Double -> Double -> Double
forall a. Num a => a -> a -> a
(Prelude.+)
zero :: Double
zero = Double
0
instance AbelianGroup Double where
(-) = Double -> Double -> Double
forall a. Num a => a -> a -> a
(Prelude.-)
negate :: Double -> Double
negate = Double -> Double
forall a. Num a => a -> a
Prelude.negate
gtimes :: forall b. (AbelianGroup b, ToInteger b) => b -> Double -> Double
gtimes b
n Double
m = b -> Double
forall a b. (ToInteger a, Ring b) => a -> b
fromIntegral b
n Double -> Double -> Double
forall a. Semiring a => a -> a -> a
* Double
m
instance Semiring Double where
* :: Double -> Double -> Double
(*) = Double -> Double -> Double
forall a. Num a => a -> a -> a
(Prelude.*)
one :: Double
one = Double
1
rescale :: Double -> Double -> (Double, Double, Double -> Double)
rescale = Double -> Double -> (Double, Double, Double -> Double)
forall a. RealFloat a => a -> a -> (a, a, a -> a)
rescaleRealFloat
instance Ring Double where
fromInteger :: Integer -> Double
fromInteger = Integer -> Double
forall a. Num a => Integer -> a
Prelude.fromInteger
instance DivisionSemiring Double where
recip :: Double -> Double
recip = (Double -> Double
forall a. Fractional a => a -> a
Prelude.recip)
instance Semifield Double where
/ :: Double -> Double -> Double
(/) = Double -> Double -> Double
forall a. Fractional a => a -> a -> a
(Prelude./)
instance DivisionRing Double
instance Field Double
instance FromRational Double where
fromRational :: Rational -> Double
fromRational = Rational -> Double
forall a. Fractional a => Rational -> a
Prelude.fromRational
instance ToRational Double where
toRational :: Double -> Rational
toRational Double
x = (Integer
mInteger -> Integer -> Rational
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
%Integer
1)Rational -> Rational -> Rational
forall a. Semiring a => a -> a -> a
*(Integer
bInteger -> Integer -> Rational
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
%Integer
1)Rational -> Int -> Rational
forall a b.
(DivisionSemiring a, AbelianGroup b, ToInteger b) =>
a -> b -> a
^^Int
n
where (Integer
m,Int
n) = Double -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
Prelude.decodeFloat Double
x
b :: Integer
b = Double -> Integer
forall a. RealFloat a => a -> Integer
Prelude.floatRadix Double
x
rescaleRealFloat :: (RealFloat a) => a -> a -> (a, a, a -> a)
{-# SPECIALISE rescaleRealFloat :: Float -> Float -> (Float, Float, Float -> Float) #-}
{-# SPECIALISE rescaleRealFloat :: Double -> Double -> (Double, Double, Double -> Double) #-}
rescaleRealFloat :: forall a. RealFloat a => a -> a -> (a, a, a -> a)
rescaleRealFloat a
x a
y = (Int -> a -> a
forall a. RealFloat a => Int -> a -> a
scaleFloat Int
mk a
x, Int -> a -> a
forall a. RealFloat a => Int -> a -> a
scaleFloat Int
mk a
y, Int -> a -> a
forall a. RealFloat a => Int -> a -> a
scaleFloat Int
k)
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
euclid :: (Eq a, Euclidean a) => a -> a -> a
euclid :: forall a. (Eq a, Euclidean a) => a -> a -> a
euclid a
x a
y
| a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. AdditiveMonoid a => a
zero = a
x
| Bool
otherwise = a -> a -> a
forall a. (Eq a, Euclidean a) => a -> a -> a
euclid a
y (a
x a -> a -> a
forall a. Euclidean a => a -> a -> a
`mod` a
y)
gcd :: (Eq a, StandardAssociate a, Euclidean a) => a -> a -> a
gcd :: forall a. (Eq a, StandardAssociate a, Euclidean a) => a -> a -> a
gcd a
x a
y = a -> a
forall a. StandardAssociate a => a -> a
stdAssociate (a -> a -> a
forall a. (Eq a, Euclidean a) => a -> a -> a
euclid a
x a
y)
lcm :: (Eq a, StandardAssociate a, Euclidean a) => a -> a -> a
lcm :: forall a. (Eq a, StandardAssociate a, Euclidean a) => a -> a -> a
lcm a
x a
y
| a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. AdditiveMonoid a => a
zero = a
forall a. AdditiveMonoid a => a
zero
| a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. AdditiveMonoid a => a
zero = a
forall a. AdditiveMonoid a => a
zero
| Bool
otherwise = a -> a
forall a. StandardAssociate a => a -> a
stdAssociate ((a
x a -> a -> a
forall a. Euclidean a => a -> a -> a
`div` a -> a -> a
forall a. (Eq a, Euclidean a) => a -> a -> a
euclid a
x a
y) a -> a -> a
forall a. Semiring a => a -> a -> a
* a
y)
instance (AdditiveMonoid a, AdditiveMonoid b) => AdditiveMonoid (a,b) where
(a
x,b
y) + :: (a, b) -> (a, b) -> (a, b)
+ (a
x',b
y') = (a
xa -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+a
x', b
yb -> b -> b
forall a. AdditiveMonoid a => a -> a -> a
+b
y')
zero :: (a, b)
zero = (a
forall a. AdditiveMonoid a => a
zero, b
forall a. AdditiveMonoid a => a
zero)
atimes :: forall b. ToInteger b => b -> (a, b) -> (a, b)
atimes b
n (a
x,b
y) = (b -> a -> a
forall b. ToInteger b => b -> a -> a
forall a b. (AdditiveMonoid a, ToInteger b) => b -> a -> a
atimes b
n a
x, b -> b -> b
forall b. ToInteger b => b -> b -> b
forall a b. (AdditiveMonoid a, ToInteger b) => b -> a -> a
atimes b
n b
y)
instance (AbelianGroup a, AbelianGroup b) => AbelianGroup (a,b) where
(a
x,b
y) - :: (a, b) -> (a, b) -> (a, b)
- (a
x',b
y') = (a
xa -> a -> a
forall a. AbelianGroup a => a -> a -> a
-a
x', b
yb -> b -> b
forall a. AbelianGroup a => a -> a -> a
-b
y')
negate :: (a, b) -> (a, b)
negate (a
x,b
y) = (a -> a
forall a. AbelianGroup a => a -> a
negate a
x, b -> b
forall a. AbelianGroup a => a -> a
negate b
y)
gtimes :: forall b. (AbelianGroup b, ToInteger b) => b -> (a, b) -> (a, b)
gtimes b
n (a
x,b
y) = (b -> a -> a
forall b. (AbelianGroup b, ToInteger b) => b -> a -> a
forall a b.
(AbelianGroup a, AbelianGroup b, ToInteger b) =>
b -> a -> a
gtimes b
n a
x, b -> b -> b
forall b. (AbelianGroup b, ToInteger b) => b -> b -> b
forall a b.
(AbelianGroup a, AbelianGroup b, ToInteger b) =>
b -> a -> a
gtimes b
n b
y)
instance (Semiring a, Semiring b) => Semiring (a,b) where
(a
x,b
y) * :: (a, b) -> (a, b) -> (a, b)
* (a
x',b
y') = (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
x', b
yb -> b -> b
forall a. Semiring a => a -> a -> a
*b
y')
one :: (a, b)
one = (a
forall a. Semiring a => a
one, b
forall a. Semiring a => a
one)
fromNatural :: Natural -> (a, b)
fromNatural Natural
n = (Natural -> a
forall a. Semiring a => Natural -> a
fromNatural Natural
n, Natural -> b
forall a. Semiring a => Natural -> a
fromNatural Natural
n)
instance (Ring a, Ring b) => Ring (a,b) where
fromInteger :: Integer -> (a, b)
fromInteger Integer
n = (Integer -> a
forall a. Ring a => Integer -> a
fromInteger Integer
n, Integer -> b
forall a. Ring a => Integer -> a
fromInteger Integer
n)
instance (FromRational a, FromRational b) => FromRational (a,b) where
fromRational :: Rational -> (a, b)
fromRational Rational
n = (Rational -> a
forall a. FromRational a => Rational -> a
fromRational Rational
n, Rational -> b
forall a. FromRational a => Rational -> a
fromRational Rational
n)
{-# SPECIALISE (%) :: Integer -> Integer -> Rational #-}
(%) :: (Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
a
x % :: forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
% a
y = ((a
x a -> a -> a
forall a. Euclidean a => a -> a -> a
`div` a
d) a -> a -> a
forall a. Semiring a => a -> a -> a
* a -> a
forall a. StandardAssociate a => a -> a
stdRecip a
y') a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a -> a
forall a. StandardAssociate a => a -> a
stdAssociate a
y'
where y' :: a
y' = a
y a -> a -> a
forall a. Euclidean a => a -> a -> a
`div` a
d
d :: a
d = a -> a -> a
forall a. (Eq a, Euclidean a) => a -> a -> a
euclid a
x a
y
numerator :: Ratio a -> a
numerator :: forall a. Ratio a -> a
numerator (a
x :% a
_) = a
x
denominator :: Ratio a -> a
denominator :: forall a. Ratio a -> a
denominator (a
_ :% a
y) = a
y
instance (Eq a, StandardAssociate a, Euclidean a) => AdditiveMonoid (Ratio a) where
(a
x:%a
y) + :: Ratio a -> Ratio a -> Ratio a
+ (a
x':%a
y') = (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
y' a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a
x'a -> a -> a
forall a. Semiring a => a -> a -> a
*a
y) a -> a -> Ratio a
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
% (a
ya -> a -> a
forall a. Semiring a => a -> a -> a
*a
y')
zero :: Ratio a
zero = a
forall a. AdditiveMonoid a => a
zero a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
forall a. Semiring a => a
one
atimes :: forall b. ToInteger b => b -> Ratio a -> Ratio a
atimes b
n (a
x:%a
y) = b -> a -> a
forall b. ToInteger b => b -> a -> a
forall a b. (AdditiveMonoid a, ToInteger b) => b -> a -> a
atimes b
n a
x a -> a -> Ratio a
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
% a
y
instance (Eq a, StandardAssociate a, Euclidean a, Ring a) => AbelianGroup (Ratio a) where
negate :: Ratio a -> Ratio a
negate (a
x:%a
y) = (-a
x) a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
y
gtimes :: forall b. (AbelianGroup b, ToInteger b) => b -> Ratio a -> Ratio a
gtimes b
n (a
x:%a
y) = b -> a -> a
forall b. (AbelianGroup b, ToInteger b) => b -> a -> a
forall a b.
(AbelianGroup a, AbelianGroup b, ToInteger b) =>
b -> a -> a
gtimes b
n a
x a -> a -> Ratio a
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
% a
y
instance (Eq a, StandardAssociate a, Euclidean a) => Semiring (Ratio a) where
(a
x:%a
y) * :: Ratio a -> Ratio a -> Ratio a
* (a
x':%a
y') = (a
x a -> a -> a
forall a. Semiring a => a -> a -> a
* a
x') a -> a -> Ratio a
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
% (a
y a -> a -> a
forall a. Semiring a => a -> a -> a
* a
y')
one :: Ratio a
one = a
forall a. Semiring a => a
one a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
forall a. Semiring a => a
one
fromNatural :: Natural -> Ratio a
fromNatural Natural
x = Natural -> a
forall a. Semiring a => Natural -> a
fromNatural Natural
x a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
forall a. Semiring a => a
one
instance (Eq a, StandardAssociate a, Euclidean a, Ring a) => Ring (Ratio a) where
fromInteger :: Integer -> Ratio a
fromInteger Integer
x = Integer -> a
forall a. Ring a => Integer -> a
fromInteger Integer
x a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
forall a. Semiring a => a
one
instance (Eq a, StandardAssociate a, Euclidean a) => DivisionSemiring (Ratio a) where
recip :: Ratio a -> Ratio a
recip (a
x:%a
y) = a
y a -> a -> Ratio a
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
% a
x
instance (Eq a, StandardAssociate a, Euclidean a) => Semifield (Ratio a) where
(a
x:%a
y) / :: Ratio a -> Ratio a -> Ratio a
/ (a
x':%a
y') = (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
y') a -> a -> Ratio a
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
% (a
ya -> a -> a
forall a. Semiring a => a -> a -> a
*a
x')
instance (Eq a, StandardAssociate a, Euclidean a, Ring a) => DivisionRing (Ratio a)
instance (Eq a, StandardAssociate a, Euclidean a, Ring a) => Field (Ratio a)
instance (Integral a) => FromRational (Ratio a) where
fromRational :: Rational -> Ratio a
fromRational Rational
x = Integer -> a
forall a. Ring a => Integer -> a
fromInteger (Rational -> Integer
forall a. Ratio a -> a
numerator Rational
x) a -> a -> Ratio a
forall a.
(Eq a, StandardAssociate a, Euclidean a) =>
a -> a -> Ratio a
%
Integer -> a
forall a. Ring a => Integer -> a
fromInteger (Rational -> Integer
forall a. Ratio a -> a
denominator Rational
x)
instance (ToInteger a, Prelude.Integral a) => ToRational (Ratio a) where
toRational :: Ratio a -> Rational
toRational (a
x:%a
y) = a -> Integer
forall a. ToInteger a => a -> Integer
toInteger a
x Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% a -> Integer
forall a. ToInteger a => a -> Integer
toInteger a
y
instance (AdditiveMonoid a) => AdditiveMonoid (Complex a) where
{-# SPECIALISE instance AdditiveMonoid (Complex Float) #-}
{-# SPECIALISE instance AdditiveMonoid (Complex Double) #-}
(a
x:+a
y) + :: Complex a -> Complex a -> Complex a
+ (a
x':+a
y') = (a
xa -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+a
x') a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ya -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+a
y')
{-# SPECIALISE instance AdditiveMonoid (Complex Float) #-}
{-# SPECIALISE instance AdditiveMonoid (Complex Double) #-}
zero :: Complex a
zero = a
forall a. AdditiveMonoid a => a
zero a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. AdditiveMonoid a => a
zero
instance (AbelianGroup a) => AbelianGroup (Complex a) where
{-# SPECIALISE instance AbelianGroup (Complex Float) #-}
{-# SPECIALISE instance AbelianGroup (Complex Double) #-}
(a
x:+a
y) - :: Complex a -> Complex a -> Complex a
- (a
x':+a
y') = (a
xa -> a -> a
forall a. AbelianGroup a => a -> a -> a
-a
x') a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ya -> a -> a
forall a. AbelianGroup a => a -> a -> a
-a
y')
negate :: Complex a -> Complex a
negate (a
x:+a
y) = a -> a
forall a. AbelianGroup a => a -> a
negate a
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
forall a. AbelianGroup a => a -> a
negate a
y
gtimes :: forall b.
(AbelianGroup b, ToInteger b) =>
b -> Complex a -> Complex a
gtimes b
n (a
x:+a
y) = b -> a -> a
forall b. (AbelianGroup b, ToInteger b) => b -> a -> a
forall a b.
(AbelianGroup a, AbelianGroup b, ToInteger b) =>
b -> a -> a
gtimes b
n a
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ b -> a -> a
forall b. (AbelianGroup b, ToInteger b) => b -> a -> a
forall a b.
(AbelianGroup a, AbelianGroup b, ToInteger b) =>
b -> a -> a
gtimes b
n a
y
instance (Ring a) => Semiring (Complex a) where
{-# SPECIALISE instance Semiring (Complex Float) #-}
{-# SPECIALISE instance Semiring (Complex Double) #-}
(a
x:+a
y) * :: Complex a -> Complex a -> Complex a
* (a
x':+a
y') = (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
x'a -> a -> a
forall a. AbelianGroup a => a -> a -> a
-a
ya -> a -> a
forall a. Semiring a => a -> a -> a
*a
y') a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
y'a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+a
ya -> a -> a
forall a. Semiring a => a -> a -> a
*a
x')
{-# SPECIALISE instance Semiring (Complex Float) #-}
{-# SPECIALISE instance Semiring (Complex Double) #-}
one :: Complex a
one = a
forall a. Semiring a => a
one a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. AdditiveMonoid a => a
zero
fromNatural :: Natural -> Complex a
fromNatural Natural
n = Natural -> a
forall a. Semiring a => Natural -> a
fromNatural Natural
n a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. AdditiveMonoid a => a
zero
instance (Ring a) => Ring (Complex a) where
{-# SPECIALISE instance Ring (Complex Float) #-}
{-# SPECIALISE instance Ring (Complex Double) #-}
fromInteger :: Integer -> Complex a
fromInteger Integer
n = Integer -> a
forall a. Ring a => Integer -> a
fromInteger Integer
n a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
forall a. AdditiveMonoid a => a
zero
instance (Ring a, ToInteger a) => StandardAssociate (Complex a) where
{-# SPECIALISE instance StandardAssociate (Complex Int) #-}
{-# SPECIALISE instance StandardAssociate (Complex Integer) #-}
stdUnit :: Complex a -> Complex a
stdUnit (a
x :+ a
y)
| a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 Bool -> Bool -> Bool
&& a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 = a
0 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
1
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 Bool -> Bool -> Bool
&& a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 = -Complex a
1
| a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 Bool -> Bool -> Bool
&& a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0 = a
0 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (-a
1)
| Bool
otherwise = Complex a
1
stdRecip :: Complex a -> Complex a
stdRecip (a
x :+ a
y)
| a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 Bool -> Bool -> Bool
&& a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 = a
0 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (-a
1)
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 Bool -> Bool -> Bool
&& a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 = -Complex a
1
| a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 Bool -> Bool -> Bool
&& a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0 = a
0 a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
1
| Bool
otherwise = Complex a
1
instance (Ring a, ToInteger a) => Euclidean (Complex a) where
{-# SPECIALISE instance Euclidean (Complex Int) #-}
{-# SPECIALISE instance Euclidean (Complex Integer) #-}
(a
x:+a
y) div :: Complex a -> Complex a -> Complex a
`div` (a
x':+a
y')
= a -> a
round_div (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
x' a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a
ya -> a -> a
forall a. Semiring a => a -> a -> a
*a
y') a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a -> a
round_div (a
x'a -> a -> a
forall a. Semiring a => a -> a -> a
*a
y a -> a -> a
forall a. AbelianGroup a => a -> a -> a
- a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
y')
where round_div :: a -> a
round_div a
i = (a
2a -> a -> a
forall a. Semiring a => a -> a -> a
*a
i a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a
n) a -> a -> a
forall a. Euclidean a => a -> a -> a
`div` (a
2a -> a -> a
forall a. Semiring a => a -> a -> a
*a
n)
n :: a
n = a
x'a -> a -> a
forall a. Semiring a => a -> a -> a
*a
x' a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a
y'a -> a -> a
forall a. Semiring a => a -> a -> a
*a
y'
Complex a
a mod :: Complex a -> Complex a -> Complex a
`mod` Complex a
b = Complex a
a Complex a -> Complex a -> Complex a
forall a. AbelianGroup a => a -> a -> a
- Complex a
bComplex a -> Complex a -> Complex a
forall a. Semiring a => a -> a -> a
*(Complex a
a Complex a -> Complex a -> Complex a
forall a. Euclidean a => a -> a -> a
`div` Complex a
b)
divMod :: Complex a -> Complex a -> (Complex a, Complex a)
divMod Complex a
a Complex a
b = (Complex a
q, Complex a
a Complex a -> Complex a -> Complex a
forall a. AbelianGroup a => a -> a -> a
- Complex a
bComplex a -> Complex a -> Complex a
forall a. Semiring a => a -> a -> a
*Complex a
q)
where q :: Complex a
q = Complex a
a Complex a -> Complex a -> Complex a
forall a. Euclidean a => a -> a -> a
`div` Complex a
b
euclideanNorm :: Complex a -> Natural
euclideanNorm (a
x:+a
y)
= Integer -> Natural
forall a. Euclidean a => a -> Natural
euclideanNorm (a -> Integer
forall a. ToInteger a => a -> Integer
toInteger (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
x)) Natural -> Natural -> Natural
forall a. AdditiveMonoid a => a -> a -> a
+
Integer -> Natural
forall a. Euclidean a => a -> Natural
euclideanNorm (a -> Integer
forall a. ToInteger a => a -> Integer
toInteger (a
ya -> a -> a
forall a. Semiring a => a -> a -> a
*a
y))
instance (FromRational a) => FromRational (Complex a) where
fromRational :: Rational -> Complex a
fromRational Rational
a = Rational -> a
forall a. FromRational a => Rational -> a
fromRational Rational
a a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
0
instance (Field a) => DivisionSemiring (Complex a) where
{-# SPECIALISE instance DivisionSemiring (Complex Float) #-}
{-# SPECIALISE instance DivisionSemiring (Complex Double) #-}
recip :: Complex a -> Complex a
recip (a
x:+a
y) = a
x' a -> a -> a
forall a. Semifield a => a -> a -> a
/ a
d a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (-a
y') a -> a -> a
forall a. Semifield a => a -> a -> a
/ a
d
where (a
x', a
y', a -> a
_) = a -> a -> (a, a, a -> a)
forall a. Semiring a => a -> a -> (a, a, a -> a)
rescale a
x a
y
d :: a
d = a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
x' a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a
ya -> a -> a
forall a. Semiring a => a -> a -> a
*a
y'
instance (Field a) => Semifield (Complex a) where
{-# SPECIALISE instance Semifield (Complex Float) #-}
{-# SPECIALISE instance Semifield (Complex Double) #-}
(a
x:+a
y) / :: Complex a -> Complex a -> Complex a
/ (a
x':+a
y') = (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
x''a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+a
ya -> a -> a
forall a. Semiring a => a -> a -> a
*a
y'') a -> a -> a
forall a. Semifield a => a -> a -> a
/ a
d a -> a -> Complex a
forall a. a -> a -> Complex a
:+ (a
ya -> a -> a
forall a. Semiring a => a -> a -> a
*a
x''a -> a -> a
forall a. AbelianGroup a => a -> a -> a
-a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
y'') a -> a -> a
forall a. Semifield a => a -> a -> a
/ a
d
where (a
x'', a
y'', a -> a
_) = a -> a -> (a, a, a -> a)
forall a. Semiring a => a -> a -> (a, a, a -> a)
rescale a
x' a
y'
d :: a
d = a
x'a -> a -> a
forall a. Semiring a => a -> a -> a
*a
x'' a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a
y'a -> a -> a
forall a. Semiring a => a -> a -> a
*a
y''
instance (Field a) => DivisionRing (Complex a)
instance (Field a) => Field (Complex a)
class (Ring a) => Num a where
abs :: a -> a
signum :: a -> a
class (Num a, ToRational a) => Real a where
class (Enum a, Real a, ToInteger a) => Integral a where
quot :: a -> a -> a
rem :: a -> a -> a
quotRem :: a -> a -> (a,a)
a
n `quot` a
d = a
q where (a
q,a
_) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
quotRem a
n a
d
a
n `rem` a
d = a
r where (a
_,a
r) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
quotRem a
n a
d
quotRem a
n a
d = if a -> a
forall a. Num a => a -> a
signum a
r a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== - a -> a
forall a. Num a => a -> a
signum a
d then (a
qa -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+a
1, a
ra -> a -> a
forall a. AbelianGroup a => a -> a -> a
-a
d) else (a, a)
qr
where qr :: (a, a)
qr@(a
q,a
r) = a -> a -> (a, a)
forall a. Euclidean a => a -> a -> (a, a)
divMod a
n a
d
class (Num a, FromRational a, Field a) => Fractional a where
class (Real a, Fractional a) => RealFrac a where
properFraction :: (Integral b) => a -> (b,a)
truncate, round :: (Integral b) => a -> b
ceiling, floor :: (Integral b) => a -> b
truncate a
x = b
m where (b
m,a
_) = a -> (b, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
forall b. Integral b => a -> (b, a)
properFraction a
x
round a
x = let (b
n,a
r) = a -> (b, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
forall b. Integral b => a -> (b, a)
properFraction a
x
m :: b
m = if a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 then b
n b -> b -> b
forall a. AbelianGroup a => a -> a -> a
- b
1 else b
n b -> b -> b
forall a. AdditiveMonoid a => a -> a -> a
+ b
1
in case a -> a
forall a. Num a => a -> a
signum (a -> a
forall a. Num a => a -> a
abs a
r a -> a -> a
forall a. AbelianGroup a => a -> a -> a
- a
0.5) of
-1 -> b
n
a
0 -> if b -> Bool
forall a. ToInteger a => a -> Bool
even b
n then b
n else b
m
a
1 -> b
m
a
_ -> [Char] -> b
forall a. HasCallStack => [Char] -> a
error [Char]
"round default defn: Bad value"
ceiling a
x = if a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 then b
n b -> b -> b
forall a. AdditiveMonoid a => a -> a -> a
+ b
1 else b
n
where (b
n,a
r) = a -> (b, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
forall b. Integral b => a -> (b, a)
properFraction a
x
floor a
x = if a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 then b
n b -> b -> b
forall a. AbelianGroup a => a -> a -> a
- b
1 else b
n
where (b
n,a
r) = a -> (b, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
forall b. Integral b => a -> (b, a)
properFraction a
x
class (Fractional a) => Floating a where
pi :: a
exp, log, sqrt :: a -> a
(**), logBase :: a -> a -> a
sin, cos, tan :: a -> a
asin, acos, atan :: a -> a
sinh, cosh, tanh :: a -> a
asinh, acosh, atanh :: a -> a
a
x ** a
y = a -> a
forall a. Floating a => a -> a
exp (a -> a
forall a. Floating a => a -> a
log a
x a -> a -> a
forall a. Semiring a => a -> a -> a
* a
y)
logBase a
x a
y = a -> a
forall a. Floating a => a -> a
log a
y a -> a -> a
forall a. Semifield a => a -> a -> a
/ a -> a
forall a. Floating a => a -> a
log a
x
sqrt a
x = a
x a -> a -> a
forall a. Floating a => a -> a -> a
** a
0.5
tan a
x = a -> a
forall a. Floating a => a -> a
sin a
x a -> a -> a
forall a. Semifield a => a -> a -> a
/ a -> a
forall a. Floating a => a -> a
cos a
x
tanh a
x = a -> a
forall a. Floating a => a -> a
sinh a
x a -> a -> a
forall a. Semifield a => a -> a -> a
/ a -> a
forall a. Floating a => a -> a
cosh a
x
class (RealFrac a, Floating a) => RealFloat a where
floatRadix :: a -> Integer
floatDigits :: a -> Int
floatRange :: a -> (Int,Int)
decodeFloat :: a -> (Integer,Int)
encodeFloat :: Integer -> Int -> a
exponent :: a -> Int
significand :: a -> a
scaleFloat :: Int -> a -> a
isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE
:: a -> Bool
atan2 :: a -> a -> a
exponent a
x = if Integer
m Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 then Int
0 else Int
n Int -> Int -> Int
forall a. AdditiveMonoid a => a -> a -> a
+ a -> Int
forall a. RealFloat a => a -> Int
floatDigits a
x
where (Integer
m,Int
n) = a -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
decodeFloat a
x
significand a
x = Integer -> Int -> a
forall a. RealFloat a => Integer -> Int -> a
encodeFloat Integer
m (- a -> Int
forall a. RealFloat a => a -> Int
floatDigits a
x)
where (Integer
m,Int
_) = a -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
decodeFloat a
x
scaleFloat Int
k a
x = Integer -> Int -> a
forall a. RealFloat a => Integer -> Int -> a
encodeFloat Integer
m (Int
nInt -> Int -> Int
forall a. AdditiveMonoid a => a -> a -> a
+Int
k)
where (Integer
m,Int
n) = a -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
decodeFloat a
x
atan2 a
y a
x
| a
xa -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a
0 = a -> a
forall a. Floating a => a -> a
atan (a
ya -> a -> a
forall a. Semifield a => a -> a -> a
/a
x)
| a
xa -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
0 Bool -> Bool -> Bool
&& a
ya -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a
0 = a
forall a. Floating a => a
pia -> a -> a
forall a. Semifield a => a -> a -> a
/a
2
| a
xa -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
0 Bool -> Bool -> Bool
&& a
ya -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a
0 = a
forall a. Floating a => a
pi a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a -> a
forall a. Floating a => a -> a
atan (a
ya -> a -> a
forall a. Semifield a => a -> a -> a
/a
x)
|(a
xa -> a -> Bool
forall a. Ord a => a -> a -> Bool
<=a
0 Bool -> Bool -> Bool
&& a
ya -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
0) Bool -> Bool -> Bool
||
(a
xa -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
0 Bool -> Bool -> Bool
&& a -> Bool
forall a. RealFloat a => a -> Bool
isNegativeZero a
y) Bool -> Bool -> Bool
||
(a -> Bool
forall a. RealFloat a => a -> Bool
isNegativeZero a
x Bool -> Bool -> Bool
&& a -> Bool
forall a. RealFloat a => a -> Bool
isNegativeZero a
y)
= -a -> a -> a
forall a. RealFloat a => a -> a -> a
atan2 (-a
y) a
x
| a
ya -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
0 Bool -> Bool -> Bool
&& (a
xa -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
0 Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isNegativeZero a
x)
= a
forall a. Floating a => a
pi
| a
xa -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
0 Bool -> Bool -> Bool
&& a
ya -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
0 = a
y
| Bool
otherwise = a
x a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a
y
even :: (ToInteger a) => a -> Bool
even :: forall a. ToInteger a => a -> Bool
even a
n = a
n a -> a -> a
forall a. Euclidean a => a -> a -> a
`mod` a
two a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. AdditiveMonoid a => a
zero
where two :: a
two = a
forall a. Semiring a => a
one a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a
forall a. Semiring a => a
one
odd :: (ToInteger a) => a -> Bool
odd :: forall a. ToInteger a => a -> Bool
odd = Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
forall a. ToInteger a => a -> Bool
even
(^) :: (Semiring a, ToInteger b) => a -> b -> a
a
x ^ :: forall a b. (Semiring a, ToInteger b) => a -> b -> a
^ b
n
| b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
>= b
forall a. AdditiveMonoid a => a
zero = a -> b -> a -> a
forall a b. (Semiring a, ToInteger b) => a -> b -> a -> a
natPower a
x b
n a
forall a. Semiring a => a
one
| Bool
otherwise = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Prelude.^: negative exponent"
natPower :: (Semiring a, ToInteger b) => a -> b -> a -> a
natPower :: forall a b. (Semiring a, ToInteger b) => a -> b -> a -> a
natPower a
x b
n a
y
| b
n b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== b
forall a. AdditiveMonoid a => a
zero = a
y
| b -> Bool
forall a. ToInteger a => a -> Bool
even b
n = a -> b -> a -> a
forall a b. (Semiring a, ToInteger b) => a -> b -> a -> a
natPower (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
x) (b
n b -> b -> b
forall a. Euclidean a => a -> a -> a
`div` b
two) a
y
| Bool
otherwise = a -> b -> a -> a
forall a b. (Semiring a, ToInteger b) => a -> b -> a -> a
natPower (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
x) (b
n b -> b -> b
forall a. Euclidean a => a -> a -> a
`div` b
two) (a
xa -> a -> a
forall a. Semiring a => a -> a -> a
*a
y)
where
two :: b
two = b
forall a. Semiring a => a
one b -> b -> b
forall a. AdditiveMonoid a => a -> a -> a
+ b
forall a. Semiring a => a
one
(^^) :: (DivisionSemiring a, AbelianGroup b, ToInteger b) => a -> b -> a
a
x ^^ :: forall a b.
(DivisionSemiring a, AbelianGroup b, ToInteger b) =>
a -> b -> a
^^ b
n = if b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
>= b
forall a. AdditiveMonoid a => a
zero then a
xa -> b -> a
forall a b. (Semiring a, ToInteger b) => a -> b -> a
^b
n else a -> a
forall a. DivisionSemiring a => a -> a
recip (a
xa -> b -> a
forall a b. (Semiring a, ToInteger b) => a -> b -> a
^(-b
n))
fromIntegral :: (ToInteger a, Ring b) => a -> b
fromIntegral :: forall a b. (ToInteger a, Ring b) => a -> b
fromIntegral = Integer -> b
forall a. Ring a => Integer -> a
fromInteger (Integer -> b) -> (a -> Integer) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Integer
forall a. ToInteger a => a -> Integer
toInteger
realToFrac :: (ToRational a, FromRational b) => a -> b
realToFrac :: forall a b. (ToRational a, FromRational b) => a -> b
realToFrac = Rational -> b
forall a. FromRational a => Rational -> a
fromRational (Rational -> b) -> (a -> Rational) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rational
forall a. ToRational a => a -> Rational
toRational
instance Num Int where
abs :: Int -> Int
abs = Int -> Int
forall a. Num a => a -> a
Prelude.abs
signum :: Int -> Int
signum = Int -> Int
forall a. Num a => a -> a
Prelude.signum
instance Real Int where
instance Integral Int where
instance Num Word where
abs :: Word -> Word
abs = Word -> Word
forall a. Num a => a -> a
Prelude.abs
signum :: Word -> Word
signum = Word -> Word
forall a. Num a => a -> a
Prelude.signum
instance Real Word where
instance Integral Word where
instance Num Integer where
abs :: Integer -> Integer
abs = Integer -> Integer
forall a. Num a => a -> a
Prelude.abs
signum :: Integer -> Integer
signum = Integer -> Integer
forall a. Num a => a -> a
Prelude.signum
instance Real Integer where
instance Integral Integer where
instance Num Float where
abs :: Float -> Float
abs = Float -> Float
forall a. Num a => a -> a
Prelude.abs
signum :: Float -> Float
signum = Float -> Float
forall a. Num a => a -> a
Prelude.signum
instance Real Float where
instance Fractional Float where
instance Floating Float where
pi :: Float
pi = Float
forall a. Floating a => a
Prelude.pi
exp :: Float -> Float
exp = Float -> Float
forall a. Floating a => a -> a
Prelude.exp
log :: Float -> Float
log = Float -> Float
forall a. Floating a => a -> a
Prelude.log
sin :: Float -> Float
sin = Float -> Float
forall a. Floating a => a -> a
Prelude.sin
cos :: Float -> Float
cos = Float -> Float
forall a. Floating a => a -> a
Prelude.cos
sinh :: Float -> Float
sinh = Float -> Float
forall a. Floating a => a -> a
Prelude.sinh
cosh :: Float -> Float
cosh = Float -> Float
forall a. Floating a => a -> a
Prelude.cosh
asin :: Float -> Float
asin = Float -> Float
forall a. Floating a => a -> a
Prelude.asin
acos :: Float -> Float
acos = Float -> Float
forall a. Floating a => a -> a
Prelude.acos
atan :: Float -> Float
atan = Float -> Float
forall a. Floating a => a -> a
Prelude.atan
asinh :: Float -> Float
asinh = Float -> Float
forall a. Floating a => a -> a
Prelude.asinh
acosh :: Float -> Float
acosh = Float -> Float
forall a. Floating a => a -> a
Prelude.acosh
atanh :: Float -> Float
atanh = Float -> Float
forall a. Floating a => a -> a
Prelude.atanh
instance RealFrac Float where
properFraction :: forall b. Integral b => Float -> (b, Float)
properFraction Float
x = (Integer -> b
forall a. Ring a => Integer -> a
fromInteger Integer
n, Float
r)
where (Integer
n,Float
r) = Float -> (Integer, Float)
forall b. Integral b => Float -> (b, Float)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
Prelude.properFraction Float
x
instance RealFloat Float where
floatRadix :: Float -> Integer
floatRadix = Float -> Integer
forall a. RealFloat a => a -> Integer
Prelude.floatRadix
floatDigits :: Float -> Int
floatDigits = Float -> Int
forall a. RealFloat a => a -> Int
Prelude.floatDigits
floatRange :: Float -> (Int, Int)
floatRange = Float -> (Int, Int)
forall a. RealFloat a => a -> (Int, Int)
Prelude.floatRange
decodeFloat :: Float -> (Integer, Int)
decodeFloat = Float -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
Prelude.decodeFloat
encodeFloat :: Integer -> Int -> Float
encodeFloat = Integer -> Int -> Float
forall a. RealFloat a => Integer -> Int -> a
Prelude.encodeFloat
isNaN :: Float -> Bool
isNaN = Float -> Bool
forall a. RealFloat a => a -> Bool
Prelude.isNaN
isInfinite :: Float -> Bool
isInfinite = Float -> Bool
forall a. RealFloat a => a -> Bool
Prelude.isInfinite
isDenormalized :: Float -> Bool
isDenormalized = Float -> Bool
forall a. RealFloat a => a -> Bool
Prelude.isDenormalized
isNegativeZero :: Float -> Bool
isNegativeZero = Float -> Bool
forall a. RealFloat a => a -> Bool
Prelude.isNegativeZero
isIEEE :: Float -> Bool
isIEEE = Float -> Bool
forall a. RealFloat a => a -> Bool
Prelude.isIEEE
instance Num Double where
abs :: Double -> Double
abs = Double -> Double
forall a. Num a => a -> a
Prelude.abs
signum :: Double -> Double
signum = Double -> Double
forall a. Num a => a -> a
Prelude.signum
instance Real Double where
instance Fractional Double where
instance Floating Double where
pi :: Double
pi = Double
forall a. Floating a => a
Prelude.pi
exp :: Double -> Double
exp = Double -> Double
forall a. Floating a => a -> a
Prelude.exp
log :: Double -> Double
log = Double -> Double
forall a. Floating a => a -> a
Prelude.log
sin :: Double -> Double
sin = Double -> Double
forall a. Floating a => a -> a
Prelude.sin
cos :: Double -> Double
cos = Double -> Double
forall a. Floating a => a -> a
Prelude.cos
sinh :: Double -> Double
sinh = Double -> Double
forall a. Floating a => a -> a
Prelude.sinh
cosh :: Double -> Double
cosh = Double -> Double
forall a. Floating a => a -> a
Prelude.cosh
asin :: Double -> Double
asin = Double -> Double
forall a. Floating a => a -> a
Prelude.asin
acos :: Double -> Double
acos = Double -> Double
forall a. Floating a => a -> a
Prelude.acos
atan :: Double -> Double
atan = Double -> Double
forall a. Floating a => a -> a
Prelude.atan
asinh :: Double -> Double
asinh = Double -> Double
forall a. Floating a => a -> a
Prelude.asinh
acosh :: Double -> Double
acosh = Double -> Double
forall a. Floating a => a -> a
Prelude.acosh
atanh :: Double -> Double
atanh = Double -> Double
forall a. Floating a => a -> a
Prelude.atanh
instance RealFrac Double where
properFraction :: forall b. Integral b => Double -> (b, Double)
properFraction Double
x = (Integer -> b
forall a. Ring a => Integer -> a
fromInteger Integer
n, Double
r)
where (Integer
n,Double
r) = Double -> (Integer, Double)
forall b. Integral b => Double -> (b, Double)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
Prelude.properFraction Double
x
instance RealFloat Double where
floatRadix :: Double -> Integer
floatRadix = Double -> Integer
forall a. RealFloat a => a -> Integer
Prelude.floatRadix
floatDigits :: Double -> Int
floatDigits = Double -> Int
forall a. RealFloat a => a -> Int
Prelude.floatDigits
floatRange :: Double -> (Int, Int)
floatRange = Double -> (Int, Int)
forall a. RealFloat a => a -> (Int, Int)
Prelude.floatRange
decodeFloat :: Double -> (Integer, Int)
decodeFloat = Double -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
Prelude.decodeFloat
encodeFloat :: Integer -> Int -> Double
encodeFloat = Integer -> Int -> Double
forall a. RealFloat a => Integer -> Int -> a
Prelude.encodeFloat
isNaN :: Double -> Bool
isNaN = Double -> Bool
forall a. RealFloat a => a -> Bool
Prelude.isNaN
isInfinite :: Double -> Bool
isInfinite = Double -> Bool
forall a. RealFloat a => a -> Bool
Prelude.isInfinite
isDenormalized :: Double -> Bool
isDenormalized = Double -> Bool
forall a. RealFloat a => a -> Bool
Prelude.isDenormalized
isNegativeZero :: Double -> Bool
isNegativeZero = Double -> Bool
forall a. RealFloat a => a -> Bool
Prelude.isNegativeZero
isIEEE :: Double -> Bool
isIEEE = Double -> Bool
forall a. RealFloat a => a -> Bool
Prelude.isIEEE
instance (Integral a) => Num (Ratio a) where
abs :: Ratio a -> Ratio a
abs (a
x:%a
y) = a -> a
forall a. Num a => a -> a
abs a
x a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
y
signum :: Ratio a -> Ratio a
signum (a
x:%a
_) = a -> a
forall a. Num a => a -> a
signum a
x a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
1
instance (Integral a, Prelude.Integral a) => Real (Ratio a) where
instance (Integral a) => Fractional (Ratio a) where
instance (Integral a, Prelude.Integral a) => RealFrac (Ratio a) where
properFraction :: forall b. Integral b => Ratio a -> (b, Ratio a)
properFraction (a
x:%a
y) = (a -> b
forall a b. (ToInteger a, Ring b) => a -> b
fromIntegral a
q, a
ra -> a -> Ratio a
forall a. a -> a -> Ratio a
:%a
y)
where (a
q,a
r) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
quotRem a
x a
y
{-# SPECIALISE magnitude :: Complex Double -> Double #-}
magnitude :: (Floating a) => Complex a -> a
magnitude :: forall a. Floating a => Complex a -> a
magnitude (a
x:+a
y) = a -> a
scale (a -> a
forall a. Floating a => a -> a
sqrt (a -> a
forall {a}. Semiring a => a -> a
sqr a
x' a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a -> a
forall {a}. Semiring a => a -> a
sqr a
y'))
where (a
x', a
y', a -> a
scale) = a -> a -> (a, a, a -> a)
forall a. Semiring a => a -> a -> (a, a, a -> a)
rescale a
x a
y
sqr :: a -> a
sqr a
z = a
z a -> a -> a
forall a. Semiring a => a -> a -> a
* a
z
{-# 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
phase (a
x:+a
y) = a -> a -> a
forall a. RealFloat a => a -> a -> a
atan2 a
y a
x
instance (RealFloat a) => Num (Complex a) where
{-# SPECIALISE instance Num (Complex Float) #-}
{-# SPECIALISE instance Num (Complex Double) #-}
abs :: Complex a -> Complex a
abs Complex a
z = Complex a -> a
forall a. Floating 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. Semifield a => a -> a -> a
/a
r a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
ya -> a -> a
forall a. Semifield a => a -> a -> a
/a
r where r :: a
r = Complex a -> a
forall a. Floating a => Complex a -> a
magnitude Complex a
z
instance (RealFloat a) => Fractional (Complex a) where
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. Semiring 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. Semiring 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. Floating 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
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. Semifield a => a -> a -> a
/ (a
u'a -> a -> a
forall a. Semiring a => a -> a -> a
*a
2)
u' :: a
u' = a -> a
forall a. Floating a => a -> a
sqrt ((Complex a -> a
forall a. Floating a => Complex a -> a
magnitude Complex a
z a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a -> a
forall a. Num a => a -> a
abs a
x) a -> a -> a
forall a. Semifield 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. Semiring 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. Semiring 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. Semiring 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. Semiring 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. Semiring a => a -> a -> a
*a
coshya -> a -> Complex a
forall a. a -> a -> Complex a
:+a
cosxa -> a -> a
forall a. Semiring a => a -> a -> a
*a
sinhy)Complex a -> Complex a -> Complex a
forall a. Semifield a => a -> a -> a
/(a
cosxa -> a -> a
forall a. Semiring a => a -> a -> a
*a
coshya -> a -> Complex a
forall a. a -> a -> Complex a
:+(-a
sinxa -> a -> a
forall a. Semiring 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. Semiring 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. Semiring 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. Semiring 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. Semiring 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. Semiring a => a -> a -> a
*a
sinhxa -> a -> Complex a
forall a. a -> a -> Complex a
:+a
sinya -> a -> a
forall a. Semiring a => a -> a -> a
*a
coshx)Complex a -> Complex a -> Complex a
forall a. Semifield a => a -> a -> a
/(a
cosya -> a -> a
forall a. Semiring a => a -> a -> a
*a
coshxa -> a -> Complex a
forall a. a -> a -> Complex a
:+a
sinya -> a -> a
forall a. Semiring 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. AdditiveMonoid 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. AbelianGroup a => a -> a -> a
- Complex a
zComplex a -> Complex a -> Complex a
forall a. Semiring 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. AdditiveMonoid 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. AbelianGroup a => a -> a -> a
- Complex a
zComplex a -> Complex a -> Complex a
forall a. Semiring 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. AbelianGroup 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. Semifield 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. AdditiveMonoid a => a -> a -> a
+Complex a
zComplex a -> Complex a -> Complex a
forall a. Semiring 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. AdditiveMonoid 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. AdditiveMonoid a => a -> a -> a
+Complex a
zComplex a -> Complex a -> Complex a
forall a. Semiring a => a -> a -> a
*Complex a
z))
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. AdditiveMonoid a => a -> a -> a
+ (Complex a
zComplex a -> Complex a -> Complex a
forall a. AdditiveMonoid a => a -> a -> a
+Complex a
1) Complex a -> Complex a -> Complex a
forall a. Semiring a => a -> a -> a
* Complex a -> Complex a
forall a. Floating a => a -> a
sqrt ((Complex a
zComplex a -> Complex a -> Complex a
forall a. AbelianGroup a => a -> a -> a
-Complex a
1)Complex a -> Complex a -> Complex a
forall a. Semifield a => a -> a -> a
/(Complex a
zComplex a -> Complex a -> Complex a
forall a. AdditiveMonoid 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. Semiring 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. AdditiveMonoid a => a -> a -> a
+Complex a
z) Complex a -> Complex a -> Complex a
forall a. Semifield a => a -> a -> a
/ (Complex a
1.0Complex a -> Complex a -> Complex a
forall a. AbelianGroup a => a -> a -> a
-Complex a
z))
ifThenElse :: Bool -> a -> a -> a
ifThenElse :: forall a. Bool -> a -> a -> a
ifThenElse Bool
True a
x a
_ = a
x
ifThenElse Bool
False a
_ a
y = a
y