PyLAFS Python Package
Date published: Aug 16, 2021
Last updated: Oct 22, 2022
Last updated: Oct 22, 2022
GitHub
Source code is available in the GitHub repository.
Description
PyLAFS
stands for Linear Algebra from Scratch in Python. This project implements common linear algebraic constructs in minimal Python 3, that is, without using external libraries.
PyLAFS
works best as a convenient matrix calculator that runs in the Python interpreter. Simplicy and convenience are optimized, not performance: it is designed to be used in the interpreter/console and not for development.
Below is the extensive documentation of the package’s functions and classes. This documentation will be gradually expanded with demonstrations.
Matrix Functions
det(matrix)
Returns the determinant of the input matrix.diag(matrix)
Returns vector of the input matrix diagonal elements.dim(matrix, dim_index=None)
Returns the dimension of the input matrix.is_colvector(matrix)
ReturnsTrue
if input is a column vector.is_diag(matrix)
Returns boolean of diagonal matrix test.is_invertible(matrix)
Returns boolean of invertibility test.is_lower(matrix)
Returns boolean of lower triangular matrix test.is_matrix(matrix)
ReturnsTrue
if input type is the lafs.Matrix class.is_rowvector(matrix)
ReturnsTrue
if input is a row vector.is_singular(matrix)
Returns boolean of singularity test.is_square(matrix)
Returns boolean of square matrix test.is_symmetric(matrix)
Returns boolean of symmetry test.is_triangular(matrix)
Returns boolean of triangular matrix test.is_upper(matrix)
Returns boolean of upper triangular matrix test.is_vector(matrix)
ReturnsTrue
if input is a vector.total(matrix)
Returns the sum total of all input matrix elements.trace(matrix)
Returns the trace of the input square matrix.transpose(matrix)
Returns the transpose of the input matrix.
Matrix Generators
I(n, n_col=None)
Returns an Identity Matrix of dimensions(n, n_col)
.Rx(t)
Returns rotation matrix about the first axis of anglet
.
Assumes default angle unit is in degrees.Ry(t)
Returns rotation matrix about the second axis of anglet
.
Assumes default angle unit is in degrees.Rz(t)
Returns rotation matrix about the third axis of anglet
.
Assumes default angle unit is in degrees.U(n, n_col=None)
Returns a Ones Matrix of dimensions(n, n_col)
.
If input is MatrixA
, returns a Ones Matrix of same size.Z(n, n_col=None)
Returns a Zeros Matrix of dimensions(n, n_col)
.lower(matrix)
Returns lower triangular version of input matrix.randm(n_row, n_col=None)
Temporary function for random matrix generation.randv(n)
Temporary function for random vector generation.upper(matrix)
Returns upper triangular version of input matrix.
Matrix Algorithms
inv(matrix)
Returns the direct inverse via Gaussian elimination of the input matrix.linsolve(A, b)
Returns the solution $x$ of the linear system $Ax = b$.lu(matrix)
Returns the naive LU decomposition of the matrix.nullity(matrix)
Returns the nullity of the input matrix.rank(matrix)
Returns the rank of the input matrix.ref(matrix)
Returns a row echelon form of the input matrix.rref(matrix)
Returns the reduced row echelon form of the input matrix.
Matrix Class
Mat(*args)
Shorthand call, aliased toMatrix(*args)
.Matrix(*args)
Matrix class member functions are documented below:T(self)
Returns matrix transpose.__add__(self, summand)
Defines matrix-matrix addition:<matrix> + <matrix>
__call__(self, i=None, j=None)
Returns the entire data in list format when called withMatrix()
.
Returns rowi
when called withMatrix(i)
.
Returns element(i, j)
when called withMatrix(i, j)
.__eq__(self, right)
Defines equality operator:<matrix> == <right>
__getitem__(self, row)
Accessor:A[i][j]
returns the element at rowi
and columnj
.__init__(self, *args)
Initialize self. See help(type(self)) for accurate signature.__matmul__(self, multiplicand)
Defines array-wise/Hadamard multiplication:
<matrix> @ <matrix>
__mul__(self, multiplicand)
Defines matrix-scalar and matrix-matrix left-multplication:
<matrix> * <scalar|matrix>
__neg__(self)
Defines matrix negation:-<matrix>
__pow__(self, n)
Defines matrix power:<matrix> ** <int>
__radd__(self, summand)
Handles scalar-matrix right-addition:<scalar> + <matrix>
__repr__(self)
Returns the string representation of the matrix with configurable equal column spacing.__rmul__(self, multiplier)
Defines scalar-matrix right-multplication:<scalar> * <matrix>
__rsub__(self, minuend)
Handles matrix-matrix and scalar-matrix subtraction:
<matrix> - <matrix>
and<scalar> - <matrix>
__setitem__(self, row, new_value)
Mutator:A[i][j] = a
sets to value at rowi
and columnj
to the value ofa
.__sub__(self, subtrahend)
Defines matrix-matrix and matrix-scalar subtraction:
<matrix> - <matrix>
and<matrix> - <scalar>
__truediv__(self, divisor)
Defines matrix-scalar left-division:<matrix> / <scalar>
dim(self, k=None)
Returns the Matrix dimension in a tuple(n_row, n_col)
, or a specific dimension as anint
.identity(self)
Returns identity matrix of the same dimensions.inner(self, matrix)
Returns the inner product with another matrix.r(self, d=4
)
Returns a rounded copy with precisiond
(default4
).sub(self, i_from, i_to, j_from, j_to)
Returns the submatrix for the specified range of rows and columns (inclusive bounds), i.e., $(a_{ij})$ with $i_\mathsf{from} \leq i \leq i_\mathsf{to}$ and $j_\mathsf{from} \leq j \leq j_\mathsf{to}$.swap_rows(self, row1, row2)
Swaps in-place the rows at indicesrow1
androw2
.validate_data(self, n_row, n_col, vals)
Validates dimensional correctness of data, raises errors otherwise.
Vector(*args)
Convenience function that implements a vector using theMatrix
class.
Global Variables
unit_angle
The default isdeg
, where rotation matrices evaluate in degrees.
It can also be set torad
for evaluation in radians._
In the interpreter, this variable (an underscore) stores the last value.
It serves the same function asans
orlast
in most calculators.