Matrix

Symbolica documentation for getting started, symbolic expressions, numerical evaluation, pattern matching, and APIs in Python and Rust.

Matrix

Matrix()

A matrix with rational polynomial coefficients.

Methods

Name Description
__add__ Add two matrices self and rhs, returning the result.
__copy__ Copy the matrix.
__eq__ Compare two matrices.
__getitem__ Get the entry at position key in the matrix.
__matmul__ Matrix multiply self and rhs, returning the result.
__mul__ Matrix multiply self and rhs, returning the result.
__neg__ Negate the matrix, returning the result.
__neq__ Compare two matrices.
__new__ Create a new zeroed matrix with nrows rows and ncols columns.
__rmatmul__ Matrix multiply rhs and self, returning the result.
__rmul__ Matrix multiply rhs and self, returning the result.
__str__ Print the matrix in a human-readable format.
__sub__ Subtract matrix rhs from self, returning the result.
__truediv__ Divide this matrix by scalar rhs and return the result.
augment Augment the matrix with another matrix, e.g
content Get the content, i.e., the GCD of the coefficients.
det Return the determinant of the matrix.
eye Create a new matrix with the scalars diag on the main diagonal and zeroes elsewhere.
format Convert the matrix into a human-readable string, with tunable settings.
from_linear Create a new matrix from a 1-dimensional vector of scalars.
from_nested Create a new matrix from a 2-dimensional vector of scalars.
identity Create a new square matrix with nrows rows and ones on the main diagonal and zeroes elsewhere.
inv Return the inverse of the matrix, if it exists.
is_diagonal Return true iff every non- main diagonal entry in the matrix is zero.
is_zero Return true iff every entry in the matrix is zero.
map Apply a function f to every entry of the matrix.
ncols Get the number of columns in the matrix.
nrows Get the number of rows in the matrix.
primitive_part Construct the same matrix, but with the content removed.
row_reduce Row-reduce the first max_col columns of the matrix in-place using Gaussian elimination and return the rank.
solve Solve A * x = b for x, where A is the current matrix.
solve_any Solve A * x = b for x, where A is the current matrix and return any solution if the system is underdetermined.
split_col Split the matrix into two matrices at column col.
swap_cols Swap columns i and j of the matrix in-place.
swap_rows Swap rows i and j of the matrix in-place, starting from column start.
to_latex Convert the matrix into a LaTeX string.
transpose Return the transpose of the matrix.
vec Create a new column vector from a list of scalars.

__add__

Matrix.__add__(rhs: Matrix) -> Matrix

Add two matrices self and rhs, returning the result.

Parameters

  • rhs (Matrix) The right-hand-side operand.

__copy__

Matrix.__copy__() -> Matrix

Copy the matrix.

__eq__

Matrix.__eq__(other: Matrix) -> bool

Compare two matrices.

Parameters

  • other (Matrix) The other operand to combine or compare with.

__getitem__

Matrix.__getitem__(key: tuple[int, int]) -> RationalPolynomial

Get the entry at position key in the matrix.

Parameters

  • key (tuple[int, int]) The (row, column) index of the entry to retrieve.

__matmul__

Matrix.__matmul__(rhs: Matrix | RationalPolynomial | Polynomial | Expression | int) -> Matrix

Matrix multiply self and rhs, returning the result.

Parameters

  • rhs (Matrix | RationalPolynomial | Polynomial | Expression | int) The right-hand-side operand.

__mul__

Matrix.__mul__(rhs: Matrix | RationalPolynomial | Polynomial | Expression | int) -> Matrix

Matrix multiply self and rhs, returning the result.

Parameters

  • rhs (Matrix | RationalPolynomial | Polynomial | Expression | int) The right-hand-side operand.

__neg__

Matrix.__neg__() -> Matrix

Negate the matrix, returning the result.

__neq__

Matrix.__neq__(other: Matrix) -> bool

Compare two matrices.

Parameters

  • other (Matrix) The other operand to combine or compare with.

__new__

Matrix.__new__(nrows: int, ncols: int) -> Matrix

Create a new zeroed matrix with nrows rows and ncols columns.

Parameters

  • nrows (int) The number of rows.
  • ncols (int) The number of columns.

__rmatmul__

Matrix.__rmatmul__(rhs: RationalPolynomial | Polynomial | Expression | int) -> Matrix

Matrix multiply rhs and self, returning the result.

Parameters

  • rhs (RationalPolynomial | Polynomial | Expression | int) The right-hand-side operand.

__rmul__

Matrix.__rmul__(rhs: RationalPolynomial | Polynomial | Expression | int) -> Matrix

Matrix multiply rhs and self, returning the result.

Parameters

  • rhs (RationalPolynomial | Polynomial | Expression | int) The right-hand-side operand.

__str__

Matrix.__str__() -> str

Print the matrix in a human-readable format.

__sub__

Matrix.__sub__(rhs: Matrix) -> Matrix

Subtract matrix rhs from self, returning the result.

Parameters

  • rhs (Matrix) The right-hand-side operand.

__truediv__

Matrix.__truediv__(rhs: RationalPolynomial | Polynomial | Expression | int) -> Matrix

Divide this matrix by scalar rhs and return the result.

Parameters

  • rhs (RationalPolynomial | Polynomial | Expression | int) The right-hand-side operand.

augment

Matrix.augment(b: Matrix) -> Matrix

Augment the matrix with another matrix, e.g. create [A B] from matrix A and B.

Returns an error when the matrices do not have the same number of rows.

Parameters

  • b (Matrix) The matrix to append as additional columns.

content

Matrix.content() -> RationalPolynomial

Get the content, i.e., the GCD of the coefficients.

det

Matrix.det() -> RationalPolynomial

Return the determinant of the matrix.

eye

Matrix.eye(diag: Sequence[RationalPolynomial | Polynomial | Expression | int]) -> Matrix

Create a new matrix with the scalars diag on the main diagonal and zeroes elsewhere.

Parameters

  • diag (Sequence[RationalPolynomial | Polynomial | Expression | int]) The entries to place on the diagonal.

format

Matrix.format(
    mode: PrintMode = PrintMode.Symbolica,
    max_line_length: int | None = 80,
    indentation: int = 4,
    fill_indented_lines: bool = True,
    pretty_matrix = True,
    number_thousands_separator: str | None = None,
    multiplication_operator: str = '*',
    double_star_for_exponentiation: bool = False,
    square_brackets_for_function: bool = False,
    function_brackets: tuple[str, str] = ('(', ')'),
    num_exp_as_superscript: bool = True,
    precision: int | None = None,
    show_namespaces: bool = False,
    hide_namespace: str | None = None,
    include_attributes: bool = False,
    max_terms: int | None = None,
    custom_print_mode: int | None = None,
) -> str

Convert the matrix into a human-readable string, with tunable settings.

Parameters

  • mode (PrintMode) The mode that controls how the input is interpreted or formatted.
  • max_line_length (int | None) The preferred maximum line length before wrapping.
  • indentation (int) The number of spaces used for wrapped lines.
  • fill_indented_lines (bool) Whether wrapped lines should be padded to the configured indentation.
  • pretty_matrix (Any) Whether matrices should be printed in the pretty multi-line layout.
  • number_thousands_separator (str | None) The separator inserted between groups of digits in printed integers.
  • multiplication_operator (str) The string used to print multiplication.
  • double_star_for_exponentiation (bool) Whether exponentiation should be printed as ** instead of ^.
  • square_brackets_for_function (bool) Whether function calls should be printed with square brackets.
  • function_brackets (tuple[str, str]) The opening and closing brackets used when printing function arguments.
  • num_exp_as_superscript (bool) Whether small integer exponents should be printed as superscripts.
  • precision (int | None) The decimal precision used when printing numeric coefficients.
  • show_namespaces (bool) Whether namespaces should be included in the formatted output.
  • hide_namespace (str | None) A namespace prefix to omit from printed symbol names.
  • include_attributes (bool) Whether symbol attributes should be included in the printed output.
  • max_terms (int | None) The maximum number of terms to print before truncating the output.
  • custom_print_mode (int | None) A custom print-mode identifier passed through to custom print callbacks.

from_linear

Matrix.from_linear(
    nrows: int,
    ncols: int,
    entries: Sequence[RationalPolynomial | Polynomial | Expression | int],
) -> Matrix

Create a new matrix from a 1-dimensional vector of scalars.

Parameters

  • nrows (int) The number of rows.
  • ncols (int) The number of columns.
  • entries (Sequence[RationalPolynomial | Polynomial | Expression | int]) The matrix entries in row-major order.

from_nested

Matrix.from_nested(entries: Sequence[Sequence[RationalPolynomial | Polynomial | Expression | int]]) -> Matrix

Create a new matrix from a 2-dimensional vector of scalars.

Parameters

  • entries (Sequence[Sequence[RationalPolynomial | Polynomial | Expression | int]]) The nested row entries of the matrix.

identity

Matrix.identity(nrows: int) -> Matrix

Create a new square matrix with nrows rows and ones on the main diagonal and zeroes elsewhere.

Parameters

  • nrows (int) The number of rows.

inv

Matrix.inv() -> Matrix

Return the inverse of the matrix, if it exists.

is_diagonal

Matrix.is_diagonal() -> bool

Return true iff every non- main diagonal entry in the matrix is zero.

is_zero

Matrix.is_zero() -> bool

Return true iff every entry in the matrix is zero.

map

Matrix.map(f: Callable[[RationalPolynomial], RationalPolynomial]) -> Matrix

Apply a function f to every entry of the matrix.

Parameters

  • f (Callable[[RationalPolynomial], RationalPolynomial]) The callback or function to apply.

ncols

Matrix.ncols() -> int

Get the number of columns in the matrix.

nrows

Matrix.nrows() -> int

Get the number of rows in the matrix.

primitive_part

Matrix.primitive_part() -> Matrix

Construct the same matrix, but with the content removed.

row_reduce

Matrix.row_reduce(max_col: int) -> int

Row-reduce the first max_col columns of the matrix in-place using Gaussian elimination and return the rank.

Parameters

  • max_col (int) The highest column index included in row reduction.

solve

Matrix.solve(b: Matrix) -> Matrix

Solve A * x = b for x, where A is the current matrix.

Parameters

  • b (Matrix) The right-hand-side matrix b in A * x = b.

solve_any

Matrix.solve_any(b: Matrix) -> Matrix

Solve A * x = b for x, where A is the current matrix and return any solution if the system is underdetermined.

Parameters

  • b (Matrix) The right-hand-side matrix b in A * x = b.

split_col

Matrix.split_col(col: int) -> tuple[Matrix, Matrix]

Split the matrix into two matrices at column col.

Parameters

  • col (int) The column index at which to split the matrix.

swap_cols

Matrix.swap_cols(i: int, j: int) -> None

Swap columns i and j of the matrix in-place.

Parameters

  • i (int) The first index.
  • j (int) The second index.

swap_rows

Matrix.swap_rows(i: int, j: int, start: int = 0) -> None

Swap rows i and j of the matrix in-place, starting from column start.

Parameters

  • i (int) The first index.
  • j (int) The second index.
  • start (int) The starting index or value.

to_latex

Matrix.to_latex() -> str

Convert the matrix into a LaTeX string.

transpose

Matrix.transpose() -> Matrix

Return the transpose of the matrix.

vec

Matrix.vec(entries: Sequence[RationalPolynomial | Polynomial | Expression | int]) -> Matrix

Create a new column vector from a list of scalars.

Parameters

  • entries (Sequence[RationalPolynomial | Polynomial | Expression | int]) The entries of the column vector, from top to bottom.