Matrices
This library provides support for creating and manipulating matrices of real numbers, including some math functions on matrices.
For high-performance dense matrices that support only a finite set of mathematical operations, see the GSL bindings.
Basic Usage
structure M = Mlmatrix(Mlvector);
(* Make a 3x3 matrix with all entries set to 0.0 *)
val m = M.make (3, 3, 0.0)
(* Set the diagonal to 1.0 to make the identity matrix *)
val _ = M.set (m, (0, 0), 1.0)
val _ = M.set (m, (1, 1), 1.0)
val _ = M.set (m, (2, 2), 1.0)
(* Scale m by 2 *)
val m2 = M.mulScalar (m, 2.0)
(* Create a matrix from a list of lists *)
val m3 = M.fromList
([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
(* Multiply two matrices *)
val m4 = M.mul (m2, m3)
val _ = print (M.toString m4)
Interface
Types
type scalar- The
scalartype is a synonym forreal.
- The
type mlvector- The type of rows and columns in the matrix. These are vectors.
type mlmatrix- The type of the matrix itself.
Methods
val size : mlmatrix -> (int * int)val makeInit : (int -> mlvector) * int -> mlmatrixval make : (int * int * real) -> mlmatrixval sub : mlmatrix * (int * int) -> scalarval row : mlmatrix * int -> mlmatrixval col : mlmatrix * int -> mlmatrixval onerow : mlmatrix * int -> mlvectorval onecol : mlmatrix * int -> mlvectorval set : mlmatrix * (int * int) * scalar -> unitval update : (scalar -> scalar) * mlmatrix * (int * int) -> unitval modify : (scalar -> scalar) * mlmatrix -> unitval modifyi : ((int * int * scalar) -> scalar) * mlmatrix -> unitval modifyrowi : ((int * mlvector) -> unit) * mlmatrix -> unitval copy : mlmatrix -> mlmatrixval vec2mat : mlvector -> mlmatrixval squeeze1 : mlmatrix -> mlvectorval squeeze2 : mlmatrix -> mlvectorval squeeze12 : mlmatrix -> scalarval map : (scalar -> scalar) * mlmatrix -> mlmatrixval mapi : ((int * int * scalar) -> scalar) * mlmatrix -> mlmatrixval map2 : ((scalar * scalar) -> scalar) * mlmatrix * mlmatrix -> mlmatrixval map2i : ((int * int * scalar * scalar) -> scalar) * mlmatrix * mlmatrix -> mlmatrixval foldl : (('a * scalar) -> 'a) * 'a * mlmatrix -> 'aval foldli : (('a * int * int * scalar) -> 'a) * 'a * mlmatrix -> 'aval foldRowl : ('a * mlmatrix -> 'a) * 'a * mlmatrix -> 'aval foldRowVecl : ('a * mlvector -> 'a) * 'a * mlmatrix -> 'aval transpose : mlmatrix -> mlmatrixval mulScalar : mlmatrix * scalar -> mlmatrixval mul : mlmatrix * mlmatrix -> mlmatrixval add : mlmatrix * mlmatrix -> mlmatrixval addModify : mlmatrix * mlmatrix -> unitval elemwise : mlmatrix * mlmatrix -> mlmatrixval elemwiseInplace : mlmatrix * mlmatrix -> mlmatrixval matMulVec : mlmatrix * mlvector -> mlvectorval fromArray2 : scalar array array -> mlmatrixval fromList : scalar list list -> mlmatrixval toStringF : (scalar -> string) * mlmatrix -> stringval toString : mlmatrix -> string
Method Overview
size mat.- Returns the dimensions of the matrix. The resulting value is
(numRows, numColumns).
- Returns the dimensions of the matrix. The resulting value is
makeInit (f, height)- Creates a matrix where each row in the matrix is the result of applying
fto the index of that row.
- Creates a matrix where each row in the matrix is the result of applying
make (numRows, numColumns, default)- Creates a
numRows-by-numColumnsmatrix with every element set todefault.
- Creates a
sub (mat, (row, column)).- Returns the element of the matrix at the given row and column.
row (mat, idx).- Returns one row of a matrix as a 1-by-n matrix.
col (mat, idx).- Returns one column of a matrix as a m-by-1 matrix.
onerow (mat, idx).- Like
row, but converts the result to vector.
- Like
onecol (mat, idx).- Like
col, but converts the result to vector.
- Like
set (mat, (row, col), r).- Updates the element in
matat therowth row andcolth column to ber. This modifiesmat.
- Updates the element in
update (f, mat, (idx1, idx2)).- Updates the element in
matat therowth row andcolth column to be the result of applyingfto the original value at that index. This modifiesmat.
- Updates the element in
modify (f, mat)- Updates every element in
matto be the result of applyingfto the original value at each index. This modifiesmat.
- Updates every element in
modifyi (f, mat)- Like
modify, but the modification function also takes the row and column numbers as input.
- Like
modifyrowi (f, mat)- Like
modifyi, butfoperates on a whole row at a time, with the row represented as a vector.
- Like
copy mat- Creates a new matrix with the same contents as
mat.
- Creates a new matrix with the same contents as
vec2mat vec- Converts a vector to a 1-by-n matrix.
squeeze1 mat- Extracts the first row of a matrix as a vector.
mlvector squeeze2(mlmatrix mat)- Extracts the first column of a matrix as a vector.
real squeeze12(mlmatrix mat)- Equivalent to
sub (mat, (0, 0)).
- Equivalent to
map (f, mat)- Creates a new matrix such that the value at each index in the new matrix is
the result of applying
fto the value at the same index in the original matrix.
- Creates a new matrix such that the value at each index in the new matrix is
the result of applying
mapi (f, mat)- Like
map, butfalso takes the row and column numbers as input.
- Like
map2 (f, mat1, mat2)- Map over two matrices. The resulting matrix will have the smaller size of these two matrices.
map2i (f, mat1, mat2)- Like
map2, butfalso takes the row and column numbers as input.
- Like
foldl (f, base, mat)- Combines the elements of
matusing the functionf, starting withbase. The functionfshould take as an argument a tuple of the result accumulated so far and the next element ofarrto process.
- Combines the elements of
foldli (f, base, mat)- Like
fold, butfalso the row and column number as input.
- Like
foldRowl (f, base, mat)- Combines the rows of
matusing the functionf, starting withbase. The functionfshould take as an argument a tuple of the result accumulated so far and the next row ofarr, represented as a matrix, to process.
- Combines the rows of
foldRowVecl (f, base, mat)- Combines the rows of
matusing the functionf, starting withbase. The functionfshould take as an argument a tuple of the result accumulated so far and the next row ofarrto process.
- Combines the rows of
transpose mat- Creates a new matrix that is the transpose of
mat.
- Creates a new matrix that is the transpose of
mulScalar (mat, a)- Creates a new matrix that is
matwith each element multiplied bya.
- Creates a new matrix that is
mul (mat1, mat2)- Creates a new matrix that is the product of
mat1andmat2.
- Creates a new matrix that is the product of
add (mat1, mat2)- Creates a new matrix that is the sum of
mat1andmat2.
- Creates a new matrix that is the sum of
addModify (mat1, mat2)- Like
add, but modifies the first matrix in-place instead of returning a new matrix. This modifiesmat1.
- Like
elemwise (mat1, mat2)- Creates a new matrix that is the element-wise product of
mat1andmat2.
- Creates a new matrix that is the element-wise product of
elemwiseInplace (mat1, mat2)- Like
elemwise, but modifies the first matrix in-place instead of returning a new matrix. This modifiesmat1.
- Like
matMulVec (mat, vec)- Creates a new matrix that is the product of the
matandvectreated as a m-by-1 matrix.
- Creates a new matrix that is the product of the
fromArray2 arr2- Converts an array of arrays of reals to a matrix. Modifications to the resulting matrix will modify the original array, and modifications to the array will modify the matrix.
fromList l2- Converts a list of lists of reals to a matrix.
toString mat- Converts a matrix to a string.