Punie/elm-matrix/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Matrix

A simple linear algebra library using flat-arrays

The Matrix type

type Matrix a

Representation of a matrix. You can create matrices of any type but arithmetic operations in Matrix.Operations requires the matrices to have numeric types.

Creating matrices

empty : Matrix a

Create an empty matrix.

size empty == (0, 0)
repeat : Int -> Int -> a -> Matrix a

Create a matrix with a given size, filled with a default value.

repeat 2 3 0 ~= [ [ 0, 0, 0 ], [ 0, 0, 0 ] ]
initialize : Int -> Int -> (( Int, Int ) -> a) -> Matrix a

Createsa matrix with a given size, with the elements at index (i, j) initialized to the result of f (i, j).

initialize 3 3 (\(i,j) -> if i == j then 1 else 0) == identity 3
identity : Int -> Matrix number

Create the identity matrix of dimension n.

Get matrix dimensions

height : Matrix a -> Int

Return the number of rows in a given matrix.

width : Matrix a -> Int

Return the number of columns in a given matrix.

size : Matrix a -> ( Int, Int )

Return the dimensions of a given matrix in the form (rows, columns).

Working with individual elements

get : Int -> Int -> Matrix a -> Maybe a

Return Just the element at the index or Nothing if the index is out of bounds.

Matrix manipulation

map : (a -> b) -> Matrix a -> Matrix b

Apply a function on every element of a matrix

map2 : (a -> b -> c) -> Matrix a -> Matrix b -> Maybe (Matrix c)

Apply a function between pairwise elements of two matrices. If the matrices are of differents sizes, returns Nothing.

transpose : Matrix a -> Matrix a

Return the transpose of a matrix.

dot : Matrix number -> Matrix number -> Maybe (Matrix number)

Perform the standard matrix multiplication. If the dimensions of the matrices are incompatible, returns Nothing.

Matrix representation

toList : Matrix a -> List a

Convert the matrix to a flat list.

toList (identity 3) == [1,0,0,0,1,0,0,0,1]
toLists : Matrix a -> List (List a)

Convert the matrix to a list of lists.

toLists (identity 3) = [ [1,0,0], [0,1,0], [0,0,1] ]
pretty : Matrix a -> String

Convert a matrix to a formatted string.

pretty (identity 3) = """
    [ [ 1, 0, 0 ]
    , [ 0, 1, 0 ]
    , [ 0, 0, 1 ] ]
"""