algolib.maths.algebra package
Submodules
algolib.maths.algebra.matrix_dense module
- class algolib.maths.algebra.matrix_dense.MatrixDense(rows: Sequence[Sequence[int | float]])[source]
Bases:
object
A simple dense matrix (row-major) without external dependencies.
Designed for correctness and clarity (teaching/learning oriented). Not optimized for large-scale numerical workloads.
- Parameters:
rows (Sequence[Sequence[Number]]) – Row-major data. Must be rectangular (all rows same length).
- Raises:
InvalidTypeError – If rows are not sequences of numbers.
InvalidValueError – If matrix is empty or not rectangular.
Examples
>>> A = MatrixDense([[1, 2], [3, 4]]) >>> B = MatrixDense.identity(2) >>> C = (A * B).rows == A.rows True
- T() MatrixDense [source]
Transpose.
- copy() MatrixDense [source]
Shallow copy (rows are tuples; safe).
- det() float [source]
Determinant via Gaussian elimination with partial pivoting (O(n^3)).
- Raises:
InvalidValueError – If matrix is not square.
- equals(other: MatrixDense, tol: float = 1e-12) bool [source]
Element-wise comparison with tolerance for floats.
- static eye(n: int) MatrixDense
Alias of
identity()
.
- static from_rows(rows: Sequence[Sequence[int | float]]) MatrixDense [source]
Construct from row-major data (alias of the constructor).
- static identity(n: int) MatrixDense [source]
Return the \(n \times n\) identity matrix.
- inv() MatrixDense [source]
Inverse via Gauss-Jordan elimination (O(n^3)).
- Raises:
InvalidValueError – If matrix is not square or singular.
- matvec(v: Sequence[int | float]) List[float] [source]
Matrix-vector product (Ax).
- Parameters:
v (Sequence[Number]) – Vector of length equal to number of columns.
- Returns:
Resulting vector.
- Return type:
list[float]
- rows: Tuple[Tuple[int | float, ...], ...]
- property shape: Tuple[int, int]
(n_rows, n_cols)
- static zeros(n_rows: int, n_cols: int) MatrixDense [source]
Return an \((n_{\mathrm{rows}} × n_{\mathrm{cols}})\) zero matrix.
algolib.maths.algebra.polynomial module
Lightweight univariate polynomial with real coefficients.
Notes
Coefficients are stored in ascending degree order:
p(x) = c0 + c1*x + … + cn*x**n
.The public API is immutable; the internal representation is a tuple of
float
.Supported operations include addition, subtraction, multiplication (convolution), evaluation (Horner scheme), derivative, and antiderivative (indefinite integral).
Examples
>>> p = Polynomial([1, 2, 3]) # 1 + 2x + 3x^2
>>> q = Polynomial.identity() # 1
>>> (p * q).coeffs == p.coeffs
True
>>> p.derivative().coeffs # d/dx (1 + 2x + 3x^2) = 2 + 6x
(2.0, 6.0)
- class algolib.maths.algebra.polynomial.Polynomial(coeffs: Iterable[int | float])[source]
Bases:
object
Univariate polynomial with real coefficients.
- Parameters:
coeffs (Sequence[Number]) – Coefficients in ascending degree order:
[c0, c1, ..., cn]
represents \(\sum_{k=0}^n c_k x^k\).- Raises:
InvalidTypeError – If any coefficient is not a real number.
InvalidValueError – If
coeffs
is empty.
Notes
Construction enforces a canonical form by stripping trailing zeros; the zero polynomial is thus represented as
(0.0,)
and has degree 0.All operations return new
Polynomial
instances (immutable API).
- coeffs: Tuple[float, ...]
- static constant(c: int | float) Polynomial [source]
Return a constant polynomial
p(x) = c
.
- property degree: int
Degree of the polynomial (
len(coeffs) - 1
). By convention,deg(0) = 0
.
- derivative() Polynomial [source]
Return the analytical derivative \(p'(x)\).
Notes
If \(p(x) = a_0 + a_1 x + \cdots + a_n x^n\), then \(p'(x) = a_1 + 2 a_2 x + \cdots + n a_n x^{n-1}\).
- static identity() Polynomial [source]
Return the multiplicative identity polynomial
1
.
- integral(c0: int | float = 0.0) Polynomial [source]
Return an antiderivative \(\int p(x)\,dx\) with constant term
c0
.- Parameters:
c0 (Number, default=0.0) – Constant of integration (the coefficient of \(x^0\)).
- Returns:
An antiderivative whose derivative equals
self
.- Return type:
- static zeros(deg: int) Polynomial [source]
Return the (canonical) zero polynomial.
- Parameters:
deg (int) – Requested degree (non‑negative). This value is accepted for API symmetry, but the canonical zero polynomial always has degree 0 after construction.
- Returns:
The zero polynomial.
- Return type:
Module contents
- class algolib.maths.algebra.MatrixDense(rows: Sequence[Sequence[int | float]])[source]
Bases:
object
A simple dense matrix (row-major) without external dependencies.
Designed for correctness and clarity (teaching/learning oriented). Not optimized for large-scale numerical workloads.
- Parameters:
rows (Sequence[Sequence[Number]]) – Row-major data. Must be rectangular (all rows same length).
- Raises:
InvalidTypeError – If rows are not sequences of numbers.
InvalidValueError – If matrix is empty or not rectangular.
Examples
>>> A = MatrixDense([[1, 2], [3, 4]]) >>> B = MatrixDense.identity(2) >>> C = (A * B).rows == A.rows True
- T() MatrixDense [source]
Transpose.
- copy() MatrixDense [source]
Shallow copy (rows are tuples; safe).
- det() float [source]
Determinant via Gaussian elimination with partial pivoting (O(n^3)).
- Raises:
InvalidValueError – If matrix is not square.
- equals(other: MatrixDense, tol: float = 1e-12) bool [source]
Element-wise comparison with tolerance for floats.
- static eye(n: int) MatrixDense
Alias of
identity()
.
- static from_rows(rows: Sequence[Sequence[int | float]]) MatrixDense [source]
Construct from row-major data (alias of the constructor).
- static identity(n: int) MatrixDense [source]
Return the \(n \times n\) identity matrix.
- inv() MatrixDense [source]
Inverse via Gauss-Jordan elimination (O(n^3)).
- Raises:
InvalidValueError – If matrix is not square or singular.
- matvec(v: Sequence[int | float]) List[float] [source]
Matrix-vector product (Ax).
- Parameters:
v (Sequence[Number]) – Vector of length equal to number of columns.
- Returns:
Resulting vector.
- Return type:
list[float]
- rows: Tuple[Tuple[int | float, ...], ...]
- property shape: Tuple[int, int]
(n_rows, n_cols)
- static zeros(n_rows: int, n_cols: int) MatrixDense [source]
Return an \((n_{\mathrm{rows}} × n_{\mathrm{cols}})\) zero matrix.
- class algolib.maths.algebra.Polynomial(coeffs: Iterable[int | float])[source]
Bases:
object
Univariate polynomial with real coefficients.
- Parameters:
coeffs (Sequence[Number]) – Coefficients in ascending degree order:
[c0, c1, ..., cn]
represents \(\sum_{k=0}^n c_k x^k\).- Raises:
InvalidTypeError – If any coefficient is not a real number.
InvalidValueError – If
coeffs
is empty.
Notes
Construction enforces a canonical form by stripping trailing zeros; the zero polynomial is thus represented as
(0.0,)
and has degree 0.All operations return new
Polynomial
instances (immutable API).
- coeffs: Tuple[float, ...]
- static constant(c: int | float) Polynomial [source]
Return a constant polynomial
p(x) = c
.
- property degree: int
Degree of the polynomial (
len(coeffs) - 1
). By convention,deg(0) = 0
.
- derivative() Polynomial [source]
Return the analytical derivative \(p'(x)\).
Notes
If \(p(x) = a_0 + a_1 x + \cdots + a_n x^n\), then \(p'(x) = a_1 + 2 a_2 x + \cdots + n a_n x^{n-1}\).
- static identity() Polynomial [source]
Return the multiplicative identity polynomial
1
.
- integral(c0: int | float = 0.0) Polynomial [source]
Return an antiderivative \(\int p(x)\,dx\) with constant term
c0
.- Parameters:
c0 (Number, default=0.0) – Constant of integration (the coefficient of \(x^0\)).
- Returns:
An antiderivative whose derivative equals
self
.- Return type:
- static zeros(deg: int) Polynomial [source]
Return the (canonical) zero polynomial.
- Parameters:
deg (int) – Requested degree (non‑negative). This value is accepted for API symmetry, but the canonical zero polynomial always has degree 0 after construction.
- Returns:
The zero polynomial.
- Return type: