Luna-Flow/linear-algebra/mutable does not have a README file

    Sqrt

    using @Luna-Flow/arithmetic { trait Sqrt }

    Tolerance

    pub trait Tolerance {
    fn tolerance() -> Self
    }

    impl Tolerance for Float
    impl Tolerance for Double

    ColView

    pub struct ColView[T] {
    data : Matrix[T]
    col : Int
    }

    impl Show for ColView[T]

    ColView::each

    fn[T] ColView::each(self : ColView[T], f : (T) -> Unit) -> Unit

    ColView::eachi

    fn[T] ColView::eachi(self : ColView[T], f : (Int, T) -> Unit) -> Unit

    ColView::get

    #alias("_[_]")
    fn[T] ColView::get(self : ColView[T], row : Int) -> T

    ColView::iter

    fn[T] ColView::iter(self : ColView[T]) -> Iter[T]

    ColView::length

    fn[T] ColView::length(self : ColView[T]) -> Int

    ColView::map_inplace

    fn[T] ColView::map_inplace(self : ColView[T], f : (T) -> T) -> Unit

    ColView::set

    #alias("_[_]=_")
    fn[T] ColView::set(self : ColView[T], row : Int, val : T) -> Unit

    ColView::to_array

    fn[T] ColView::to_array(self : ColView[T]) -> Array[T]

    ColView::to_string

    fn[T : Show] ColView::to_string(self : ColView[T]) -> String

    ColView::to_vector

    fn[T] ColView::to_vector(self : ColView[T]) -> Vector[T]

    Lens

    type Lens[T]

    Lens::at

    #alias("_[_]")
    fn[T] Lens::at(self : Lens[T], snd : Int) -> T

    Retrieves the value at the specified column index from the lens.

    Parameters:

    • self : The lens to access.
    • col : The column index to retrieve the value from.

    Returns the value at the specified column index.

    Lens::set

    #alias("_[_]=_")
    fn[T] Lens::set(self : Lens[T], snd : Int, elem : T) -> Unit

    Sets the value at the specified column index in the lens.

    Parameters:

    • self : The lens object to modify.
    • col : The column index where the value should be set.
    • elem : The value to set at the specified column index.

    Example:

    let m = Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])

    Matrix

    type Matrix[T] derive(Eq)

    Shared matrix type for all mutable backends.
    impl HasShape for Matrix[T]
    impl Add for Matrix[T]
    impl Mul for Matrix[T]
    impl Neg for Matrix[T]
    impl Show for Matrix[T]
    impl Sub for Matrix[T]

    Matrix::add

    fn[T : Add] Matrix::add(self : Matrix[T], other : Matrix[T]) -> Matrix[T]

    Matrix::add_constant

    fn[T : Add] Matrix::add_constant(self : Matrix[T], cst : T) -> Matrix[T]

    Adds a constant value to each element of the matrix.

    Parameters:

    • self : The matrix to which the constant will be added.
    • cst : The constant value to add to each element.

    Returns a new matrix with the constant added to each element of the original matrix.

    Example:

    let m = Matrix::from_2d_array([[1, 2], [3, 4]])
    let result = m.add_constant(5)
    inspect(result, content="|6, 7|\n|8, 9|")

    Matrix::adjoint

    Computes the adjoint (conjugate transpose) of the matrix.

    Parameters:

    • self : The matrix to compute the adjoint of.

    Returns the conjugate transpose of the original matrix.

    Matrix::at

    #alias("_[_]")
    fn[T] Matrix::at(self : Matrix[T], row : Int) -> Lens[T]

    Returns a lens that provides indexed access to a specific row of the matrix.

    Performance Note: Calling m[row] allocates a new Lens object and two closures. For performance-critical bulk operations on a row, it is highly recommended to:
    1. Cache the lens: Store the result of m[row] in a variable before the loop.
    2. Use built-in tools: For common operations, use each_row, map_row_inplace, or iter_row which avoid Lens overhead entirely.

    Parameters:

    • self : The matrix to access.
    • row : The zero-based index of the row to access.

    Returns a Lens[T] object that allows getting and setting elements in the specified row by column index.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let row_lens = m[1] // Caching the lens for row 1
    inspect(row_lens[0], content="4") // Access element at column 0
    row_lens[2] = 10 // Set element at column 2
    inspect(m[1][2], content="10")

    Matrix::col

    fn[T] Matrix::col(self : Matrix[T]) -> Int

    Returns the number of columns in the matrix.

    Parameters:

    • self : The matrix to query.

    Returns:

    Int - The number of columns in the matrix.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    inspect(m.col(), content="3")

    Matrix::col_to_array

    fn[T] Matrix::col_to_array(self : Matrix[T], col : Int) -> Array[T]

    Extracts a specific column from the matrix and returns it as an array.

    Parameters:

    • self : The matrix from which to extract the column.
    • col : The zero-based index of the column to extract.

    Returns an array containing all elements from the specified column, ordered from top to bottom.

    Panics if col is negative or greater than or equal to the number of columns in the matrix.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let col1 = m.col_to_array(1)
    inspect(col1, content="[2, 5]")

    Matrix::col_to_vector

    fn[T] Matrix::col_to_vector(self : Matrix[T], col : Int) -> Vector[T]

    Extracts a specific column from the matrix and returns it as a Vector.

    Parameters:

    • self : The matrix from which to extract the column.
    • col : The zero-based index of the column to extract.

    Returns a Vector containing all elements from the specified column, ordered from top to bottom.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let col1 = m.col_to_vector(1)
    // col1 is a Vector containing [2, 5]
    inspect(col1, content="|2, 5|")

    Matrix::col_view

    fn[T] Matrix::col_view(self : Matrix[T], col : Int) -> ColView[T]

    Matrix::copy

    fn[T] Matrix::copy(self : Matrix[T]) -> Matrix[T]

    Creates a deep copy of the matrix with the same dimensions and element values.

    Parameters:

    • self : The matrix to be copied.

    Returns a new matrix that is an independent copy of the original matrix.

    Example:

    let original = @mutable.Matrix::from_2d_array([[1, 2], [3, 4]])
    let copy = original.copy()
    copy[0][0] = 99
    inspect(original[0][0], content="1") // Original remains unchanged
    inspect(copy[0][0], content="99") // Copy is modified

    Matrix::determinant

    Calculates the determinant of a square matrix.

    This method uses an efficient algorithm based on self decomposition with partial pivoting. It transforms the matrix into an upper triangular form while tracking row swaps. The determinant is then the product of the diagonal elements of the resulting matrix, adjusted by the sign determined by the number of swaps. This approach is numerically stable and has a time complexity of O(n³).

    Parameters:
    • self: The square matrix.

    Returns the determinant of the matrix.

    Panics if the matrix is not square.

    Example:
    let m = @mutable.Matrix::from_2d_array([[4.0, 3.0], [6.0, 3.0]])
    inspect(m.determinant().unwrap(), content="-6")

    Matrix::each

    fn[T] Matrix::each(self : Matrix[T], f : (T) -> Unit) -> Unit

    Applies a function to each element in the matrix.

    Parameters:

    • self : The matrix whose elements will be processed.
    • f : The function to apply to each element.

    Matrix::each_col

    fn[T] Matrix::each_col(self : Matrix[T], col : Int, f : (T) -> Unit) -> Unit

    Iterates over all elements in a specific column of the matrix, applying a function to each element.

    Parameters:

    • self : The matrix to iterate over.
    • col : The column index to iterate over.
    • f : The function to apply to each element in the column.

    Matrix::each_row

    fn[T] Matrix::each_row(self : Matrix[T], row : Int, f : (T) -> Unit) -> Unit

    Applies a function to each element in the specified row of the matrix.

    Parameters:

    • self : The matrix to iterate over.
    • row : The index of the row to iterate through.
    • f : The function to apply to each element in the row.

    Matrix::each_row_col

    fn[T] Matrix::each_row_col(self : Matrix[T], f : (Int, Int, T) -> Unit) -> Unit

    Iterates through all elements of the matrix, calling the provided function with the row index, column index, and value for each element.

    Parameters:

    • self : The matrix to iterate through.
    • f : A function that takes three parameters: the row index (Int), the column index (Int), and the element value (T). This function is called for each element in the matrix.

    Matrix::eachi

    fn[T] Matrix::eachi(self : Matrix[T], f : (Int, T) -> Unit) -> Unit

    Iterates over each element of the matrix with its index, calling the provided function for each element.

    Parameters:

    • self : The matrix to iterate over.
    • f : A function that takes an index and an element value. The index represents the linear position in the underlying array (calculated as `row
      • col_count + col`).

    Matrix::eachi_col

    fn[T] Matrix::eachi_col(self : Matrix[T], col : Int, f : (Int, T) -> Unit) -> Unit

    Iterates over all elements in a specific column of the matrix with their row indices, applying a function to each element and its corresponding row index.

    Parameters:

    • self : The matrix to iterate over.
    • col : The zero-based index of the column to iterate through.
    • f : The function to apply to each element in the column, which takes the row index and the element value as parameters.

    Example:

    let m = Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    m.eachi_col(1, fn(row_idx, value) {
    println("Row \{row_idx}: \{value}")
    })
    // Output:
    // Row 0: 2
    // Row 1: 5

    Matrix::eachi_row

    fn[T] Matrix::eachi_row(self : Matrix[T], row : Int, f : (Int, T) -> Unit) -> Unit

    Iterates over all elements in a specific row of the matrix with their column indices, applying a function to each element.

    Parameters:

    • self : The matrix to iterate over.
    • row : The zero-based index of the row to iterate through.
    • f : A function that takes two parameters: the column index (Int) and the element value (T). This function is called for each element in the specified row.

    Example:

    let m = Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    m.eachi_row(1, fn(col_index, value) {
    println("Row 1, Column \{col_index}: \{value}")
    })
    // Prints:
    // Row 1, Column 0: 4
    // Row 1, Column 1: 5
    // Row 1, Column 2: 6

    Matrix::eigen

    Computes eigenpairs for symmetric real matrices using the QR algorithm.

    This routine currently supports only symmetric real matrices. General real matrices may require complex eigenvalues and eigenvectors, which are not yet available in this package.

    Parameters:

    • self : The square matrix to compute eigenvalues and eigenvectors for.

    Returns a tuple containing:
    • A vector of eigenvalues.
    • A matrix where each column is an eigenvector corresponding to an eigenvalue for supported symmetric inputs.

    Panics if the matrix is not square.

    Example:

    let m = Matrix::from_2d_array([[6.0, -2.0], [-2.0, 9.0]])
    let (eigenvals, _) = m.eigen()
    // eigenvals should be close to [5.0, 10.0]
    // eigenvecs should be close to [[1.0, 0.0], [0.0, 1.0]]
    let v1_ok = (eigenvals[0] - 5.0).abs() < Tolerance::tolerance() &&
    (eigenvals[1] - 10.0).abs() < Tolerance::tolerance()
    let v2_ok = (eigenvals[0] - 10.0).abs() < Tolerance::tolerance() &&
    (eigenvals[1] - 5.0).abs() < Tolerance::tolerance()
    assert_true(v1_ok || v2_ok)

    Matrix::equal

    fn[T : Eq] Matrix::equal(Matrix[T], Matrix[T]) -> Bool

    Matrix::frobenius_norm

    Calculates the Frobenius norm of the matrix.

    Parameters:

    • self : The matrix to calculate the norm of.

    Returns the Frobenius norm.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1.0, 2.0], [3.0, 4.0]])
    // sqrt(1^2 + 2^2 + 3^2 + 4^2) = sqrt(30) ≈ 5.477

    let norm = m.frobenius_norm()

    Matrix::from_2d_array

    fn[T] Matrix::from_2d_array(arr : Array[Array[T]]) -> Matrix[T]

    Creates a matrix from a 2D array.

    Parameters:

    • arr : A 2D array where each sub-array represents a row of the matrix.

    Returns a new Matrix[T] with the same dimensions and elements as the input 2D array.

    Panics if the input array is empty or if any sub-array is empty.

    Example:

    let data = [[1, 2, 3], [4, 5, 6]]
    let matrix = Matrix::from_2d_array(data)
    inspect(matrix, content="|1, 2, 3|\n|4, 5, 6|")

    Matrix::from_array

    fn[T] Matrix::from_array(row : Int, col : Int, data : Array[T]) -> Matrix[T]

    Creates a matrix from a flat array with specified dimensions.

    Parameters:

    • row : The number of rows in the matrix.
    • col : The number of columns in the matrix.
    • data : A flat array containing the matrix elements in row-major order.

    Returns a new matrix with the specified dimensions.

    Panics if the product of row and col does not equal the length of data.

    Example:

    let data = [1, 2, 3, 4, 5, 6]
    let matrix = @mutable.Matrix::from_array(2, 3, data)
    inspect(matrix, content="|1, 2, 3|\n|4, 5, 6|")

    Matrix::get

    fn[T] Matrix::get(self : Matrix[T], row : Int, col : Int) -> T

    Matrix::horizontal_combine

    fn[T] Matrix::horizontal_combine(self : Matrix[T], other : Matrix[T]) -> Matrix[T]

    Combines two matrices horizontally by placing them side by side.

    Parameters:

    • self : The left matrix to be combined.
    • other : The right matrix to be combined.

    Returns a new matrix with the same number of rows as the input matrices and columns equal to the sum of both matrices' column counts.

    Panics if the two matrices have different numbers of rows.

    Example:

    let m1 = Matrix::from_2d_array([[1, 2], [3, 4]])
    let m2 = Matrix::from_2d_array([[5, 6], [7, 8]])
    let result = m1.horizontal_combine(m2)
    inspect(result, content="|1, 2, 5, 6|\n|3, 4, 7, 8|")

    Matrix::inverse

    Computes the inverse of a square matrix using LU decomposition with partial pivoting.

    This method factorizes the matrix once and then solves against the identity matrix. If successful, the resulting matrix is the inverse.

    Parameters:

    • self : The square matrix to compute the inverse of.

    Returns an Option[Matrix[T]] where:
    • Some(inverse_matrix) if the matrix is invertible
    • None if the matrix is singular (non-invertible)

    Panics if the matrix is not square.

    Matrix::is_invertible

    Checks if a matrix is invertible (non-singular).

    A matrix is invertible if its determinant is non-zero.

    Parameters:

    • self : The square matrix to check for invertibility.

    Returns true if the matrix is invertible, false otherwise.

    Panics if the matrix is not square.

    Example:

    let m1 = @mutable.Matrix::from_2d_array([
    [1.0, 2.0],
    [3.0, 4.0]
    ])
    let m2 = @mutable.Matrix::from_2d_array([
    [1.0, 2.0],
    [2.0, 4.0] // This is singular (second row is 2 times first row)
    ])
    inspect(m1.is_invertible(), content="true")
    inspect(m2.is_invertible(), content="false")

    Matrix::is_positive_definite

    Checks if the matrix is positive definite.

    A matrix is positive definite if it is symmetric and all its eigenvalues are positive.

    Parameters:

    • self : The matrix to check.

    Returns true if positive definite, false otherwise.

    Example:

    let m = @mutable.Matrix::from_2d_array([[2.0, 0.0], [0.0, 2.0]])
    inspect(m.is_positive_definite(), content="true")

    Matrix::is_square

    fn[T] Matrix::is_square(self : Matrix[T]) -> Bool

    Checks if the matrix is square (i.e., has the same number of rows and columns).

    Parameters:
    • self : The matrix to check for squareness.

    Returns true if the matrix is square, false otherwise.

    Example:
    let m1 = @mutable.Matrix::from_2d_array([[1, 2], [3, 4]])
    let m2 = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    inspect(m1.is_square(), content="true") // 2x2 matrix is
    inspect(m2.is_square(), content="false") // 2x3 matrix is not square

    Matrix::is_symmetric

    Checks if the matrix is symmetric (A^T = A).

    Parameters:

    • self : The matrix to check.

    Returns true if symmetric, false otherwise.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1.0, 2.0], [2.0, 1.0]])
    inspect(m.is_symmetric(), content="true")

    Matrix::iter

    fn[T] Matrix::iter(self : Matrix[T]) -> Iter[T]

    Make a iterator over the matrix elements.

    Parameters:
    • self : The matrix to iterate over.

    Returns an iterator that yields each element of the matrix in row-major

    Example:
    let m = @mutable.Matrix::from_2d_array([[1, 2], [3, 4]])
    let iter = m.iter()
    inspect(iter.collect(), content="[1, 2, 3, 4]") // Collects all elements in row-major order

    Matrix::iter_col

    fn[T] Matrix::iter_col(self : Matrix[T], col : Int) -> Iter[T]

    Creates an iterator over the elements of a specific column in the matrix.

    Parameters:
    • self : The matrix to iterate over.
    • col : The zero-based index of the column to iterate.

    Returns an iterator that yields each element in the specified column from top to bottom.

    Panics if col is negative or greater than or equal to the number of columns.

    Example:
    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let col_iter = m.iter_col(1)
    for elem in col_iter {
    println(elem) // prints: 2, 5
    }

    Matrix::iter_row

    fn[T] Matrix::iter_row(self : Matrix[T], row : Int) -> Iter[T]

    Creates an iterator over the elements of a specific row in the matrix.

    Parameters:
    • self : The matrix to iterate over.
    • row : The zero-based index of the row to iterate.

    Returns an iterator that yields each element in the specified row from left to right.

    Panics if row is negative or greater than or equal to the number of rows.

    Example:
    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let row_iter = m.iter_row(1)
    for elem in row_iter {
    println(elem) // prints: 4, 5, 6
    }

    Matrix::make

    fn[A] Matrix::make(row : Int, col : Int, f : (Int, Int) -> A) -> Matrix[A]

    Creates a new matrix with specified dimensions using a generator function.

    Parameters:

    • row : The number of rows in the matrix.
    • col : The number of columns in the matrix.
    • f : A function that takes row and column indices and returns the element at that position.

    Returns a new Matrix[A] with the specified dimensions where each element is generated by calling the function with its row and column indices.

    Example:

    // Create a 3x3 identity matrix
    let identity = Matrix::make(3, 3, fn(i, j) { if i == j { 1 } else { 0 } })
    inspect(identity, content="|1, 0, 0|\n|0, 1, 0|\n|0, 0, 1|")
    // Create a matrix where each element equals its row index plus column index
    let sum_matrix = Matrix::make(2, 3, fn(i, j) { i + j })
    inspect(sum_matrix, content="|0, 1, 2|\n|1, 2, 3|")

    Matrix::map

    fn[T, U] Matrix::map(self : Matrix[T], f : (T) -> U) -> Matrix[U]

    Applies a function to every element of the matrix, creating a new matrix with the transformed elements.

    Parameters:

    • self : The input matrix to transform.
    • f : The function to apply to each element, taking a value of type T and returning a value of type U.

    Returns a new matrix with the same dimensions as the input matrix, where each element is the result of applying the function f to the corresponding element in the input matrix.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2], [3, 4]])
    let doubled = m.map(fn(x) { x * 2 })
    inspect(doubled, content="|2, 4|\n|6, 8|")

    Matrix::map_col_inplace

    #alias(map_col_in_place, deprecated="`map_col_in_place` is deprecated, use `map_col_inplace` instead")
    fn[T] Matrix::map_col_inplace(self : Matrix[T], col : Int, f : (T) -> T) -> Unit

    Applies a transformation function to all elements in a specific column of the matrix in-place.

    Parameters:

    • self : The matrix to modify.
    • col : The index of the column to transform (0-based).
    • f : The transformation function to apply to each element in the column.

    Example:

    let matrix = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    matrix.map_col_inplace(1, fn(x) { x * 2 })
    // matrix is now [[1, 4, 3], [4, 10, 6]]

    Matrix::map_inplace

    #alias(map_in_place, deprecated="`map_in_place` is deprecated, use `map_inplace` instead")
    fn[T] Matrix::map_inplace(self : Matrix[T], f : (T) -> T) -> Unit

    Applies a transformation function to all elements in the matrix in-place, modifying the original matrix.

    Parameters:

    • self : The matrix whose elements will be transformed.
    • f : The transformation function to apply to each element, taking a value of type T and returning a transformed value of the same type.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2], [3, 4]])
    m.map_inplace(fn(x) { x * 2 })
    inspect(m, content="|2, 4|\n|6, 8|")

    Matrix::map_row_inplace

    #alias(map_row_in_place, deprecated="`map_row_in_place` is deprecated, use `map_row_inplace` instead")
    fn[T] Matrix::map_row_inplace(self : Matrix[T], row : Int, f : (T) -> T) -> Unit

    Applies a transformation function to all elements in a specific row of the matrix in-place.

    Parameters:

    • self : The matrix to modify.
    • row : The zero-based index of the row to transform.
    • f : The transformation function to apply to each element in the row.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    m.map_row_inplace(0, fn(x) { x * 2 })
    // The first row becomes [2, 4, 6], second row remains [4, 5, 6]

    Matrix::mapi

    fn[T, U] Matrix::mapi(self : Matrix[T], f : (Int, Int, T) -> U) -> Matrix[U]

    Applies a function to every element of the matrix with its index, creating a new matrix with the transformed elements.

    Parameters:

    • self : The input matrix to transform.
    • f : The function to apply to each element, taking the row index, column index, and value of type T, and returning a value of type U.

    Returns a new matrix with the same dimensions as the input matrix.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2], [3, 4]])
    let m2 = m.mapi(fn(i, j, x) { x + i + j })
    inspect(m2, content="|1, 3|\n|4, 6|")

    Matrix::matmul

    Multiplies two matrices, returning a dimension mismatch instead of aborting.

    Matrix::matrix_power

    Calculates the n-th power of the matrix.

    Parameters:

    • self : The matrix to raise to the power.
    • n : The exponent.

    Returns the matrix raised to the power n.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 1], [1, 0]])
    let m2 = m.matrix_power(2)
    inspect(m2, content="|2, 1|\n|1, 1|")

    Matrix::max_element

    Finds the maximum element in the matrix.

    Parameters:

    • self : The matrix to search.

    Returns the maximum element.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 5, 3], [2, 8, 1]])
    inspect(m.max_element(), content="8")

    Matrix::mean

    Calculates the mean (average) of all elements in the matrix.

    Parameters:

    • self : The matrix to calculate the mean of.

    Returns the mean value.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1.0, 2.0], [3.0, 4.0]])
    inspect(m.mean(), content="2.5")

    Matrix::min_element

    Finds the minimum element in the matrix.

    Parameters:

    • self : The matrix to search.

    Returns the minimum element.

    Example:

    let m = @mutable.Matrix::from_2d_array([[5, 2, 8], [1, 9, 3]])
    inspect(m.min_element(), content="1")

    Matrix::mul_vec

    Multiplies a matrix by a vector.

    Parameters:

    • self : The matrix to multiply.
    • vector : The vector to multiply.

    Returns a new vector that is the product of the matrix and the vector.

    Panics if the number of columns in the matrix does not equal the length of the vector.

    Matrix::neg

    fn[T : Neg] Matrix::neg(self : Matrix[T]) -> Matrix[T]

    Matrix::new

    fn[T] Matrix::new(row : Int, col : Int, elem : T) -> Matrix[T]

    Creates a new matrix with the specified dimensions where all elements are initialized to the same value.

    Parameters:

    • row : The number of rows in the matrix.
    • col : The number of columns in the matrix.
    • elem : The value to initialize all elements in the matrix.

    Returns a new Matrix[T] with all elements set to elem.

    Example:

    let m = Matrix::new(2, 3, 42)
    inspect(m, content="|42, 42, 42|\n|42, 42, 42|")

    Matrix::null

    fn[T : Compare +
    Zero
    + Eq] Matrix::null(self : Matrix[T]) -> Bool

    Checks whether the matrix is a null (zero) matrix by testing if all elements are equal to zero.

    Parameters:

    • self : The matrix to check for nullity.

    Returns true if all elements in the matrix are zero, false otherwise.

    Example:

    let zero_matrix = @mutable.Matrix::new(2, 2, 0)
    let non_zero_matrix = @mutable.Matrix::from_2d_array([[1, 0], [0, 1]])
    inspect(zero_matrix.null(), content="true")
    inspect(non_zero_matrix.null(), content="false")

    Matrix::pow

    Computes the power of a square matrix using fast exponentiation.

    Parameters:

    • self : The square matrix to be raised to a power.
    • power : The non-negative integer exponent.

    Returns the matrix raised to the specified power.

    Example:

    let m = Matrix::from_2d_array([[1, 2], [3, 4]])
    let m_squared = m.pow(2).unwrap()
    // Equivalent to m * m
    inspect(m_squared, content="|7, 10|\n|15, 22|")

    Matrix::power_method

    Computes the dominant real eigenpair using the power method.

    Parameters:
    • self : The square matrix to compute the dominant eigenvalue and eigenvector for.
    • max_iterations : The maximum number of iterations to perform.

    Returns a tuple containing:
    • The dominant eigenvalue.
    • The corresponding eigenvector as a normalized vector.

    Returns None if the residual test is not met within max_iterations steps, or if the iteration reaches a zero vector (zero or nilpotent cases).

    Panics if the matrix is not square or empty.

    Matrix::rank

    Calculates the rank of the matrix using row reduction.

    Parameters:

    • self : The matrix to get the rank of.

    Returns the rank of the matrix, which is the dimension of the vector space spanned by its rows or columns.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [7.0, 8.0, 9.0]])
    let rank = m.rank()
    inspect(rank, content="2") // The rank of the matrix is 2

    Matrix::reduce_row_elimination

    Performs Gaussian elimination with partial pivoting to reduce the matrix to reduced row echelon form (RREF).

    This method applies the Gauss-Jordan elimination algorithm, which includes:

    1. Partial pivoting: selecting the row with the largest absolute value in the current column as the pivot
    2. Row swapping to move the pivot to the diagonal position
    3. Scaling the pivot row to make the pivot element equal to 1
    4. Eliminating all other elements in the current column to make them zero

    The matrix is modified in-place during the elimination process.

    Parameters:

    • self : The matrix to be reduced to reduced row echelon form.

    Returns the same matrix instance after performing the row elimination operations.

    Columns whose remaining entries are all zero are skipped. Pivot inversion is only performed after a nonzero pivot has been selected.

    Example:

    let m = @mutable.Matrix::from_2d_array([
    [1.0, 2.0, 3.0, 7.0],
    [1.0, 1.0, 1.0, 2.0],
    [2.0, 3.0, 3.0, 5.0]
    ])
    let result = m.reduce_row_elimination()
    // Result will be in reduced row echelon form:
    // |1, 0, 0, 1|
    // |0, 1, 0, -3|
    // |0, 0, 1, 4|
    inspect(result, content="|1, 0, 0, 1|\n|0, 1, 0, -3|\n|0, 0, 1, 4|")

    Matrix::row

    fn[T] Matrix::row(self : Matrix[T]) -> Int

    Indexing interface that provides mutable access to elements along a matrix row.

    Fields:

    Since this struct is not marked with any visibility modifier, it is abstract and users cannot directly access its fields. The struct can only be used through its associated methods.

    Performance Best Practices:

    1. Use Direct Access (Recommended): While m[i][j] is convenient, Matrix::get(row, col) and Matrix::set(row, col, value) are significantly faster as they avoid creating temporary Lens objects and closures. Prefer these for performance-critical code.
    2. Avoid Repeated Lens Allocation: If you must use Lens, accessing m[i][j] in a loop creates a Lens object on every call to m[i]. Cache the row lens:
      let row = m[i] // Allocate once
      for j in 0..<m.col {
      row[j] = ... // Reuse cached lens
      }
    3. Prefer Built-in Row/Col Tools: Methods like map_row_inplace, each_row, eachi_row, and iter_row are highly optimized and bypass the Lens abstraction entirely.
    4. Manual Index Tracking: For custom low-level loops, manually incrementing a 1D offset is more efficient than calculating row * col + j in every iteration.

    Example:

    let m = Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let row_lens = m[0] // Get lens for first row
    inspect(row_lens[1], content="2") // Access element at column 1
    row_lens[1] = 9 // Modify element at column 1
    inspect(row_lens[1], content="9")

    Returns the number of rows in the matrix.

    Parameters:

    • self : The matrix to query.

    Returns the number of rows as an Int.

    Example:

    let matrix = Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    inspect(matrix.row(), content="2")

    Matrix::row_to_array

    fn[T] Matrix::row_to_array(self : Matrix[T], row : Int) -> Array[T]

    Matrix::row_to_vector

    fn[T] Matrix::row_to_vector(self : Matrix[T], row : Int) -> Vector[T]

    Extracts a specific row from the matrix and returns it as a vector.

    Parameters:

    • self : The matrix from which to extract the row.
    • row : The zero-based index of the row to extract.

    Returns a new vector containing all elements from the specified row in column order.

    Panics if row is negative or greater than or equal to the number of rows in the matrix.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let row_vector = m.row_to_vector(1)
    // row_vector contains [4, 5, 6]
    inspect(row_vector, content="|4, 5, 6|")

    Matrix::row_view

    fn[T] Matrix::row_view(self : Matrix[T], row : Int) -> RowView[T]

    Matrix::scale

    fn[T : Mul] Matrix::scale(self : Matrix[T], cst : T) -> Matrix[T]

    Multiplies each element of the matrix by a scalar value.

    Parameters:

    • self : The matrix to be scaled.
    • cst : The scalar value to multiply each element by.

    Returns a new matrix where each element is the product of the corresponding element in the original matrix and the scalar.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2], [3, 4]])
    let scaled = m.scale(2)
    inspect(scaled, content="|2, 4|\n|6, 8|")

    Matrix::set

    fn[T] Matrix::set(self : Matrix[T], row : Int, col : Int, elem : T) -> Unit

    Matrix::shape

    fn[T] Matrix::shape(self : Matrix[T]) -> (Int, Int)

    Matrix::std_dev

    Calculates the standard deviation of the elements in the matrix.

    Parameters:

    • self : The matrix to calculate the standard deviation of.

    Returns the standard deviation.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1.0, 2.0], [3.0, 4.0]])
    // sqrt(1.25) ≈ 1.118

    let std = m.std_dev()

    Matrix::sub

    fn[T : Add + Neg] Matrix::sub(self : Matrix[T], other : Matrix[T]) -> Matrix[T]

    Matrix::swap_cols

    fn[T] Matrix::swap_cols(self : Matrix[T], c1 : Int, c2 : Int) -> Unit

    Swaps two columns in the matrix in-place.

    Parameters:

    • self : The matrix whose columns will be swapped.
    • c1 : The index of the first column to swap.
    • c2 : The index of the second column to swap.

    Panics if either c1 or c2 is negative or greater than or equal to the number of columns in the matrix.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    m.swap_cols(0, 2)
    inspect(m, content="|3, 2, 1|\n|6, 5, 4|")

    Matrix::swap_rows

    fn[T] Matrix::swap_rows(self : Matrix[T], r1 : Int, r2 : Int) -> Unit

    Swaps the positions of two rows in the matrix.

    Parameters:

    • self : The matrix to perform the row swap operation on.
    • r1 : The index of the first row to swap.
    • r2 : The index of the second row to swap.

    Panics if either r1 or r2 is out of bounds (negative or greater than or equal to the number of rows in the matrix).

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    m.swap_rows(0, 1)
    inspect(m, content="|4, 5, 6|\n|1, 2, 3|")

    Matrix::to_2d_array

    fn[T] Matrix::to_2d_array(self : Matrix[T]) -> Array[Array[T]]

    Converts a matrix to a 2D array representation where each sub-array represents a row of the matrix.

    Parameters:

    • self : The matrix to convert to a 2D array.

    Returns a 2D array where each element is an array representing a row from the original matrix, preserving the order of rows and elements within each row.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let array_2d = m.to_2d_array()
    inspect(array_2d, content="[[1, 2, 3], [4, 5, 6]]")

    Matrix::to_array

    fn[T] Matrix::to_array(self : Matrix[T]) -> Array[T]

    Creates a copy of the matrix's underlying data as a new array.

    Parameters:

    • self : The matrix whose data will be copied.

    Returns a new array containing all elements of the matrix in row-major order.

    Example:

    let matrix = Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let array = matrix.to_array()
    inspect(array, content="[1, 2, 3, 4, 5, 6]")

    Matrix::to_string

    fn[T : Show] Matrix::to_string(self : Matrix[T]) -> String

    Matrix::to_transpose

    fn[T] Matrix::to_transpose(self : Matrix[T]) -> Transpose[T]

    Converts a matrix to its transpose representation without copying the underlying data.

    Parameters:

    • self : The matrix to convert to transpose form.

    Returns a Transpose[T] that represents the transposed view of the original matrix, where rows and columns are swapped but the underlying data array remains shared.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    inspect(m.to_transpose(), content="|1, 4|\n|2, 5|\n|3, 6|")
    // The transpose has 3 rows and 2 columns (original was 2x3)

    Matrix::to_vector

    fn[T] Matrix::to_vector(self : Matrix[T]) -> Vector[T]

    Converts the matrix to a vector by copying its internal array representation.

    Parameters:

    • self : The matrix to be converted to a vector.

    Returns a new vector containing all elements of the matrix in row-major order (elements are stored row by row from left to right, top to bottom).

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let vector = m.to_vector()
    inspect(vector, content="|1, 2, 3, 4, 5, 6|")

    Matrix::trace

    Calculates the trace of a square matrix.

    The trace is the sum of all diagonal elements of the matrix. This function only works on square matrices (where row count equals column count).

    Parameters:

    • self : The square matrix to calculate the trace of.

    Returns the sum of all diagonal elements.

    Panics if the matrix is not square (row count != column count).

    Example:

    let m = @mutable.Matrix::from_2d_array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    let trace = m.trace().unwrap()
    inspect(trace, content="15") // 1 + 5 + 9 = 15

    Matrix::transpose

    fn[T] Matrix::transpose(self : Matrix[T]) -> Matrix[T]

    Transposes the matrix by swapping rows and columns.

    Parameters:

    • self : The matrix to transpose.

    Returns a new matrix that is the transpose of the input matrix, where the element at position (i, j) in the original matrix becomes the element at position (j, i) in the transposed matrix.

    Example:

    let m = Matrix::from_2d_array([[1, 2, 3], [4, 5, 6]])
    let transposed = m.transpose()
    inspect(transposed, content="|1, 4|\n|2, 5|\n|3, 6|")

    Matrix::unchecked_matmul

    Multiplies two matrices without checking shape compatibility.

    Callers must ensure self.col() == other.row() before calling this method.

    Matrix::unchecked_max_element

    fn[T : Compare + Eq] Matrix::unchecked_max_element(self : Matrix[T]) -> T

    Matrix::unchecked_min_element

    fn[T : Compare + Eq] Matrix::unchecked_min_element(self : Matrix[T]) -> T

    Matrix::unchecked_mul_vec

    fn[T :
    AddMonoid
    + Mul + Add +
    Zero
    ] Matrix::unchecked_mul_vec(self : Matrix[T], vector : Vector[T]) -> Vector[T]

    Matrix::unchecked_trace

    Matrix::variance

    Calculates the variance of the elements in the matrix.

    Parameters:

    • self : The matrix to calculate the variance of.

    Returns the variance.

    Example:

    let m = @mutable.Matrix::from_2d_array([[1.0, 2.0], [3.0, 4.0]])
    inspect(m.variance(), content="1.25")

    Matrix::vertical_combine

    fn[T] Matrix::vertical_combine(self : Matrix[T], other : Matrix[T]) -> Matrix[T]

    Combines two matrices vertically by stacking the second matrix below the first matrix.

    Parameters:

    • self : The first matrix to be placed on top.
    • other : The second matrix to be placed below the first matrix.

    Returns a new matrix with the rows of self followed by the rows of other.

    Panics if the two matrices have different numbers of columns.

    Example:

    let m1 = Matrix::from_2d_array([[1, 2], [3, 4]])
    let m2 = Matrix::from_2d_array([[5, 6], [7, 8]])
    let result = m1.vertical_combine(m2)
    // Result is a 4x2 matrix: [[1, 2], [3, 4], [5, 6], [7, 8]]
    inspect(result, content="|1, 2|\n|3, 4|\n|5, 6|\n|7, 8|")

    RowView

    pub struct RowView[T] {
    data : Matrix[T]
    row : Int
    }

    impl Show for RowView[T]

    RowView::each

    fn[T] RowView::each(self : RowView[T], f : (T) -> Unit) -> Unit

    RowView::eachi

    fn[T] RowView::eachi(self : RowView[T], f : (Int, T) -> Unit) -> Unit

    RowView::get

    #alias("_[_]")
    fn[T] RowView::get(self : RowView[T], col : Int) -> T

    RowView::iter

    fn[T] RowView::iter(self : RowView[T]) -> Iter[T]

    RowView::length

    fn[T] RowView::length(self : RowView[T]) -> Int

    RowView::map_inplace

    fn[T] RowView::map_inplace(self : RowView[T], f : (T) -> T) -> Unit

    RowView::set

    #alias("_[_]=_")
    fn[T] RowView::set(self : RowView[T], col : Int, val : T) -> Unit

    RowView::to_array

    fn[T] RowView::to_array(self : RowView[T]) -> Array[T]

    RowView::to_string

    fn[T : Show] RowView::to_string(self : RowView[T]) -> String

    RowView::to_vector

    fn[T] RowView::to_vector(self : RowView[T]) -> Vector[T]

    Transpose

    pub struct Transpose[T](Matrix[T])

    Shared transpose view type for all mutable backends.
    impl Add for Transpose[T]
    impl Eq for Transpose[T]
    impl Mul for Transpose[T]
    impl Neg for Transpose[T]
    impl Show for Transpose[T]
    impl Sub for Transpose[T]

    Transpose::add

    fn[T : Add] Transpose::add(a : Transpose[T], b : Transpose[T]) -> Transpose[T]

    Transpose::add_constant

    fn[T : Add] Transpose::add_constant(self : Transpose[T], cst : T) -> Transpose[T]

    Adds a constant value to all elements of a transposed matrix.

    Parameters:

    • self : The transposed matrix to which the constant will be added.
    • constant : The constant value to add to each element.

    Returns a new transposed matrix where each element is the sum of the corresponding element in the original matrix and the constant.

    Example:

    let matrix = Matrix::make(2, 3, (i, j) => i + j)
    inspect(matrix, content=
    #||0, 1, 2|
    #||1, 2, 3|
    )
    let transpose = matrix.to_transpose()
    inspect(transpose, content=
    #||0, 1|
    #||1, 2|
    #||2, 3|
    )
    let result = transpose.add_constant(5)
    // Each element in the result will be 5 more than the corresponding element in the original transpose
    inspect(result, content=
    #||5, 6|
    #||6, 7|
    #||7, 8|
    )

    Transpose::at

    #alias("_[_]")
    fn[T] Transpose::at(self : Transpose[T], row : Int) -> Lens[T]

    Returns a lens that provides indexed access to a specific row of the transposed matrix.

    Performance Note: Calling t[row] allocates a new Lens object and several closures. Since this is a transposed view, every access t[row][col] maps back to the underlying matrix. For performance-critical bulk operations, it is highly recommended to:
    1. Cache the lens: Store the result of t[row] in a variable before the loop.
    2. Use built-in tools: For common operations, use eachi_row, map_row_inplace, or iter_row which avoid Lens overhead entirely.

    Parameters:

    • self : The transposed matrix to access.
    • row : The zero-based index of the row to access.

    Returns a Lens[T] object for accessing elements in the specified row by column index.

    Transpose::col

    fn[T] Transpose::col(self : Transpose[T]) -> Int

    Returns the number of columns in the transpose matrix.

    Parameters:

    • self : The transpose matrix to get the column count from.

    Returns the number of columns in the transpose matrix (which corresponds to the number of rows in the original matrix).

    Example:

    let matrix = @mutable.Matrix::make(3, 4, (i, j) => i * j)
    let transpose = matrix.to_transpose()
    inspect(transpose.col(), content="3")

    Transpose::col_to_array

    fn[T] Transpose::col_to_array(self : Transpose[T], col : Int) -> Array[T]

    Extracts a column from the transpose matrix as an array.

    Parameters:

    • self : The transpose matrix to extract the column from.
    • col : The zero-based index of the column to extract.

    Returns an array containing all elements in the specified column of the transpose matrix.

    Panics if the column index is out of bounds.

    Transpose::col_to_vector

    fn[T] Transpose::col_to_vector(self : Transpose[T], col : Int) -> Vector[T]

    Extracts the specified column from a transposed matrix as a vector.

    Parameters:

    • self : The transposed matrix to extract the column from.
    • col : The zero-based index of the column to extract.

    Returns a Vector[T] containing the elements of the specified column.

    Example:

    let m = @mutable.Matrix::make(3, 2, (i, j) => i + j)
    inspect(m, content=
    #||0, 1|
    #||1, 2|
    #||2, 3|
    )
    let t = m.to_transpose()
    inspect(t, content=
    #||0, 1, 2|
    #||1, 2, 3|
    )
    let col_vec = t.col_to_vector(0)
    // col_vec contains the first column of the transposed matrix
    inspect(col_vec, content="|0, 1|")

    Transpose::copy

    fn[T] Transpose::copy(self : Transpose[T]) -> Transpose[T]

    Creates a deep copy of the transpose matrix.

    Parameters:

    • self : The transpose matrix to copy.

    Returns a new transpose matrix that is a deep copy of the original.

    Example:

    let m = @mutable.Matrix::make(2, 3, fn(i, j) { i + j })
    let t = m.to_transpose()
    let t_copy = t.copy()
    // t_copy is a separate copy of t
    inspect(t_copy, content="|0, 1|\n|1, 2|\n|2, 3|")

    Transpose::each

    fn[T] Transpose::each(self : Transpose[T], f : (T) -> Unit) -> Unit

    Iterates over each element in the transpose matrix and applies the given function to each element.

    Parameters:

    • self : The transpose matrix to iterate over.
    • f : A function that takes an element of type T and performs some operation on it.

    Example:

    let matrix = @mutable.Matrix::make(2, 3, (i, j) => i + j)
    let transpose = matrix.to_transpose()
    inspect(transpose, content="|0, 1|\n|1, 2|\n|2, 3|")

    Transpose::each_col

    fn[T] Transpose::each_col(self : Transpose[T], col : Int, f : (T) -> Unit) -> Unit

    Iterates over all elements in the specified column of the transposed matrix, applying the given function to each element.

    Parameters:

    • self : The transposed matrix to iterate over.
    • col : The column index to iterate through.
    • f : The function to apply to each element in the column.

    Panics if col is out of bounds (less than 0 or greater than or equal to the number of columns).

    Transpose::each_row

    fn[T] Transpose::each_row(self : Transpose[T], row : Int, f : (T) -> Unit) -> Unit

    Iterates over all elements in the specified row of the transposed matrix, applying the given function to each element.

    Parameters:

    • self : The transposed matrix to iterate over.
    • row : The zero-based index of the row to iterate over.
    • f : The function to apply to each element in the row.

    Panics if row is out of bounds (less than 0 or greater than or equal to the number of rows).

    Transpose::each_row_col

    fn[T] Transpose::each_row_col(self : Transpose[T], f : (Int, Int, T) -> Unit) -> Unit

    Iterates over all elements in the transpose matrix with their row and column indices.

    Parameters:

    • self : The transpose matrix to iterate over.
    • f : A function that takes the row index, column index, and element value. Called for each element in the transpose matrix.

    Transpose::eachi

    fn[T] Transpose::eachi(self : Transpose[T], f : (Int, T) -> Unit) -> Unit

    Iterates over each element in the transpose with its linear index.

    Parameters:

    • self : The transpose matrix to iterate over.
    • f : A function that takes the linear index and the element value. The linear index is calculated in row-major order of the transpose (which corresponds to column-major order of the underlying matrix).

    Transpose::eachi_col

    fn[T] Transpose::eachi_col(self : Transpose[T], col : Int, f : (Int, T) -> Unit) -> Unit

    Iterates over each element in the specified column of the transposed matrix with their indices within that column.

    Parameters:

    • self : The transposed matrix to iterate over.
    • col : The zero-based index of the column to iterate through.
    • f : A function that takes the element index within the column and the element value.

    Panics if col is out of bounds (negative or greater than or equal to the number of columns).

    Transpose::eachi_row

    fn[T] Transpose::eachi_row(self : Transpose[T], row : Int, f : (Int, T) -> Unit) -> Unit

    Iterates over each element in the specified row of the transposed matrix with its column index, applying the given function to each index-element pair.

    Parameters:

    • self : The transposed matrix to iterate over.
    • row : The zero-based index of the row to iterate over.
    • f : The function to apply to each column index and element in the row. The first parameter is the column index, and the second parameter is the element value.

    Panics if row is out of bounds (less than 0 or greater than or equal to the number of rows).

    Transpose::equal

    fn[T : Eq] Transpose::equal(a : Transpose[T], b : Transpose[T]) -> Bool

    Transpose::get

    fn[T] Transpose::get(self : Transpose[T], row : Int, col : Int) -> T

    Transpose::horizontal_combine

    fn[T] Transpose::horizontal_combine(self : Transpose[T], other : Transpose[T]) -> Transpose[T]

    Horizontally combines two transpose matrices by placing them side by side.

    Parameters:

    • self : The left transpose matrix.
    • other : The right transpose matrix to be combined with the left one.

    Returns a new transpose matrix with other placed to the right of self.

    Example:

    let m1 = Matrix::make(2, 3, (i, j) => i + j)
    let m2 = Matrix::make(2, 3, (i, j) => (i + j) * 10)
    let t1 = m1.to_transpose()
    let t2 = m2.to_transpose()
    let combined = t1.horizontal_combine(t2)
    inspect(combined.col(), content="4")
    inspect(combined.row(), content="3")

    Transpose::map

    fn[T] Transpose::map(self : Transpose[T], f : (T) -> T) -> Transpose[T]

    Applies a function to each element of the transpose matrix and returns a new transpose matrix with the transformed elements.

    Parameters:

    • self : The transpose matrix to transform.
    • f : The function to apply to each element.

    Returns a new transpose matrix where each element is the result of applying f to the corresponding element in the original transpose matrix.

    Example:

    let m = Matrix::make(2, 3, (i, j) => i + j)
    let t = m.to_transpose()
    let doubled = t.map(x => x * 2)
    inspect(doubled, content="|0, 2|\n|2, 4|\n|4, 6|")

    Transpose::map_col_inplace

    #alias(map_col_in_place, deprecated="`map_col_in_place` is deprecated, use `map_col_inplace` instead")
    fn[T] Transpose::map_col_inplace(self : Transpose[T], col : Int, f : (T) -> T) -> Unit

    Applies a transformation function to each element in the specified column of the transpose matrix in-place.

    Parameters:

    • self : The transpose matrix to modify.
    • col : The zero-based index of the column to transform.
    • f : The transformation function to apply to each element in the column.

    Panics if col is out of bounds (negative or greater than or equal to the number of columns).

    Example:

    let matrix = @mutable.Matrix::make(2, 3, fn(i, j) { i + j })
    let transpose = matrix.to_transpose()
    // Double all elements in column 1
    transpose.map_col_inplace(1, fn(x) { x * 2 })

    Transpose::map_inplace

    #alias(map_in_place, deprecated="`map_in_place` is deprecated, use `map_inplace` instead")
    fn[T] Transpose::map_inplace(self : Transpose[T], f : (T) -> T) -> Unit

    Applies a function to each element of the transpose matrix in-place, modifying the original matrix.

    Parameters:

    • self : The transpose matrix to modify.
    • f : The function to apply to each element, which takes an element and returns a new element of the same type.

    Example:
    let matrix = @mutable.Matrix::make(2, 3, fn(i, j) { i + j })
    inspect(matrix, content=>
    #||0, 1, 2|
    #||1, 2, 3|
    )
    let transpose = matrix.to_transpose()
    transpose.map_inplace(fn(x) { x + 1 })
    inspect(transpose, content=>
    #||1, 2|
    #||2, 3|
    #||3, 4|
    )

    Transpose::map_row_inplace

    #alias(map_row_in_place, deprecated="`map_row_in_place` is deprecated, use `map_row_inplace` instead")
    fn[T] Transpose::map_row_inplace(self : Transpose[T], row : Int, f : (T) -> T) -> Unit

    Applies a transformation function to each element in the specified row of the transposed matrix in-place.

    Parameters:

    • self : The transposed matrix to modify.
    • row : The index of the row to transform.
    • f : The transformation function to apply to each element in the row.

    Panics if row is out of bounds (less than 0 or greater than or equal to the number of rows).

    Example:

    let matrix = @mutable.Matrix::make(3, 2, (i, j) => i + j)
    let transposed = matrix.to_transpose()
    // Double all elements in row 1
    transposed.map_row_inplace(1, (x) => x * 2)

    Transpose::materialize

    fn[T] Transpose::materialize(self : Transpose[T]) -> Matrix[T]

    Converts a transpose view into a regular matrix by creating a new matrix with the transposed data layout.

    Parameters:

    • self : The transpose view to materialize.

    Returns a new Matrix[T] containing the same data as the transpose view but with the elements physically rearranged in the transposed layout.

    Example:

    let original = Matrix::make(2, 3, (i, j) => i * 3 + j)
    inspect(original, content=
    #||0, 1, 2|
    #||3, 4, 5|
    )
    let transposed = original.to_transpose()
    let materialized = transposed.materialize()
    // materialized is now a 3x2 matrix with transposed layout
    inspect(materialized, content=
    #||0, 3|
    #||1, 4|
    #||2, 5|
    )

    Transpose::neg

    fn[T : Neg] Transpose::neg(a : Transpose[T]) -> Transpose[T]

    Transpose::row

    fn[T] Transpose::row(self : Transpose[T]) -> Int

    Returns the number of rows in the transpose matrix.

    Parameters:

    • self : The transpose matrix.

    Returns the number of rows, which equals the number of columns in the original matrix.

    Example:

    let m = Matrix::make(3, 4, (i, j) => i + j)
    let t = m.to_transpose()
    inspect(t.row(), content="4") // Original matrix has 4 columns, so transpose has 4 rows

    Transpose::row_to_array

    fn[T] Transpose::row_to_array(self : Transpose[T], row : Int) -> Array[T]

    Extracts the specified row of the transpose matrix as an array.

    Parameters:

    • self : The transpose matrix to extract from.
    • row : The index of the row to extract.

    Returns an array containing all elements in the specified row.

    Panics if row is out of bounds (negative or greater than or equal to the number of rows in the transpose matrix).

    Transpose::row_to_vector

    fn[T] Transpose::row_to_vector(self : Transpose[T], row : Int) -> Vector[T]

    Extracts the specified row from the transposed matrix and returns it as a vector.

    Parameters:

    • self : The transposed matrix to extract the row from.
    • row : The index of the row to extract (0-based indexing).

    Returns a Vector[T] containing the elements from the specified row of the transposed matrix.

    Transpose::scale

    fn[T : Mul] Transpose::scale(self : Transpose[T], cst : T) -> Transpose[T]

    Scales all elements of the transposed matrix by a constant factor.

    Parameters:

    • self : The transposed matrix to scale.
    • constant : The scaling factor to multiply each element by.

    Returns a new transposed matrix where each element is multiplied by the scaling factor.

    Example:

    let matrix = Matrix::make(2, 3, (i, j) => i + j)
    inspect(matrix, content=
    #||0, 1, 2|
    #||1, 2, 3|
    )
    let transpose = matrix.to_transpose()
    inspect(transpose, content=
    #||0, 1|
    #||1, 2|
    #||2, 3|
    )
    let scaled = transpose.scale(3)
    inspect(scaled, content=
    #||0, 3|
    #||3, 6|
    #||6, 9|
    )
    // Each element in the result will be 3 times the corresponding element in the original transpose

    Transpose::set

    fn[T] Transpose::set(self : Transpose[T], row : Int, col : Int, elem : T) -> Unit

    Transpose::sub

    fn[T : Add + Neg] Transpose::sub(a : Transpose[T], b : Transpose[T]) -> Transpose[T]

    Transpose::swap_cols

    fn[T] Transpose::swap_cols(self : Transpose[T], c1 : Int, c2 : Int) -> Unit

    Swaps two columns in the transpose matrix.

    Parameters:

    • self : The transpose matrix to modify.
    • c1 : The index of the first column to swap.
    • c2 : The index of the second column to swap.

    Transpose::swap_rows

    fn[T] Transpose::swap_rows(self : Transpose[T], r1 : Int, r2 : Int) -> Unit

    Swaps two rows in the transpose matrix.

    Parameters:

    • self : The transpose matrix to modify.
    • r1 : The index of the first row to swap.
    • r2 : The index of the second row to swap.

    Transpose::to_string

    fn[T : Show] Transpose::to_string(self : Transpose[T]) -> String

    Transpose::transpose

    fn[T] Transpose::transpose(self : Transpose[T]) -> Matrix[T]

    Returns the underlying matrix by converting the transposed matrix back to its original form.

    Parameters:

    • self : The transposed matrix to convert.

    Returns the original Matrix[T] that was transposed to create this Transpose[T].

    Example:

    let m = Matrix::make(2, 3, (i, j) => i + j)
    let t = m.to_transpose()
    let original = t.transpose()
    inspect(original == m, content="true")

    Transpose::vertical_combine

    fn[T] Transpose::vertical_combine(self : Transpose[T], other : Transpose[T]) -> Transpose[T]

    Vertically combines this transposed matrix with another transposed matrix, placing the other matrix below this matrix.

    Parameters:

    • self : The transposed matrix to be placed on top.
    • other : The transposed matrix to be placed below this matrix.

    Returns a new transposed matrix with the combined content.

    Example:

    let m1 = Matrix::make(2, 3, (i, j) => i + j)
    let m2 = Matrix::make(2, 3, (i, j) => (i + j) * 10)
    let t1 = m1.to_transpose()
    let t2 = m2.to_transpose()
    let combined = t1.vertical_combine(t2)
    inspect(combined.row(), content="6") // 3 + 3 columns becomes 6 rows in transpose
    inspect(combined.col(), content="2") // 2 rows becomes 2 columns in transpose

    Vector

    pub struct Vector[A](Array[A])

    A one-dimensional array-like container for mathematical vector operations.

    Example:

    let v1 = @mutable.Vector::make(3, 0.0)
    let v2 = @mutable.Vector::makei(3, fn(i) { i.to_double() })
    let v3 = @mutable.Vector::from_array([1.0, 2.0, 3.0])

    // Access elements
    inspect(v1, content="|0, 0, 0|")
    inspect(v2, content="|0, 1, 2|")
    inspect(v3, content="|1, 2, 3|")

    // Vector operations
    let sum = v2 + v3
    let scaled = v2.left_scale(2.0)

    inspect(sum, content="|1, 3, 5|")
    inspect(scaled, content="|0, 2, 4|")
    impl Add for Vector[T]
    impl Eq for Vector[T]
    impl Mul for Vector[T]
    impl Neg for Vector[T]
    impl Show for Vector[T]
    impl Arbitrary for Vector[T]

    Vector::add

    fn[T : Add] Vector::add(self : Vector[T], other : Vector[T]) -> Vector[T]

    Vector::add_constant

    fn[T : Add] Vector::add_constant(self : Vector[T], cst : T) -> Vector[T]

    Adds a constant value to each element of the vector.

    Parameters:

    • self : The vector to which the constant will be added.
    • cst : The constant value to add to each element.

    Returns a new vector with the constant added to each element of the original vector.

    Example:

    let v = Vector::from_array([1, 2, 3])
    let result = v.add_constant(5)
    inspect(result, content="|6, 7, 8|")

    Vector::at

    #alias("_[_]")
    fn[A] Vector::at(self : Vector[A], index : Int) -> A

    Retrieves the element at the specified index from the vector.

    Parameters:

    • self : The vector to access.
    • index : The zero-based index of the element to retrieve.

    Returns the element at the specified index.

    Panics if the index is out of bounds (less than 0 or greater than or equal to the vector's length).

    Example:

    let v = @mutable.Vector::from_array([1, 2, 3, 4, 5])
    inspect(v[2], content="3")

    Vector::copy

    fn[A] Vector::copy(self : Vector[A]) -> Vector[A]

    Creates a deep copy of the vector.

    Parameters:

    • self : The vector to be copied.

    Returns a new vector containing the same elements as the original vector.

    Example:

    let original = @mutable.Vector::makei(3, fn(i) { i * 2 })
    let copied = original.copy()
    original[0] = 100
    inspect(copied[0], content="0") // copied is independent of original

    Vector::dot

    Computes the dot product of two vectors.

    Parameters:

    • self : The first vector.
    • other : The second vector.

    Returns the dot product of the two vectors.

    Panics:
    • If the two vectors have different lengths.

    Vector::equal

    fn[T : Eq] Vector::equal(self : Vector[T], other : Vector[T]) -> Bool

    Vector::from_array

    fn[A] Vector::from_array(a : Array[A]) -> Vector[A]

    Converts an array into a vector.

    Parameters:

    • array : The array to be converted into a vector.

    Returns a new vector containing the elements from the input array.

    Example:

    let arr = [1, 2, 3, 4, 5]
    let vec = Vector::from_array(arr)
    inspect(vec, content="|1, 2, 3, 4, 5|")

    Vector::iter

    fn[T] Vector::iter(self : Vector[T]) -> Iter[T]

    Returns an iterator over the elements of the vector.

    Parameters:

    • self : The vector to iterate over.

    Returns an Iter[T] that yields each element of the vector in order.

    Vector::left_scale

    fn[A : Mul] Vector::left_scale(self : Vector[A], scalar : A) -> Vector[A]

    Scales the vector by multiplying each component by the given scalar from the left.

    Parameters:

    • self : The vector to be scaled.
    • scalar : The scalar value to multiply each component of the vector.

    Returns a new vector where each component is multiplied by the scalar.

    Example:

    let v = Vector::from_array([1, 2, 3])
    let scaled = v.left_scale(2)
    inspect(scaled, content="|2, 4, 6|")
    // scaled is now equivalent to Vector::from_array([2, 4, 6])

    Vector::left_scale_inplace

    #alias(left_scale_in_place, deprecated="`left_scale_in_place` is deprecated, use `left_scale_inplace` instead")
    fn[A : Mul] Vector::left_scale_inplace(self : Vector[A], scalar : A) -> Unit

    Scales the vector in-place by multiplying each element with the given scalar.

    Parameters:

    • self : The vector to be scaled in-place.
    • scalar : The scalar value to multiply each element of the vector with.

    Example:

    let v = @mutable.Vector::from_array([1, 2, 3])
    v.left_scale_inplace(2)
    inspect(v, content="|2, 4, 6|")

    Vector::length

    fn[A] Vector::length(self : Vector[A]) -> Int

    Returns the number of elements in the vector.

    Parameters:

    • self : The vector whose length is to be determined.

    Returns the number of elements in the vector as an Int.

    Example:

    let v = @mutable.Vector::make(5, 42)
    inspect(v.length(), content="5")

    Vector::lerp

    fn[T :
    MulMonoid
    + Add + Neg + Mul +
    One
    ] Vector::lerp(self : Vector[T], other : Vector[T], alpha : T) -> Vector[T]

    Performs linear interpolation between two vectors.

    Parameters:

    • self : The starting vector.
    • other : The ending vector.
    • alpha : The interpolation parameter, where 0 returns self and 1 returns other.

    Returns a new vector that is the linear interpolation between self and other based on the alpha parameter.

    Example:

    let v1: Vector[Double] = Vector::from_array([1, 2, 3])
    let v2: Vector[Double] = Vector::from_array([4, 5, 6])
    let result = v1.lerp(v2, 0.5)
    inspect(result, content="|2.5, 3.5, 4.5|")

    Vector::lin_comb

    fn[T : Mul + Add +
    Zero
    ] Vector::lin_comb(weights : Array[T], vectors : Array[Vector[T]]) -> Vector[T]

    Vector::make

    fn[A] Vector::make(n : Int, elem : A) -> Vector[A]

    Creates a new vector of the specified length with all elements initialized to the same value.

    Parameters:

    • n : The length of the vector to create.
    • elem : The value to initialize all elements with.

    Returns a new vector of length n where each element is elem.

    Example:

    let v = Vector::make(3, 42)
    inspect(v, content="|42, 42, 42|")

    Vector::makei

    fn[A] Vector::makei(n : Int, f : (Int) -> A) -> Vector[A]

    Creates a vector of length n by applying the given function to each index.

    Parameters:

    • n : The length of the vector to create.
    • f : A function that takes an index and returns the element at that position.

    Returns a new vector where each element at index i is the result of calling f(i).

    Examples:

    // Create a vector [0, 1, 4, 9, 16] (squares of indices)
    let squares = @mutable.Vector::makei(5, fn(i) { i * i })
    inspect(squares, content="|0, 1, 4, 9, 16|")
    // Create a vector [2, 4, 6, 8] (even numbers starting from 2)
    let evens = @mutable.Vector::makei(4, fn(i) { 2 * (i + 1) })
    inspect(evens, content="|2, 4, 6, 8|")

    Vector::map

    fn[A, B] Vector::map(self : Vector[A], f : (A) -> B) -> Vector[B]

    Transforms each element in the vector using the provided mapping function.

    Parameters:

    • self : The vector to transform.
    • f : The mapping function to apply to each element.

    Returns a new vector containing the transformed elements.

    Example:

    let v = Vector::from_array([1, 2, 3, 4])
    let doubled = v.map(fn(x) { x * 2 })
    inspect(doubled, content="|2, 4, 6, 8|")

    Vector::map_inplace

    #alias(map_in_place, deprecated="`map_in_place` is deprecated, use `map_inplace` instead")
    fn[A] Vector::map_inplace(self : Vector[A], f : (A) -> A) -> Unit

    Applies a function to each element of the vector in place, modifying the original vector.

    Parameters:

    • self : The vector to be modified.
    • f : The function to apply to each element. Takes an element of type A and returns a new element of type A.

    Example:

    let v = @mutable.Vector::from_array([1, 2, 3, 4])
    v.map_inplace(fn(x) { x * 2 })
    inspect(v, content="|2, 4, 6, 8|")

    Vector::mul

    fn[T : Mul] Vector::mul(self : Vector[T], other : Vector[T]) -> Vector[T]

    Vector::neg

    fn[T : Neg] Vector::neg(self : Vector[T]) -> Vector[T]

    Vector::right_scale

    fn[A : Mul] Vector::right_scale(self : Vector[A], scalar : A) -> Vector[A]

    Multiplies each element of a vector by a scalar value from the right.

    Parameters:

    • self : The vector to be scaled.
    • scalar : The scalar value to multiply each vector element by.

    Returns a new vector where each element is the result of multiplying the corresponding element in the input vector by the scalar.

    Example:

    let v = Vector::from_array([1, 2, 3])
    let scaled = v.right_scale(2)
    inspect(scaled, content="|2, 4, 6|")

    Vector::right_scale_inplace

    #alias(right_scale_in_place, deprecated="`right_scale_in_place` is deprecated, use `right_scale_inplace` instead")
    fn[A : Mul] Vector::right_scale_inplace(self : Vector[A], scalar : A) -> Unit

    Multiplies each element of the vector by a scalar value in-place, modifying the original vector.

    Parameters:

    • self : The vector to be modified.
    • scalar : The scalar value to multiply each vector element by.

    Example:

    let v = @mutable.Vector::from_array([1, 2, 3])
    v.right_scale_inplace(2)
    // v is now [2, 4, 6]

    Vector::scaled_matrix

    fn[T :
    Zero
    ] Vector::scaled_matrix(self : Vector[T]) -> Matrix[T]

    Creates a diagonal matrix where each diagonal element is the corresponding element from the vector.

    Parameters:

    • self : The vector whose elements will form the diagonal of the resulting matrix.

    Returns a square matrix of size n×n (where n is the length of the vector) with the vector elements on the main diagonal and zeros elsewhere.

    Example:

    let v = Vector::from_array([1, 2, 3])
    let m = v.scaled_matrix()
    inspect(m, content=(
    #||1, 0, 0|
    #||0, 2, 0|
    #||0, 0, 3|
    ))

    Vector::set

    #alias("_[_]=_")
    fn[A] Vector::set(self : Vector[A], index : Int, value : A) -> Unit

    Sets the element at the specified index in the vector to the given value.

    Parameters:

    • self : The vector to modify.
    • index : The zero-based index of the element to set.
    • value : The new value to assign to the element at the specified index.

    Panics if index is out of bounds (negative or greater than or equal to the vector length).

    Example:

    let v = @mutable.Vector::make(3, 0) // Creates [0, 0, 0]
    v[1] = 42
    // v is now [0, 42, 0]

    Vector::tensor_product

    fn[T : Mul] Vector::tensor_product(self : Vector[T], other : Vector[T]) -> Matrix[T]

    Computes the tensor product (outer product) of two vectors.

    Parameters:

    • self : The first vector.
    • other : The second vector.

    Returns a matrix where element at position (i, j) is the product of the i-th element of the first vector and the j-th element of the second vector.

    Example:

    let v1 = Vector::from_array([1, 2])
    let v2 = Vector::from_array([3, 4, 5])
    let result = v1.tensor_product(v2)
    inspect(result, content=(
    #||3, 4, 5|
    #||6, 8, 10|
    ))

    Vector::to_col_matrix

    fn[T] Vector::to_col_matrix(self : Vector[T]) -> Matrix[T]

    Converts a vector to a column matrix representation.

    Parameters:

    • self : The vector to convert to a column matrix.

    Returns a matrix with the same number of rows as the vector length and exactly one column, where each element of the vector becomes a row in the resulting matrix.

    Example:

    let v = Vector::from_array([1, 2, 3])
    let col_matrix = v.to_col_matrix()
    // Results in a 3x1 matrix:
    // |1|
    // |2|
    // |3|
    inspect(col_matrix, content=(
    #||1|
    #||2|
    #||3|
    )
    )

    Vector::to_row_matrix

    fn[T] Vector::to_row_matrix(self : Vector[T]) -> Matrix[T]

    Converts a vector to a row matrix with a single row.

    Parameters:

    • self : The vector to convert to a row matrix.

    Returns a matrix with one row and columns equal to the length of the input vector, where the single row contains all elements of the vector in order.

    Example:

    let v = Vector::from_array([1, 2, 3, 4])
    let row_matrix = v.to_row_matrix()
    // Creates a 1x4 matrix: |1 2 3 4|
    inspect(row_matrix, content=(
    #||1, 2, 3, 4|
    )
    )

    Vector::to_string

    fn[T : Show] Vector::to_string(self : Vector[T]) -> String

    Vector::zip_with

    fn[A, U, V] Vector::zip_with(self : Vector[A], other : Vector[U], f : (A, U) -> V) -> Vector[V]

    Combines two vectors element-wise using a provided function to produce a new vector.

    Parameters:

    • self : The first input vector of type A.
    • other : The second input vector of type U.
    • f : A function that takes an element from the first vector and an element from the second vector, and returns a new element of type V.

    Returns a new vector where each element is the result of applying the function f to the corresponding elements from both input vectors.

    Panics if the two input vectors have different lengths.

    Example:

    let v1 = Vector::from_array([1, 2, 3])
    let v2 = Vector::from_array([4, 5, 6])
    let result = v1.zip_with(v2, fn(a, b) { a + b })
    inspect(result, content="|5, 7, 9|")

    identity

    Creates an identity matrix of the specified size.

    Parameters:

    • size : The number of rows and columns of the identity matrix.

    Returns a square matrix where diagonal elements are one and all other elements are zero.

    Panics if size is negative.

    Example:

    let id : Matrix[Int] = @mutable.identity(3)
    inspect(id, content="|1, 0, 0|\n|0, 1, 0|\n|0, 0, 1|")

    lin_comb

    fn[T : Add + Mul] lin_comb(scalar_a : T, self : Vector[T], scalar_b : T, other : Vector[T]) -> Vector[T]

    Computes a linear combination of two vectors.

    Parameters:

    • scalar_a : The scalar coefficient for the first vector.
    • vector_a : The first vector in the linear combination.
    • scalar_b : The scalar coefficient for the second vector.
    • vector_b : The second vector in the linear combination.

    Returns a new vector that is the linear combination scalar_a * vector_a +scalar_b * vector_b.

    Example:

    let v1 = Vector::from_array([1, 2, 3])
    let v2 = Vector::from_array([4, 5, 6])
    let result = lin_comb(2, v1, 3, v2)
    inspect(result, content="|14, 19, 24|")