Module

Linear.Metric

Package
purescript-linear
Repository
afcondon/purescript-linear

The Metric typeclass for inner products and norms.

This module provides the Metric typeclass which extends Additive with operations for computing dot products, norms, and distances.

#Metric Source

class Metric :: (Type -> Type) -> Constraintclass (Additive f) <= Metric f  where

A metric space with an inner product.

Laws:

  • dot v v >= 0 (positive semi-definite)
  • dot v v = 0 implies v = zero
  • dot u v = dot v u (symmetry)
  • dot (a *^ u) v = a * dot u v (linearity)

Members

  • dot :: forall a. Semiring a => f a -> f a -> a

    The inner (dot) product of two vectors.

    dot (V2 1.0 2.0) (V2 3.0 4.0) = 11.0  -- 1*3 + 2*4
    
  • quadrance :: forall a. Semiring a => f a -> a

    The squared norm of a vector (quadrance from rational trigonometry).

    This is more efficient than norm when you only need to compare magnitudes.

    quadrance (V2 3.0 4.0) = 25.0  -- 3² + 4²
    
  • qd :: forall a. Ring a => f a -> f a -> a

    The squared distance between two vectors.

    qd (V2 0.0 0.0) (V2 3.0 4.0) = 25.0
    
  • distance :: f Number -> f Number -> Number

    The Euclidean distance between two vectors.

    distance (V2 0.0 0.0) (V2 3.0 4.0) = 5.0
    
  • norm :: f Number -> Number

    The Euclidean norm (magnitude) of a vector.

    norm (V2 3.0 4.0) = 5.0
    
  • signorm :: f Number -> f Number

    Convert a non-zero vector to a unit vector (signorm = "sign of norm").

    Returns the zero vector when given the zero vector.

    signorm (V2 3.0 4.0) = V2 0.6 0.8
    signorm (V2 0.0 0.0) = V2 0.0 0.0
    

#normalize Source

normalize :: forall f. Metric f => Epsilon Number => f Number -> f Number

Normalize a vector to unit length.

Unlike signorm, this function handles the zero vector by returning zero rather than NaN.

normalize (V2 3.0 4.0) = V2 0.6 0.8
normalize (V2 0.0 0.0) = V2 0.0 0.0

#project Source

project :: forall f. Metric f => f Number -> f Number -> f Number

Project the first vector onto the second.

project (V2 1.0 0.0) (V2 1.0 1.0) = V2 0.5 0.5