A simple linear algebra library using flat-arrays
Representation of a matrix. You can create matrices of any type but arithmetic operations in Matrix.Operations requires the matrices to have numeric types.
empty : Matrix a
Create an empty matrix.
size empty == ( 0, 0 )
repeat : Basics.Int -> Basics.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 : Basics.Int -> Basics.Int -> (( Basics.Int, Basics.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 : Basics.Int -> Matrix number
Create the identity matrix of dimension n
.
fromList : Basics.Int -> Basics.Int -> List a -> Maybe (Matrix a)
Create a matrix from a list given the desired size.
If the list has a length inferior to n * m
, returns Nothing
.
fromList 2 2 [ 1, 1, 1, 1, 1 ] == Just <| repeat 2 2 1
fromList 3 3 [ 0, 1, 2 ] == Nothing
fromLists : List (List a) -> Maybe (Matrix a)
Create a matrix from a list of lists.
If any inner list is shorter than the first, returns Nothing
.
Otherwise, the length of the first list determines the width of the matrix.
fromLists [] == Just empty
fromLists [ [] ] == Just empty
fromLists [ [ 1, 2, 3 ], [ 1, 2 ] ] == Nothing
fromLists [ [ 1, 0 ], [ 0, 1 ] ] == Just <| identity 2
height : Matrix a -> Basics.Int
Return the number of rows in a given matrix.
width : Matrix a -> Basics.Int
Return the number of columns in a given matrix.
size : Matrix a -> ( Basics.Int, Basics.Int )
Return the dimensions of a given matrix in the form (rows, columns)
.
get : Basics.Int -> Basics.Int -> Matrix a -> Maybe a
Return Just
the element at the index or Nothing
if the index is out of bounds.
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
.
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 : (a -> String) -> Matrix a -> String
Convert a matrix to a formatted string.
pretty (identity 3) = """
[ [ 1, 0, 0 ]
, [ 0, 1, 0 ]
, [ 0, 0, 1 ] ]
"""