impl Show for MatrixErrortest "det computes the determinant of a dense matrix" {
let m = @symmatrices.matrix([
[@symcore.int(1), @symcore.int(2)],
[@symcore.int(3), @symcore.int(4)],
])
inspect(@symprint.pretty_string(m.det()), content="-2")
}test "inv computes a dense matrix inverse" {
let m = @symmatrices.matrix([
[@symcore.int(1), @symcore.int(2)],
[@symcore.int(3), @symcore.int(4)],
])
inspect(m.inv().to_string(), content="Matrix([[-2, 1], [3/2, -1/2]])")
}test "rref reports pivot columns" {
let m = @symmatrices.matrix([
[@symcore.int(1), @symcore.int(2)],
[@symcore.int(2), @symcore.int(4)],
])
let (_, pivots) = m.rref()
debug_inspect(pivots, content="[0]")
}test "solve returns the dense solution matrix" {
let lhs = @symmatrices.matrix([
[@symcore.int(2), @symcore.int(1)],
[@symcore.int(1), @symcore.int(3)],
])
let rhs = @symmatrices.matrix([[@symcore.int(1)], [@symcore.int(2)]])
inspect(lhs.solve(rhs).to_string(), content="Matrix([[1/5], [3/5]])")
}pub enum MatrixExpr {
Concrete(Matrix)
Symbol(String, Int, Int)
Identity(Int)
Zero(Int, Int)
Add(Array[MatrixExpr])
Mul(Array[MatrixExpr])
BlockDiag(Array[MatrixExpr])
Block(Array[Array[MatrixExpr]])
Transpose(MatrixExpr)
Inverse(MatrixExpr)
Pow(MatrixExpr, Int)
}impl Show for MatrixExprfn MatrixExpr::as_explicit(self : MatrixExpr, env : Map[String, Matrix]) -> Matrix raise MatrixErrorimpl Add for SparseMatriximpl Mul for SparseMatriximpl Show for SparseMatriximpl Sub for SparseMatrixfn SparseMatrix::applyfunc(self : SparseMatrix, f : (Expr) -> Expr) -> SparseMatrix raise MatrixErrorfn SparseMatrix::col_insert(self : SparseMatrix, pos : Int, other : SparseMatrix) -> SparseMatrix raise MatrixErrorfn SparseMatrix::col_join(self : SparseMatrix, other : SparseMatrix) -> SparseMatrix raise MatrixErrorfn SparseMatrix::eigenvects(self : SparseMatrix) -> Array[(Expr, Int, Array[Array[Expr]])] raise MatrixErrorfn SparseMatrix::extract(self : SparseMatrix, row_ids : Array[Int], col_ids : Array[Int]) -> SparseMatrix raise MatrixErrorfn SparseMatrix::jordan_form(self : SparseMatrix, calc_transform? : Bool) -> (Matrix, Matrix) raise MatrixErrorfn SparseMatrix::lower_triangular_solve(self : SparseMatrix, rhs : Matrix) -> Matrix raise MatrixErrorfn SparseMatrix::reshape(self : SparseMatrix, rows : Int, cols : Int) -> SparseMatrix raise MatrixErrorfn SparseMatrix::row_insert(self : SparseMatrix, pos : Int, other : SparseMatrix) -> SparseMatrix raise MatrixErrorfn SparseMatrix::row_join(self : SparseMatrix, other : SparseMatrix) -> SparseMatrix raise MatrixErrorfn SparseMatrix::scalar_multiply(self : SparseMatrix, scalar : Expr) -> SparseMatrix raise MatrixErrorfn SparseMatrix::setitem(self : SparseMatrix, row : Int, col : Int, value : Expr) -> SparseMatrix raise MatrixErrorfn SparseMatrix::upper_triangular_solve(self : SparseMatrix, rhs : Matrix) -> Matrix raise MatrixErrortest "eye builds an identity matrix" {
let ident = @symmatrices.eye(3)
inspect(
ident.to_string(),
content="Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])",
)
}test "hstack joins matrices side by side" {
let left = @symmatrices.eye(2)
let right = @symmatrices.ones(2, cols=Some(1))
inspect(
@symmatrices.hstack([left, right]).to_string(),
content="Matrix([[1, 0, 1], [0, 1, 1]])",
)
}test "matrix builds a rectangular dense value" {
let m = @symmatrices.matrix([
[@symcore.int(1), @symcore.int(2)],
[@symcore.int(3), @symcore.int(4)],
])
guard m.shape() == (2, 2) else { fail("unexpected dense matrix shape") }
inspect(m.to_string(), content="Matrix([[1, 2], [3, 4]])")
}fn sparse_matrix(rows : Int, cols : Int, entries : Map[(Int, Int), Expr]) -> SparseMatrix raise MatrixErrorA symbolic mathematics library for Moonbit.
Dependencies