This namespace contains an implementation of a Matrix
datatype and various
operations for creating and working with Matrix
instances.
sicmutils.matrix
also extends many SICMUtils generic operations
to the Matrix
datatype.
This namespace contains an implementation of a [[Matrix]] datatype and various operations for creating and working with [[Matrix]] instances. [[sicmutils.matrix]] also extends many SICMUtils generic operations to the [[Matrix]] datatype.
Set this dynamic variable to false
to allow s->m
to operate
on structures for which (* ls ms rs)
does NOT yield a numerical value.
Set this dynamic variable to `false` to allow [[s->m]] to operate on structures for which `(* ls ms rs)` does NOT yield a numerical value.
(->structure m)
(->structure m outer-orientation inner-orientation t?)
Returns a structure generated by converting m
into a nested structure with
the supplied outer-orientation
and inner-orientation
.
If t?
is true, the columns of m
will form the inner tuples. If t?
is
false, the rows of m
will form the inner tuples.
By default, if you supply a single argument (the matrix m
), a matrix turns
into a single outer ::s/down
of inner columns represented as ::up
structures.
Returns a structure generated by converting `m` into a nested structure with the supplied `outer-orientation` and `inner-orientation`. If `t?` is true, the columns of `m` will form the inner tuples. If `t?` is false, the rows of `m` will form the inner tuples. By default, if you supply a single argument (the matrix `m`), a matrix turns into a single outer `::s/down` of inner columns represented as `::up` structures.
(as-matrix F)
Any one argument function of a structure can be seen as a matrix. This is only useful if the function has a linear multiplier (e.g. derivative)
Any one argument function of a structure can be seen as a matrix. This is only useful if the function has a linear multiplier (e.g. derivative)
(by-cols & cols)
Returns a matrix whose columns consist of the supplied sequence of cols
.
These all must be the same length.
Variadic equivalent to by-cols*
.
Returns a matrix whose columns consist of the supplied sequence of `cols`. These all must be the same length. Variadic equivalent to [[by-cols*]].
(by-cols* cols)
Returns a matrix whose columns consist of the supplied sequence of cols
.
These all must be the same length.
for a variadic equivalent, see by-cols
.
Returns a matrix whose columns consist of the supplied sequence of `cols`. These all must be the same length. for a variadic equivalent, see [[by-cols]].
(by-rows & rows)
Returns a matrix whose rows consist of the supplied sequence of rows
. These
all must be the same length.
Variadic equivalent to by-rows*
.
Returns a matrix whose rows consist of the supplied sequence of `rows`. These all must be the same length. Variadic equivalent to [[by-rows*]].
(by-rows* rows)
Returns a matrix whose rows consist of the supplied sequence of rows
. These
all must be the same length.
for a variadic equivalent, see by-rows
.
Returns a matrix whose rows consist of the supplied sequence of `rows`. These all must be the same length. for a variadic equivalent, see [[by-rows]].
(characteristic-polynomial m x)
Compute the characteristic polynomial of the square matrix m, evaluated at x
.
Typically x
will be a symbolic variable, but if you wanted to get the value
of the characteristic polynomial at some particular numerical point x'
you
could pass that too.
Compute the characteristic polynomial of the square matrix m, evaluated at `x`. Typically `x` will be a symbolic variable, but if you wanted to get the value of the characteristic polynomial at some particular numerical point `x'` you could pass that too.
(cofactors m)
Returns the matrix of cofactors of the supplied square matrix m
.
Returns the matrix of cofactors of the supplied square matrix `m`.
(column & xs)
Returns a column matrix populated by the supplied xs
. Variadic equivalent
to column*
.
Returns a column matrix populated by the supplied `xs`. Variadic equivalent to [[column*]].
(column* xs)
Returns a column matrix populated by the supplied xs
. For a variadic equivalent,
see column
.
Returns a column matrix populated by the supplied `xs`. For a variadic equivalent, see [[column]].
(column-matrix->up m)
Returns the single column from the supplied column matrix as an up
. Errors if
some other type is supplied.
Returns the single column from the supplied column matrix as an `up`. Errors if some other type is supplied.
(column-matrix->vector m)
Returns the single column from the supplied column matrix as a vector. Errors if some other type is supplied.
Returns the single column from the supplied column matrix as a vector. Errors if some other type is supplied.
(column? m)
Returns true if m
is a matrix with a single column (a 'column matrix'),
false otherwise.
Returns true if `m` is a matrix with a single column (a 'column matrix'), false otherwise.
(determinant m)
Returns the determinant of the supplied square matrix m
.
Generic operations are used, so this works on symbolic square matrices.
Returns the determinant of the supplied square matrix `m`. Generic operations are used, so this works on symbolic square matrices.
(diagonal m)
Returns the diagonal of the supplied matrix m
as an up structure. Errors if a
type other than a diagonal matrix is supplied.
Returns the diagonal of the supplied matrix `m` as an up structure. Errors if a type other than a diagonal matrix is supplied.
(diagonal? m)
Returns true if m
is a diagonal matrix (ie, a square matrix where every
non-diagonal element is zero), false otherwise.
Returns true if `m` is a diagonal matrix (ie, a square matrix where every non-diagonal element is zero), false otherwise.
(dimension m)
Returns the 'dimension', ie, the number of rows & columns, of the supplied square matrix. Errors if some other type is supplied.
Returns the 'dimension', ie, the number of rows & columns, of the supplied square matrix. Errors if some other type is supplied.
(down->row-matrix v)
Returns a row matrix with the contents of the supplied down
structure.
Errors if any other type is provided.
Returns a row matrix with the contents of the supplied `down` structure. Errors if any other type is provided.
(fmap f m)
Maps f
over the elements of the matrix m
returning a new matrix of the same
dimensions as m
.
Maps `f` over the elements of the matrix `m` returning a new matrix of the same dimensions as `m`.
(fmap-indexed f m)
Maps f
over three arguments:
m
i
j
and returns a new matrix of the same dimensions as m
.
Maps `f` over three arguments: - each element of the matrix `m` - its row `i` - its column `j` and returns a new matrix of the same dimensions as `m`.
(generate n f)
(generate r c f)
Returns a matrix with r
rows and c
columns, whose entries are generated by
the supplied function f
.
If you only supply one dimension n
the returned matrix will be square.
The entry in the i
th row and j
-th column is (f i j)
.
Returns a matrix with `r` rows and `c` columns, whose entries are generated by the supplied function `f`. If you only supply one dimension `n` the returned matrix will be square. The entry in the `i`th row and `j`-th column is `(f i j)`.
(get-in m is)
Like [[clojure.core/get-in]] for matrices, but obeying the scmutils convention: only one index is required to get an unboxed element from a column vector.
NOTE that this is perhaps an unprincipled exception...
Like [[clojure.core/get-in]] for matrices, but obeying the scmutils convention: only one index is required to get an unboxed element from a column vector. NOTE that this is perhaps an unprincipled exception...
(I n)
Return the identity matrix of order n
.
Return the identity matrix of order `n`.
(identity-like M)
Return an identity matrix whose ones and zeros match the types of the supplied
square matrix M
. Errors if a non-square matrix M
is supplied.
Return an identity matrix whose ones and zeros match the types of the supplied square matrix `M`. Errors if a non-square matrix `M` is supplied.
(identity? m)
Returns true if the supplied matrix m
is an identity matrix, false
otherwise.
Returns true if the supplied matrix `m` is an identity matrix, false otherwise.
(invert m)
Returns the inverse of the supplied square matrix m
.
Returns the inverse of the supplied square matrix `m`.
(literal-matrix sym nrows ncols)
Generates a nrows
x ncols
matrix of symbolic entries, each prefixed by
the supplied symbol sym
.
NOTE: The symbols in the returned matrix record their Einstein-notation path
into the structure that this matrix represents; a down
of up
columns. This
means that the returned indices embedded in the symbols look flipped, ji
vs
ij
.
For example:
(= (literal-matrix 'x 2 2)
(by-rows ['x_0↑0 'x_1↑0]
['x_0↑1 'x_1↑1]))
Generates a `nrows` x `ncols` matrix of symbolic entries, each prefixed by the supplied symbol `sym`. NOTE: The symbols in the returned matrix record their Einstein-notation path into the structure that this matrix represents; a `down` of `up` columns. This means that the returned indices embedded in the symbols look flipped, `ji` vs `ij`. For example: ```clojure (= (literal-matrix 'x 2 2) (by-rows ['x_0↑0 'x_1↑0] ['x_0↑1 'x_1↑1])) ```
(m->s ls m rs)
Convert the matrix m
into a structure S
, guided by the requirement that (* ls S rs)
should be a scalar.
Convert the matrix `m` into a structure `S`, guided by the requirement that `(* ls S rs)` should be a scalar.
(make-diagonal v)
Returns the diagonal matrix of order (count v)
with the elements of the
sequence v
along the diagonal.
Returns the diagonal matrix of order `(count v)` with the elements of the sequence `v` along the diagonal.
(make-zero n)
(make-zero m n)
Return a zero-valued matrix of m
rows and n
columns (nXn
if only n
is
supplied).
Return a zero-valued matrix of `m` rows and `n` columns (`nXn` if only `n` is supplied).
(matrix->vector m)
If m
is already a vector, acts as identity. Else, returns the matrix as a
vector of rows (or throws if neither of these types is passed).
If `m` is already a vector, acts as identity. Else, returns the matrix as a vector of rows (or throws if neither of these types is passed).
(matrix? m)
Returns true if the supplied m
is an instance of Matrix
, false
otherwise.
Returns true if the supplied `m` is an instance of [[Matrix]], false otherwise.
(nth-col m n)
Returns the n
-th column of the supplied matrix m
as an up
structure.
Returns the `n`-th column of the supplied matrix `m` as an `up` structure.
(nth-row m n)
Returns the n
-th row of the supplied matrix m
as a down
structure.
Returns the `n`-th row of the supplied matrix `m` as a `down` structure.
(num-cols m)
Returns the number of columns of the supplied matrix m
. Throws if a
non-matrix is supplied.
Returns the number of columns of the supplied matrix `m`. Throws if a non-matrix is supplied.
(num-rows m)
Returns the number of rows of the supplied matrix m
. Throws if a
non-matrix is supplied.
Returns the number of rows of the supplied matrix `m`. Throws if a non-matrix is supplied.
(row & xs)
Returns a row matrix populated by the supplied xs
. Variadic equivalent
to row*
.
Returns a row matrix populated by the supplied `xs`. Variadic equivalent to [[row*]].
(row* xs)
Returns a row matrix populated by the supplied xs
. For a variadic equivalent,
see row
.
Returns a row matrix populated by the supplied `xs`. For a variadic equivalent, see [[row]].
(row-matrix->down m)
Returns the single row from the supplied row matrix as a down
. Errors if some
other type is supplied.
Returns the single row from the supplied row matrix as a `down`. Errors if some other type is supplied.
(row-matrix->vector m)
Returns the single row from the supplied row matrix as a vector. Errors if some other type is supplied.
Returns the single row from the supplied row matrix as a vector. Errors if some other type is supplied.
(row? m)
Returns true if m
is a matrix with a single row (a 'row matrix'), false
otherwise.
Returns true if `m` is a matrix with a single row (a 'row matrix'), false otherwise.
(s->m ls ms rs)
Convert the structure ms
, which would be a scalar if the (compatible)
multiplication(* ls ms rs)
were performed, to a matrix.
Convert the structure `ms`, which would be a scalar if the (compatible) multiplication`(* ls ms rs)` were performed, to a matrix.
(seq-> xs)
Convert a sequence xs
(typically, of function arguments) to an up-structure.
Any matrix present in the argument list will be converted to row of columns
via ->structure
.
Convert a sequence `xs` (typically, of function arguments) to an up-structure. Any matrix present in the argument list will be converted to row of columns via [[->structure]].
(some f m)
Returns true if f
is true for some element of the matrix m
, false
otherwise. (Also works on arbitrary nested sequences.)
Returns true if `f` is true for some element of the matrix `m`, false otherwise. (Also works on arbitrary nested sequences.)
(square-structure-> s continue)
Converts the square structure s
into a matrix, and calls the supplied
continuation continue
with
Returns the result of the continuation call.
Converts the square structure `s` into a matrix, and calls the supplied continuation `continue` with - the generated matrix - a function which will restore a matrix to a structure with the same inner and outer orientations as s Returns the result of the continuation call.
(square-structure-operation s f)
Applies matrix operation f
to square structure s
and returns a structure of
the same type as the supplied structure.
Applies matrix operation `f` to square structure `s` and returns a structure of the same type as the supplied structure.
(square? m)
Returns true if m
is a square matrix, false otherwise.
Returns true if `m` is a square matrix, false otherwise.
(submatrix x lowrow hirow lowcol hicol)
Returns the submatrix of the matrix (or matrix-like structure) s
generated by
taking
lowrow
-> hirow
,lowcol
-> hicol
Returns the submatrix of the matrix (or matrix-like structure) `s` generated by taking - rows from `lowrow` -> `hirow`, - columns from `lowcol` -> `hicol`
(trace m)
Returns the trace (the sum of diagonal elements) of the square matrix m
.
Generic operations are used, so this works on symbolic square matrices.
Returns the trace (the sum of diagonal elements) of the square matrix `m`. Generic operations are used, so this works on symbolic square matrices.
(transpose m)
Returns the transpose of the matrix m
. The transpose is the original matrix,
with rows and columns swapped.
Returns the transpose of the matrix `m`. The transpose is the original matrix, with rows and columns swapped.
(up->column-matrix v)
Returns a column matrix with the contents of the supplied up
structure.
Errors if any other type is provided.
Returns a column matrix with the contents of the supplied `up` structure. Errors if any other type is provided.
(with-substituted-row m i v)
Returns a new matrix of identical shape to m
, with the vector v
substituted
for the i
th row.
Returns a new matrix of identical shape to `m`, with the vector `v` substituted for the `i`th row.
(without m i j)
Returns the matrix formed by deleting the i
-th row and j
-th column of the
given matrix m
.
Returns the matrix formed by deleting the `i`-th row and `j`-th column of the given matrix `m`.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close