Standard library · Machine learning

matrix

Linear algebra: matrices, systems of equations, decompositions, eigenvalues and statistics.

import matrix

This package is complete linear algebra over 64 bit floats: from making a matrix and multiplying through to solving systems, decompositions, eigenvalues and singular values, and statistical tools such as principal component analysis. To solve one system, call Solve; for several systems with one matrix, Decompose once and LUSolve many times.

Matrices hold floats and the results carry a little rounding error; compare with AllClose and display with Chop. For neural networks see tensor.

Types

PCA

struct PCA
Principal component analysis.
NameTypeDescription
componentsMatrixThe components in the rows.
variancesMatrixThe variance of each component.
centerMatrixThe column means.
matrix-Stats.salam
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func show(name: str, m &: matrix.Matrix):
    println name, matrix.ToRepr(matrix.Chop(m, 0.000001), 3)
end

func r(value: f64): str:
    ret matrix.ToStringPrec(matrix.Full(1, 1, value), 3)
end

func main:
    show("Hilbert", matrix.Hilbert(2))
    show("Hilbert times its inverse", matrix.MatMul(matrix.Hilbert(3), matrix.HilbertInverse(3)))
    show("Vandermonde", matrix.Vandermonde(build(3, 1, "1 2 3"), 3))
    show("Toeplitz", matrix.Toeplitz(build(3, 1, "1 2 3"), build(1, 3, "1 4 5")))
    show("Hankel", matrix.Hankel(build(3, 1, "1 2 3"), build(1, 3, "3 4 5")))
    show("Circulant", matrix.Circulant(build(1, 3, "1 2 3")))
    show("Companion", matrix.Companion(build(1, 3, "1 -3 2")))
    show("Pascal", matrix.Pascal(3))
    show("Rotation2D", matrix.Rotation2D(0.0))
    show("Rotation3D", matrix.Rotation3D(2, 0.0))
    show("Householder", matrix.Householder(build(2, 1, "1 0")))
    show("Givens", matrix.Givens(2, 0, 1, 0.0))
    show("Permutation", matrix.Permutation(build(1, 3, "2 0 1")))

    mut rng := matrix.NewRng(7)
    println rng.next_bits() > 0, rng.next_float() < 1.0, rng.next_normal() < 10.0
    mut random := matrix.Random(2, 3, -1.0, 1.0, 5)
    println matrix.ShapeString(random), matrix.Min(random) >= -1.0, matrix.Max(random) < 1.0, matrix.AllClose(random, matrix.Random(2, 3, -1.0, 1.0, 5), 0.0)
    println matrix.ShapeString(matrix.RandomNormal(2, 2, 1))
    println matrix.IsSymmetric(matrix.RandomSymmetric(3, 1), 0.000001), matrix.IsPositiveDefinite(matrix.RandomSPD(3, 1), 0.000001), matrix.IsOrthogonal(matrix.RandomOrthogonal(3, 1), 0.00001)

    mut data := build(4, 2, "1 2 2 4 3 6 4 8")
    show("CenterColumns", matrix.CenterColumns(data))
    show("ColumnMeans", matrix.ColumnMeans(data))
    show("ColumnStds", matrix.ColumnStds(data, false))
    show("Standardize", matrix.Standardize(data, false))
    show("Covariance", matrix.Covariance(data, true))
    show("Correlation", matrix.Correlation(data))
    mut pca := matrix.PCAFit(data)
    println matrix.ShapeString(pca.components), matrix.ShapeString(pca.variances), matrix.ShapeString(pca.center)
    show("ExplainedVariance", matrix.ExplainedVariance(pca))
    show("PCATransform", matrix.Abs(matrix.PCATransform(pca, data, 1)))
    pca.free()
    mut pca2 := matrix.PCAFromSVD(data)
    show("from the SVD", matrix.ExplainedVariance(pca2))
    pca2.free()

    mut x := build(4, 1, "1 2 3 4")
    mut y := build(4, 1, "3 5 7 9")
    coefficients := matrix.LinearRegression(x, y)
    show("LinearRegression", coefficients)
    println r(matrix.RSquared(y, y))
end
OutputHilbert [1.000,0.500; 0.500,0.333] Hilbert times its inverse [1.000,0.000,0.000; 0.000,1.000,0.000; 0.000,0.000,1.000] Vandermonde [1.000,1.000,1.000; 1.000,2.000,4.000; 1.000,3.000,9.000] Toeplitz [1.000,4.000,5.000; 2.000,1.000,4.000; 3.000,2.000,1.000] Hankel [1.000,2.000,3.000; 2.000,3.000,4.000; 3.000,4.000,5.000] Circulant [1.000,2.000,3.000; 3.000,1.000,2.000; 2.000,3.000,1.000] Companion [0.000,-0.500; 1.000,1.500] Pascal [1.000,1.000,1.000; 1.000,2.000,3.000; 1.000,3.000,6.000] Rotation2D [1.000,0.000; 0.000,1.000] Rotation3D [1.000,0.000,0.000; 0.000,1.000,0.000; 0.000,0.000,1.000] Householder [-1.000,0.000; 0.000,1.000] Givens [1.000,0.000; 0.000,1.000] Permutation [0.000,0.000,1.000; 1.000,0.000,0.000; 0.000,1.000,0.000] true true true 2x3 true true true 2x2 true true true CenterColumns [-1.500,-3.000; -0.500,-1.000; 0.500,1.000; 1.500,3.000] ColumnMeans [2.500,5.000] ColumnStds [1.118,2.236] Standardize [-1.342,-1.342; -0.447,-0.447; 0.447,0.447; 1.342,1.342] Covariance [1.667,3.333; 3.333,6.667] Correlation [1.000,1.000; 1.000,1.000] 2x2 1x2 1x2 ExplainedVariance [1.000,0.000] PCATransform [3.354; 1.118; 1.118; 3.354] from the SVD [1.000,0.000] LinearRegression [1.000; 2.000] 1.000

free

func free()
Frees it.

The example for this item is shown with the ones above: matrix-Stats.salam

Eigen

struct Eigen
The eigenvalues of a general matrix.
NameTypeDescription
nint = 0The size.
reMatrixThe real part.
imMatrixThe imaginary part.
iterationsint = 0The number of iterations.
okbool = falseIt converged.
matrix-Eigen.salam
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func show(name: str, m &: matrix.Matrix):
    println name, matrix.ToRepr(matrix.Chop(m, 0.000001), 3)
end

func r(value: f64): str:
    ret matrix.ToStringPrec(matrix.Full(1, 1, value), 3)
end

func main:
    mut symmetric := build(2, 2, "2 1 1 2")
    mut decomposition := matrix.EigSym(symmetric)
    println decomposition.ok, decomposition.n, decomposition.sweeps >= 0
    show("values", decomposition.values)
    show("EigReconstruct", matrix.EigReconstruct(decomposition))
    decomposition.free()
    show("EigValsSym", matrix.EigValsSym(symmetric))
    println r(matrix.SpectralRadiusSym(symmetric))
    show("PowSym", matrix.PowSym(symmetric, 2.0))
    show("SqrtSym squared", matrix.MatMul(matrix.SqrtSym(symmetric), matrix.SqrtSym(symmetric)))

    mut general := build(2, 2, "0 1 -2 -3")
    mut eigen := matrix.EigVals(general)
    println eigen.ok, eigen.n, matrix.ToRepr(eigen.re, 3), matrix.ToRepr(eigen.im, 3), eigen.iterations >= 0
    eigen.free()
    mut rotation := build(2, 2, "0 -1 1 0")
    mut complex := matrix.EigValsAuto(rotation)
    show("imaginary", complex.im)
    println r(matrix.SpectralRadius(general))
    show("Hessenberg", matrix.Hessenberg(build(3, 3, "1 2 3 4 5 6 7 8 10")))
    show("CharPoly", matrix.CharPoly(general))
    mut vector := matrix.EigVecFor(symmetric, 3.0, 50)
    show("EigVecFor", matrix.Abs(vector))
    mut power := matrix.PowerIteration(symmetric, 100)
    mut dominant := matrix.SubMatrix(power, 0, 0, 2, 1)
    println r(power.at(2, 0)), r(matrix.Rayleigh(symmetric, dominant)), r(matrix.ShiftedInverseIteration(symmetric, 0.9, 50))

    mut m := build(3, 2, "3 0 0 4 0 0")
    mut svd := matrix.SVDecompose(m)
    println svd.ok, matrix.ShapeString(svd.u), matrix.ShapeString(svd.v)
    show("singular values", svd.s)
    svd.free()
    show("SingularValues", matrix.SingularValues(m))
    println matrix.MatrixRank(m, 0.000001)
    show("PseudoInverse", matrix.PseudoInverse(m, 0.000001))
    show("NullSpace", matrix.NullSpace(build(2, 2, "1 1 1 1"), 0.000001))
    show("ColumnSpace", matrix.Abs(matrix.ColumnSpace(build(2, 2, "1 1 1 1"), 0.000001)))
    show("LowRankApprox", matrix.LowRankApprox(build(2, 2, "3 0 0 1"), 1))

    tol := 0.000001
    identity := matrix.Eye(3)
    println matrix.IsSquare(identity), matrix.AllClose(identity, identity, tol), matrix.IsZero(matrix.Zeros(2, 2), tol), matrix.IsIdentity(identity, tol)
    println matrix.IsSymmetric(symmetric, tol), matrix.IsSkewSymmetric(rotation, tol), matrix.IsDiagonal(identity, tol)
    println matrix.IsUpperTriangular(build(2, 2, "1 2 0 3"), tol), matrix.IsLowerTriangular(build(2, 2, "1 0 2 3"), tol)
    tridiagonal := build(3, 3, "2 1 0 1 2 1 0 1 2")
    println matrix.IsBanded(tridiagonal, 1, 1, tol), matrix.LowerBandwidth(tridiagonal, tol), matrix.UpperBandwidth(tridiagonal, tol)
    println matrix.IsOrthogonal(rotation, tol), matrix.IsNormal(symmetric, tol), matrix.IsIdempotent(build(2, 2, "1 0 0 0"), tol)
    println matrix.IsInvolutory(build(2, 2, "0 1 1 0"), tol), matrix.IsNilpotent(build(2, 2, "0 1 0 0"), tol)
    println matrix.IsPositiveDefinite(symmetric, tol), matrix.IsPositiveSemiDefinite(build(2, 2, "1 1 1 1"), tol)
    println matrix.IsSingular(build(2, 2, "1 2 2 4")), matrix.IsInvertible(symmetric)
    probability := build(2, 2, "0.5 0.5 0.5 0.5")
    println matrix.IsRowStochastic(probability, tol), matrix.IsColStochastic(probability, tol), matrix.IsDoublyStochastic(probability, tol)
    println matrix.IsPermutation(build(2, 2, "0 1 1 0"), tol), matrix.IsToeplitz(tridiagonal, tol), matrix.IsHankel(build(2, 2, "1 2 2 3"), tol)
    println r(matrix.Density(tridiagonal))
end
Outputtrue 2 true values [3.000,1.000] EigReconstruct [2.000,1.000; 1.000,2.000] EigValsSym [3.000,1.000] 3.000 PowSym [5.000,4.000; 4.000,5.000] SqrtSym squared [2.000,1.000; 1.000,2.000] true 2 [-1.000,-2.000] [0.000,0.000] true imaginary [1.000,-1.000] 2.000 Hessenberg [1.000,-3.597,-0.248; -8.062,14.800,2.400; 0.000,0.400,0.200] CharPoly [2.000,3.000,1.000] EigVecFor [0.707; 0.707] 3.000 3.000 1.000 true 3x2 2x2 singular values [4.000,3.000] SingularValues [4.000,3.000] 2 PseudoInverse [0.333,0.000,0.000; 0.000,0.250,0.000] NullSpace [0.707; -0.707] ColumnSpace [0.707; 0.707] LowRankApprox [3.000,0.000; 0.000,0.000] true true true true true true true true true true 1 1 true true true true true true true true true true true true true true true 0.778

free

func free()
Frees it.

The example for this item is shown with the ones above: matrix-Eigen.salam

LU

struct LU
The lower upper decomposition with row swaps.
NameTypeDescription
nint = 0The size.
luMatrixL and U in one matrix.
pivVectorThe row swaps.
signint = 1The sign of the permutation.
singularbool = falseThe matrix is singular.
matrix-Solve.salam
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func show(name: str, m &: matrix.Matrix):
    println name, matrix.ToRepr(matrix.Chop(m, 0.000001), 3)
end

func r(value: f64): str:
    ret matrix.ToStringPrec(matrix.Full(1, 1, value), 3)
end

func main:
    mut a := build(3, 3, "4 1 0 1 3 1 0 1 2")
    mut b := build(3, 1, "1 2 3")
    println r(matrix.Det(a)), r(matrix.LogAbsDet(a))
    show("Solve", matrix.Solve(a, b))
    show("SolveVec", matrix.SolveVec(a, b))
    mut lu := matrix.Decompose(a)
    println lu.n, lu.piv.len(), lu.sign, lu.singular, matrix.ShapeString(lu.lu)
    show("LUSolve", matrix.LUSolve(lu, b))
    lu.free()
    mut inverse := matrix.Inverse(a)
    show("a times its inverse", matrix.MatMul(a, inverse))
    show("Adjugate", matrix.Adjugate(build(2, 2, "1 2 3 4")))
    show("Residual", matrix.Residual(a, matrix.Solve(a, b), b))

    mut qr := matrix.QRDecompose(a)
    println qr.m, qr.n, qr.full_rank, matrix.ShapeString(qr.qr), matrix.ShapeString(qr.rdiag)
    show("Q times R", matrix.MatMul(matrix.QRGetQ(qr), matrix.QRGetR(qr)))
    show("QRSolve", matrix.QRSolve(qr, b))
    qr.free()
    mut points := build(4, 2, "1 0 1 1 1 2 1 3")
    mut values := build(4, 1, "1 3 5 7")
    show("LeastSquares", matrix.LeastSquares(points, values))
    show("Orthonormalize", matrix.Gram(matrix.Orthonormalize(points)))

    show("Cholesky", matrix.Cholesky(a))
    show("SolveSPD", matrix.SolveSPD(a, b))
    show("CholeskySolve", matrix.CholeskySolve(matrix.Cholesky(a), b))
    show("InverseSPD", matrix.MatMul(a, matrix.InverseSPD(a)))
    println r(matrix.DetSPD(a)), r(matrix.LogDetSPD(a))
    mut ldl := matrix.LDLDecompose(a)
    println ldl.ok, ldl.n, matrix.ShapeString(ldl.l), matrix.ShapeString(ldl.d)
    show("LDLSolve", matrix.LDLSolve(ldl, b))
    ldl.free()
    show("Inertia", matrix.Inertia(build(2, 2, "1 0 0 -1")))

    mut singular := build(3, 3, "1 2 3 2 4 6 1 1 1")
    show("RowEchelon", matrix.RowEchelon(singular))
    show("RREF", matrix.RREF(singular))
    show("PivotColumns", matrix.PivotColumns(singular))
    println matrix.RankExact(singular), matrix.Nullity(singular)
    show("NullSpaceExact", matrix.NullSpaceExact(singular))
    show("ColumnSpaceExact", matrix.ColumnSpaceExact(singular))
    mut right := build(3, 1, "6 12 3")
    println matrix.IsConsistent(singular, right), matrix.IsConsistent(singular, build(3, 1, "1 3 3"))
    show("SolveGeneral", matrix.SolveGeneral(singular, right))

    println matrix.IsDiagonallyDominant(a)
    mut jacobi := matrix.SolveJacobi(a, b, 200, 0.00000001)
    println jacobi.converged, jacobi.iterations > 0, jacobi.residual < 0.0001
    show("Jacobi", jacobi.x)
    jacobi.free()
    each method in [1, 2, 3]:
        mut result := matrix.SolveGaussSeidel(a, b, 200, 0.00000001)
        if method == 2:
            result = matrix.SolveSOR(a, b, 1.2, 200, 0.00000001)
        else method == 3:
            result = matrix.SolveCG(a, b, 200, 0.00000001)
        end
        show("iterative", result.x)
        result.free()
    end

    mut d := build(2, 2, "1 0 0 2")
    show("Expm", matrix.Expm(matrix.Zeros(2, 2)))
    show("Sqrtm", matrix.Sqrtm(build(2, 2, "4 0 0 9")))
    show("MatrixSign", matrix.MatrixSign(build(2, 2, "2 0 0 -3")))
    show("PolyEval", matrix.PolyEval(d, build(1, 3, "1 0 1")))
    show("CayleyHamilton", matrix.CayleyHamilton(d))
    show("LogmSym", matrix.LogmSym(matrix.Eye(2)))
end
Output18.000 2.890 Solve [0.222; 0.111; 1.444] SolveVec [0.222; 0.111; 1.444] 3 3 1 false 3x3 LUSolve [0.222; 0.111; 1.444] a times its inverse [1.000,0.000,0.000; 0.000,1.000,0.000; 0.000,0.000,1.000] Adjugate [4.000,-2.000; -3.000,1.000] Residual [0.000; 0.000; 0.000] 3 3 true 3x3 1x3 Q times R [4.000,1.000,0.000; 1.000,3.000,1.000; 0.000,1.000,2.000] QRSolve [0.222; 0.111; 1.444] LeastSquares [1.000; 2.000] Orthonormalize [1.000,0.000; 0.000,1.000] Cholesky [2.000,0.000,0.000; 0.500,1.658,0.000; 0.000,0.603,1.279] SolveSPD [0.222; 0.111; 1.444] CholeskySolve [0.222; 0.111; 1.444] InverseSPD [1.000,0.000,0.000; 0.000,1.000,0.000; 0.000,0.000,1.000] 18.000 2.890 true 3 3x3 1x3 LDLSolve [0.222; 0.111; 1.444] Inertia [1.000,1.000,0.000] RowEchelon [2.000,4.000,6.000; 0.000,-1.000,-2.000; 0.000,0.000,0.000] RREF [1.000,0.000,-1.000; 0.000,1.000,2.000; 0.000,0.000,0.000] PivotColumns [0.000,1.000] 2 1 NullSpaceExact [1.000; -2.000; 1.000] ColumnSpaceExact [1.000,2.000; 2.000,4.000; 1.000,1.000] true false SolveGeneral [0.000; 3.000; 0.000] true true true true Jacobi [0.222; 0.111; 1.444] iterative [0.222; 0.111; 1.444] iterative [0.222; 0.111; 1.444] iterative [0.222; 0.111; 1.444] Expm [1.000,0.000; 0.000,1.000] Sqrtm [2.000,0.000; 0.000,3.000] MatrixSign [1.000,0.000; 0.000,-1.000] PolyEval [2.000,0.000; 0.000,5.000] CayleyHamilton [0.000,0.000; 0.000,0.000] LogmSym [0.000,0.000; 0.000,0.000]

free

func free()
Frees it.

The example for this item is shown with the ones above: matrix-Solve.salam

IterResult

struct IterResult
The result of an iterative method.
NameTypeDescription
xMatrixThe solution.
iterationsint = 0The number of iterations.
residualf64 = 0The norm of the residual.
convergedbool = falseIt converged.

The example for this item is shown with the ones above: matrix-Solve.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: matrix-Solve.salam

LDL

struct LDL
The LDL decomposition.
NameTypeDescription
nint = 0The size.
lMatrixThe unit lower triangular matrix.
dMatrixThe diagonal D.
okbool = falseThe decomposition succeeded.

The example for this item is shown with the ones above: matrix-Solve.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: matrix-Solve.salam

Matrix

struct Matrix
A 64 bit float matrix with its elements in row order.
NameTypeDescription
rowsint = 0The number of rows.
colsint = 0The number of columns.
aVectorThe elements.
matrix-Create.salam
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func main:
    mut a := build(2, 3, "1 2 3 4 5 6")
    matrix.Print(a)
    println matrix.ShapeString(a), a.rows, a.cols, a.a.len(), a.count(), a.min_dim(), a.is_square(), a.is_empty()
    println a.at(1, 2), a.flat(4)
    a.set(0, 0, 10.0)
    a.add_at(0, 1, 0.5)
    a.mul_at(0, 2, 2.0)
    a.set_flat(5, -1.0)
    matrix.PrintPrec(a, 1)
    matrix.PrintLabeled("a", a)

    println matrix.ToString(matrix.Zeros(1, 2)), matrix.ToString(matrix.Ones(1, 2)), matrix.ToString(matrix.Full(1, 2, 7.0))
    println matrix.ToString(matrix.Eye(2))
    println matrix.ToString(matrix.EyeOffset(2, 3, 1))
    mut data := Vector {} as Vector<f64>
    data.push(1.0)
    data.push(2.0)
    println matrix.ShapeString(matrix.RowVector(data)), matrix.ShapeString(matrix.ColVector(data)), matrix.ToString(matrix.Diag(data))
    mut b := matrix.Clone(a)
    matrix.SetAll(b, 0.0)
    matrix.CopyInto(b, a)
    println b.same_shape(a), b.at(0, 0)
    println matrix.ToStringPrec(matrix.Arange(0.0, 1.0, 0.25), 2)
    println matrix.ToStringPrec(matrix.Linspace(0.0, 1.0, 5), 2)
    println matrix.ToStringPrec(matrix.Logspace(0.0, 2.0, 3, 10.0), 1)
    println matrix.ToCSV(a, 1)
    println matrix.ToRepr(a, 1)
    println matrix.MAT_EPS > 0.0, matrix.MAT_TOL > 0.0, matrix.MAT_INF > 1000.0
    matrix.Free(b)
    a.free()
end
Output1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 2x3 2 3 6 6 2 false false 6 5 10.0 2.5 6.0 4.0 5.0 -1.0 a (2x3) 10.0000 2.5000 6.0000 4.0000 5.0000 -1.0000 0.0000 0.0000 1.0000 1.0000 7.0000 7.0000 1.0000 0.0000 0.0000 1.0000 0.0000 1.0000 0.0000 0.0000 0.0000 1.0000 1x2 2x1 1.0000 0.0000 0.0000 2.0000 true 10 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 1.00 1.0 10.0 100.0 10.0,2.5,6.0 4.0,5.0,-1.0 [10.0,2.5,6.0; 4.0,5.0,-1.0] true true true

at

func at(i: int, j: int): f64
The element at row i and column j.

The example for this item is shown with the ones above: matrix-Create.salam

set

func set(i: int, j: int, v: f64)
Sets the element.

The example for this item is shown with the ones above: matrix-Create.salam

add_at

func add_at(i: int, j: int, v: f64)
Adds to the element.

The example for this item is shown with the ones above: matrix-Create.salam

mul_at

func mul_at(i: int, j: int, v: f64)
Multiplies the element by a number.

The example for this item is shown with the ones above: matrix-Create.salam

flat

func flat(k: int): f64
Element number k in row order.

The example for this item is shown with the ones above: matrix-Create.salam

set_flat

func set_flat(k: int, v: f64)
Sets element k.

The example for this item is shown with the ones above: matrix-Create.salam

count

func count(): int
The number of elements.

The example for this item is shown with the ones above: matrix-Create.salam

is_square

func is_square(): bool
It is square.

The example for this item is shown with the ones above: matrix-Create.salam

is_empty

func is_empty(): bool
It has no elements.

The example for this item is shown with the ones above: matrix-Create.salam

same_shape

func same_shape(o &: Matrix): bool
It has the same shape as another matrix.

The example for this item is shown with the ones above: matrix-Create.salam

min_dim

func min_dim(): int
The smaller of the row and column counts.

The example for this item is shown with the ones above: matrix-Create.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: matrix-Create.salam

EigenSym

struct EigenSym
The eigendecomposition of a symmetric matrix.
NameTypeDescription
nint = 0The size.
valuesMatrixThe eigenvalues, descending.
vectorsMatrixThe eigenvectors in the columns.
sweepsint = 0The number of Jacobi sweeps.
okbool = falseIt converged.

The example for this item is shown with the ones above: matrix-Eigen.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: matrix-Eigen.salam

Rng

struct Rng
A deterministic random generator.
NameTypeDescription
su64 = 0The state.

The example for this item is shown with the ones above: matrix-Stats.salam

next_bits

func next_bits(): u64
64 random bits.

The example for this item is shown with the ones above: matrix-Stats.salam

next_float

func next_float(): f64
A number between zero and one.

The example for this item is shown with the ones above: matrix-Stats.salam

next_normal

func next_normal(): f64
A standard normal number.

The example for this item is shown with the ones above: matrix-Stats.salam

SVD

struct SVD
The singular value decomposition.
NameTypeDescription
uMatrixThe left singular vectors.
sMatrixThe singular values, descending.
vMatrixThe right singular vectors.
okbool = falseIt converged.

The example for this item is shown with the ones above: matrix-Eigen.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: matrix-Eigen.salam

QR

struct QR
The QR decomposition.
NameTypeDescription
mint = 0The number of rows.
nint = 0The number of columns.
qrMatrixThe reflections and R, packed.
rdiagMatrixThe diagonal of R.
full_rankbool = trueThe columns are independent.

The example for this item is shown with the ones above: matrix-Solve.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: matrix-Solve.salam

Constants and variables

MAT_EPS

const MAT_EPS: f64 = 2.22045e-16
The machine precision of 64 bit floats.

The example for this item is shown with the ones above: matrix-Create.salam

MAT_TOL

const MAT_TOL: f64 = 1e-09
The default tolerance for comparisons.

The example for this item is shown with the ones above: matrix-Create.salam

MAT_INF

const MAT_INF: f64 = 1e+300
Infinity.

The example for this item is shown with the ones above: matrix-Create.salam

Functions

CenterColumns

func CenterColumns(x &: Matrix): Matrix
Subtracts the mean of each column.

The example for this item is shown with the ones above: matrix-Stats.salam

ColumnMeans

func ColumnMeans(x &: Matrix): Matrix
The mean of each column.

The example for this item is shown with the ones above: matrix-Stats.salam

ColumnStds

func ColumnStds(x &: Matrix, sample: bool): Matrix
The standard deviation of each column.

The example for this item is shown with the ones above: matrix-Stats.salam

Standardize

func Standardize(x &: Matrix, sample: bool): Matrix
Every column with mean zero and standard deviation one.

The example for this item is shown with the ones above: matrix-Stats.salam

Covariance

func Covariance(x &: Matrix, sample: bool): Matrix
The covariance matrix of the columns.

The example for this item is shown with the ones above: matrix-Stats.salam

Correlation

func Correlation(x &: Matrix): Matrix
The Pearson correlation matrix.

The example for this item is shown with the ones above: matrix-Stats.salam

PCAFit

func PCAFit(x &: Matrix): PCA
Principal component analysis through the covariance.

The example for this item is shown with the ones above: matrix-Stats.salam

PCAFromSVD

func PCAFromSVD(x &: Matrix): PCA
The same, through the singular value decomposition; more stable.

The example for this item is shown with the ones above: matrix-Stats.salam

PCATransform

func PCATransform(p &: PCA, x &: Matrix, k: int): Matrix
Projects the data onto the first k components.

The example for this item is shown with the ones above: matrix-Stats.salam

ExplainedVariance

func ExplainedVariance(p &: PCA): Matrix
The share each component takes of the total variance.

The example for this item is shown with the ones above: matrix-Stats.salam

LinearRegression

func LinearRegression(x &: Matrix, y &: Matrix): Matrix
The coefficients of a linear regression; a column of ones is added for the intercept.

The example for this item is shown with the ones above: matrix-Stats.salam

RSquared

func RSquared(y &: Matrix, fitted &: Matrix): f64
The coefficient of determination.

The example for this item is shown with the ones above: matrix-Stats.salam

Expm

func Expm(x &: Matrix): Matrix
The matrix exponential.

The example for this item is shown with the ones above: matrix-Solve.salam

Sqrtm

func Sqrtm(x &: Matrix): Matrix
The matrix square root.

The example for this item is shown with the ones above: matrix-Solve.salam

MatrixSign

func MatrixSign(x &: Matrix): Matrix
The matrix sign function.

The example for this item is shown with the ones above: matrix-Solve.salam

PolyEval

func PolyEval(x &: Matrix, coeffs &: Matrix): Matrix
A polynomial with coefficients from degree zero, over a matrix.

The example for this item is shown with the ones above: matrix-Solve.salam

CayleyHamilton

func CayleyHamilton(x &: Matrix): Matrix
The characteristic polynomial over the matrix itself; by the theorem it is zero.

The example for this item is shown with the ones above: matrix-Solve.salam

LogmSym

func LogmSym(x &: Matrix): Matrix
The matrix logarithm for a symmetric positive definite matrix.

The example for this item is shown with the ones above: matrix-Solve.salam

Hessenberg

func Hessenberg(x &: Matrix): Matrix
The Hessenberg form.

The example for this item is shown with the ones above: matrix-Eigen.salam

EigVals

func EigVals(x &: Matrix): Eigen
The eigenvalues by the QR method.

The example for this item is shown with the ones above: matrix-Eigen.salam

EigValsAuto

func EigValsAuto(x &: Matrix): Eigen
The eigenvalues by whichever method suits the matrix, symmetric or general.

The example for this item is shown with the ones above: matrix-Eigen.salam

SpectralRadius

func SpectralRadius(x &: Matrix): f64
The greatest absolute eigenvalue.

The example for this item is shown with the ones above: matrix-Eigen.salam

CharPoly

func CharPoly(x &: Matrix): Matrix
The coefficients of the characteristic polynomial from degree zero.

The example for this item is shown with the ones above: matrix-Eigen.salam

EigVecFor

func EigVecFor(x &: Matrix, lambda: f64, iters: int): Matrix
The eigenvector for one eigenvalue.

The example for this item is shown with the ones above: matrix-Eigen.salam

Decompose

func Decompose(x &: Matrix): LU
The lower upper decomposition; for solving several systems with one matrix.

The example for this item is shown with the ones above: matrix-Solve.salam

Det

func Det(x &: Matrix): f64
The determinant.

The example for this item is shown with the ones above: matrix-Solve.salam

LogAbsDet

func LogAbsDet(x &: Matrix): f64
The logarithm of the absolute determinant; without overflow.

The example for this item is shown with the ones above: matrix-Solve.salam

Solve

func Solve(a &: Matrix, b &: Matrix): Matrix
Solves the system AX = B.

The example for this item is shown with the ones above: matrix-Solve.salam

LUSolve

func LUSolve(f &: LU, b &: Matrix): Matrix
Solves it with a ready decomposition.

The example for this item is shown with the ones above: matrix-Solve.salam

SolveVec

func SolveVec(a &: Matrix, b &: Matrix): Matrix
The system with one vector.

The example for this item is shown with the ones above: matrix-Solve.salam

Inverse

func Inverse(x &: Matrix): Matrix
The inverse.
matrix-Math.salam
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func show(name: str, m &: matrix.Matrix):
    println name, matrix.ToRepr(m, 1)
end

func r(value: f64): str:
    ret matrix.ToStringPrec(matrix.Full(1, 1, value), 3)
end

func main:
    mut a := build(2, 2, "1 2 3 4")
    mut b := build(2, 2, "0 1 1 0")
    mut row := build(1, 2, "10 20")
    mut col := build(2, 1, "1 -1")
    show("Transpose", matrix.Transpose(a))
    show("MatMul", matrix.MatMul(a, b))
    mut out := matrix.Zeros(2, 2)
    println matrix.TransposeInto(a, out), matrix.MatMulInto(a, b, out)
    show("into", out)
    show("TransposeMul", matrix.TransposeMul(a, b))
    show("MulTranspose", matrix.MulTranspose(a, b))
    show("Gram", matrix.Gram(a))
    show("MatVec", matrix.MatVec(a, col))
    show("VecMat", matrix.VecMat(row, a))
    println matrix.Dot(col, col), matrix.Trace(a)
    show("Outer", matrix.Outer(col, col))
    show("Kron", matrix.Kron(matrix.Eye(2), matrix.Full(1, 2, 3.0)))
    show("MatPow", matrix.MatPow(a, 2))
    show("Cross3", matrix.Cross3(build(3, 1, "1 0 0"), build(3, 1, "0 1 0")))

    show("Add", matrix.Add(a, b))
    show("Sub", matrix.Sub(a, b))
    show("MulElem", matrix.MulElem(a, b))
    show("DivElem", matrix.DivElem(a, a))
    show("Neg", matrix.Neg(a))
    show("with scalars", matrix.AddScalar(matrix.SubScalar(matrix.Scale(matrix.DivScalar(a, 2.0), 4.0), 1.0), 1.0))
    mut c := matrix.Clone(a)
    matrix.AddInPlace(c, b)
    matrix.SubInPlace(c, b)
    matrix.MulElemInPlace(c, a)
    matrix.ScaleInPlace(c, 0.5)
    matrix.AddScalarInPlace(c, 1.0)
    matrix.NegInPlace(c)
    matrix.AddScaled(c, a, 2.0)
    show("in place", c)
    show("Combine", matrix.Combine(a, 2.0, b, -1.0))
    show("MaxElem", matrix.MaxElem(a, matrix.Full(2, 2, 2.5)))
    show("MinElem", matrix.MinElem(a, matrix.Full(2, 2, 2.5)))
    show("AddRowVector", matrix.AddRowVector(a, row))
    show("AddColVector", matrix.AddColVector(a, col))
    show("MulRowVector", matrix.MulRowVector(a, row))
    show("MulColVector", matrix.MulColVector(a, col))

    mut m := build(1, 4, "-2.25 0 0.5 9")
    show("Abs", matrix.Abs(m))
    show("Sqrt", matrix.Sqrt(matrix.Abs(m)))
    show("Cbrt", matrix.Cbrt(build(1, 2, "8 -27")))
    show("Floor and Ceil", matrix.HStack(matrix.Floor(m), matrix.Ceil(m)))
    show("Round and Trunc", matrix.HStack(matrix.Round(m), matrix.Trunc(m)))
    show("Sign", matrix.Sign(m))
    show("Square", matrix.Square(m))
    show("Reciprocal", matrix.Reciprocal(build(1, 2, "2 4")))
    show("PowElem", matrix.PowElem(build(1, 2, "2 3"), 3.0))
    show("Clip", matrix.Clip(m, 0.0, 1.0))
    show("MaxScalar and MinScalar", matrix.HStack(matrix.MaxScalar(m, 0.0), matrix.MinScalar(m, 0.0)))
    show("Chop", matrix.Chop(build(1, 2, "0.0000001 1"), 0.0001))
    mut zero := matrix.Zeros(1, 1)
    println matrix.ToStringPrec(matrix.HStack(matrix.Exp(zero), matrix.Log(matrix.Ones(1, 1))), 3), matrix.ToStringPrec(matrix.Log10(matrix.Full(1, 1, 100.0)), 3), matrix.ToStringPrec(matrix.Log2(matrix.Full(1, 1, 8.0)), 3)
    println matrix.ToStringPrec(matrix.HStack(matrix.Sin(zero), matrix.Cos(zero)), 3), matrix.ToStringPrec(matrix.Tan(zero), 3)
    println matrix.ToStringPrec(matrix.HStack(matrix.Asin(zero), matrix.Acos(matrix.Ones(1, 1))), 3), matrix.ToStringPrec(matrix.Atan(zero), 3)
    println matrix.ToStringPrec(matrix.HStack(matrix.Sinh(zero), matrix.Cosh(zero)), 3), matrix.ToStringPrec(matrix.Tanh(zero), 3)

    println matrix.Sum(a), matrix.Prod(a), matrix.Mean(a), matrix.Min(a), matrix.Max(a), matrix.Range(a)
    k := matrix.ArgMax(a)
    println matrix.ArgMin(a), k, matrix.IndexRow(a, k), matrix.IndexCol(a, k), matrix.MaxAbs(m)
    println r(matrix.Var(a, true)), r(matrix.Std(a, false)), matrix.Median(a)
    show("RowSums", matrix.RowSums(a))
    show("ColSums", matrix.ColSums(a))
    show("RowMeans", matrix.RowMeans(a))
    show("ColMeans", matrix.ColMeans(a))
    show("RowMins and RowMaxs", matrix.HStack(matrix.RowMins(a), matrix.RowMaxs(a)))
    show("ColMins and ColMaxs", matrix.VStack(matrix.ColMins(a), matrix.ColMaxs(a)))
    show("cumulative", matrix.HStack(matrix.CumSumRows(a), matrix.CumSumCols(a)))
    show("CumProdRows", matrix.CumProdRows(a))
    println matrix.CountNonZero(b), matrix.Any(b), matrix.All(b), matrix.AllFinite(b)

    println r(matrix.NormFrobenius(a)), matrix.Norm1(a), matrix.NormInf(a), matrix.NormMax(a)
    println r(matrix.Norm2(a)), r(matrix.NormNuclear(a)), matrix.VecNorm(col, 1.0), r(matrix.Distance(col, matrix.Zeros(2, 1)))
    show("Normalize", matrix.Normalize(build(1, 2, "3 4")))
    println matrix.ToStringPrec(matrix.NormalizeRows(a), 2)
    println matrix.ToStringPrec(matrix.NormalizeCols(a), 2)
    println r(matrix.Cond1(a)), r(matrix.CondInf(a)), r(matrix.Cond2(a))
end
OutputTranspose [1.0,3.0; 2.0,4.0] MatMul [2.0,1.0; 4.0,3.0] true true into [2.0,1.0; 4.0,3.0] TransposeMul [3.0,1.0; 4.0,2.0] MulTranspose [2.0,1.0; 4.0,3.0] Gram [10.0,14.0; 14.0,20.0] MatVec [-1.0; -1.0] VecMat [70.0,100.0] 2 5 Outer [1.0,-1.0; -1.0,1.0] Kron [3.0,3.0,0.0,0.0; 0.0,0.0,3.0,3.0] MatPow [7.0,10.0; 15.0,22.0] Cross3 [0.0; 0.0; 1.0] Add [1.0,3.0; 4.0,4.0] Sub [1.0,1.0; 2.0,4.0] MulElem [0.0,2.0; 3.0,0.0] DivElem [1.0,1.0; 1.0,1.0] Neg [-1.0,-2.0; -3.0,-4.0] with scalars [2.0,4.0; 6.0,8.0] in place [0.5,1.0; 0.5,-1.0] Combine [2.0,3.0; 5.0,8.0] MaxElem [2.5,2.5; 3.0,4.0] MinElem [1.0,2.0; 2.5,2.5] AddRowVector [11.0,22.0; 13.0,24.0] AddColVector [2.0,3.0; 2.0,3.0] MulRowVector [10.0,40.0; 30.0,80.0] MulColVector [1.0,2.0; -3.0,-4.0] Abs [2.3,0.0,0.5,9.0] Sqrt [1.5,0.0,0.7,3.0] Cbrt [2.0,-3.0] Floor and Ceil [-3.0,0.0,0.0,9.0,-2.0,0.0,1.0,9.0] Round and Trunc [-2.0,0.0,1.0,9.0,-2.0,0.0,0.0,9.0] Sign [-1.0,0.0,1.0,1.0] Square [5.1,0.0,0.3,81.0] Reciprocal [0.5,0.3] PowElem [8.0,27.0] Clip [0.0,0.0,0.5,1.0] MaxScalar and MinScalar [0.0,0.0,0.5,9.0,-2.3,0.0,0.0,0.0] Chop [0.0,1.0] 1.000 0.000 2.000 3.000 0.000 1.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 10 24 2.5 1 4 3 0 3 1 1 9 1.667 1.118 2.5 RowSums [3.0; 7.0] ColSums [4.0,6.0] RowMeans [1.5; 3.5] ColMeans [2.0,3.0] RowMins and RowMaxs [1.0,2.0; 3.0,4.0] ColMins and ColMaxs [1.0,2.0; 3.0,4.0] cumulative [1.0,3.0,1.0,2.0; 3.0,7.0,4.0,6.0] CumProdRows [1.0,2.0; 3.0,12.0] 2 true false true 5.477 6 7 4 5.465 5.831 2 1.414 Normalize [0.6,0.8] 0.45 0.89 0.60 0.80 0.32 0.45 0.95 0.89 21.000 21.000 14.933

Adjugate

func Adjugate(x &: Matrix): Matrix
The adjugate matrix.

The example for this item is shown with the ones above: matrix-Solve.salam

IsDiagonallyDominant

func IsDiagonallyDominant(a &: Matrix): bool
It is diagonally dominant; the Jacobi and Gauss Seidel methods then converge.

The example for this item is shown with the ones above: matrix-Solve.salam

SolveJacobi

func SolveJacobi(a &: Matrix, b &: Matrix, iters: int, tol: f64): IterResult
The Jacobi method.

The example for this item is shown with the ones above: matrix-Solve.salam

SolveGaussSeidel

func SolveGaussSeidel(a &: Matrix, b &: Matrix, iters: int, tol: f64): IterResult
The Gauss Seidel method.

The example for this item is shown with the ones above: matrix-Solve.salam

SolveSOR

func SolveSOR(a &: Matrix, b &: Matrix, omega: f64, iters: int, tol: f64): IterResult
Successive over relaxation with a factor omega.

The example for this item is shown with the ones above: matrix-Solve.salam

SolveCG

func SolveCG(a &: Matrix, b &: Matrix, iters: int, tol: f64): IterResult
Conjugate gradient for a symmetric positive definite matrix.

The example for this item is shown with the ones above: matrix-Solve.salam

PowerIteration

func PowerIteration(a &: Matrix, iters: int): Matrix
Power iteration: the dominant eigenvector and, in the last element, its eigenvalue.

The example for this item is shown with the ones above: matrix-Eigen.salam

Rayleigh

func Rayleigh(a &: Matrix, x &: Matrix): f64
The Rayleigh quotient; the eigenvalue estimate for a vector.

The example for this item is shown with the ones above: matrix-Eigen.salam

ShiftedInverseIteration

func ShiftedInverseIteration(a &: Matrix, shift: f64, iters: int): f64
The eigenvalue nearest the shift.

The example for this item is shown with the ones above: matrix-Eigen.salam

Cholesky

func Cholesky(x &: Matrix): Matrix
The Cholesky decomposition of a symmetric positive definite matrix, the L whose product with its transpose is A.

The example for this item is shown with the ones above: matrix-Solve.salam

SolveSPD

func SolveSPD(a &: Matrix, b &: Matrix): Matrix
Solves a symmetric positive definite system.

The example for this item is shown with the ones above: matrix-Solve.salam

CholeskySolve

func CholeskySolve(l &: Matrix, b &: Matrix): Matrix
Solves it with a ready L.

The example for this item is shown with the ones above: matrix-Solve.salam

InverseSPD

func InverseSPD(x &: Matrix): Matrix
The inverse of a positive definite matrix.

The example for this item is shown with the ones above: matrix-Solve.salam

DetSPD

func DetSPD(x &: Matrix): f64
The determinant of a positive definite matrix.

The example for this item is shown with the ones above: matrix-Solve.salam

LogDetSPD

func LogDetSPD(x &: Matrix): f64
The logarithm of the determinant; for the multivariate normal distribution.

The example for this item is shown with the ones above: matrix-Solve.salam

LDLDecompose

func LDLDecompose(x &: Matrix): LDL
The LDL decomposition of a symmetric matrix.

The example for this item is shown with the ones above: matrix-Solve.salam

LDLSolve

func LDLSolve(f &: LDL, b &: Matrix): Matrix
Solves it with the LDL decomposition.

The example for this item is shown with the ones above: matrix-Solve.salam

Inertia

func Inertia(x &: Matrix): Matrix
The number of positive, negative and zero eigenvalues.

The example for this item is shown with the ones above: matrix-Solve.salam

Zeros

func Zeros(rows: int, cols: int): Matrix
A matrix of zeros.

The example for this item is shown with the ones above: matrix-Create.salam

Ones

func Ones(rows: int, cols: int): Matrix
A matrix of ones.

The example for this item is shown with the ones above: matrix-Create.salam

Full

func Full(rows: int, cols: int, x: f64): Matrix
A matrix filled with one value.

The example for this item is shown with the ones above: matrix-Create.salam

Eye

func Eye(n: int): Matrix
The n by n identity matrix.

The example for this item is shown with the ones above: matrix-Create.salam

EyeOffset

func EyeOffset(rows: int, cols: int, k: int): Matrix
Ones on the kth diagonal.

The example for this item is shown with the ones above: matrix-Create.salam

FromArray

func FromArray(rows: int, cols: int, data &: Vector<f64>): Matrix
A matrix from a row count, a column count and the data in row order.

The example for this item is shown with the ones above: matrix-Create.salam

RowVector

func RowVector(data &: Vector<f64>): Matrix
A row vector.

The example for this item is shown with the ones above: matrix-Create.salam

ColVector

func ColVector(data &: Vector<f64>): Matrix
A column vector.

The example for this item is shown with the ones above: matrix-Create.salam

Diag

func Diag(data &: Vector<f64>): Matrix
A diagonal matrix from a set of values.

The example for this item is shown with the ones above: matrix-Create.salam

Clone

func Clone(x &: Matrix): Matrix
An independent copy.

The example for this item is shown with the ones above: matrix-Create.salam

CopyInto

func CopyInto(dst &: Matrix, src &: Matrix)
Copies the data into a matrix of the same shape.

The example for this item is shown with the ones above: matrix-Create.salam

Free

func Free(x &: Matrix)
Frees it.

The example for this item is shown with the ones above: matrix-Create.salam

SetAll

func SetAll(x &: Matrix, v: f64)
Sets everything to one value.

The example for this item is shown with the ones above: matrix-Create.salam

Arange

func Arange(start: f64, stop: f64, step: f64): Matrix
A row vector from the start up to but not including the end, by a step.

The example for this item is shown with the ones above: matrix-Create.salam

Linspace

func Linspace(start: f64, stop: f64, num: int): Matrix
n evenly spaced numbers from the start to the end.

The example for this item is shown with the ones above: matrix-Create.salam

Logspace

func Logspace(start: f64, stop: f64, num: int, base: f64): Matrix
n logarithmically spaced numbers; from the base to the power of the start to the base to the power of the end.

The example for this item is shown with the ones above: matrix-Create.salam

RowEchelon

func RowEchelon(x &: Matrix): Matrix
The row echelon form.

The example for this item is shown with the ones above: matrix-Solve.salam

RREF

func RREF(x &: Matrix): Matrix
The reduced row echelon form.

The example for this item is shown with the ones above: matrix-Solve.salam

PivotColumns

func PivotColumns(x &: Matrix): Matrix
The indices of the pivot columns.

The example for this item is shown with the ones above: matrix-Solve.salam

RankExact

func RankExact(x &: Matrix): int
The rank by Gaussian elimination.

The example for this item is shown with the ones above: matrix-Solve.salam

Nullity

func Nullity(x &: Matrix): int
The dimension of the null space.

The example for this item is shown with the ones above: matrix-Solve.salam

NullSpaceExact

func NullSpaceExact(x &: Matrix): Matrix
A basis of the null space from the echelon form.

The example for this item is shown with the ones above: matrix-Solve.salam

ColumnSpaceExact

func ColumnSpaceExact(x &: Matrix): Matrix
A basis of the column space from the pivot columns.

The example for this item is shown with the ones above: matrix-Solve.salam

SolveGeneral

func SolveGeneral(a &: Matrix, b &: Matrix): Matrix
One solution of the system, even when the matrix is singular.

The example for this item is shown with the ones above: matrix-Solve.salam

IsConsistent

func IsConsistent(a &: Matrix, b &: Matrix): bool
The system has a solution.

The example for this item is shown with the ones above: matrix-Solve.salam

EigSym

func EigSym(x &: Matrix): EigenSym
The eigendecomposition of a symmetric matrix by the Jacobi method.

The example for this item is shown with the ones above: matrix-Eigen.salam

EigValsSym

func EigValsSym(x &: Matrix): Matrix
The eigenvalues alone.

The example for this item is shown with the ones above: matrix-Eigen.salam

EigReconstruct

func EigReconstruct(f &: EigenSym): Matrix
Rebuilds the matrix from the eigenvectors and eigenvalues.

The example for this item is shown with the ones above: matrix-Eigen.salam

SpectralRadiusSym

func SpectralRadiusSym(x &: Matrix): f64
The spectral radius of a symmetric matrix.

The example for this item is shown with the ones above: matrix-Eigen.salam

PowSym

func PowSym(x &: Matrix, p: f64): Matrix
A float power of a symmetric matrix.

The example for this item is shown with the ones above: matrix-Eigen.salam

SqrtSym

func SqrtSym(x &: Matrix): Matrix
The square root of a symmetric positive definite matrix.

The example for this item is shown with the ones above: matrix-Eigen.salam

HStack

func HStack(x &: Matrix, y &: Matrix): Matrix
Two matrices side by side.
matrix-Shape.salam
import matrix

func build(rows: int, cols: int, numbers: str): matrix.Matrix:
    mut data := Vector {} as Vector<f64>
    each number in numbers.split(" "):
        data.push(number.to_float())
    end
    ret matrix.FromArray(rows, cols, data)
end

func show(name: str, m &: matrix.Matrix):
    println name, matrix.ToRepr(m, 1)
end

func main:
    mut a := build(2, 3, "1 2 3 4 5 6")
    mut square := build(3, 3, "1 2 3 4 5 6 7 8 9")
    show("Reshape", matrix.Reshape(a, 3, 2))
    show("Flatten", matrix.Flatten(a))
    show("FlattenCol", matrix.FlattenCol(a))
    mut row := matrix.Row(a, 1)
    mut col := matrix.Col(a, 2)
    show("Row", row)
    show("Col", col)
    mut b := matrix.Clone(a)
    matrix.SetRow(b, 0, row)
    matrix.SetCol(b, 0, col)
    show("SetRow and SetCol", b)
    show("SubMatrix", matrix.SubMatrix(square, 1, 1, 2, 2))
    show("Minor", matrix.Minor(square, 0, 0))
    show("DeleteRow", matrix.DeleteRow(square, 1))
    show("DeleteCol", matrix.DeleteCol(square, 1))
    show("InsertRow", matrix.InsertRow(a, 1, matrix.Row(a, 0)))
    show("InsertCol", matrix.InsertCol(a, 0, matrix.Col(a, 2)))
    matrix.SwapRows(b, 0, 1)
    matrix.SwapCols(b, 0, 2)
    show("swapped", b)
    show("Diagonal", matrix.Diagonal(square))
    show("DiagonalOffset", matrix.DiagonalOffset(square, 1))
    mut c := matrix.Clone(square)
    matrix.SetDiagonal(c, matrix.Ones(3, 1))
    matrix.ShiftDiagonal(c, 10.0)
    show("new diagonal", matrix.Diagonal(c))
    show("HStack", matrix.HStack(a, a))
    show("VStack", matrix.VStack(a, a))
    show("Augment", matrix.Augment(square, matrix.Eye(3)))
    show("BlockDiag", matrix.BlockDiag(matrix.Eye(1), matrix.Full(1, 1, 2.0)))
    show("Triu", matrix.Triu(square, 0))
    show("Tril", matrix.Tril(square, -1))
    show("SymmetrizeUpper", matrix.SymmetrizeUpper(square))
    show("SymPart", matrix.SymPart(square))
    show("SkewPart", matrix.SkewPart(square))
    show("FlipVertical", matrix.FlipVertical(a))
    show("FlipHorizontal", matrix.FlipHorizontal(a))
    show("Rot90", matrix.Rot90(a, 1))
    show("Roll", matrix.Roll(a, 0, 1))
    show("Tile", matrix.Tile(matrix.Eye(1), 2, 2))
    show("Pad", matrix.Pad(matrix.Eye(1), 1, 0, 0, 1, 9.0))
end
OutputReshape [1.0,2.0; 3.0,4.0; 5.0,6.0] Flatten [1.0,2.0,3.0,4.0,5.0,6.0] FlattenCol [1.0; 2.0; 3.0; 4.0; 5.0; 6.0] Row [4.0,5.0,6.0] Col [3.0; 6.0] SetRow and SetCol [3.0,5.0,6.0; 6.0,5.0,6.0] SubMatrix [5.0,6.0; 8.0,9.0] Minor [5.0,6.0; 8.0,9.0] DeleteRow [1.0,2.0,3.0; 7.0,8.0,9.0] DeleteCol [1.0,3.0; 4.0,6.0; 7.0,9.0] InsertRow [1.0,2.0,3.0; 1.0,2.0,3.0; 4.0,5.0,6.0] InsertCol [3.0,1.0,2.0,3.0; 6.0,4.0,5.0,6.0] swapped [6.0,5.0,6.0; 6.0,5.0,3.0] Diagonal [1.0,5.0,9.0] DiagonalOffset [2.0,6.0] new diagonal [11.0,11.0,11.0] HStack [1.0,2.0,3.0,1.0,2.0,3.0; 4.0,5.0,6.0,4.0,5.0,6.0] VStack [1.0,2.0,3.0; 4.0,5.0,6.0; 1.0,2.0,3.0; 4.0,5.0,6.0] Augment [1.0,2.0,3.0,1.0,0.0,0.0; 4.0,5.0,6.0,0.0,1.0,0.0; 7.0,8.0,9.0,0.0,0.0,1.0] BlockDiag [1.0,0.0; 0.0,2.0] Triu [1.0,2.0,3.0; 0.0,5.0,6.0; 0.0,0.0,9.0] Tril [0.0,0.0,0.0; 4.0,0.0,0.0; 7.0,8.0,0.0] SymmetrizeUpper [1.0,2.0,3.0; 2.0,5.0,6.0; 3.0,6.0,9.0] SymPart [1.0,3.0,5.0; 3.0,5.0,7.0; 5.0,7.0,9.0] SkewPart [0.0,-1.0,-2.0; 1.0,0.0,-1.0; 2.0,1.0,0.0] FlipVertical [4.0,5.0,6.0; 1.0,2.0,3.0] FlipHorizontal [3.0,2.0,1.0; 6.0,5.0,4.0] Rot90 [3.0,6.0; 2.0,5.0; 1.0,4.0] Roll [3.0,1.0,2.0; 6.0,4.0,5.0] Tile [1.0,1.0; 1.0,1.0] Pad [9.0,9.0; 1.0,9.0]

VStack

func VStack(x &: Matrix, y &: Matrix): Matrix
Two matrices one above the other.

The example for this item is shown with the ones above: matrix-Shape.salam

Augment

func Augment(a &: Matrix, b &: Matrix): Matrix
The augmented matrix, for solving a system.

The example for this item is shown with the ones above: matrix-Shape.salam

BlockDiag

func BlockDiag(x &: Matrix, y &: Matrix): Matrix
A block diagonal matrix.

The example for this item is shown with the ones above: matrix-Shape.salam

Triu

func Triu(x &: Matrix, k: int): Matrix
The part above diagonal k.

The example for this item is shown with the ones above: matrix-Shape.salam

Tril

func Tril(x &: Matrix, k: int): Matrix
The part below diagonal k.

The example for this item is shown with the ones above: matrix-Shape.salam

SymmetrizeUpper

func SymmetrizeUpper(x &: Matrix): Matrix
Mirrors the upper half into the lower.

The example for this item is shown with the ones above: matrix-Shape.salam

SymPart

func SymPart(x &: Matrix): Matrix
The symmetric part, half of A and its transpose.

The example for this item is shown with the ones above: matrix-Shape.salam

SkewPart

func SkewPart(x &: Matrix): Matrix
The skew symmetric part.

The example for this item is shown with the ones above: matrix-Shape.salam

FlipVertical

func FlipVertical(x &: Matrix): Matrix
Reverses the rows.

The example for this item is shown with the ones above: matrix-Shape.salam

FlipHorizontal

func FlipHorizontal(x &: Matrix): Matrix
Reverses the columns.

The example for this item is shown with the ones above: matrix-Shape.salam

Rot90

func Rot90(x &: Matrix, k: int): Matrix
k ninety degree turns anticlockwise.

The example for this item is shown with the ones above: matrix-Shape.salam

Roll

func Roll(x &: Matrix, dr: int, dc: int): Matrix
Rotates the rows and columns around.

The example for this item is shown with the ones above: matrix-Shape.salam

Tile

func Tile(x &: Matrix, ry: int, rx: int): Matrix
Repeats the matrix across rows and columns.

The example for this item is shown with the ones above: matrix-Shape.salam

Pad

func Pad(x &: Matrix, top: int, bottom: int, left: int, right: int, v: f64): Matrix
Adds a border of a value around the matrix.

The example for this item is shown with the ones above: matrix-Shape.salam

Sum

func Sum(x &: Matrix): f64
The sum of everything.

The example for this item is shown with the ones above: matrix-Math.salam

Prod

func Prod(x &: Matrix): f64
The product of everything.

The example for this item is shown with the ones above: matrix-Math.salam

Mean

func Mean(x &: Matrix): f64
The mean.

The example for this item is shown with the ones above: matrix-Math.salam

Min

func Min(x &: Matrix): f64
The smallest.

The example for this item is shown with the ones above: matrix-Create.salam

Max

func Max(x &: Matrix): f64
The greatest.

The example for this item is shown with the ones above: matrix-Math.salam

Range

func Range(x &: Matrix): f64
The greatest minus the smallest.

The example for this item is shown with the ones above: matrix-Math.salam

ArgMin

func ArgMin(x &: Matrix): int
Where the minimum is, in row order.

The example for this item is shown with the ones above: matrix-Math.salam

ArgMax

func ArgMax(x &: Matrix): int
Where the maximum is.

The example for this item is shown with the ones above: matrix-Math.salam

IndexRow

func IndexRow(x &: Matrix, k: int): int
The row of place k in row order.

The example for this item is shown with the ones above: matrix-Math.salam

IndexCol

func IndexCol(x &: Matrix, k: int): int
The column of place k.

The example for this item is shown with the ones above: matrix-Math.salam

MaxAbs

func MaxAbs(x &: Matrix): f64
The greatest absolute value.

The example for this item is shown with the ones above: matrix-Math.salam

Var

func Var(x &: Matrix, sample: bool): f64
The variance; the sample one with sample.

The example for this item is shown with the ones above: matrix-Math.salam

Std

func Std(x &: Matrix, sample: bool): f64
The standard deviation.

The example for this item is shown with the ones above: matrix-Math.salam

Median

func Median(x &: Matrix): f64
The median.

The example for this item is shown with the ones above: matrix-Math.salam

RowSums

func RowSums(x &: Matrix): Matrix
The sum of each row.

The example for this item is shown with the ones above: matrix-Math.salam

ColSums

func ColSums(x &: Matrix): Matrix
The sum of each column.

The example for this item is shown with the ones above: matrix-Math.salam

RowMeans

func RowMeans(x &: Matrix): Matrix
The mean of each row.

The example for this item is shown with the ones above: matrix-Math.salam

ColMeans

func ColMeans(x &: Matrix): Matrix
The mean of each column.

The example for this item is shown with the ones above: matrix-Math.salam

RowMins

func RowMins(x &: Matrix): Matrix
The smallest of each row.

The example for this item is shown with the ones above: matrix-Math.salam

RowMaxs

func RowMaxs(x &: Matrix): Matrix
The greatest of each row.

The example for this item is shown with the ones above: matrix-Math.salam

ColMins

func ColMins(x &: Matrix): Matrix
The smallest of each column.

The example for this item is shown with the ones above: matrix-Math.salam

ColMaxs

func ColMaxs(x &: Matrix): Matrix
The greatest of each column.

The example for this item is shown with the ones above: matrix-Math.salam

CumSumRows

func CumSumRows(x &: Matrix): Matrix
The cumulative sum along each row.

The example for this item is shown with the ones above: matrix-Math.salam

CumSumCols

func CumSumCols(x &: Matrix): Matrix
The cumulative sum along each column.

The example for this item is shown with the ones above: matrix-Math.salam

CumProdRows

func CumProdRows(x &: Matrix): Matrix
The cumulative product along each row.

The example for this item is shown with the ones above: matrix-Math.salam

CountNonZero

func CountNonZero(x &: Matrix): int
The number of non zero elements.

The example for this item is shown with the ones above: matrix-Math.salam

Any

func Any(x &: Matrix): bool
At least one element is non zero.

The example for this item is shown with the ones above: matrix-Create.salam

All

func All(x &: Matrix): bool
Every element is non zero.

The example for this item is shown with the ones above: matrix-Create.salam

AllFinite

func AllFinite(x &: Matrix): bool
No element is NaN or infinite.

The example for this item is shown with the ones above: matrix-Math.salam

NewRng

func NewRng(seed: i64): Rng
A generator from a seed.

The example for this item is shown with the ones above: matrix-Stats.salam

Hilbert

func Hilbert(n: int): Matrix
The Hilbert matrix; the famous example of an ill conditioned matrix.

The example for this item is shown with the ones above: matrix-Stats.salam

HilbertInverse

func HilbertInverse(n: int): Matrix
The exact inverse of the Hilbert matrix.

The example for this item is shown with the ones above: matrix-Stats.salam

Vandermonde

func Vandermonde(x &: Matrix, cols: int): Matrix
The Vandermonde matrix; for polynomial interpolation.

The example for this item is shown with the ones above: matrix-Stats.salam

Toeplitz

func Toeplitz(first_col &: Matrix, first_row &: Matrix): Matrix
A Toeplitz matrix from its first column and row.

The example for this item is shown with the ones above: matrix-Eigen.salam

Hankel

func Hankel(first_col &: Matrix, last_row &: Matrix): Matrix
A Hankel matrix from its first column and last row.

The example for this item is shown with the ones above: matrix-Eigen.salam

Circulant

func Circulant(c &: Matrix): Matrix
A circulant matrix.

The example for this item is shown with the ones above: matrix-Stats.salam

Companion

func Companion(coeffs &: Matrix): Matrix
The companion matrix of a polynomial with coefficients from degree zero; its eigenvalues are the roots.

The example for this item is shown with the ones above: matrix-Stats.salam

Pascal

func Pascal(n: int): Matrix
The Pascal matrix.

The example for this item is shown with the ones above: matrix-Stats.salam

Rotation2D

func Rotation2D(theta: f64): Matrix
A two dimensional rotation by an angle in radians.

The example for this item is shown with the ones above: matrix-Stats.salam

Rotation3D

func Rotation3D(axis: int, theta: f64): Matrix
A three dimensional rotation about axis 0, 1 or 2.

The example for this item is shown with the ones above: matrix-Stats.salam

Householder

func Householder(v &: Matrix): Matrix
A Householder reflection.

The example for this item is shown with the ones above: matrix-Stats.salam

Givens

func Givens(n: int, p: int, q: int, theta: f64): Matrix
A Givens rotation in the p and q plane.

The example for this item is shown with the ones above: matrix-Stats.salam

Permutation

func Permutation(perm &: Matrix): Matrix
A permutation matrix.

The example for this item is shown with the ones above: matrix-Stats.salam

Random

func Random(rows: int, cols: int, lo: f64, hi: f64, seed: i64): Matrix
A uniform random matrix from a seed.

The example for this item is shown with the ones above: matrix-Eigen.salam

RandomNormal

func RandomNormal(rows: int, cols: int, seed: i64): Matrix
A normal random matrix.

The example for this item is shown with the ones above: matrix-Stats.salam

RandomSymmetric

func RandomSymmetric(n: int, seed: i64): Matrix
A random symmetric matrix.

The example for this item is shown with the ones above: matrix-Stats.salam

RandomSPD

func RandomSPD(n: int, seed: i64): Matrix
A random positive definite matrix.

The example for this item is shown with the ones above: matrix-Stats.salam

RandomOrthogonal

func RandomOrthogonal(n: int, seed: i64): Matrix
A random orthogonal matrix.

The example for this item is shown with the ones above: matrix-Stats.salam

Add

func Add(x &: Matrix, y &: Matrix): Matrix
Addition.

The example for this item is shown with the ones above: matrix-Math.salam

Sub

func Sub(x &: Matrix, y &: Matrix): Matrix
Subtraction.

The example for this item is shown with the ones above: matrix-Math.salam

MulElem

func MulElem(x &: Matrix, y &: Matrix): Matrix
Elementwise multiplication.

The example for this item is shown with the ones above: matrix-Math.salam

DivElem

func DivElem(x &: Matrix, y &: Matrix): Matrix
Elementwise division.

The example for this item is shown with the ones above: matrix-Math.salam

Neg

func Neg(x &: Matrix): Matrix
The negation.

The example for this item is shown with the ones above: matrix-Math.salam

AddScalar

func AddScalar(x &: Matrix, s: f64): Matrix
Addition of a number.

The example for this item is shown with the ones above: matrix-Math.salam

SubScalar

func SubScalar(x &: Matrix, s: f64): Matrix
Subtraction of a number.

The example for this item is shown with the ones above: matrix-Math.salam

Scale

func Scale(x &: Matrix, s: f64): Matrix
Multiplication by a number.

The example for this item is shown with the ones above: matrix-Math.salam

DivScalar

func DivScalar(x &: Matrix, s: f64): Matrix
Division by a number.

The example for this item is shown with the ones above: matrix-Math.salam

AddInPlace

func AddInPlace(dst &: Matrix, src &: Matrix)
Adds in place.

The example for this item is shown with the ones above: matrix-Math.salam

SubInPlace

func SubInPlace(dst &: Matrix, src &: Matrix)
Subtracts in place.

The example for this item is shown with the ones above: matrix-Math.salam

MulElemInPlace

func MulElemInPlace(dst &: Matrix, src &: Matrix)
Multiplies elementwise in place.

The example for this item is shown with the ones above: matrix-Math.salam

ScaleInPlace

func ScaleInPlace(dst &: Matrix, s: f64)
Multiplies by a number in place.

The example for this item is shown with the ones above: matrix-Math.salam

AddScalarInPlace

func AddScalarInPlace(dst &: Matrix, s: f64)
Adds a number in place.

The example for this item is shown with the ones above: matrix-Math.salam

NegInPlace

func NegInPlace(dst &: Matrix)
Negates in place.

The example for this item is shown with the ones above: matrix-Math.salam

AddScaled

func AddScaled(dst &: Matrix, src &: Matrix, alpha: f64)
Adds alpha times the source to the destination.

The example for this item is shown with the ones above: matrix-Math.salam

Combine

func Combine(x &: Matrix, a: f64, y &: Matrix, b: f64): Matrix
The linear combination a times x plus b times y.

The example for this item is shown with the ones above: matrix-Math.salam

MaxElem

func MaxElem(x &: Matrix, y &: Matrix): Matrix
The elementwise greater of two matrices.

The example for this item is shown with the ones above: matrix-Math.salam

MinElem

func MinElem(x &: Matrix, y &: Matrix): Matrix
The elementwise smaller.

The example for this item is shown with the ones above: matrix-Math.salam

AddRowVector

func AddRowVector(x &: Matrix, row &: Matrix): Matrix
Adds a row vector to every row.

The example for this item is shown with the ones above: matrix-Math.salam

AddColVector

func AddColVector(x &: Matrix, col &: Matrix): Matrix
Adds a column vector to every column.

The example for this item is shown with the ones above: matrix-Math.salam

MulRowVector

func MulRowVector(x &: Matrix, row &: Matrix): Matrix
Multiplies every row elementwise by a row vector.

The example for this item is shown with the ones above: matrix-Math.salam

MulColVector

func MulColVector(x &: Matrix, col &: Matrix): Matrix
Multiplies every column by a column vector.

The example for this item is shown with the ones above: matrix-Math.salam

SVDecompose

func SVDecompose(x &: Matrix): SVD
The singular value decomposition.

The example for this item is shown with the ones above: matrix-Eigen.salam

SingularValues

func SingularValues(x &: Matrix): Matrix
The singular values alone.

The example for this item is shown with the ones above: matrix-Eigen.salam

MatrixRank

func MatrixRank(x &: Matrix, tol: f64): int
The rank, within a tolerance.

The example for this item is shown with the ones above: matrix-Solve.salam

PseudoInverse

func PseudoInverse(x &: Matrix, tol: f64): Matrix
The Moore Penrose pseudoinverse.

The example for this item is shown with the ones above: matrix-Eigen.salam

NullSpace

func NullSpace(x &: Matrix, tol: f64): Matrix
An orthogonal basis of the null space.

The example for this item is shown with the ones above: matrix-Solve.salam

ColumnSpace

func ColumnSpace(x &: Matrix, tol: f64): Matrix
An orthogonal basis of the column space.

The example for this item is shown with the ones above: matrix-Solve.salam

LowRankApprox

func LowRankApprox(x &: Matrix, k: int): Matrix
The best rank k approximation; for compression.

The example for this item is shown with the ones above: matrix-Eigen.salam

NormFrobenius

func NormFrobenius(x &: Matrix): f64
The Frobenius norm, the square root of the sum of the squares.

The example for this item is shown with the ones above: matrix-Math.salam

Norm1

func Norm1(x &: Matrix): f64
The one norm, the greatest absolute column sum.

The example for this item is shown with the ones above: matrix-Math.salam

NormInf

func NormInf(x &: Matrix): f64
The infinity norm, the greatest absolute row sum.

The example for this item is shown with the ones above: matrix-Math.salam

NormMax

func NormMax(x &: Matrix): f64
The greatest absolute element.

The example for this item is shown with the ones above: matrix-Math.salam

Norm2

func Norm2(x &: Matrix): f64
The two norm, the largest singular value.

The example for this item is shown with the ones above: matrix-Math.salam

NormNuclear

func NormNuclear(x &: Matrix): f64
The nuclear norm, the sum of the singular values.

The example for this item is shown with the ones above: matrix-Math.salam

VecNorm

func VecNorm(x &: Matrix, p: f64): f64
The p norm of a vector.

The example for this item is shown with the ones above: matrix-Math.salam

Distance

func Distance(x &: Matrix, y &: Matrix): f64
The Euclidean distance.

The example for this item is shown with the ones above: matrix-Math.salam

Normalize

func Normalize(x &: Matrix): Matrix
The vector scaled to length one.

The example for this item is shown with the ones above: matrix-Math.salam

NormalizeRows

func NormalizeRows(x &: Matrix): Matrix
Every row scaled to length one.

The example for this item is shown with the ones above: matrix-Math.salam

NormalizeCols

func NormalizeCols(x &: Matrix): Matrix
Every column scaled to length one.

The example for this item is shown with the ones above: matrix-Math.salam

Cond1

func Cond1(x &: Matrix): f64
The condition number in the one norm.

The example for this item is shown with the ones above: matrix-Math.salam

CondInf

func CondInf(x &: Matrix): f64
The condition number in the infinity norm.

The example for this item is shown with the ones above: matrix-Math.salam

Cond2

func Cond2(x &: Matrix): f64
The condition number in the two norm; a large one means solving the system is unstable.

The example for this item is shown with the ones above: matrix-Math.salam

QRDecompose

func QRDecompose(x &: Matrix): QR
The QR decomposition by Householder reflection.

The example for this item is shown with the ones above: matrix-Solve.salam

QRGetQ

func QRGetQ(f &: QR): Matrix
The orthogonal matrix Q.

The example for this item is shown with the ones above: matrix-Solve.salam

QRGetR

func QRGetR(f &: QR): Matrix
The upper triangular matrix R.

The example for this item is shown with the ones above: matrix-Solve.salam

LeastSquares

func LeastSquares(a &: Matrix, b &: Matrix): Matrix
The least squares solution of an overdetermined system; fitting a line, for instance.

The example for this item is shown with the ones above: matrix-Solve.salam

QRSolve

func QRSolve(f &: QR, b &: Matrix): Matrix
Solves it with the QR decomposition.

The example for this item is shown with the ones above: matrix-Solve.salam

Residual

func Residual(a &: Matrix, x &: Matrix, b &: Matrix): Matrix
The residual, AX minus B.

The example for this item is shown with the ones above: matrix-Solve.salam

Orthonormalize

func Orthonormalize(x &: Matrix): Matrix
Makes the columns orthonormal.

The example for this item is shown with the ones above: matrix-Solve.salam

ToStringPrec

func ToStringPrec(x &: Matrix, prec: int): str
Multi line text with a number of decimal places.

The example for this item is shown with the ones above: matrix-Create.salam

ToString

func ToString(x &: Matrix): str
Multi line text.

The example for this item is shown with the ones above: matrix-Create.salam

ShapeString

func ShapeString(x &: Matrix): str
The shape, such as 2x3.

The example for this item is shown with the ones above: matrix-Create.salam

Print

func Print(x &: Matrix)
Prints the matrix.

The example for this item is shown with the ones above: matrix-Create.salam

PrintPrec

func PrintPrec(x &: Matrix, prec: int)
Prints it with a number of decimal places.

The example for this item is shown with the ones above: matrix-Create.salam

PrintLabeled

func PrintLabeled(label: str, x &: Matrix)
Prints it with a label and its shape.

The example for this item is shown with the ones above: matrix-Create.salam

ToCSV

func ToCSV(x &: Matrix, prec: int): str
CSV text.

The example for this item is shown with the ones above: matrix-Create.salam

ToRepr

func ToRepr(x &: Matrix, prec: int): str
One line text such as [1,2; 3,4].

The example for this item is shown with the ones above: matrix-Create.salam

Transpose

func Transpose(x &: Matrix): Matrix
The transpose.

The example for this item is shown with the ones above: matrix-Math.salam

MatMul

func MatMul(x &: Matrix, y &: Matrix): Matrix
Matrix multiplication.

The example for this item is shown with the ones above: matrix-Math.salam

TransposeInto

func TransposeInto(x &: Matrix, out &: Matrix): bool
The transpose into a ready matrix.

The example for this item is shown with the ones above: matrix-Math.salam

MatMulInto

func MatMulInto(x &: Matrix, y &: Matrix, out &: Matrix): bool
The product into a ready matrix.

The example for this item is shown with the ones above: matrix-Math.salam

TransposeMul

func TransposeMul(x &: Matrix, y &: Matrix): Matrix
The transpose of x times y.

The example for this item is shown with the ones above: matrix-Math.salam

MulTranspose

func MulTranspose(x &: Matrix, y &: Matrix): Matrix
x times the transpose of y.

The example for this item is shown with the ones above: matrix-Math.salam

Gram

func Gram(x &: Matrix): Matrix
The Gram matrix, the transpose of x times x.

The example for this item is shown with the ones above: matrix-Math.salam

MatVec

func MatVec(a &: Matrix, x &: Matrix): Matrix
A matrix times a column vector.

The example for this item is shown with the ones above: matrix-Math.salam

VecMat

func VecMat(x &: Matrix, a &: Matrix): Matrix
A row vector times a matrix.

The example for this item is shown with the ones above: matrix-Math.salam

Dot

func Dot(x &: Matrix, y &: Matrix): f64
The dot product of two vectors.

The example for this item is shown with the ones above: matrix-Math.salam

Outer

func Outer(u &: Matrix, v &: Matrix): Matrix
The outer product.

The example for this item is shown with the ones above: matrix-Math.salam

Kron

func Kron(x &: Matrix, y &: Matrix): Matrix
The Kronecker product.

The example for this item is shown with the ones above: matrix-Math.salam

MatPow

func MatPow(x &: Matrix, k: int): Matrix
An integer power of a square matrix.

The example for this item is shown with the ones above: matrix-Math.salam

Trace

func Trace(x &: Matrix): f64
The trace, the sum of the diagonal.

The example for this item is shown with the ones above: matrix-Math.salam

Cross3

func Cross3(u &: Matrix, v &: Matrix): Matrix
The three dimensional cross product.

The example for this item is shown with the ones above: matrix-Math.salam

IsSquare

func IsSquare(x &: Matrix): bool
The matrix is square.

The example for this item is shown with the ones above: matrix-Create.salam

AllClose

func AllClose(x &: Matrix, y &: Matrix, tol: f64): bool
The two matrices are equal within a tolerance.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsZero

func IsZero(x &: Matrix, tol: f64): bool
The matrix is zero.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsIdentity

func IsIdentity(x &: Matrix, tol: f64): bool
The matrix is the identity.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsSymmetric

func IsSymmetric(x &: Matrix, tol: f64): bool
The matrix is symmetric.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsSkewSymmetric

func IsSkewSymmetric(x &: Matrix, tol: f64): bool
The matrix is skew symmetric.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsDiagonal

func IsDiagonal(x &: Matrix, tol: f64): bool
The matrix is diagonal.

The example for this item is shown with the ones above: matrix-Solve.salam

IsUpperTriangular

func IsUpperTriangular(x &: Matrix, tol: f64): bool
The matrix is upper triangular.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsLowerTriangular

func IsLowerTriangular(x &: Matrix, tol: f64): bool
The matrix is lower triangular.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsBanded

func IsBanded(x &: Matrix, lower: int, upper: int, tol: f64): bool
Every element sits within a band below and above the diagonal.

The example for this item is shown with the ones above: matrix-Eigen.salam

LowerBandwidth

func LowerBandwidth(x &: Matrix, tol: f64): int
The bandwidth below the diagonal.

The example for this item is shown with the ones above: matrix-Eigen.salam

UpperBandwidth

func UpperBandwidth(x &: Matrix, tol: f64): int
The bandwidth above the diagonal.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsOrthogonal

func IsOrthogonal(x &: Matrix, tol: f64): bool
The matrix is orthogonal.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsNormal

func IsNormal(x &: Matrix, tol: f64): bool
The matrix is normal, meaning it commutes with its transpose.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsIdempotent

func IsIdempotent(x &: Matrix, tol: f64): bool
The matrix is idempotent, meaning its square is itself.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsInvolutory

func IsInvolutory(x &: Matrix, tol: f64): bool
The matrix is its own inverse.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsNilpotent

func IsNilpotent(x &: Matrix, tol: f64): bool
The matrix is nilpotent.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsPositiveDefinite

func IsPositiveDefinite(x &: Matrix, tol: f64): bool
The matrix is positive definite.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsPositiveSemiDefinite

func IsPositiveSemiDefinite(x &: Matrix, tol: f64): bool
The matrix is positive semidefinite.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsSingular

func IsSingular(x &: Matrix): bool
The matrix is singular.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsInvertible

func IsInvertible(x &: Matrix): bool
The matrix is invertible.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsRowStochastic

func IsRowStochastic(x &: Matrix, tol: f64): bool
The matrix is row stochastic, meaning every row sums to one.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsColStochastic

func IsColStochastic(x &: Matrix, tol: f64): bool
The matrix is column stochastic.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsDoublyStochastic

func IsDoublyStochastic(x &: Matrix, tol: f64): bool
The matrix is doubly stochastic.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsPermutation

func IsPermutation(x &: Matrix, tol: f64): bool
The matrix is a permutation matrix.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsToeplitz

func IsToeplitz(x &: Matrix, tol: f64): bool
The matrix is Toeplitz, meaning every diagonal is constant.

The example for this item is shown with the ones above: matrix-Eigen.salam

IsHankel

func IsHankel(x &: Matrix, tol: f64): bool
The matrix is Hankel, meaning every antidiagonal is constant.

The example for this item is shown with the ones above: matrix-Eigen.salam

Density

func Density(x &: Matrix): f64
The fraction of non zero elements.

The example for this item is shown with the ones above: matrix-Eigen.salam

Reshape

func Reshape(x &: Matrix, rows: int, cols: int): Matrix
A copy with a new shape.

The example for this item is shown with the ones above: matrix-Shape.salam

Flatten

func Flatten(x &: Matrix): Matrix
A row vector of every element.

The example for this item is shown with the ones above: matrix-Shape.salam

FlattenCol

func FlattenCol(x &: Matrix): Matrix
A column vector, column by column.

The example for this item is shown with the ones above: matrix-Shape.salam

Row

func Row(x &: Matrix, i: int): Matrix
Row i as a row vector.

The example for this item is shown with the ones above: matrix-Shape.salam

Col

func Col(x &: Matrix, j: int): Matrix
Column j as a column vector.

The example for this item is shown with the ones above: matrix-Shape.salam

SetRow

func SetRow(x &: Matrix, i: int, v &: Matrix)
Sets the row.

The example for this item is shown with the ones above: matrix-Shape.salam

SetCol

func SetCol(x &: Matrix, j: int, v &: Matrix)
Sets the column.

The example for this item is shown with the ones above: matrix-Shape.salam

SubMatrix

func SubMatrix(x &: Matrix, r0: int, c0: int, dr: int, dc: int): Matrix
A submatrix from a starting row and column, with a size.

The example for this item is shown with the ones above: matrix-Shape.salam

Minor

func Minor(x &: Matrix, i: int, j: int): Matrix
The matrix without row i and column j.

The example for this item is shown with the ones above: matrix-Shape.salam

DeleteRow

func DeleteRow(x &: Matrix, i: int): Matrix
Without row i.

The example for this item is shown with the ones above: matrix-Shape.salam

DeleteCol

func DeleteCol(x &: Matrix, j: int): Matrix
Without column j.

The example for this item is shown with the ones above: matrix-Shape.salam

InsertRow

func InsertRow(x &: Matrix, i: int, v &: Matrix): Matrix
With a new row at place i.

The example for this item is shown with the ones above: matrix-Shape.salam

InsertCol

func InsertCol(x &: Matrix, j: int, v &: Matrix): Matrix
With a new column at place j.

The example for this item is shown with the ones above: matrix-Shape.salam

SwapRows

func SwapRows(x &: Matrix, i: int, j: int)
Swaps two rows in place.

The example for this item is shown with the ones above: matrix-Shape.salam

SwapCols

func SwapCols(x &: Matrix, i: int, j: int)
Swaps two columns in place.

The example for this item is shown with the ones above: matrix-Shape.salam

Diagonal

func Diagonal(x &: Matrix): Matrix
The main diagonal.

The example for this item is shown with the ones above: matrix-Shape.salam

DiagonalOffset

func DiagonalOffset(x &: Matrix, k: int): Matrix
The kth diagonal; positive is above the main one.

The example for this item is shown with the ones above: matrix-Shape.salam

SetDiagonal

func SetDiagonal(x &: Matrix, v &: Matrix)
Sets the diagonal.

The example for this item is shown with the ones above: matrix-Shape.salam

ShiftDiagonal

func ShiftDiagonal(x &: Matrix, c: f64)
Adds a number to the diagonal.

The example for this item is shown with the ones above: matrix-Shape.salam

Abs

func Abs(x &: Matrix): Matrix
The absolute value, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Sqrt

func Sqrt(x &: Matrix): Matrix
The square root, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Exp

func Exp(x &: Matrix): Matrix
The exponential, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Log

func Log(x &: Matrix): Matrix
The natural logarithm, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Log10

func Log10(x &: Matrix): Matrix
The base ten logarithm, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Log2

func Log2(x &: Matrix): Matrix
The base two logarithm, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Sin

func Sin(x &: Matrix): Matrix
The sine, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Cos

func Cos(x &: Matrix): Matrix
The cosine, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Tan

func Tan(x &: Matrix): Matrix
The tangent, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Asin

func Asin(x &: Matrix): Matrix
The inverse sine, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Acos

func Acos(x &: Matrix): Matrix
The inverse cosine, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Atan

func Atan(x &: Matrix): Matrix
The inverse tangent, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Sinh

func Sinh(x &: Matrix): Matrix
The hyperbolic sine, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Cosh

func Cosh(x &: Matrix): Matrix
The hyperbolic cosine, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Tanh

func Tanh(x &: Matrix): Matrix
The hyperbolic tangent, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Floor

func Floor(x &: Matrix): Matrix
The floor, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Ceil

func Ceil(x &: Matrix): Matrix
The ceiling, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Round

func Round(x &: Matrix): Matrix
Rounds to the nearest integer, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Trunc

func Trunc(x &: Matrix): Matrix
Drops the fractional part, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Sign

func Sign(x &: Matrix): Matrix
The sign, meaning -1, 0 or 1, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Square

func Square(x &: Matrix): Matrix
The square, element by element.

The example for this item is shown with the ones above: matrix-Create.salam

Reciprocal

func Reciprocal(x &: Matrix): Matrix
The reciprocal, one over the element, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

Cbrt

func Cbrt(x &: Matrix): Matrix
The cube root, negative numbers included, element by element.

The example for this item is shown with the ones above: matrix-Math.salam

PowElem

func PowElem(x &: Matrix, p: f64): Matrix
Raises the elements to a power.

The example for this item is shown with the ones above: matrix-Math.salam

Clip

func Clip(x &: Matrix, lo: f64, hi: f64): Matrix
Holds the elements within a range.

The example for this item is shown with the ones above: matrix-Math.salam

MaxScalar

func MaxScalar(x &: Matrix, s: f64): Matrix
The greater of each element and a number.

The example for this item is shown with the ones above: matrix-Math.salam

MinScalar

func MinScalar(x &: Matrix, s: f64): Matrix
The smaller of each element and a number.

The example for this item is shown with the ones above: matrix-Math.salam

Chop

func Chop(x &: Matrix, tol: f64): Matrix
Zeroes the elements smaller than a tolerance; for a clean display.

The example for this item is shown with the ones above: matrix-Math.salam