HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulelinear-1.22Haskell2010

Linear.Matrix

Simple matrix operation for low-dimensional primitives.

  • 9 types
  • 1 class
  • 45 values
  • Packagelinear-1.22
  • Exports55
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMatrix.hs
value(!*!)
  1. :: (Functor m, Foldable t, Additive t, Additive n, Num a)
  2. => m (t a)
  3. -> t (n a)
  4. -> m (n a)
#

Matrix product. This can compute any combination of sparse and dense multiplication.

Example1 expression
V2 (V3 1 2 3) (V3 4 5 6) !*! V3 (V2 1 2) (V2 3 4) (V2 4 5)V2 (V2 19 25) (V2 43 58)
Example1 expression
V2 (IntMap.fromList [(1,2)]) (IntMap.fromList [(2,3)]) !*! IntMap.fromList [(1,V3 0 0 1), (2, V3 0 0 5)]V2 (V3 0 0 2) (V3 0 0 15)
value(!+!) :: (Additive m, Additive n, Num a) => m (n a) -> m (n a) -> m (n a)
#

Entry-wise matrix addition.

Example1 expression
V2 (V3 1 2 3) (V3 4 5 6) !+! V2 (V3 7 8 9) (V3 1 2 3)V2 (V3 8 10 12) (V3 5 7 9)
value(!-!) :: (Additive m, Additive n, Num a) => m (n a) -> m (n a) -> m (n a)
#

Entry-wise matrix subtraction.

Example1 expression
V2 (V3 1 2 3) (V3 4 5 6) !-! V2 (V3 7 8 9) (V3 1 2 3)V2 (V3 (-6) (-6) (-6)) (V3 3 3 3)
value(!*) :: (Functor m, Foldable r, Additive r, Num a) => m (r a) -> r a -> m a
#

Matrix * column vector

Example1 expression
V2 (V3 1 2 3) (V3 4 5 6) !* V3 7 8 9V2 50 122
value(*!) :: (Num a, Foldable t, Additive f, Additive t) => t a -> t (f a) -> f a
#

Row vector * matrix

Example1 expression
V2 1 2 *! V2 (V3 3 4 5) (V3 6 7 8)V3 15 18 21
value(!!*) :: (Functor m, Functor r, Num a) => m (r a) -> a -> m (r a)
#

Matrix-scalar product

Example1 expression
V2 (V2 1 2) (V2 3 4) !!* 5V2 (V2 5 10) (V2 15 20)
value(*!!) :: (Functor m, Functor r, Num a) => a -> m (r a) -> m (r a)
#

Scalar-matrix product

Example1 expression
5 *!! V2 (V2 1 2) (V2 3 4)V2 (V2 5 10) (V2 15 20)
valuecolumn
  1. :: Representable f
  2. => LensLike (Context a b) s t a b
  3. -> Lens (f s) (f t) (f a) (f b)
#

This is a generalization of inside to work over any corepresentable Functor.

column :: Representable f => Lens s t a b -> Lens (f s) (f t) (f a) (f b)

In practice it is used to access a column of a matrix.

Example1 expression
V2 (V3 1 2 3) (V3 4 5 6) ^._xV3 1 2 3
Example1 expression
V2 (V3 1 2 3) (V3 4 5 6) ^.column _xV2 1 4
valueadjoint :: (Functor m, Distributive n, Conjugate a) => m (n a) -> n (m a)
#

Hermitian conjugate or conjugate transpose

Example1 expression
adjoint (V2 (V2 (1 :+ 2) (3 :+ 4)) (V2 (5 :+ 6) (7 :+ 8)))V2 (V2 (1.0 :+ (-2.0)) (5.0 :+ (-6.0))) (V2 (3.0 :+ (-4.0)) (7.0 :+ (-8.0)))
typetype M22 a = V2 (V2 a)
#

A 2x2 matrix with row-major representation

typetype M23 a = V2 (V3 a)
#

A 2x3 matrix with row-major representation

typetype M24 a = V2 (V4 a)
#

A 2x4 matrix with row-major representation

typetype M32 a = V3 (V2 a)
#

A 3x2 matrix with row-major representation

typetype M33 a = V3 (V3 a)
#

A 3x3 matrix with row-major representation

typetype M34 a = V3 (V4 a)
#

A 3x4 matrix with row-major representation

typetype M42 a = V4 (V2 a)
#

A 4x2 matrix with row-major representation

typetype M43 a = V4 (V3 a)
#

A 4x3 matrix with row-major representation

typetype M44 a = V4 (V4 a)
#

A 4x4 matrix with row-major representation

valuem33_to_m44 :: Num a => M33 a -> M44 a
#

Convert a 3x3 matrix to a 4x4 matrix extending it with 0's in the new row and column.

valuem43_to_m44 :: Num a => M43 a -> M44 a
#

Convert from a 4x3 matrix to a 4x4 matrix, extending it with the [ 0 0 0 1 ] column vector

valuedet22 :: Num a => M22 a -> a
#

2x2 matrix determinant.

Example1 expression
det22 (V2 (V2 a b) (V2 c d))a * d - b * c
valuedet33 :: Num a => M33 a -> a
#

3x3 matrix determinant.

Example1 expression
det33 (V3 (V3 a b c) (V3 d e f) (V3 g h i))a * (e * i - f * h) - d * (b * i - c * h) + g * (b * f - c * e)
valuedet44 :: Num a => M44 a -> a
#

4x4 matrix determinant.

valueinv22 :: Fractional a => M22 a -> M22 a
#

2x2 matrix inverse.

Example1 expression
inv22 $ V2 (V2 1 2) (V2 3 4)V2 (V2 (-2.0) 1.0) (V2 1.5 (-0.5))
valueinv33 :: Fractional a => M33 a -> M33 a
#

3x3 matrix inverse.

Example1 expression
inv33 $ V3 (V3 1 2 4) (V3 4 2 2) (V3 1 1 1)V3 (V3 0.0 0.5 (-1.0)) (V3 (-0.5) (-0.75) 3.5) (V3 0.5 0.25 (-1.5))
valueidentity :: (Num a, Traversable t, Applicative t) => t (t a)
#

The identity matrix for any dimension vector.

Example2 expressions
identity :: M44 IntV4 (V4 1 0 0 0) (V4 0 1 0 0) (V4 0 0 1 0) (V4 0 0 0 1)identity :: V3 (V3 Int)V3 (V3 1 0 0) (V3 0 1 0) (V3 0 0 1)
classclass Functor m => Trace (m :: Type -> Type) where
#

Methods

  • trace :: Num a => m (m a) -> a

    Compute the trace of a matrix

    Example1 expression
    trace (V2 (V2 a b) (V2 c d))a + d
  • diagonal :: m (m a) -> m a

    Compute the diagonal of a matrix

    Example1 expression
    diagonal (V2 (V2 a b) (V2 c d))V2 a d
Instances14Trace, …
value_m22 :: (Representable t, R2 t, R2 v) => Lens' (t (v a)) (M22 a)
#

Extract a 2x2 matrix from a matrix of higher dimensions by dropping excess rows and columns.

value_m23 :: (Representable t, R2 t, R3 v) => Lens' (t (v a)) (M23 a)
#

Extract a 2x3 matrix from a matrix of higher dimensions by dropping excess rows and columns.

value_m24 :: (Representable t, R2 t, R4 v) => Lens' (t (v a)) (M24 a)
#

Extract a 2x4 matrix from a matrix of higher dimensions by dropping excess rows and columns.

value_m32 :: (Representable t, R3 t, R2 v) => Lens' (t (v a)) (M32 a)
#

Extract a 3x2 matrix from a matrix of higher dimensions by dropping excess rows and columns.

value_m33 :: (Representable t, R3 t, R3 v) => Lens' (t (v a)) (M33 a)
#

Extract a 3x3 matrix from a matrix of higher dimensions by dropping excess rows and columns.

value_m34 :: (Representable t, R3 t, R4 v) => Lens' (t (v a)) (M34 a)
#

Extract a 3x4 matrix from a matrix of higher dimensions by dropping excess rows and columns.

value_m42 :: (Representable t, R4 t, R2 v) => Lens' (t (v a)) (M42 a)
#

Extract a 4x2 matrix from a matrix of higher dimensions by dropping excess rows and columns.

value_m43 :: (Representable t, R4 t, R3 v) => Lens' (t (v a)) (M43 a)
#

Extract a 4x3 matrix from a matrix of higher dimensions by dropping excess rows and columns.

value_m44 :: (Representable t, R4 t, R4 v) => Lens' (t (v a)) (M44 a)
#

Extract a 4x4 matrix from a matrix of higher dimensions by dropping excess rows and columns.