algolib.maths.algebra 包
子模块
algolib.maths.algebra.matrix_dense module
- class algolib.maths.algebra.matrix_dense.MatrixDense(rows: Sequence[Sequence[int | float]])[源代码]
基类:
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.
- 参数:
rows (Sequence[Sequence[Number]]) -- Row-major data. Must be rectangular (all rows same length).
- 抛出:
InvalidTypeError -- If rows are not sequences of numbers.
InvalidValueError -- If matrix is empty or not rectangular.
示例
>>> A = MatrixDense([[1, 2], [3, 4]]) >>> B = MatrixDense.identity(2) >>> C = (A * B).rows == A.rows True
- T() MatrixDense [源代码]
Transpose.
- copy() MatrixDense [源代码]
Shallow copy (rows are tuples; safe).
- det() float [源代码]
Determinant via Gaussian elimination with partial pivoting (O(n^3)).
- 抛出:
InvalidValueError -- If matrix is not square.
- equals(other: MatrixDense, tol: float = 1e-12) bool [源代码]
Element-wise comparison with tolerance for floats.
- static eye(n: int) MatrixDense
Alias of
identity()
.
- static from_rows(rows: Sequence[Sequence[int | float]]) MatrixDense [源代码]
Construct from row-major data (alias of the constructor).
- static identity(n: int) MatrixDense [源代码]
Return the \(n \times n\) identity matrix.
- inv() MatrixDense [源代码]
Inverse via Gauss-Jordan elimination (O(n^3)).
- 抛出:
InvalidValueError -- If matrix is not square or singular.
- matvec(v: Sequence[int | float]) List[float] [源代码]
Matrix-vector product (Ax).
- 参数:
v (Sequence[Number]) -- Vector of length equal to number of columns.
- 返回:
Resulting vector.
- 返回类型:
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 [源代码]
Return an \((n_{\mathrm{rows}} × n_{\mathrm{cols}})\) zero matrix.
algolib.maths.algebra.polynomial module
Lightweight univariate polynomial with real coefficients.
备注
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).
示例
>>> 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])[源代码]
基类:
object
Univariate polynomial with real coefficients.
- 参数:
coeffs (Sequence[Number]) -- Coefficients in ascending degree order:
[c0, c1, ..., cn]
represents \(\sum_{k=0}^n c_k x^k\).- 抛出:
InvalidTypeError -- If any coefficient is not a real number.
InvalidValueError -- If
coeffs
is empty.
备注
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 [源代码]
Return a constant polynomial
p(x) = c
.
- property degree: int
Degree of the polynomial (
len(coeffs) - 1
). By convention,deg(0) = 0
.
- derivative() Polynomial [源代码]
Return the analytical derivative \(p'(x)\).
备注
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 [源代码]
Return the multiplicative identity polynomial
1
.
- integral(c0: int | float = 0.0) Polynomial [源代码]
Return an antiderivative \(\int p(x)\,dx\) with constant term
c0
.- 参数:
c0 (Number, default=0.0) -- Constant of integration (the coefficient of \(x^0\)).
- 返回:
An antiderivative whose derivative equals
self
.- 返回类型:
- static zeros(deg: int) Polynomial [源代码]
Return the (canonical) zero polynomial.
- 参数:
deg (int) -- Requested degree (non‑negative). This value is accepted for API symmetry, but the canonical zero polynomial always has degree 0 after construction.
- 返回:
The zero polynomial.
- 返回类型:
Module contents
- class algolib.maths.algebra.MatrixDense(rows: Sequence[Sequence[int | float]])[源代码]
基类:
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.
- 参数:
rows (Sequence[Sequence[Number]]) -- Row-major data. Must be rectangular (all rows same length).
- 抛出:
InvalidTypeError -- If rows are not sequences of numbers.
InvalidValueError -- If matrix is empty or not rectangular.
示例
>>> A = MatrixDense([[1, 2], [3, 4]]) >>> B = MatrixDense.identity(2) >>> C = (A * B).rows == A.rows True
- T() MatrixDense [源代码]
Transpose.
- copy() MatrixDense [源代码]
Shallow copy (rows are tuples; safe).
- det() float [源代码]
Determinant via Gaussian elimination with partial pivoting (O(n^3)).
- 抛出:
InvalidValueError -- If matrix is not square.
- equals(other: MatrixDense, tol: float = 1e-12) bool [源代码]
Element-wise comparison with tolerance for floats.
- static eye(n: int) MatrixDense
Alias of
identity()
.
- static from_rows(rows: Sequence[Sequence[int | float]]) MatrixDense [源代码]
Construct from row-major data (alias of the constructor).
- static identity(n: int) MatrixDense [源代码]
Return the \(n \times n\) identity matrix.
- inv() MatrixDense [源代码]
Inverse via Gauss-Jordan elimination (O(n^3)).
- 抛出:
InvalidValueError -- If matrix is not square or singular.
- matvec(v: Sequence[int | float]) List[float] [源代码]
Matrix-vector product (Ax).
- 参数:
v (Sequence[Number]) -- Vector of length equal to number of columns.
- 返回:
Resulting vector.
- 返回类型:
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 [源代码]
Return an \((n_{\mathrm{rows}} × n_{\mathrm{cols}})\) zero matrix.
- class algolib.maths.algebra.Polynomial(coeffs: Iterable[int | float])[源代码]
基类:
object
Univariate polynomial with real coefficients.
- 参数:
coeffs (Sequence[Number]) -- Coefficients in ascending degree order:
[c0, c1, ..., cn]
represents \(\sum_{k=0}^n c_k x^k\).- 抛出:
InvalidTypeError -- If any coefficient is not a real number.
InvalidValueError -- If
coeffs
is empty.
备注
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 [源代码]
Return a constant polynomial
p(x) = c
.
- property degree: int
Degree of the polynomial (
len(coeffs) - 1
). By convention,deg(0) = 0
.
- derivative() Polynomial [源代码]
Return the analytical derivative \(p'(x)\).
备注
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 [源代码]
Return the multiplicative identity polynomial
1
.
- integral(c0: int | float = 0.0) Polynomial [源代码]
Return an antiderivative \(\int p(x)\,dx\) with constant term
c0
.- 参数:
c0 (Number, default=0.0) -- Constant of integration (the coefficient of \(x^0\)).
- 返回:
An antiderivative whose derivative equals
self
.- 返回类型:
- static zeros(deg: int) Polynomial [源代码]
Return the (canonical) zero polynomial.
- 参数:
deg (int) -- Requested degree (non‑negative). This value is accepted for API symmetry, but the canonical zero polynomial always has degree 0 after construction.
- 返回:
The zero polynomial.
- 返回类型: