{-# LANGUAGE TypeOperators, FlexibleContexts, DataKinds, TypeFamilies, CPP,
             ScopedTypeVariables, ConstraintKinds, GeneralizedNewtypeDeriving #-}

#if __GLASGOW_HASKELL__ >= 711
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
#endif

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Metrology.Vector
-- Copyright   :  (C) 2014 Richard Eisenberg
-- License     :  BSD-style (see LICENSE)
-- Maintainer  :  Richard Eisenberg (rae@cs.brynmawr.edu)
-- Stability   :  experimental
-- Portability :  non-portable
--
-- Exports combinators for building quantities out of vectors, from the
-- vector-space library.
------------------------------------------------------------------------------

module Data.Metrology.Vector (
  -- * Term-level combinators

  -- | The term-level arithmetic operators are defined by
  -- applying vertical bar(s) to the sides the dimensioned
  -- quantities acts on.

  -- ** Additive operations
  zero, (|+|), (|-|), qSum, qNegate,

  -- ** Multiplicative operations between non-vector quantities
  (|*|), (|/|), (/|),

  -- ** Multiplicative operations between a vector and a scalar
  (*|), (|*), (|/),

  -- ** Multiplicative operations on vectors
  (|*^|), (|^*|), (|^/|), (|.|),

  -- ** Exponentiation
  (|^), (|^^), qNthRoot,
  qSq, qCube, qSqrt, qCubeRoot,

  -- ** Other vector operations
  qMagnitudeSq, qMagnitude, qNormalized, qProject, qCross2, qCross3,

  -- ** Affine operations
  Point(..), QPoint, (|.-.|), (|.+^|), (|.-^|), qDistanceSq, qDistance,
  pointNumIn, (.#), quOfPoint, (%.),

  -- ** Comparison
  qCompare, (|<|), (|>|), (|<=|), (|>=|), (|==|), (|/=|),
  qApprox, qNapprox,

  -- * Nondimensional units, conversion between quantities and numeric values
  numIn, (#), quOf, (%), showIn,
  unity, redim, convert,
  defaultLCSU, constant,

  -- * Type-level unit combinators
  (:*)(..), (:/)(..), (:^)(..), (:@)(..),
  UnitPrefix(..),

  -- * Type-level quantity combinators
  type (%*), type (%/), type (%^),

  -- * Creating quantity types
  Qu, MkQu_D, MkQu_DLN, MkQu_U, MkQu_ULN,

  -- * Creating new dimensions
  Dimension,

  -- * Creating new units
  Unit(type BaseUnit, type DimOfUnit, conversionRatio),
  Canonical,

  -- * Numbers, the only built-in unit
  Dimensionless(..), Number(..), Count, quantity,

  -- * LCSUs (locally coherent system of units)
  MkLCSU, LCSU(DefaultLCSU), DefaultUnitOfDim,

  -- * Validity checks and assertions
  CompatibleUnit, CompatibleDim, ConvertibleLCSUs_D,
  DefaultConvertibleLCSU_D, DefaultConvertibleLCSU_U,
  MultDimFactors, MultUnitFactors, UnitOfDimFactors,

  -- * Type-level integers
  Z(..), Succ, Pred, type (#+), type (#-), type (#*), type (#/), Negate,

  -- ** Synonyms for small numbers
  One, Two, Three, Four, Five, MOne, MTwo, MThree, MFour, MFive,

  -- ** Term-level singletons
  sZero, sOne, sTwo, sThree, sFour, sFive,
  sMOne, sMTwo, sMThree, sMFour, sMFive,
  sSucc, sPred, sNegate,

  -- * Internal definitions
  -- | The following module is re-exported solely to prevent noise in error messages;
  -- we do not recommend trying to use these definitions in user code.
  module Data.Metrology.Internal

  ) where

import Data.Metrology.Qu
import Data.Metrology.LCSU
import Data.Metrology.Validity
import Data.Metrology.Factor
import Data.Metrology.Z as Z
import Data.Metrology.Units
import Data.Metrology.Combinators
import Data.Metrology.Dimensions
import Data.Metrology.Internal

import Data.AffineSpace
import Data.VectorSpace
import Data.Cross hiding ( One, Two, Three )

import Data.Proxy
import Data.Coerce
import Data.Foldable as F

---------------------------------------
-- Additive operations
---------------------------------------

-- | The number 0, polymorphic in its dimension. Use of this will
-- often require a type annotation.
zero :: AdditiveGroup n => Qu dimspec l n
zero :: Qu dimspec l n
zero = n -> Qu dimspec l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu n
forall v. AdditiveGroup v => v
zeroV

infixl 6 |+|
-- | Add two compatible quantities
(|+|) :: (d1 @~ d2, AdditiveGroup n) => Qu d1 l n -> Qu d2 l n -> Qu d1 l n
(Qu n
a) |+| :: Qu d1 l n -> Qu d2 l n -> Qu d1 l n
|+| (Qu n
b) = n -> Qu d1 l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> n -> n
forall v. AdditiveGroup v => v -> v -> v
^+^ n
b)

-- | Negate a quantity
qNegate :: AdditiveGroup n => Qu d l n -> Qu d l n
qNegate :: Qu d l n -> Qu d l n
qNegate (Qu n
x) = n -> Qu d l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n -> n
forall v. AdditiveGroup v => v -> v
negateV n
x)

infixl 6 |-|
-- | Subtract two compatible quantities
(|-|) :: (d1 @~ d2, AdditiveGroup n) => Qu d1 l n -> Qu d2 l n -> Qu d1 l n
Qu d1 l n
a |-| :: Qu d1 l n -> Qu d2 l n -> Qu d1 l n
|-| Qu d2 l n
b = Qu d1 l n
a Qu d1 l n -> Qu d2 l n -> Qu d1 l n
forall (d1 :: [Factor *]) (d2 :: [Factor *]) n (l :: LCSU *).
(d1 @~ d2, AdditiveGroup n) =>
Qu d1 l n -> Qu d2 l n -> Qu d1 l n
|+| Qu d2 l n -> Qu d2 l n
forall n (d :: [Factor *]) (l :: LCSU *).
AdditiveGroup n =>
Qu d l n -> Qu d l n
qNegate Qu d2 l n
b

-- | Take the sum of a list of quantities
qSum :: (Foldable f, AdditiveGroup n) => f (Qu d l n) -> Qu d l n
qSum :: f (Qu d l n) -> Qu d l n
qSum = (Qu d l n -> Qu d l n -> Qu d l n)
-> Qu d l n -> f (Qu d l n) -> Qu d l n
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr Qu d l n -> Qu d l n -> Qu d l n
forall (d1 :: [Factor *]) (d2 :: [Factor *]) n (l :: LCSU *).
(d1 @~ d2, AdditiveGroup n) =>
Qu d1 l n -> Qu d2 l n -> Qu d1 l n
(|+|) Qu d l n
forall n (dimspec :: [Factor *]) (l :: LCSU *).
AdditiveGroup n =>
Qu dimspec l n
zero

---------------------------------------
-- Vector multiplicative operations
---------------------------------------

infixl 7 |*^|, |^*|, |^/|
-- | Multiply a scalar quantity by a vector quantity
(|*^|) :: VectorSpace n => Qu d1 l (Scalar n) -> Qu d2 l n -> Qu (Normalize (d1 @+ d2)) l n
(Qu Scalar n
a) |*^| :: Qu d1 l (Scalar n) -> Qu d2 l n -> Qu (Normalize (d1 @+ d2)) l n
|*^| (Qu n
b) = n -> Qu (Normalize (d1 @@+ Reorder d2 d1)) l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (Scalar n
a Scalar n -> n -> n
forall v. VectorSpace v => Scalar v -> v -> v
*^ n
b)

-- | Multiply a vector quantity by a scalar quantity
(|^*|) :: VectorSpace n => Qu d1 l n -> Qu d2 l (Scalar n) -> Qu (Normalize (d1 @+ d2)) l n
(Qu n
a) |^*| :: Qu d1 l n -> Qu d2 l (Scalar n) -> Qu (Normalize (d1 @+ d2)) l n
|^*| (Qu Scalar n
b) = n -> Qu (Normalize (d1 @@+ Reorder d2 d1)) l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> Scalar n -> n
forall v s. (VectorSpace v, s ~ Scalar v) => v -> s -> v
^* Scalar n
b)

-- | Divide a vector quantity by a scalar quantity
(|^/|) :: (VectorSpace n, Fractional (Scalar n))
       => Qu d1 l n -> Qu d2 l (Scalar n) -> Qu (Normalize (d1 @- d2)) l n
(Qu n
a) |^/| :: Qu d1 l n -> Qu d2 l (Scalar n) -> Qu (Normalize (d1 @- d2)) l n
|^/| (Qu Scalar n
b) = n -> Qu (Normalize (d1 @- d2)) l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> Scalar n -> n
forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ Scalar n
b)

infixl 7 |/
-- | Divide a quantity by a scalar
(|/) :: (VectorSpace n, Fractional (Scalar n)) => Qu a l n -> Scalar n -> Qu a l n
(Qu n
a) |/ :: Qu a l n -> Scalar n -> Qu a l n
|/ Scalar n
b = n -> Qu a l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> Scalar n -> n
forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ Scalar n
b)
-- The above function should *not* need to be privileged. But, GHC can't figure
-- out that a @@- '[] ~ a. Urgh.

infixl 7 *| , |*
-- | Multiply a quantity by a scalar from the left
(*|) :: VectorSpace n => Scalar n -> Qu b l n -> Qu b l n
Scalar n
a *| :: Scalar n -> Qu b l n -> Qu b l n
*| (Qu n
b) =  n -> Qu b l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (Scalar n
a Scalar n -> n -> n
forall v. VectorSpace v => Scalar v -> v -> v
*^ n
b)

-- | Multiply a quantity by a scalar from the right
(|*) :: VectorSpace n => Qu a l n -> Scalar n -> Qu a l n
(Qu n
a) |* :: Qu a l n -> Scalar n -> Qu a l n
|* Scalar n
b = n -> Qu a l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> Scalar n -> n
forall v s. (VectorSpace v, s ~ Scalar v) => v -> s -> v
^* Scalar n
b)

---------------------------------------
-- Multiplicative operations
---------------------------------------

infixl 7 |.|
-- | Take a inner (dot) product between two quantities.
(|.|) :: InnerSpace n => Qu d1 l n -> Qu d2 l n -> Qu (Normalize (d1 @+ d2)) l (Scalar n)
(Qu n
a) |.| :: Qu d1 l n -> Qu d2 l n -> Qu (Normalize (d1 @+ d2)) l (Scalar n)
|.| (Qu n
b) = Scalar n -> Qu (Normalize (d1 @@+ Reorder d2 d1)) l (Scalar n)
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> n -> Scalar n
forall v. InnerSpace v => v -> v -> Scalar v
<.> n
b)

-- | Square the length of a vector.
qMagnitudeSq :: InnerSpace n => Qu d l n -> Qu (d @* Z.Two) l (Scalar n)
qMagnitudeSq :: Qu d l n -> Qu (d @* Two) l (Scalar n)
qMagnitudeSq (Qu n
x) = Scalar n -> Qu (d @* Two) l (Scalar n)
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n -> Scalar n
forall v s. (InnerSpace v, s ~ Scalar v) => v -> s
magnitudeSq n
x)

-- | Length of a vector.
qMagnitude :: (InnerSpace n, Floating (Scalar n)) => Qu d l n -> Qu d l (Scalar n)
qMagnitude :: Qu d l n -> Qu d l (Scalar n)
qMagnitude (Qu n
x) = Scalar n -> Qu d l (Scalar n)
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n -> Scalar n
forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude n
x)

-- | Vector in same direction as given one but with length of one. If given the zero
-- vector, then return it. The returned vector is dimensionless.
qNormalized :: (InnerSpace n, Floating (Scalar n)) => Qu d l n -> Qu '[] l n
qNormalized :: Qu d l n -> Qu '[] l n
qNormalized (Qu n
x) = n -> Qu '[] l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n -> n
forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> v
normalized n
x)

-- | @qProject u v@ computes the projection of @v@ onto @u@.
qProject :: (InnerSpace n, Floating (Scalar n)) => Qu d2 l n -> Qu d1 l n -> Qu d1 l n
qProject :: Qu d2 l n -> Qu d1 l n -> Qu d1 l n
qProject (Qu n
u) (Qu n
v) = n -> Qu d1 l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
u n -> n -> n
forall v s.
(InnerSpace v, s ~ Scalar v, Fractional s) =>
v -> v -> v
`project` n
v)

-- | Cross product of 2D vectors.
qCross2 :: HasCross2 n => Qu d l n -> Qu d l n
qCross2 :: Qu d l n -> Qu d l n
qCross2 (Qu n
x) = n -> Qu d l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n -> n
forall v. HasCross2 v => v -> v
cross2 n
x)

-- | Cross product of 3D vectors.
qCross3 :: HasCross3 n => Qu d1 l n -> Qu d2 l n -> Qu (Normalize (d1 @+ d2)) l n
qCross3 :: Qu d1 l n -> Qu d2 l n -> Qu (Normalize (d1 @+ d2)) l n
qCross3 (Qu n
x) (Qu n
y) = n -> Qu (Normalize (d1 @@+ Reorder d2 d1)) l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
x n -> n -> n
forall v. HasCross3 v => v -> v -> v
`cross3` n
y)

---------------------------------------
-- Affine space operations
---------------------------------------

-- | A @Point n@ is an affine space built over @n@. Two @Point@s cannot be added,
-- but they can be subtracted to yield a difference of type @n@.
newtype Point n = Point n
  deriving (Int -> Point n -> ShowS
[Point n] -> ShowS
Point n -> String
(Int -> Point n -> ShowS)
-> (Point n -> String) -> ([Point n] -> ShowS) -> Show (Point n)
forall n. Show n => Int -> Point n -> ShowS
forall n. Show n => [Point n] -> ShowS
forall n. Show n => Point n -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Point n] -> ShowS
$cshowList :: forall n. Show n => [Point n] -> ShowS
show :: Point n -> String
$cshow :: forall n. Show n => Point n -> String
showsPrec :: Int -> Point n -> ShowS
$cshowsPrec :: forall n. Show n => Int -> Point n -> ShowS
Show, Point n -> Point n -> Bool
(Point n -> Point n -> Bool)
-> (Point n -> Point n -> Bool) -> Eq (Point n)
forall n. Eq n => Point n -> Point n -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Point n -> Point n -> Bool
$c/= :: forall n. Eq n => Point n -> Point n -> Bool
== :: Point n -> Point n -> Bool
$c== :: forall n. Eq n => Point n -> Point n -> Bool
Eq, Int -> Point n
Point n -> Int
Point n -> [Point n]
Point n -> Point n
Point n -> Point n -> [Point n]
Point n -> Point n -> Point n -> [Point n]
(Point n -> Point n)
-> (Point n -> Point n)
-> (Int -> Point n)
-> (Point n -> Int)
-> (Point n -> [Point n])
-> (Point n -> Point n -> [Point n])
-> (Point n -> Point n -> [Point n])
-> (Point n -> Point n -> Point n -> [Point n])
-> Enum (Point n)
forall n. Enum n => Int -> Point n
forall n. Enum n => Point n -> Int
forall n. Enum n => Point n -> [Point n]
forall n. Enum n => Point n -> Point n
forall n. Enum n => Point n -> Point n -> [Point n]
forall n. Enum n => Point n -> Point n -> Point n -> [Point n]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Point n -> Point n -> Point n -> [Point n]
$cenumFromThenTo :: forall n. Enum n => Point n -> Point n -> Point n -> [Point n]
enumFromTo :: Point n -> Point n -> [Point n]
$cenumFromTo :: forall n. Enum n => Point n -> Point n -> [Point n]
enumFromThen :: Point n -> Point n -> [Point n]
$cenumFromThen :: forall n. Enum n => Point n -> Point n -> [Point n]
enumFrom :: Point n -> [Point n]
$cenumFrom :: forall n. Enum n => Point n -> [Point n]
fromEnum :: Point n -> Int
$cfromEnum :: forall n. Enum n => Point n -> Int
toEnum :: Int -> Point n
$ctoEnum :: forall n. Enum n => Int -> Point n
pred :: Point n -> Point n
$cpred :: forall n. Enum n => Point n -> Point n
succ :: Point n -> Point n
$csucc :: forall n. Enum n => Point n -> Point n
Enum, Point n
Point n -> Point n -> Bounded (Point n)
forall a. a -> a -> Bounded a
forall n. Bounded n => Point n
maxBound :: Point n
$cmaxBound :: forall n. Bounded n => Point n
minBound :: Point n
$cminBound :: forall n. Bounded n => Point n
Bounded)

-- | Make a point quantity from a non-point quantity.
type family QPoint n where
  QPoint (Qu d l n) = Qu d l (Point n)

instance AdditiveGroup n => AffineSpace (Point n) where
  type Diff (Point n) = n
  .-. :: Point n -> Point n -> Diff (Point n)
(.-.) = (n -> n -> n) -> Point n -> Point n -> n
coerce (n -> n -> n
forall v. AdditiveGroup v => v -> v -> v
(^-^) :: n -> n -> n)
  .+^ :: Point n -> Diff (Point n) -> Point n
(.+^) = (n -> n -> n) -> Point n -> n -> Point n
coerce (n -> n -> n
forall v. AdditiveGroup v => v -> v -> v
(^+^) :: n -> n -> n)

-- | Make a point quantity at the given unit. Like 'quOf'.
quOfPoint :: forall dim lcsu unit n.
             ( ValidDLU dim lcsu unit
             , VectorSpace n
             , Fractional (Scalar n) )
          => n -> unit -> Qu dim lcsu (Point n)
quOfPoint :: n -> unit -> Qu dim lcsu (Point n)
quOfPoint n
n unit
unit = Point n -> Qu dim lcsu (Point n)
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n -> Point n
forall n. n -> Point n
Point n
x)
  where Qu n
x = n -> unit -> Qu dim lcsu n
forall unit (dim :: [Factor *]) (lcsu :: LCSU *) n.
(ValidDLU dim lcsu unit, VectorSpace n, Fractional (Scalar n)) =>
n -> unit -> Qu dim lcsu n
quOf n
n unit
unit :: Qu dim lcsu n

infix 5 %.
-- | Infix synonym of 'quOfPoint'
(%.) :: ( ValidDLU dim lcsu unit
        , VectorSpace n
        , Fractional (Scalar n) )
     => n -> unit -> Qu dim lcsu (Point n)
%. :: n -> unit -> Qu dim lcsu (Point n)
(%.) = n -> unit -> Qu dim lcsu (Point n)
forall (dim :: [Factor *]) (lcsu :: LCSU *) unit n.
(ValidDLU dim lcsu unit, VectorSpace n, Fractional (Scalar n)) =>
n -> unit -> Qu dim lcsu (Point n)
quOfPoint

-- | Extract the numerical value from a point quantity. Like 'numIn'.
pointNumIn :: forall unit dim lcsu n.
              ( ValidDLU dim lcsu unit
              , VectorSpace n
              , Fractional (Scalar n) )
           => Qu dim lcsu (Point n) -> unit -> n
pointNumIn :: Qu dim lcsu (Point n) -> unit -> n
pointNumIn (Qu (Point n
n)) unit
unit = Qu dim lcsu n -> unit -> n
forall unit (dim :: [Factor *]) (lcsu :: LCSU *) n.
(ValidDLU dim lcsu unit, VectorSpace n, Fractional (Scalar n)) =>
Qu dim lcsu n -> unit -> n
numIn (n -> Qu dim lcsu n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu n
n :: Qu dim lcsu n) unit
unit

infix 5 .#
-- | Infix synonym for 'pointNumIn'.
(.#) :: (ValidDLU dim lcsu unit, VectorSpace n, Fractional (Scalar n))
     => Qu dim lcsu (Point n) -> unit -> n
.# :: Qu dim lcsu (Point n) -> unit -> n
(.#) = Qu dim lcsu (Point n) -> unit -> n
forall unit (dim :: [Factor *]) (lcsu :: LCSU *) n.
(ValidDLU dim lcsu unit, VectorSpace n, Fractional (Scalar n)) =>
Qu dim lcsu (Point n) -> unit -> n
pointNumIn

infixl 6 |.-.|, |.+^|, |.-^|

-- | Subtract point quantities.
(|.-.|) :: (d1 @~ d2, AffineSpace n) => Qu d1 l n -> Qu d2 l n -> Qu d1 l (Diff n)
(Qu n
a) |.-.| :: Qu d1 l n -> Qu d2 l n -> Qu d1 l (Diff n)
|.-.| (Qu n
b) = Diff n -> Qu d1 l (Diff n)
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> n -> Diff n
forall p. AffineSpace p => p -> p -> Diff p
.-. n
b)

-- | Add a point to a vector.
(|.+^|) :: (d1 @~ d2, AffineSpace n) => Qu d1 l n -> Qu d2 l (Diff n) -> Qu d1 l n
(Qu n
a) |.+^| :: Qu d1 l n -> Qu d2 l (Diff n) -> Qu d1 l n
|.+^| (Qu Diff n
b) = n -> Qu d1 l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> Diff n -> n
forall p. AffineSpace p => p -> Diff p -> p
.+^ Diff n
b)

-- | Subract a vector from a point.
(|.-^|) :: (d1 @~ d2, AffineSpace n) => Qu d1 l n -> Qu d2 l (Diff n) -> Qu d1 l n
(Qu n
a) |.-^| :: Qu d1 l n -> Qu d2 l (Diff n) -> Qu d1 l n
|.-^| (Qu Diff n
b) = n -> Qu d1 l n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> Diff n -> n
forall p. AffineSpace p => p -> Diff p -> p
.-^ Diff n
b)

-- | Square of the distance between two points.
qDistanceSq :: (d1 @~ d2, AffineSpace n, InnerSpace (Diff n))
            => Qu d1 l n -> Qu d2 l n -> Qu (d1 @* Z.Two) l (Scalar (Diff n))
qDistanceSq :: Qu d1 l n -> Qu d2 l n -> Qu (d1 @* Two) l (Scalar (Diff n))
qDistanceSq (Qu n
a) (Qu n
b) = Scalar (Diff n) -> Qu (d1 @* Two) l (Scalar (Diff n))
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> n -> Scalar (Diff n)
forall p v.
(AffineSpace p, v ~ Diff p, InnerSpace v) =>
p -> p -> Scalar v
`distanceSq` n
b)

-- | Distance between two points.
qDistance :: (d1 @~ d2, AffineSpace n, InnerSpace (Diff n), Floating (Scalar (Diff n)))
          => Qu d1 l n -> Qu d2 l n -> Qu d1 l (Scalar (Diff n))
qDistance :: Qu d1 l n -> Qu d2 l n -> Qu d1 l (Scalar (Diff n))
qDistance (Qu n
a) (Qu n
b) = Scalar (Diff n) -> Qu d1 l (Scalar (Diff n))
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
a n -> n -> Scalar (Diff n)
forall p v s.
(AffineSpace p, v ~ Diff p, InnerSpace v, s ~ Scalar v,
 Floating (Scalar v)) =>
p -> p -> s
`distance` n
b)

---------------------------------------
-- Top-level operations
---------------------------------------

-- | Extracts a numerical value from a dimensioned quantity, expressed in
--   the given unit. For example:
--
--   > inMeters :: Length -> Double
--   > inMeters x = numIn x Meter
--
--   or
--
--   > inMeters x = x # Meter
numIn :: forall unit dim lcsu n.
         ( ValidDLU dim lcsu unit
         , VectorSpace n
         , Fractional (Scalar n) )
      => Qu dim lcsu n -> unit -> n
numIn :: Qu dim lcsu n -> unit -> n
numIn (Qu n
val) unit
u
  = n
val n -> Scalar n -> n
forall v s. (VectorSpace v, s ~ Scalar v) => v -> s -> v
^* Rational -> Scalar n
forall a. Fractional a => Rational -> a
fromRational
             (Proxy (LookupList dim lcsu) -> Rational
forall (units :: [Factor *]).
UnitFactor units =>
Proxy units -> Rational
canonicalConvRatioSpec (Proxy (LookupList dim lcsu)
forall k (t :: k). Proxy t
Proxy :: Proxy (LookupList dim lcsu))
              Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ unit -> Rational
forall unit. Unit unit => unit -> Rational
canonicalConvRatio unit
u)

infix 5 #
-- | Infix synonym for 'numIn'
(#) :: ( ValidDLU dim lcsu unit
       , VectorSpace n
       , Fractional (Scalar n) )
    => Qu dim lcsu n -> unit -> n
# :: Qu dim lcsu n -> unit -> n
(#) = Qu dim lcsu n -> unit -> n
forall unit (dim :: [Factor *]) (lcsu :: LCSU *) n.
(ValidDLU dim lcsu unit, VectorSpace n, Fractional (Scalar n)) =>
Qu dim lcsu n -> unit -> n
numIn

-- | Creates a dimensioned quantity in the given unit. For example:
--
--   > height :: Length
--   > height = quOf 2.0 Meter
--
--   or
--
--   > height = 2.0 % Meter
quOf :: forall unit dim lcsu n.
         ( ValidDLU dim lcsu unit
         , VectorSpace n
         , Fractional (Scalar n) )
      => n -> unit -> Qu dim lcsu n
quOf :: n -> unit -> Qu dim lcsu n
quOf n
d unit
u
  = n -> Qu dim lcsu n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n
d n -> Scalar n -> n
forall v s. (VectorSpace v, s ~ Scalar v) => v -> s -> v
^* Rational -> Scalar n
forall a. Fractional a => Rational -> a
fromRational
               (unit -> Rational
forall unit. Unit unit => unit -> Rational
canonicalConvRatio unit
u
                Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Proxy (LookupList dim lcsu) -> Rational
forall (units :: [Factor *]).
UnitFactor units =>
Proxy units -> Rational
canonicalConvRatioSpec (Proxy (LookupList dim lcsu)
forall k (t :: k). Proxy t
Proxy :: Proxy (LookupList dim lcsu))))

infix 5 %
-- | Infix synonym for 'quOf'
(%) :: ( ValidDLU dim lcsu unit
       , VectorSpace n
       , Fractional (Scalar n) )
    => n -> unit -> Qu dim lcsu n
% :: n -> unit -> Qu dim lcsu n
(%) = n -> unit -> Qu dim lcsu n
forall unit (dim :: [Factor *]) (lcsu :: LCSU *) n.
(ValidDLU dim lcsu unit, VectorSpace n, Fractional (Scalar n)) =>
n -> unit -> Qu dim lcsu n
quOf

-- | Dimension-keeping cast between different CSUs.
convert :: forall d l1 l2 n.
  ( ConvertibleLCSUs d l1 l2
  , VectorSpace n
  , Fractional (Scalar n) )
  => Qu d l1 n -> Qu d l2 n
convert :: Qu d l1 n -> Qu d l2 n
convert (Qu n
x) = n -> Qu d l2 n
forall (a :: [Factor *]) (lcsu :: LCSU *) n. n -> Qu a lcsu n
Qu (n -> Qu d l2 n) -> n -> Qu d l2 n
forall a b. (a -> b) -> a -> b
$ n
x n -> Scalar n -> n
forall v s. (VectorSpace v, s ~ Scalar v) => v -> s -> v
^* Rational -> Scalar n
forall a. Fractional a => Rational -> a
fromRational (
  Proxy (LookupList d l1) -> Rational
forall (units :: [Factor *]).
UnitFactor units =>
Proxy units -> Rational
canonicalConvRatioSpec (Proxy (LookupList d l1)
forall k (t :: k). Proxy t
Proxy :: Proxy (LookupList d l1))
  Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Proxy (LookupList d l2) -> Rational
forall (units :: [Factor *]).
UnitFactor units =>
Proxy units -> Rational
canonicalConvRatioSpec (Proxy (LookupList d l2)
forall k (t :: k). Proxy t
Proxy :: Proxy (LookupList d l2)))


-- | Compute the argument in the @DefaultLCSU@, and present the result as
-- lcsu-polymorphic dimension-polymorphic value. Named 'constant' because one
-- of its dominant usecase is to inject constant quantities into
-- dimension-polymorphic expressions.
constant :: ( d @~ e
            , ConvertibleLCSUs e DefaultLCSU l
            , VectorSpace n
            , Fractional (Scalar n) )
         => Qu d DefaultLCSU n -> Qu e l n
constant :: Qu d 'DefaultLCSU n -> Qu e l n
constant = Qu e 'DefaultLCSU n -> Qu e l n
forall (d :: [Factor *]) (l1 :: LCSU *) (l2 :: LCSU *) n.
(ConvertibleLCSUs d l1 l2, VectorSpace n, Fractional (Scalar n)) =>
Qu d l1 n -> Qu d l2 n
convert (Qu e 'DefaultLCSU n -> Qu e l n)
-> (Qu d 'DefaultLCSU n -> Qu e 'DefaultLCSU n)
-> Qu d 'DefaultLCSU n
-> Qu e l n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Qu d 'DefaultLCSU n -> Qu e 'DefaultLCSU n
forall (d :: [Factor *]) (e :: [Factor *]) (l :: LCSU *) n.
(d @~ e) =>
Qu d l n -> Qu e l n
redim

infix 1 `showIn`
-- | Show a dimensioned quantity in a given unit. (The default @Show@
-- instance always uses units as specified in the LCSU.)
showIn :: ( ValidDLU dim lcsu unit
          , VectorSpace n
          , Fractional (Scalar n)
          , Show unit
          , Show n )
       => Qu dim lcsu n -> unit -> String
showIn :: Qu dim lcsu n -> unit -> String
showIn Qu dim lcsu n
x unit
u = n -> String
forall a. Show a => a -> String
show (Qu dim lcsu n
x Qu dim lcsu n -> unit -> n
forall (dim :: [Factor *]) (lcsu :: LCSU *) unit n.
(ValidDLU dim lcsu unit, VectorSpace n, Fractional (Scalar n)) =>
Qu dim lcsu n -> unit -> n
# unit
u) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" " String -> ShowS
forall a. [a] -> [a] -> [a]
++ unit -> String
forall a. Show a => a -> String
show unit
u