ryry0 / elm-numeric / Matrix

A matrix library written completely in Elm. This library aims to be a reasonably complete suite of linear algebra tools.

Some highlights are that this library has generic sized matrices, transposes, multiplication, and inversion.

import Matrix as Mt


--and program away!

The Matrix Type


type Matrix

The Matrix type. It can either be an actual matrix or an error string.

Creating Matrices

fromList : ( Basics.Int, Basics.Int ) -> List Basics.Float -> Matrix

Create a (n rows x m columns) matrix with the list as the elements. Fails if dimension mismatch. Elements need to be specified in row-major order.

matrix =
    Matrix.fromList ( 2, 3 ) [ 2, 2, 2, 3, 3, 3 ]

from2DList : List (List Basics.Float) -> Matrix

Create a (n x m) matrix with inner lists being rows. The following is a 2 x 3 matrix:

matrix =
    Matrix.from2DList
        [ [ 2, 2, 2 ]
        , [ 3, 3, 3 ]
        ]

mat : List (List Basics.Float) -> Matrix

Create a (n x m) matrix with inner lists being rows. The following is a 2 x 3 matrix:

matrix =
    Matrix.mat
        [ [ 2, 2, 2 ]
        , [ 3, 3, 3 ]
        ]

fromString : String -> Matrix

Create a (n x m) matrix with inner lists being rows. In string format you can use Matlab/Julia-esque syntax. Spaces denote elements in a row. Semicolons denote elements in a column. The string must begin with [ and end with ].

The following is a 2 x 3 matrix:

matrix =
    Matrix.fromString "[ 2 2 2; 3 3 3]"

Any alpha/garbage characters will be set to zero.

mats : String -> Matrix

Shorthand for fromString

zeroes : ( Basics.Int, Basics.Int ) -> Matrix

Generate a matrix of zeroes.

lots_of_zeroes =
    Matrix.zeroes ( 3, 4 )

ones : ( Basics.Int, Basics.Int ) -> Matrix

Generate a matrix of ones.

lots_of_ones =
    Matrix.ones ( 4, 3 )

eye : Basics.Int -> Matrix

Create an nxn identity matrix.

identity =
    Matrix.eye 3

upper : Basics.Int -> Matrix

Create an nxn upper triangular matrix.

triangle =
    Matrix.upper 4

lower : Basics.Int -> Matrix

Create an nxn lower triangular matrix.

ltriangle =
    Matrix.lower 4

strictLower : Basics.Int -> Matrix

Create an nxn strict lower triangular matrix. This means that elements along the diagonal are zero.

sltriangle =
    Matrix.strictLower 4

strictUpper : Basics.Int -> Matrix

Create an nxn strict upper triangular matrix. This means that elements along the diagonal are zero.

striangle =
    Matrix.strictUpper 4

Creating Vectors

cvecFromList : List Basics.Float -> Matrix

Create a column vector from a list.

column =
    Matrix.cvecFromList [ 1, 2, 3, 4 ]

rvecFromList : List Basics.Float -> Matrix

Create a row vector from a list.

cvec : List Basics.Float -> Matrix

Create a column vector from a list.

rvec : List Basics.Float -> Matrix

Create a row vector from a list.

vec : List Basics.Float -> Matrix

Create a column vector from a list.

Vector Operations

cross : Matrix -> Matrix -> Matrix

Get the cross product of two 3d vectors. a >< b

x =
    Matrix.vec [ 1, 0, 0 ]

y =
    Matrix.vec [ 0, 1, 0 ]

z =
    Matrix.cross x y

dot : Matrix -> Matrix -> Maybe Basics.Float

Performs the dot product of two nxn vectors

x =
    Matrix.vec [ 1, 0, 0 ]

y =
    Matrix.vec [ 0, 1, 0 ]

zero =
    Matrix.dot x y

Matrix Element-wise Operations

add : Matrix -> Matrix -> Matrix

Add two matrices of identical dimensions together

a =
    Matrix.add (Matrix.zeroes ( 2, 2 )) (Matrix.ones ( 2, 2 ))

equivalent : Basics.Float -> Matrix -> Matrix -> Basics.Bool

Checks if two matrices are equivalent within some epsilon.

epsilon =
    10 ^ -4

is_equivalent =
    equivalent epsilon a b

sMul : Basics.Float -> Matrix -> Matrix

Perform scalar multiplication on a matrix.

sDiv : Basics.Float -> Matrix -> Matrix

Perform scalar division on a matrix.

map : (Basics.Float -> Basics.Float) -> Matrix -> Matrix

Map a function over all elements individually

map2 : (Basics.Float -> Basics.Float -> Basics.Float) -> Matrix -> Matrix -> Matrix

Map a function over elements of same index between matrices

eMul : Matrix -> Matrix -> Matrix

Perform element by element multiplication on a matrix.

Matrix Operations

mul : Matrix -> Matrix -> Matrix

Perform matrix multiplication

A * B

a =
    Matrix.eye (2, 2)

b =
    Matrix.mats "[2, 3; 4 5]"

c =
    mul a b

vcat : Matrix -> Matrix -> Matrix

Concatenate two matrices vertically.

hcat : Matrix -> Matrix -> Matrix

Concatenate two matrices horizontally.

get : ( Basics.Int, Basics.Int ) -> Matrix -> Maybe Basics.Float

Get an item at index (row, column). Indices are 1-indexed.

a =
    Matrix.mats "[3 4; 5 6]"

b =
    -- equals 4
    get ( 1, 2 ) a

set : ( Basics.Int, Basics.Int ) -> Basics.Float -> Matrix -> Matrix

Get an item at index (row, column). Indices are 1-indexed.

transpose : Matrix -> Matrix

Transpose a matrix.

determinant : Matrix -> Maybe Basics.Float

Get the determinant of a square matrix.

a =
    Matrix.mats "[1 2 3; 4 5 6; 7 8 9]"

is_singular =
    if determinant a == 0 then
        "Matrix is singular"

    else
        "Matrix is not singular"

det : Matrix -> Maybe Basics.Float

Shorthand for determinant.

solveV : Matrix -> Matrix -> Matrix

Solve a system of equations of the form Ax = b. You provide A and b and get back x Where A is a matrix, and b and x are vectors

a =
    Matrix.eye 3

b =
    Matrix.vec [ 3, 2, 1 ]

x =
    Matrix.solve a b

solve : Matrix -> Matrix -> Matrix

Solve a system of equations of the form AX = B. You provide A and b and get back x Where A, B, X are matrices B is a matrix of solution vectors horizontally concatenated.

a =
    Matrix.eye 3

b =
    Matrix.hcat (Matrix.vec [ 3, 2, 1 ]) (Matrix.vec [ 1, 2, 3 ])

x =
    Matrix.solve a b

invert : Matrix -> Matrix

Invert a square matrix.

a =
    "[ 2 5; 6 7]"

inva =
    invert a

identity =
    mul a inva

inv : Matrix -> Matrix

Shorthand for invert.

luDecomp : Matrix -> ( Matrix, Matrix )

Get the lu decomposition of a matrix Since pivoting isn't implemented, watch out for numerical instability

getRows : Matrix -> List Matrix

Returns the rows of a matrix in a list.

getColumns : Matrix -> List Matrix

Returns the columns of a matrix in a list.

size : Matrix -> ( Basics.Int, Basics.Int )

Returns size of matrix, (rows, columns)

Matrix Display

toString : Matrix -> String

Change matrix into string form, such as what would be displayed in the terminal.

Matrix.toString (Matrix.eye 3) == " 1 0 0\n 0 1 0\n 0 0 1"

debugPrint : Matrix -> String

Helper to debug print. Most useful in repl.

> Matrix.debugPrint (Matrix.eye 3)
(3, 3) Matrix
 1 0 0
 0 1 0
 0 0 1
 : ""
 ""

Interop

to2DList : Matrix -> List (List Basics.Float)

Returns matrix as 2d list. Returns empty list if Matrix is in error.

Matrix.to2DList (Matrix.eye 2) == [ [ 1, 0 ], [ 0, 1 ] ]

toFlatList : Matrix -> List Basics.Float

Returns matrix as flat list

Matrix.toFlatList (Matrix.eye 2) == [ 1, 0, 0, 1 ]