hgeometry:kernel
Copyright(C) Frank Staals
Licensesee the LICENSE file
MaintainerFrank Staals
Safe HaskellNone
LanguageGHC2021

HGeometry.Matrix.Class

Description

A class of types representing matrices

Synopsis

Documentation

class (r ~ NumType matrix, Ixed matrix, IxValue matrix ~ r, Index matrix ~ (Int, Int), HasElements matrix matrix) => Matrix_ matrix (n :: Nat) (m :: Nat) r | matrix -> n, matrix -> m, matrix -> r where Source #

A matrix of n rows, each of m columns, storing values of type r.

Minimal complete definition

generateMatrix, matrixFromRows, rows

Methods

identityMatrix :: matrix Source #

Produces the Identity Matrix.

>>> printMatrix $ identityMatrix @(Matrix 3 3 Int)
Vector3 1 0 0
Vector3 0 1 0
Vector3 0 0 1

generateMatrix :: ((Int, Int) -> r) -> matrix Source #

Given a function that specifies the values, generate the matrix

matrixFromList :: [r] -> Maybe matrix Source #

Given a list of the elements in the matrix, in row by row order, constructs the matrix. requires that there are exactly n*m elements.

>>> matrixFromList @(Matrix 2 3 Int) [1,2,3,4,5,6]
Just (Matrix (Vector2 (Vector3 1 2 3) (Vector3 4 5 6)))
>>> matrixFromList @(Matrix 2 3 Int) [1,2,3,4,5,6,7]
Nothing

matrixFromRows :: Vector_ rowVector n (Vector m r) => rowVector -> matrix Source #

Given a list of the elements in the matrix, in row by row order, constructs the matrix. requires that there are exactly n*m elements.

>>> matrixFromRows @(Matrix 2 3 Int) (Vector2 (Vector3 1 2 3) (Vector3 4 5 6))
Matrix (Vector2 (Vector3 1 2 3) (Vector3 4 5 6))

(!*!) :: forall matrix' (m' :: Nat) matrix''. (Matrix_ matrix' m m' r, Matrix_ matrix'' n m' r, Num r, Has_ Additive_ m r) => matrix -> matrix' -> matrix'' infixl 7 Source #

Matrix multiplication

>>> let m1  = matrixFromRows @(Matrix 2 3 Int) (Vector2 (Vector3 1 2 3) (Vector3 4 5 6))
>>> let r i = Vector4 i (i*10) (i*100) (i*1000)
>>> let m2  = matrixFromRows @(Matrix 3 4 Int) (Vector3 (r 1) (r 2) (r 3))
>>> printMatrix $ (m1 !*! m2 :: Matrix 2 4 Int)
Vector4 14 140 1400 14000
Vector4 32 320 3200 32000

(!*) :: matrix -> Vector m r -> Vector n r infixl 7 Source #

Multiply a matrix and a vector.

>>> let m = matrixFromRows @(Matrix 2 3 Int) (Vector2 (Vector3 1 2 3) (Vector3 4 5 6))
>>> m !* Vector3 2 3 1
Vector2 11 29

(*!!) :: r -> matrix -> matrix infixl 7 Source #

Multiply a scalar and a matrix

(!!*) :: matrix -> r -> matrix infixl 7 Source #

Multiply a matrix and a scalar

rows :: Lens' matrix (Vector n (Vector m r)) Source #

Lens to access all rows

row :: Int -> matrix -> Maybe (Vector m r) Source #

Access the i^th row in the matrix.

column :: Int -> matrix -> Maybe (Vector n r) Source #

Access the j^th column in the matrix.

Instances

Instances details
(Has_ Vector_ n (Vector m r), Has_ Additive_ m r, Ixed (Vector n (Vector m r)), Ixed (Vector m r)) => Matrix_ (Matrix n m r) n m r Source # 
Instance details

Defined in HGeometry.Matrix.ByRows

Methods

identityMatrix :: Matrix n m r Source #

generateMatrix :: ((Int, Int) -> r) -> Matrix n m r Source #

matrixFromList :: [r] -> Maybe (Matrix n m r) Source #

matrixFromRows :: Vector_ rowVector n (Vector m r) => rowVector -> Matrix n m r Source #

(!*!) :: forall matrix' (m' :: Nat) matrix''. (Matrix_ matrix' m m' r, Matrix_ matrix'' n m' r, Num r, Has_ Additive_ m r) => Matrix n m r -> matrix' -> matrix'' Source #

(!*) :: Matrix n m r -> Vector m r -> Vector n r Source #

(*!!) :: r -> Matrix n m r -> Matrix n m r Source #

(!!*) :: Matrix n m r -> r -> Matrix n m r Source #

rows :: Lens' (Matrix n m r) (Vector n (Vector m r)) Source #

row :: Int -> Matrix n m r -> Maybe (Vector m r) Source #

column :: Int -> Matrix n m r -> Maybe (Vector n r) Source #

class HasElements matrix matrix' where Source #

Types that have an elements field lens.

Methods

elements :: IndexedTraversal1 (Int, Int) matrix matrix' (NumType matrix) (NumType matrix') Source #

IndexedTraversal over the elements of the matrix, each index is a (row,column) index pair.

Instances

Instances details
(Has_ Vector_ n (Vector m r), Has_ Vector_ m r, Has_ Vector_ n (Vector m s), Has_ Vector_ m s, HasComponents (Vector m r) (Vector m s), HasComponents (Vector n (Vector m r)) (Vector n (Vector m s))) => HasElements (Matrix n m r) (Matrix n m s) Source # 
Instance details

Defined in HGeometry.Matrix.ByRows

Methods

elements :: IndexedTraversal1 (Int, Int) (Matrix n m r) (Matrix n m s) (NumType (Matrix n m r)) (NumType (Matrix n m s)) Source #

class HasDeterminant (d :: Nat) where Source #

Dimensions for which we can compute the determinant of a matrix

Methods

det :: (Num r, Matrix_ matrix d d r) => matrix -> r Source #

Instances

Instances details
HasDeterminant 1 Source # 
Instance details

Defined in HGeometry.Matrix.Class

Methods

det :: (Num r, Matrix_ matrix 1 1 r) => matrix -> r Source #

HasDeterminant 2 Source # 
Instance details

Defined in HGeometry.Matrix.Class

Methods

det :: (Num r, Matrix_ matrix 2 2 r) => matrix -> r Source #

HasDeterminant 3 Source # 
Instance details

Defined in HGeometry.Matrix.Class

Methods

det :: (Num r, Matrix_ matrix 3 3 r) => matrix -> r Source #

HasDeterminant 4 Source # 
Instance details

Defined in HGeometry.Matrix.Class

Methods

det :: (Num r, Matrix_ matrix 4 4 r) => matrix -> r Source #

class Invertible (n :: Nat) where Source #

Class of matrices that are invertible.

Methods

inverseMatrix :: (Fractional r, Matrix_ matrix n n r, Has_ Vector_ n r) => matrix -> matrix Source #

given an invertable square \(n \times n\) matrix A, computes the \(n \times n\) matrix B such that A !*! B = identityMatrix

Instances

Instances details
Invertible 1 Source # 
Instance details

Defined in HGeometry.Matrix.Class

Methods

inverseMatrix :: (Fractional r, Matrix_ matrix 1 1 r, Has_ Vector_ 1 r) => matrix -> matrix Source #

Invertible 2 Source # 
Instance details

Defined in HGeometry.Matrix.Class

Methods

inverseMatrix :: (Fractional r, Matrix_ matrix 2 2 r, Has_ Vector_ 2 r) => matrix -> matrix Source #

Invertible 3 Source # 
Instance details

Defined in HGeometry.Matrix.Class

Methods

inverseMatrix :: (Fractional r, Matrix_ matrix 3 3 r, Has_ Vector_ 3 r) => matrix -> matrix Source #