{-# LANGUAGE ExplicitNamespaces, DataKinds, FlexibleInstances, TypeFamilies,
TypeOperators, ConstraintKinds, ScopedTypeVariables,
FlexibleContexts, UndecidableInstances, CPP #-}
#if __GLASGOW_HASKELL__ >= 711
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
#endif
module Data.Metrology.Poly (
zero, (|+|), (|-|), qSum, qNegate,
(|*|), (|/|),
(*|), (|*), (/|), (|/),
(|^), (|^^), qNthRoot,
qSq, qCube, qSqrt, qCubeRoot,
qCompare, (|<|), (|>|), (|<=|), (|>=|), (|==|), (|/=|),
qApprox, qNapprox,
numIn, (#), quOf, (%), showIn,
unity, redim, convert,
defaultLCSU, constant,
(:*)(..), (:/)(..), (:^)(..), (:@)(..),
UnitPrefix(..),
type (%*), type (%/), type (%^),
Qu, MkQu_D, MkQu_DLN, MkQu_U, MkQu_ULN,
Dimension,
Unit(type BaseUnit, type DimOfUnit, conversionRatio),
Canonical,
Dimensionless(..), Number(..), Count, quantity,
MkLCSU, LCSU(DefaultLCSU), DefaultUnitOfDim,
CompatibleUnit, CompatibleDim, ConvertibleLCSUs_D,
DefaultConvertibleLCSU_D, DefaultConvertibleLCSU_U,
MultDimFactors, MultUnitFactors, UnitOfDimFactors,
Z(..), Succ, Pred, type (#+), type (#-), type (#*), type (#/), Negate,
One, Two, Three, Four, Five, MOne, MTwo, MThree, MFour, MFive,
sZero, sOne, sTwo, sThree, sFour, sFive,
sMOne, sMTwo, sMThree, sMFour, sMFive,
sSucc, sPred, sNegate,
pZero, pOne, pTwo, pThree, pFour, pFive,
pMOne, pMTwo, pMThree, pMFour, pMFive,
pSucc, pPred,
module Data.Metrology.Internal
) where
import Data.Metrology.Z
import Data.Metrology.Qu
import Data.Metrology.Dimensions
import Data.Metrology.Factor
import Data.Metrology.Units
import Data.Metrology.Combinators
import Data.Metrology.LCSU
import Data.Metrology.Validity
import Data.Metrology.Internal
import Data.Foldable as F
import Data.Proxy
numIn :: forall unit dim lcsu n.
( ValidDLU dim lcsu unit
, Fractional n )
=> Qu dim lcsu n -> unit -> n
numIn (Qu val) u
= val * fromRational
(canonicalConvRatioSpec (Proxy :: Proxy (LookupList dim lcsu))
/ canonicalConvRatio u)
infix 5 #
(#) :: ( ValidDLU dim lcsu unit
, Fractional n )
=> Qu dim lcsu n -> unit -> n
(#) = numIn
quOf :: forall unit dim lcsu n.
( ValidDLU dim lcsu unit
, Fractional n )
=> n -> unit -> Qu dim lcsu n
quOf d u
= Qu (d * fromRational
(canonicalConvRatio u
/ canonicalConvRatioSpec (Proxy :: Proxy (LookupList dim lcsu))))
infix 5 %
(%) :: ( ValidDLU dim lcsu unit
, Fractional n )
=> n -> unit -> Qu dim lcsu n
(%) = quOf
infix 1 `showIn`
showIn :: ( ValidDLU dim lcsu unit
, Fractional n
, Show unit
, Show n )
=> Qu dim lcsu n -> unit -> String
showIn x u = show (x # u) ++ " " ++ show u
convert :: forall d l1 l2 n.
( ConvertibleLCSUs d l1 l2
, Fractional n )
=> Qu d l1 n -> Qu d l2 n
convert (Qu x) = Qu $ x * fromRational (
canonicalConvRatioSpec (Proxy :: Proxy (LookupList d l1))
/ canonicalConvRatioSpec (Proxy :: Proxy (LookupList d l2)))
constant :: ( d @~ e
, ConvertibleLCSUs e DefaultLCSU l
, Fractional n )
=> Qu d DefaultLCSU n -> Qu e l n
constant = convert . redim
zero :: Num n => Qu dimspec l n
zero = Qu 0
infixl 6 |+|
(|+|) :: (d1 @~ d2, Num n) => Qu d1 l n -> Qu d2 l n -> Qu d1 l n
(Qu a) |+| (Qu b) = Qu (a + b)
infixl 6 |-|
(|-|) :: (d1 @~ d2, Num n) => Qu d1 l n -> Qu d2 l n -> Qu d1 l n
a |-| b = a |+| qNegate b
qSum :: (Foldable f, Num n) => f (Qu d l n) -> Qu d l n
qSum = F.foldr (|+|) zero
qNegate :: Num n => Qu d l n -> Qu d l n
qNegate (Qu x) = Qu (negate x)
infixl 7 *| , |* , |/
(*|) :: Num n => n -> Qu b l n -> Qu b l n
a *| (Qu b) = Qu (a * b)
(|*) :: Num n => Qu a l n -> n -> Qu a l n
(Qu a) |* b = Qu (a * b)
(|/) :: Fractional n => Qu a l n -> n -> Qu a l n
(Qu a) |/ b = Qu (a / b)