Skip to content

matrix

import matrix
FunctionSignatureDescription
zeros(Int, Int) -> MatrixCreate a zero-filled matrix.
ones(Int, Int) -> MatrixCreate a matrix filled with ones.
shape(Matrix) -> (Int, Int)Get the (rows, cols) shape of a matrix.
transpose(Matrix) -> MatrixTranspose a matrix.
from_lists(List[List[Float]]) -> MatrixCreate a matrix from a list of row lists.
to_lists(Matrix) -> List[List[Float]]Convert a matrix to a list of row lists.
get(Matrix, Int, Int) -> FloatGet element at (row, col).
rows(Matrix) -> IntGet the number of rows.
cols(Matrix) -> IntGet the number of columns.
add(Matrix, Matrix) -> MatrixElement-wise addition of two matrices.
mul(Matrix, Matrix) -> MatrixMatrix multiplication.
scale(Matrix, Float) -> MatrixMultiply all elements by a scalar.
map(Matrix, fn(Float) -> Float) -> MatrixApply a function to every element.

matrix.zeros(rows: Int, cols: Int) -> Matrix

Section titled “matrix.zeros(rows: Int, cols: Int) -> Matrix”

Create a zero-filled matrix.

matrix.zeros(3, 4)

matrix.ones(rows: Int, cols: Int) -> Matrix

Section titled “matrix.ones(rows: Int, cols: Int) -> Matrix”

Create a matrix filled with ones.

matrix.ones(3, 4)

Get the (rows, cols) shape of a matrix.

matrix.shape(m) // => (3, 4)

Transpose a matrix.

matrix.transpose(m)

matrix.from_lists(rows: List[List[Float]]) -> Matrix

Section titled “matrix.from_lists(rows: List[List[Float]]) -> Matrix”

Create a matrix from a list of row lists.

matrix.from_lists([[1.0, 2.0], [3.0, 4.0]])

matrix.to_lists(m: Matrix) -> List[List[Float]]

Section titled “matrix.to_lists(m: Matrix) -> List[List[Float]]”

Convert a matrix to a list of row lists.

matrix.to_lists(m) // => [[1.0, 2.0], [3.0, 4.0]]

matrix.get(m: Matrix, row: Int, col: Int) -> Float

Section titled “matrix.get(m: Matrix, row: Int, col: Int) -> Float”

Get element at (row, col).

matrix.get(m, 0, 1) // => 2.0

Get the number of rows.

matrix.rows(m) // => 3

Get the number of columns.

matrix.cols(m) // => 4

matrix.add(a: Matrix, b: Matrix) -> Matrix

Section titled “matrix.add(a: Matrix, b: Matrix) -> Matrix”

Element-wise addition of two matrices.

matrix.add(a, b)

matrix.mul(a: Matrix, b: Matrix) -> Matrix

Section titled “matrix.mul(a: Matrix, b: Matrix) -> Matrix”

Matrix multiplication.

matrix.mul(a, b)

matrix.scale(m: Matrix, s: Float) -> Matrix

Section titled “matrix.scale(m: Matrix, s: Float) -> Matrix”

Multiply all elements by a scalar.

matrix.scale(m, 2.0)

matrix.map(m: Matrix, f: fn(Float) -> Float) -> Matrix

Section titled “matrix.map(m: Matrix, f: fn(Float) -> Float) -> Matrix”

Apply a function to every element.

matrix.map(m, (x) => x * x)