{-# LANGUAGE RebindableSyntax #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.YAP.Algebra.Internal
-- Copyright   :  (c) Ross Paterson 2011
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  R.Paterson@city.ac.uk
-- Stability   :  provisional
-- Portability :  portable
--
-- Internal definitions.
--
-----------------------------------------------------------------------------

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  %

-------------------------------------------------------------------------
-- Algebraic classes
-------------------------------------------------------------------------

-- | A commutative associative binary operation with an identity.
--
-- /Rationale:/
--
-- * This is the common superclass of 'AbelianGroup' and 'Semiring'.
--   General monoids are also useful, but it is a common expectation that
--   an operation denoted by '+' is commutative.
--
-- * 'zero' is required because this class is insufficient for integer
--   literals.
--
-- * Ideally, @0@ could be defined as equivalent to 'zero', with other
--   integer literals handled by 'fromNatural'.
class  AdditiveMonoid a  where
    -- | An associative operation.
    (+)              :: a -> a -> a

    -- | The identity of @('+')@.
    zero             :: a

    -- | Sum of @n@ copies of @x@. @n@ should be non-negative.
    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

-- | An Abelian group has a commutative associative binary operation
-- with an identity and inverses.
--
-- /Rationale:/
--
-- * The 'Prelude.abs' and 'Prelude.signum' operations lack sensible
--   definitions for many useful instances, such as complex numbers,
--   polynomials, matrices, etc.
--
-- * Types that have subtraction but not multiplication include vectors
--   and dimensioned quantities.
class  (AdditiveMonoid a) => AbelianGroup a  where
    -- | Subtraction operator.
    (-)              :: a -> a -> a

    -- | Inverse for @('+')@ (unary negation).
    negate           :: a -> a

    -- | Sum of @n@ copies of @x@.
    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 #-}

-- | A semiring: addition defines a commutative monoid, and multiplication
-- defines a monoid and distributes over addition and zero.
-- Multiplication is not guaranteed to be commutative.
--
-- /Rationale:/
--
-- * 'Natural' is the key example of a type with multiplication but not
--   subtraction, but there are many more.
--
-- * 'one' is required because this class is insufficient for integer
--   literals.
--
-- * In an ideal world, an integer literal @i@ would be treated as
--   equivalent to @'fromNatural' i@ and have type @('Semiring' a) => a@.
--   (The lexical syntax already permits only non-negative numbers.)
--
-- * 'rescale' is available here with a trivial default definition so
--   that some operations on complex numbers, whose direct definitions
--   would often overflow on 'Float' or 'Double' components, can be
--   defined for other types as well.
class  (AdditiveMonoid a) => Semiring a where
    -- | An associative operation that distributes over '(+)'.
    (*)              :: a -> a -> a

    -- | The identity of @('*')@.
    one              :: a
    one              =  Natural -> a
forall a. Semiring a => Natural -> a
fromNatural (Integer -> Natural
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
1)

    -- | Conversion from 'Natural', the initial semiring:
    -- 'fromNatural' is the unique function preserving 'zero', 'one',
    -- @('+')@ and @('*')@.
    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' x y = (x', y', s)@ where @s@ is a linear function such
    -- that @s x' = x@ and @s y' = y@, chosen so that multiplications
    -- by @x'@ and @y'@ are less likely to overflow.  In the default
    -- definition, @s@ is 'id'.
    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) #-}

-- | A ring: addition forms an Abelian group, and multiplication defines
-- a monoid and distributes over addition.
-- Multiplication is not guaranteed to be commutative.
--
-- /Rationale:/
--
-- * This is sufficient to define 'fromInteger', but often a much more
--   efficient definition is available.
class  (AbelianGroup a, Semiring a) => Ring a where
    -- | Conversion from 'Integer', the initial ring:
    -- 'fromInteger' is the unique function preserving 'zero', 'one',
    -- @('+')@, @('-')@ and @('*')@.
    --
    -- An integer literal represents the application of the function
    -- 'fromInteger' to the appropriate value of type 'Integer',
    -- so such literals have type @('Ring' a) => a@.
    -- (Ideally, they would represent an application of 'fromNatural',
    -- and have type @('Semiring' a) => a@.)
    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

-- | A cancellative commutative semiring with a designated canonical
-- factoring of each value as the product of a unit (invertible value)
-- and an associate.
--
-- /Rationale:/
--
-- * This normalization is required so that the results of operations
--   such as 'gcd' and 'lcm' can be uniquely defined.  In the original
--   Prelude, 'Prelude.abs' and 'Prelude.signum' were used for this,
--   but they lack coherent definitions on many useful instances.
--
-- * This class is usually used together with 'Euclidean', and
--   generally has matching instances.  Merging the two classes would
--   be a possibility.  That would allow a default definition of
--   'stdAssociate'.
class  (Semiring a) => StandardAssociate a  where
    -- | A representative associate:
    --
    -- * if @x@ and @y@ are factors of each other,
    --   then @'stdAssociate' x = 'stdAssociate' y@
    --
    -- * @'stdAssociate' ('stdAssociate' x) = 'stdAssociate' x@
    --
    -- * @'stdAssociate' 'zero' = 'zero'@
    --
    -- * @'stdAssociate' 'one' = 'one'@
    --
    -- For integral types, @'stdAssociate' x@ is a non-negative integer.
    stdAssociate     :: a -> a

    -- | @'stdUnit' x@ has a multiplicative inverse and satisfies
    --
    -- * @'stdAssociate' x * 'stdUnit' x = x@
    --
    -- * @'stdUnit' 'zero' = 'one'@
    --
    -- * @'stdUnit' 'one' = 'one'@
    --
    -- For integral types, @'stdUnit' x@ is @1@ or @-1@.
    stdUnit          :: a -> a

    -- | multiplicative inverse of @'stdUnit' x@
    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

-- | A Euclidean semiring: a commutative semiring with Euclidean division,
-- yielding a quotient and a remainder that is smaller than the divisor in
-- a well-founded ordering.  This is sufficient to implement Euclid's
-- algorithm for the greatest common divisor.
--
-- /Rationale:/
--
-- * This class, together with 'StandardAssociate', is sufficient
--   to define 'gcd', and thus to define arithmetic operations on
--   @'Data.Ratio.Ratio' a@.
--
-- * The uniformity condition is required to make modular arithmetic work.
--   Non-integer examples include @'Data.Complex.Complex' 'Integer'@
--   (Gaussian integers) and polynomials.
--
-- * The usual definition of a Euclidean domain assumes a ring, but
--   division with remainder can be defined in the absence of negation,
--   e.g. for 'Natural'.
class  (Semiring a) => Euclidean a  where
    -- | Division with remainder: for any @d@ that is not 'zero',
    --
    -- * @n = 'div' n d * d + 'mod' n d@
    div              :: a -> a -> a

    -- | Remainder of division: for any @d@ that is not 'zero',
    --
    -- * @n = 'div' n d * d + 'mod' n d@
    --
    -- * @'mod' (n + a*d) d = 'mod' n d@
    --
    -- * @'div' 'zero' d = 'zero'@
    --
    -- * either @'mod' n d@ is 'zero' or
    --   @'euclideanNorm' ('mod' n d) < 'euclideanNorm' d@.
    --
    -- For integral types, @'mod' n d@ is a non-negative integer smaller
    -- than the absolute value of @d@.
    mod              :: a -> a -> a

    -- | @'divMod' n d = ('div' n d, 'mod' n d)@
    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

    -- | A measure of the size of a non-zero value.
    -- This may be undefined on 'zero'.
    -- If the argument is non-zero, the value is positive.
    euclideanNorm :: a -> Natural

    {-# MINIMAL ((divMod | (div, mod)), euclideanNorm) #-}

-- | Types that can be faithfully embedded in 'Rational'.
--
-- /Rationale:/
--
-- * This is essentially equivalent to the old 'Prelude.Real' class,
--   but with the 'Prelude.Num' superclass reduced to 'Semiring'
--   (so it does not assume negative values).
class  (Ord a, Semiring a) => ToRational a where
    -- | The rational equivalent of its argument with full precision
    toRational       ::  a -> Rational

-- | Types representing a contiguous set of integers, including 0, 1 and 2.
--
-- /Rationale:/
--
-- * This is similar to the old 'Prelude.Integral' class, but does not
--   require subtraction, which does not work for 'Natural'.
class  (StandardAssociate a, Euclidean a, ToRational a) => ToInteger a where
    -- | Conversion to 'Integer', satisfying
    --
    -- @'fromInteger' ('toInteger' x) = x@
    toInteger        :: a -> Integer

-- | A 'Semiring' in which all non-zero elements have multiplicative inverses.
--
-- /Rationale:/
--
-- * Quaternions have multiplicative inverses, but do not form a field,
--   because multiplication is not commutative.
--
-- * Some semirings, such as the tropical semiring and its dual, support
--   division but not subtraction.
class  (Semiring a) => DivisionSemiring a  where
    -- | Multiplicative inverse of any value but 'zero'.
    recip            :: a -> a

-- | A 'Ring' in which all non-zero elements have multiplicative inverses.
--
-- /Rationale:/
--
-- * Quaternions have multiplicative inverses, but multiplication is not
--   commutative, so they do not have a well-defined division operation,
--   and do not form a field.
class  (Ring a, DivisionSemiring a) => DivisionRing a

-- | A commutative 'Semiring' in which all non-zero elements have
-- multiplicative inverses, so that division is uniquely defined.
--
-- /Rationale:/
--
-- * Some semirings, such as the tropical semiring and its dual, support
--   division but not subtraction.
class  (DivisionSemiring a) => Semifield a  where
    -- | Division operator.
    (/)              :: 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 #-}

-- | A commutative 'Ring' in which all non-zero elements have
-- multiplicative inverses.
--
-- /Rationale:/
--
-- * 'fromRational' needs to be in an independent class, because 'Rational'
--   cannot be embedded in finite fields.
--
-- * While fields trivially support Euclidean division and standard
--   associates, they are kept apart for backward compatibility.
class  (DivisionRing a, Semifield a) => Field a

-- | Rings extending 'Rational'.
-- Such a ring should have characteristic 0, i.e. no sum @1 + ... + 1@
-- should equal zero.
--
-- /Rationale:/
--
-- * For 'fromRational' to be injective, its domain must be infinite,
--   which naturally excludes finite fields.
--   Indeed 'Rational' is initial in the category of infinite fields.
--   (It's true that 'Float' and 'Double' aren't infinite, but they aren't
--   even additive monoids.)
--
-- * Conversely, many rings that are not fields support embedding of
--   rationals, e.g. polynomials and matrices.
class  (Ring a) => FromRational a  where
    -- | Conversion from a 'Rational' (that is, @'Ratio' 'Integer'@)
    -- preserving 'zero', 'one', @('+')@, @('-')@ and @('*')@.
    -- Under the assumption that the ring has characteristic 0, this
    -- implies that 'fromRational' is injective.
    --
    -- For an infinite field, 'fromRational' is the unique function
    -- preserving 'zero', 'one', @('+')@, @('-')@, @('*')@ and @('/')@.
    --
    -- A floating literal stands for an application of 'fromRational'
    -- to a value of type 'Rational', so such literals have type
    -- @('FromRational' a) => a@.
    fromRational    :: Rational -> a

-------------------------------------------------------------------------
-- instances for Prelude integral types
-------------------------------------------------------------------------

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

-- | Units have absolute value 1. Standard associates are non-negative.
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

-- | `mod` is non-negative
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

-- | The only unit is 1. 'stdAssociate' is the identity.
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

-- | The only unit is 1. 'stdAssociate' is the identity.
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

-- | Units have absolute value 1. Standard associates are non-negative.
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

-- | `mod` is non-negative
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

-------------------------------------------------------------------------
-- instances for Prelude floating point types
-------------------------------------------------------------------------

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

-- rescaling for IEEE floats
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

-- Numeric functions

-- | Euclid's algorithm, yielding a maximal common divisor of @x@ and @y@,
-- but not necessarily a canonical associate.
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' x y@ is a common factor of @x@ and @y@ such that
--
-- * @'stdAssociate' ('gcd' x y) = 'gcd' x y@, and
--
-- * any common factor of @x@ and @y@ is a factor of @'gcd' x 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' x y@ is a common multiple of @x@ and @y@ such that
--
-- * @'stdAssociate' ('lcm' x y) = 'lcm' x y@, and
--
-- * any common multiple of @x@ and @y@ is a multiple of @'lcm' x 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)

-------------------------------------------------------------------------
-- instances for other Prelude types
-------------------------------------------------------------------------

-- | Direct product
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)

-- | Direct product
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)

-- | Direct product
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)

-- | Direct product
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)

-- | Direct product
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)

-- -----------------------------------------------------------------------------
-- Functions on Ratio

-- | Forms the ratio of two values in a Euclidean domain (e.g. 'Integer').
{-# SPECIALISE (%) :: Integer -> Integer -> Rational #-}
(%)                     :: (Eq a, StandardAssociate a, Euclidean a) =>
                           a -> a -> Ratio a
-- y /= 0 && x % y = n :% d ==>
--      stdAssociate d = d &&
--      x*d = y*n
--      x*d' = y*n' ==> exists a. d' = a*d && n' = a*n
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

-- | Extract the numerator of the ratio in reduced form:
-- the numerator and denominator have no common factor and the denominator
-- is positive.
numerator               :: Ratio a -> a
numerator :: forall a. Ratio a -> a
numerator (a
x :% a
_)      =  a
x

-- | Extract the denominator of the ratio in reduced form:
-- the numerator and denominator have no common factor and the denominator
-- is positive.
denominator             :: Ratio a -> a
denominator :: forall a. Ratio a -> a
denominator (a
_ :% a
y)    =  a
y

-- -----------------------------------------------------------------------------
-- Instances of the type Ratio (defined in the standard module Data.Ratio)
-- need to be defined here so that they're not orphans.

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)

-- | The original version of 'Integral' is required here by the old instance
-- @'Ord' ('Ratio' a)@.  Ideally this would be replaced with @'Ord' a@.
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

{- instances defined in Data.Ratio: derived Eq plus

instance  (Ord a, Semiring a) => Ord (Ratio a)  where
    (x:%y) <= (x':%y')  =  x * y' <= x' * y
    (x:%y) <  (x':%y')  =  x * y' <  x' * y

ratPrec = 7 :: Int

instance  (Eq a, StandardAssociate a, Euclidean a, Read a) => Read (Ratio a)  where
    readsPrec p  =  readParen (p > ratPrec)
                              (\r -> [(x%y,u) | (x,s)   <- readsPrec (ratPrec+1) r,
                                                ("%",t) <- lex s,
                                                (y,u)   <- readsPrec (ratPrec+1) t ])

instance  (Show a)  => Show (Ratio a)  where
    showsPrec p (x:%y)  =  showParen (p > ratPrec)
                               (showsPrec (ratPrec+1) x . 
                                showString " % " . 
                                showsPrec (ratPrec+1) y)
-}

-- -----------------------------------------------------------------------------
-- Instances of the type Complex (defined in the standard module Data.Complex)
-- need to be defined here so that they're not orphans.

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

-- | Gaussian integers:
-- units have magnitude 1;
-- standard associates are natural numbers or in the positive quadrant.
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

-- | Gaussian integers:
-- if @b@ is non-zero, the norm (squared magnitude) of @'mod' a b@
-- is at most half that of @b@.
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) => Field (Complex a)  where
    (x:+y) / (x':+y')   =  (x*x'+y*y') / d :+ (y*x'-x*y') / d
                           where d   = x'*x' + y'*y'
-}

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

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

-------------------------------------------------------------------------
-- refactored Haskell 98 classes
-------------------------------------------------------------------------

-- all builtin numeric types, plus Ratio and Complex
-- | Haskell 98 compatibility class
class  (Ring a) => Num a  where
    -- | Absolute value.
    abs                 :: a -> a
    -- | Sign of a number.
    -- The functions 'abs' and 'signum' should satisfy the law:
    --
    -- > abs x * signum x == x
    --
    -- For real numbers, the 'signum' is either @-1@ (negative), @0@ (zero)
    -- or @1@ (positive).
    signum              :: a -> a

-- | Haskell 98 compatibility class
class  (Num a, ToRational a) => Real a  where

-- | Subsets of 'Integer', supporting integer division.
class  (Enum a, Real a, ToInteger a) => Integral a  where
    -- | Integer division truncated toward zero
    quot                :: a -> a -> a
    -- | Integer remainder, satisfying
    --
    -- > (x `quot` y)*y + (x `rem` y) == x
    rem                 :: a -> a -> a
    -- | simultaneous 'quot' and 'rem'
    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

-- | Haskell 98 compatibility class
class  (Num a, FromRational a, Field a) => Fractional a  where

-------------------------------------------------------------------------
-- unchanged Haskell 98 classes
-------------------------------------------------------------------------

-- | unchanged from Haskell 98
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

        -- Minimal complete definition:
        --      properFraction
    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

-- | unchanged from Haskell 98
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

        -- Minimal complete definition:
        --      pi, exp, log, sin, cos, sinh, cosh
        --      asin, acos, atan
        --      asinh, acosh, atanh
    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

-- | unchanged from Haskell 98
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

        -- Minimal complete definition:
        --      All except exponent, significand, 
        --                 scaleFloat, atan2
    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    -- must be after the previous test on zero y
      | 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     -- must be after the other double zero tests
      | Bool
otherwise     =  a
x a -> a -> a
forall a. AdditiveMonoid a => a -> a -> a
+ a
y -- x or y is a NaN, return a NaN (via +)

-------------------------------------------------------------------------
-- Numeric functions
-------------------------------------------------------------------------

-- | The input value divides evenly by 2.
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

-- | The input value does not divide evenly by 2.
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

-- | raise a number to a non-negative integral power
(^)              :: (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

-- | raise a number to an integral power
(^^)             :: (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))

-- | General conversion from integral types, via the 'Integer' type.
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

-- | General conversion to fractional types, via the 'Rational' type.
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

-------------------------------------------------------------------------
-- instances for Prelude numeric types
-------------------------------------------------------------------------

-- | As in "Prelude".
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

-- | As in "Prelude".
instance  Real Int  where

-- | As in "Prelude".
instance  Integral Int  where

-- | As in "Prelude".
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

-- | As in "Prelude".
instance  Real Word  where

-- | As in "Prelude".
instance  Integral Word  where

-- | As in "Prelude".
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

-- | As in "Prelude".
instance  Real Integer  where

-- | As in "Prelude".
instance  Integral Integer  where

-- | As in "Prelude".
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

-- | As in "Prelude".
instance  Real Float  where

-- | As in "Prelude".
instance  Fractional Float  where

-- | As in "Prelude".
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

-- | As in "Prelude".
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

-- | As in "Prelude".
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

-- | As in "Prelude".
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

-- | As in "Prelude".
instance  Real Double  where

-- | As in "Prelude".
instance  Fractional Double  where

-- | As in "Prelude".
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

-- | As in "Prelude".
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

-- | As in "Prelude".
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

-- | As in "Data.Ratio".
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

-- -----------------------------------------------------------------------------
-- Instances of Ratio

-- | As in "Data.Ratio".  The original version of 'Integral' is required
-- here by the old instance @'Ord' ('Ratio' a)@.  Ideally there would
-- be only one.
instance  (Integral a, Prelude.Integral a)  => Real (Ratio a)  where

-- | As in "Data.Ratio".
instance  (Integral a)  => Fractional (Ratio a)  where

-- | As in "Data.Ratio".  The original version of 'Integral' is required
-- here by the old instance @'Ord' ('Ratio' a)@.  Ideally there would
-- be only one.
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

-- -----------------------------------------------------------------------------
-- Functions over Complex

-- | The nonnegative magnitude of a complex number.
{-# 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

-- | The phase of a complex number, in the range @(-'pi', 'pi']@.
-- If the magnitude is zero, then so is the phase.
--
-- 'RealFloat' is needed for 'atan2'.
{-# 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            -- SLPJ July 97 from John Peterson
phase (a
x:+a
y)     = a -> a -> a
forall a. RealFloat a => a -> a -> a
atan2 a
y a
x

-- -----------------------------------------------------------------------------
-- Instances of Complex (largely broken)

-- | As in "Data.Complex".
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

-- | As in "Data.Complex".
instance  (RealFloat a) => Fractional (Complex a)  where

-- sqrt uses (<) and abs
-- log uses phase, which uses atan2
-- inverse trig and hyp functions use sqrt and log

-- | As in "Data.Complex".
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))

-- | Standard implementation of if-then-else, needed for @RebindableSyntax@.
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