hgeometry-vector
Copyright(C) Frank Staals
Licensesee the LICENSE file
MaintainerFrank Staals
Safe HaskellNone
LanguageGHC2024

HGeometry.Vector.Class

Description

A Class defining d-dimensional vectors

Synopsis

Documentation

class (r ~ IxValue vector, s ~ IxValue vector', d ~ Dimension vector, d ~ Dimension vector') => AsVector_ vector vector' (d :: Nat) r s | vector -> d, vector -> r, vector' -> s where Source #

Types that can be converted to and from our canonical 'Vector d r' type

Methods

_Vector :: Iso vector vector' (Vector d r) (Vector d s) Source #

Convert from a 'Vector d r' and into a 'Vector d s'

Instances

Instances details
AsVector_ (V1 r) (V1 s) 1 r s Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

_Vector :: Iso (V1 r) (V1 s) (Vector 1 r) (Vector 1 s) Source #

AsVector_ (V2 r) (V2 s) 2 r s Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

_Vector :: Iso (V2 r) (V2 s) (Vector 2 r) (Vector 2 s) Source #

AsVector_ (V3 r) (V3 s) 3 r s Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

_Vector :: Iso (V3 r) (V3 s) (Vector 3 r) (Vector 3 s) Source #

AsVector_ (V4 r) (V4 s) 4 r s Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

_Vector :: Iso (V4 r) (V4 s) (Vector 4 r) (Vector 4 s) Source #

AsVector_ (Vector d r) (Vector d s) d r s Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

_Vector :: Iso (Vector d r) (Vector d s) (Vector d r) (Vector d s) Source #

class (HasComponents vector vector, AsVector_ vector vector d r r, KnownNat d) => Vector_ vector (d :: Nat) r where Source #

Class for representing d dimensional vectors.

Minimal complete definition

generateA

Methods

generateA :: Applicative f => (Int -> f r) -> f vector Source #

Generates a vector from an Applicative operation (that takes the index)

component' :: Int -> IndexedTraversal' Int vector r Source #

traversal to access the i^th coordinate.

default component' :: (Index vector ~ Int, Ixed vector) => Int -> IndexedTraversal' Int vector r Source #

Instances

Instances details
Vector_ (V1 r) 1 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

generateA :: Applicative f => (Int -> f r) -> f (V1 r) Source #

component' :: Int -> IndexedTraversal' Int (V1 r) r Source #

Vector_ (V2 r) 2 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

generateA :: Applicative f => (Int -> f r) -> f (V2 r) Source #

component' :: Int -> IndexedTraversal' Int (V2 r) r Source #

Vector_ (V3 r) 3 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

generateA :: Applicative f => (Int -> f r) -> f (V3 r) Source #

component' :: Int -> IndexedTraversal' Int (V3 r) r Source #

Vector_ (V4 r) 4 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

generateA :: Applicative f => (Int -> f r) -> f (V4 r) Source #

component' :: Int -> IndexedTraversal' Int (V4 r) r Source #

Vector_ (Vector 1 r) 1 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

generateA :: Applicative f => (Int -> f r) -> f (Vector 1 r) Source #

component' :: Int -> IndexedTraversal' Int (Vector 1 r) r Source #

Vector_ (Vector 2 r) 2 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

generateA :: Applicative f => (Int -> f r) -> f (Vector 2 r) Source #

component' :: Int -> IndexedTraversal' Int (Vector 2 r) r Source #

Vector_ (Vector 3 r) 3 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

generateA :: Applicative f => (Int -> f r) -> f (Vector 3 r) Source #

component' :: Int -> IndexedTraversal' Int (Vector 3 r) r Source #

Vector_ (Vector 4 r) 4 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

generateA :: Applicative f => (Int -> f r) -> f (Vector 4 r) Source #

component' :: Int -> IndexedTraversal' Int (Vector 4 r) r Source #

type Has_ (c :: Type -> Nat -> Type -> k) (d :: Nat) r = c (Vector d r) d r Source #

Specifies that we have an appropriate constraint for the vector implementation

generate :: forall vector (d :: Nat) r. Vector_ vector d r => (Int -> r) -> vector Source #

Generate a vector from a given function.

vectorFromList :: forall vector (d :: Nat) r. Vector_ vector d r => [r] -> Maybe vector Source #

Convert a list of exactly d elements into a vector with dimension d.

>>> vectorFromList [10,2,3] :: Maybe (Vector 3 Int)
Just (Vector3 10 2 3)
>>> vectorFromList [10,2,3,5] :: Maybe (Vector 3 Int)
Nothing
>>> vectorFromList [10,2] :: Maybe (Vector 3 Int)
Nothing

component :: forall (i :: Natural) vector r (d :: Natural). (i <= (d - 1), KnownNat i, Vector_ vector d r) => IndexedLens' Int vector r Source #

Lens to access te i^t component.

>>> myVec3 ^. component @0
1
>>> myVec3 ^. component @1
2
>>> myVec3 & component @1 %~ (*5)
Vector3 1 10 3
>>> myVec2 & component @1 %~ (*5)
Vector2 10 100

xComponent :: forall vector (d :: Nat) r. (Vector_ vector d r, 0 <= (d - 1)) => IndexedLens' Int vector r Source #

Shorthand for accessing the x-component

>>> Vector3 1 2 3 ^. xComponent
1
>>> Vector2 1 2 & xComponent .~ 10
Vector2 10 2

yComponent :: forall vector (d :: Nat) r. (Vector_ vector d r, 1 <= (d - 1)) => IndexedLens' Int vector r Source #

Shorthand for accessing the y-component

>>> Vector3 1 2 3 ^. yComponent
2
>>> Vector2 1 2 & yComponent .~ 10
Vector2 1 10

zComponent :: forall vector (d :: Nat) r. (Vector_ vector d r, 2 <= (d - 1)) => IndexedLens' Int vector r Source #

Shorthand for accessing the z-component

>>> Vector3 1 2 3 ^. zComponent
3
>>> Vector3 1 2 3 & zComponent .~ 10
Vector3 1 2 10

wComponent :: forall vector (d :: Nat) r. (Vector_ vector d r, 3 <= (d - 1)) => IndexedLens' Int vector r Source #

Shorthand for accessing the w-component

>>> Vector4 1 2 3 4 ^. wComponent
4
>>> Vector4 1 2 3 4 & wComponent .~ 10
Vector4 1 2 3 10

prefix :: forall (i :: Nat) (d :: Nat) vector vector' r. (i <= d, Vector_ vector d r, Vector_ vector' i r) => vector -> vector' Source #

Take a prefix of length i of the vector

>>> prefix myVec3 :: Vector 2 Int
Vector2 1 2

suffix :: forall (i :: Nat) (d :: Nat) vector vector' r. (i <= d, Vector_ vector d r, Vector_ vector' i r) => vector -> vector' Source #

Take a suffix of length i of the vector

>>> suffix @_ @_ @_ @(Vector 2 Int) myVec3
Vector2 2 3

cons :: forall vector' vector (d :: Nat) r. (Vector_ vector d r, Vector_ vector' (d + 1) r) => r -> vector -> vector' Source #

Add an element to the front of the vector

>>> cons 5 myVec2 :: Vector 3 Int
Vector3 5 10 20

snoc :: forall vector' vector (d :: Nat) r. (Vector_ vector d r, Vector_ vector' (d + 1) r) => vector -> r -> vector' Source #

Add an element to the back of the vector.

>>> snoc myVec2 5 :: Vector 3 Int
Vector3 10 20 5

uncons :: forall vector' vector (d :: Natural) r. (Vector_ vector (d + 1) r, Vector_ vector' d r, 0 <= ((d + 1) - 1), d <= Dimension vector) => vector -> (r, vector') Source #

Extract the first element from the vector

>>> uncons myVec3 :: (Int, Vector 2 Int)
(1,Vector2 2 3)

unsnoc :: forall vector' vector (d :: Natural) r. (Vector_ vector (d + 1) r, Vector_ vector' d r, d <= ((d + 1) - 1), d <= Dimension vector) => vector -> (vector', r) Source #

Extract the last element from the vector

>>> unsnoc myVec3  :: (Vector 2 Int, Int)
(Vector2 1 2,3)

class Vector_ vector d r => Additive_ vector (d :: Nat) r where Source #

Class that supports additive opreations on vectors

Methods

liftU2 :: (r -> r -> r) -> vector -> vector -> vector Source #

Apply a function to merge the 'non-zero' components of two vectors, unioning the rest of the values.

liftI2A :: Apply f => (r -> r -> f r) -> vector -> vector -> f vector Source #

Apply an Applicative function to the components of two vectors.

Instances

Instances details
Additive_ (V1 r) 1 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

liftU2 :: (r -> r -> r) -> V1 r -> V1 r -> V1 r Source #

liftI2A :: Apply f => (r -> r -> f r) -> V1 r -> V1 r -> f (V1 r) Source #

Additive_ (V2 r) 2 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

liftU2 :: (r -> r -> r) -> V2 r -> V2 r -> V2 r Source #

liftI2A :: Apply f => (r -> r -> f r) -> V2 r -> V2 r -> f (V2 r) Source #

Additive_ (V3 r) 3 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

liftU2 :: (r -> r -> r) -> V3 r -> V3 r -> V3 r Source #

liftI2A :: Apply f => (r -> r -> f r) -> V3 r -> V3 r -> f (V3 r) Source #

Additive_ (V4 r) 4 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

liftU2 :: (r -> r -> r) -> V4 r -> V4 r -> V4 r Source #

liftI2A :: Apply f => (r -> r -> f r) -> V4 r -> V4 r -> f (V4 r) Source #

Additive_ (Vector 1 r) 1 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

liftU2 :: (r -> r -> r) -> Vector 1 r -> Vector 1 r -> Vector 1 r Source #

liftI2A :: Apply f => (r -> r -> f r) -> Vector 1 r -> Vector 1 r -> f (Vector 1 r) Source #

Additive_ (Vector 2 r) 2 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

liftU2 :: (r -> r -> r) -> Vector 2 r -> Vector 2 r -> Vector 2 r Source #

liftI2A :: Apply f => (r -> r -> f r) -> Vector 2 r -> Vector 2 r -> f (Vector 2 r) Source #

Additive_ (Vector 3 r) 3 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

liftU2 :: (r -> r -> r) -> Vector 3 r -> Vector 3 r -> Vector 3 r Source #

liftI2A :: Apply f => (r -> r -> f r) -> Vector 3 r -> Vector 3 r -> f (Vector 3 r) Source #

Additive_ (Vector 4 r) 4 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

liftU2 :: (r -> r -> r) -> Vector 4 r -> Vector 4 r -> Vector 4 r Source #

liftI2A :: Apply f => (r -> r -> f r) -> Vector 4 r -> Vector 4 r -> f (Vector 4 r) Source #

zero :: forall r vector (d :: Nat). (Num r, Additive_ vector d r) => vector Source #

zero vector

liftI2 :: forall vector (d :: Nat) r. Additive_ vector d r => (r -> r -> r) -> vector -> vector -> vector Source #

Apply a function to the components of two vectors.

lerp :: forall r vector (d :: Nat). (Num r, Additive_ vector d r) => r -> vector -> vector -> vector Source #

Linearly interpolate between the two vectors

(^+^) :: forall r vector (d :: Nat). (Num r, Additive_ vector d r) => vector -> vector -> vector infixl 6 Source #

add two vectors

(^-^) :: forall r vector (d :: Nat). (Num r, Additive_ vector d r) => vector -> vector -> vector infixl 6 Source #

subtract vectors

negated :: forall r vector (d :: Nat). (Num r, Vector_ vector d r) => vector -> vector Source #

negate v

(*^) :: forall r vector (d :: Nat). (Num r, Vector_ vector d r) => r -> vector -> vector infixl 7 Source #

left scalar multiplication

(^*) :: forall r vector (d :: Nat). (Num r, Vector_ vector d r) => vector -> r -> vector infixl 7 Source #

right scalar multiplication

(^/) :: forall vector (d :: Nat) r. (Vector_ vector d r, Fractional r) => vector -> r -> vector infixl 7 Source #

scalar division

sumV :: forall f vector (d :: Nat) r. (Foldable f, Additive_ vector d r, Num r) => f vector -> vector Source #

sum a collection of vectors.

basis :: forall vector (d :: Nat) r. (Additive_ vector d r, Num r) => [vector] Source #

Produce a default basis for a vector space. If the dimensionality of the vector space is not statically known, see basisFor.

unit :: forall vector (d :: Nat) r. (Additive_ vector d r, Num r) => vector Source #

unit vector

foldMapZip :: forall vector (d :: Nat) r m. (Additive_ vector d r, Semigroup m) => (r -> r -> m) -> vector -> vector -> m Source #

"zip through the two vectors", folding over the result.

as an example, we can implement the dot product of two vectors u and v using:

>>> let myDot u v = getSum $ foldMapZip (\x x' -> Sum $ x * x') u v
>>> myDot (Vector3 1 2 3) (Vector3 10 20 30)
140

class Additive_ vector d r => Metric_ vector (d :: Nat) r where Source #

The equivalent class of Linear.Metric

Note that we do not define a distance itself, and that norm and signorm have a Radical constraint rather than Floating.

Minimal complete definition

Nothing

Methods

dot :: vector -> vector -> r Source #

Compute the inner product of two vectors or (equivalently) convert a vector f a into a covector f a -> a.

quadrance :: vector -> r Source #

Compute the squared norm. The name quadrance arises from Norman J. Wildberger's rational trigonometry.

qd :: vector -> vector -> r Source #

Compute the quadrance of the difference

norm :: vector -> r Source #

Compute the norm of a vector in a metric space

signorm :: vector -> vector Source #

Convert a non-zero vector to unit vector.

Instances

Instances details
Metric_ (V1 r) 1 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

dot :: V1 r -> V1 r -> r Source #

quadrance :: V1 r -> r Source #

qd :: V1 r -> V1 r -> r Source #

norm :: V1 r -> r Source #

signorm :: V1 r -> V1 r Source #

Metric_ (V2 r) 2 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

dot :: V2 r -> V2 r -> r Source #

quadrance :: V2 r -> r Source #

qd :: V2 r -> V2 r -> r Source #

norm :: V2 r -> r Source #

signorm :: V2 r -> V2 r Source #

Metric_ (V3 r) 3 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

dot :: V3 r -> V3 r -> r Source #

quadrance :: V3 r -> r Source #

qd :: V3 r -> V3 r -> r Source #

norm :: V3 r -> r Source #

signorm :: V3 r -> V3 r Source #

Metric_ (V4 r) 4 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

dot :: V4 r -> V4 r -> r Source #

quadrance :: V4 r -> r Source #

qd :: V4 r -> V4 r -> r Source #

norm :: V4 r -> r Source #

signorm :: V4 r -> V4 r Source #

Metric_ (Vector 1 r) 1 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

dot :: Vector 1 r -> Vector 1 r -> r Source #

quadrance :: Vector 1 r -> r Source #

qd :: Vector 1 r -> Vector 1 r -> r Source #

norm :: Vector 1 r -> r Source #

signorm :: Vector 1 r -> Vector 1 r Source #

Metric_ (Vector 2 r) 2 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

dot :: Vector 2 r -> Vector 2 r -> r Source #

quadrance :: Vector 2 r -> r Source #

qd :: Vector 2 r -> Vector 2 r -> r Source #

norm :: Vector 2 r -> r Source #

signorm :: Vector 2 r -> Vector 2 r Source #

Metric_ (Vector 3 r) 3 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

dot :: Vector 3 r -> Vector 3 r -> r Source #

quadrance :: Vector 3 r -> r Source #

qd :: Vector 3 r -> Vector 3 r -> r Source #

norm :: Vector 3 r -> r Source #

signorm :: Vector 3 r -> Vector 3 r Source #

Metric_ (Vector 4 r) 4 r Source # 
Instance details

Defined in HGeometry.Vector.Class

Methods

dot :: Vector 4 r -> Vector 4 r -> r Source #

quadrance :: Vector 4 r -> r Source #

qd :: Vector 4 r -> Vector 4 r -> r Source #

norm :: Vector 4 r -> r Source #

signorm :: Vector 4 r -> Vector 4 r Source #

Orphan instances

(Vector_ (Vector d r) d r, Read r, KnownNat d) => Read (Vector d r) Source # 
Instance details

(Additive_ (Vector d r) d r, Uniform r, UniformRange r, Ord (Vector d r)) => Random (Vector d r) Source # 
Instance details

Methods

randomR :: RandomGen g => (Vector d r, Vector d r) -> g -> (Vector d r, g) Source #

random :: RandomGen g => g -> (Vector d r, g) Source #

randomRs :: RandomGen g => (Vector d r, Vector d r) -> g -> [Vector d r] Source #

randoms :: RandomGen g => g -> [Vector d r] Source #

(Vector_ (Vector d r) d r, Uniform r) => Uniform (Vector d r) Source # 
Instance details

Methods

uniformM :: StatefulGen g m => g -> m (Vector d r) Source #

(Additive_ (Vector d r) d r, UniformRange r, Ord (Vector d r)) => UniformRange (Vector d r) Source # 
Instance details

Methods

uniformRM :: StatefulGen g m => (Vector d r, Vector d r) -> g -> m (Vector d r) Source #

isInRange :: (Vector d r, Vector d r) -> Vector d r -> Bool Source #