A comprehensive Python library for vector and matrix operations with a clean, Pythonic API
Project description
MatPy
A comprehensive Python library for vector and matrix operations with a clean, Pythonic API. MatPy provides an intuitive interface for linear algebra operations, making it perfect for educational purposes, scientific computing, and mathematical applications.
Features
๐ฏ Core Components
- Vector Class: N-dimensional vectors with full operator support (2D, 3D, and beyond)
- Matrix Class: Dynamic-size matrices with comprehensive operations
- Linear System Solvers: Gaussian elimination, LU decomposition, Cramer's rule, least squares
- ODE Solvers: Systems of differential equations (homogeneous and non-homogeneous)
- Coordinate Systems: Convert between Cartesian, Polar, Spherical, and Cylindrical coordinates
- Visualization: Rich plotting capabilities with matplotlib (optional)
- Custom Error Handling: Descriptive exceptions for better debugging
- Pure Python Core: No required dependencies for core functionality
โก Vector Operations
- Basic Operations:
+,-,*,/with full operator support - Vector Products: Dot product, cross product (3D)
- Magnitude & Normalization: Length calculation and unit vectors
- Advanced Operations: Projection, rejection, reflection
- Geometric Functions: Angle calculation, distance, component-wise min/max
- Interpolation: Linear interpolation (lerp), clamping
- Parallel & Perpendicular Tests: Check vector relationships
- N-Dimensional Support: Works with 2D, 3D, 4D, and higher dimensions
- Full Python Protocols: Iteration, indexing, equality, hashing, etc.
๐ข Matrix Operations
- Arithmetic: Element-wise operations, matrix multiplication (
@), power (**) - Basic Operations: Transpose, trace, determinant, rank
- Advanced Operations: Inverse, adjugate, cofactor, matrix exponential
- Matrix Creation: Zeros, ones, identity, diagonal, from rows/columns
- Matrix Properties: Symmetric, orthogonal, singular, triangular, diagonal
- Hadamard Product: Element-wise multiplication
- Kronecker Product: Tensor product of matrices
- Row Operations: Row echelon form, reduced row echelon form
- Concatenation: Horizontal and vertical matrix joining
๐งฎ Linear Algebra Solvers
- Linear Systems: Solve Ax = b using multiple methods
- Gaussian elimination with partial pivoting
- LU decomposition
- Cramer's rule
- Least squares (for overdetermined systems)
- Differential Equations: Systems of linear ODEs
- Homogeneous systems (dx/dt = Ax)
- Non-homogeneous systems (dx/dt = Ax + b(t))
- Matrix exponential computation
- Euler method and Runge-Kutta 4
๐ Coordinate Systems
- 2D Conversions: Cartesian โ Polar โ Complex
- 3D Conversions: Cartesian โ Spherical โ Cylindrical
- Easy API:
VectorCoordinatesclass with intuitive methods
๐ Visualization (Optional)
- Vector Plotting: 2D and 3D vector arrows with labels
- Vector Fields: Visualize 2D vector fields
- Matrix Heatmaps: Color-coded matrix visualization
- Transformations: Before/after visualization of linear transformations
- Coordinate Systems: Side-by-side comparison of coordinate representations
- Customizable: Colors, labels, titles, and full matplotlib control
Installation
From Source
# Clone the repository
git clone https://github.com/njryan-boou/matpy.git
cd matpy
# Install core library
pip install .
# Or install in development mode
pip install -e .
# Install with visualization support
pip install .[viz]
# Install with all development tools
pip install .[dev]
From PyPI (Coming Soon)
# Core installation
pip install matpy-linalg
# With visualization support
pip install matpy-linalg[viz]
Quick Start
Vector Examples
from matpy.vector.core import Vector
from matpy.vector import ops
# Create vectors (n-dimensional)
v1 = Vector(3, 4, 5) # 3D vector
v2 = Vector(1, 2, 3) # 3D vector
v_2d = Vector(3, 4) # 2D vector
v_4d = Vector(1, 2, 3, 4) # 4D vector
# Arithmetic operations
v3 = v1 + v2 # Vector addition
v4 = v1 - v2 # Vector subtraction
v5 = v1 * 2 # Scalar multiplication
v6 = v1 / 2 # Scalar division
# Vector operations
dot_product = v1.dot(v2) # Dot product
cross_product = v1.cross(v2) # Cross product (3D only)
magnitude = v1.magnitude() # Magnitude/length
normalized = v1.normalize() # Unit vector
# Advanced operations
angle = ops.angle_between(v1, v2) # Angle in radians
projection = ops.projection(v1, v2)
rejection = ops.rejection(v1, v2)
reflected = ops.reflect(v1, Vector(0, 1, 0))
interpolated = ops.lerp(v1, v2, 0.5) # 50% between v1 and v2
distance = ops.distance(v1, v2)
# Component operations
min_vec = ops.component_min(v1, v2) # Element-wise minimum
max_vec = ops.component_max(v1, v2) # Element-wise maximum
clamped = ops.clamp(v1, -1, 1) # Clamp all components
# Vector tests
parallel = ops.is_parallel(v1, v2)
perpendicular = ops.is_perpendicular(v1, v2)
# Properties for 2D/3D vectors
print(v1.x, v1.y, v1.z) # Access x, y, z components
# Python protocols
print(v1) # (3.0, 4.0, 5.0)
print(repr(v1)) # <3.0, 4.0, 5.0>
print(len(v1)) # 3
print(v1[0]) # 3.0
for component in v1: # Iteration
print(component)
Matrix Examples
from matpy.matrix.core import Matrix
from matpy.matrix import ops
# Create matrices
m1 = Matrix(2, 2, [[1, 2], [3, 4]])
m2 = Matrix(2, 2, [[5, 6], [7, 8]])
# Matrix creation utilities
zeros = ops.zeros(3, 3) # 3x3 zero matrix
ones = ops.ones(2, 4) # 2x4 matrix of ones
identity = ops.identity(3) # 3x3 identity matrix
diagonal = ops.diagonal([1, 2, 3]) # 3x3 diagonal matrix
from_rows = ops.from_rows([[1, 2], [3, 4]])
from_cols = ops.from_columns([[1, 3], [2, 4]])
# Arithmetic operations
m3 = m1 + m2 # Matrix addition
m4 = m1 - m2 # Matrix subtraction
m5 = m1 * 3 # Scalar multiplication
m6 = m1 @ m2 # Matrix multiplication (@ operator)
m7 = m1 ** 3 # Matrix power
# Matrix operations
transposed = m1.transpose()
determinant = m1.determinant()
inverted = m1.inverse()
trace = m1.trace()
rank = m1.rank()
# Advanced operations
adjugate = m1.adjugate()
cofactor = m1.cofactor(0, 1)
hadamard = ops.hadamard_product(m1, m2) # Element-wise multiplication
kronecker = ops.kronecker_product(m1, m2)
rref = ops.reduced_row_echelon_form(m1)
# Matrix properties
is_square = m1.is_square()
is_symmetric = m1.is_symmetric()
is_singular = m1.is_singular()
is_invertible = m1.is_invertible()
is_orthogonal = m1.is_orthogonal()
is_diagonal = ops.is_diagonal(m1)
is_identity = ops.is_identity(m1)
# Concatenation
h_concat = ops.concatenate_horizontal(m1, m2)
v_concat = ops.concatenate_vertical(m1, m2)
# Python protocols
print(m1) # Formatted matrix output
print(f"{m1:.2f}") # Formatted to 2 decimal places
for value in m1: # Iterate over all elements
print(value)
Linear Systems
from matpy.matrix.core import Matrix
from matpy.matrix import solve
# Solve Ax = b
A = Matrix(3, 3, [[2, 1, -1], [-3, -1, 2], [-2, 1, 2]])
b = [8, -11, -3]
# Multiple solution methods
x = solve.solve_linear_system(A, b) # Gaussian elimination
x_lu = solve.solve_lu(A, b) # LU decomposition
x_cramer = solve.solve_cramer(A, b) # Cramer's rule
# Least squares for overdetermined systems
A_over = Matrix(4, 2, [[1, 1], [2, 1], [3, 1], [4, 1]])
b_over = [2, 3, 5, 4]
x_ls = solve.solve_least_squares(A_over, b_over)
# LU decomposition
L, U = solve.lu_decomposition(A)
# Matrix exponential
exp_A = solve.matrix_exponential(A, t=1.0)
Differential Equations
from matpy.matrix.core import Matrix
from matpy.matrix import solve
# Solve dx/dt = Ax
A = Matrix(2, 2, [[-2, 1], [1, -2]])
x0 = [1, 0] # Initial conditions
t = 1.0 # Time
# Homogeneous system
x_t = solve.solve_linear_ode_system_homogeneous(A, x0, t)
# Non-homogeneous system: dx/dt = Ax + b(t)
def b_func(t):
return [t, 0]
x_t_nh = solve.solve_linear_ode_system_nonhomogeneous(A, x0, b_func, t)
# Numerical methods
euler_result = solve.euler_method(A, x0, t_final=2.0, steps=100)
rk4_result = solve.runge_kutta_4(A, x0, t_final=2.0, steps=100)
Coordinate Systems
from matpy.vector.core import Vector
from matpy.vector.coordinates import VectorCoordinates
import math
# 2D Coordinate conversions
v_2d = Vector(3, 4)
coords_2d = VectorCoordinates(v_2d)
# Convert to polar
r, theta = coords_2d.to_polar()
print(f"Polar: r={r}, ฮธ={theta}")
# Create from polar
v_from_polar = VectorCoordinates.from_polar(5, math.pi/4)
# Complex representation
z = coords_2d.to_complex() # 3 + 4j
v_from_complex = VectorCoordinates.from_complex(3 + 4j)
# 3D Coordinate conversions
v_3d = Vector(1, 1, math.sqrt(2))
coords_3d = VectorCoordinates(v_3d)
# Spherical coordinates
r, theta, phi = coords_3d.to_spherical()
v_from_spherical = VectorCoordinates.from_spherical(2, math.pi/4, math.pi/4)
# Cylindrical coordinates
rho, phi, z = coords_3d.to_cylindrical()
v_from_cylindrical = VectorCoordinates.from_cylindrical(math.sqrt(2), math.pi/4, math.sqrt(2))
# Generic conversion
polar_coords = coords_2d.convert('polar')
spherical_coords = coords_3d.convert('spherical')
Visualization
from matpy.vector.core import Vector
from matpy.matrix.core import Matrix
from matpy.visualization import (
plot_vectors_2d, plot_vectors_3d,
plot_transformation_2d, plot_matrix_heatmap
)
import math
# Plot 2D vectors
v1 = Vector(2, 3)
v2 = Vector(-1, 2)
plot_vectors_2d([v1, v2], labels=['v1', 'v2'], title="My Vectors")
# Plot 3D vectors
i = Vector(1, 0, 0)
j = Vector(0, 1, 0)
k = Vector(0, 0, 1)
plot_vectors_3d([i, j, k], labels=['i', 'j', 'k'], colors=['red', 'green', 'blue'])
# Visualize transformation
angle = math.pi / 4 # 45 degrees
rotation = Matrix(2, 2, [
[math.cos(angle), -math.sin(angle)],
[math.sin(angle), math.cos(angle)]
])
plot_transformation_2d(rotation, title="45ยฐ Rotation")
# Matrix heatmap
m = Matrix(4, 4, [[i+j for j in range(4)] for i in range(4)])
plot_matrix_heatmap(m, title="Sample Matrix")
Running Tests
# Run all tests
python -m unittest discover -s tests -p "test_*.py"
# Run with pytest (if installed)
pytest tests/
# Run specific test file
python -m unittest tests.test_vector_core
# Run with coverage (if pytest-cov installed)
pytest --cov=matpy tests/
Examples
Check out the examples/ directory for comprehensive demonstrations:
Vector Examples
vector_arithmatic.py- Vector arithmetic operationspython_methods.py- Python dunder methods and protocols
Matrix Examples
matrix_examples.py- Complete matrix operations showcase- Matrix creation and basic operations
- Linear algebra operations (determinant, inverse, etc.)
- Linear systems solving
- Least squares fitting
- Advanced operations (Hadamard, Kronecker products)
- Differential equations
Visualization Examples
visualization_examples.py- Complete visualization demonstrations- 2D and 3D vector plotting
- Vector fields
- Matrix heatmaps and grids
- Linear transformations (rotations, scaling, shearing)
- Coordinate system comparisons
- Vector operations visualization
Project Structure
matpy/
โโโ src/
โ โโโ matpy/
โ โโโ __init__.py
โ โโโ version.py
โ โโโ error.py # Custom exceptions
โ โโโ core/ # Core utilities
โ โ โโโ __init__.py
โ โ โโโ utils.py # Utility functions (formatting, math, etc.)
โ โ โโโ validate.py # Validation functions
โ โโโ vector/ # Vector implementation
โ โ โโโ __init__.py
โ โ โโโ core.py # N-dimensional Vector class
โ โ โโโ ops.py # Vector operations and functions
โ โ โโโ coordinates.py # Coordinate system conversions
โ โโโ matrix/ # Matrix implementation
โ โ โโโ __init__.py
โ โ โโโ core.py # Matrix class
โ โ โโโ ops.py # Matrix operations and utilities
โ โ โโโ solve.py # Linear systems and ODE solvers
โ โโโ visualization/ # Visualization tools (optional)
โ โโโ __init__.py
โ โโโ vector_plot.py # Vector plotting functions
โ โโโ matrix_plot.py # Matrix visualization
โ โโโ coordinate_plot.py # Coordinate system plots
โโโ tests/ # Comprehensive test suite
โ โโโ test_vector_core.py # Vector class tests
โ โโโ test_vector_ops.py # Vector operations tests
โ โโโ test_matrix_core.py # Matrix class tests
โ โโโ test_matrix_ops.py # Matrix operations tests
โ โโโ test_matrix_solve.py # Solver tests
โโโ examples/ # Example scripts
โ โโโ vector_arithmatic.py
โ โโโ python_methods.py
โ โโโ matrix_examples.py
โ โโโ visualization_examples.py
โโโ pyproject.toml # Project configuration
โโโ README.md # This file
Custom Error Handling
MatPy provides descriptive custom exceptions for better error handling:
from matpy.error import (
MatPyError, # Base exception
VectorDimensionError, # Dimension mismatch
MatrixDimensionError, # Matrix dimension issues
InvalidOperationError, # Invalid operations
SingularMatrixError # Singular matrix operations
)
try:
v1 = Vector(1, 2, 3)
v2 = Vector(1, 2)
result = v1 + v2 # Raises VectorDimensionError
except VectorDimensionError as e:
print(f"Error: {e}")
API Reference
Vector Class
Constructor:
Vector(*args)- Create an n-dimensional vectorVector()- Creates 3D zero vector (0, 0, 0)Vector(x, y)- 2D vectorVector(x, y, z)- 3D vectorVector(x, y, z, w, ...)- N-dimensional vector
Properties (2D/3D):
x,y,z- Access first three componentscomponents- Tuple of all components
Methods:
dot(other)- Dot productcross(other)- Cross product (3D only)magnitude()- Calculate magnitudenormalize()- Return unit vector
Operators:
+,-,*,/- Arithmetic operations==,!=- Equality comparisonabs()- Magnitudelen()- Number of dimensions[]- Index accessin- Membership testiter()- Iteration supportbool()- True if non-zeroround(n)- Round components
Matrix Class
Constructor:
Matrix(rows, cols, data=None)- Create a matrix- If
data=None, creates zero matrix datashould be a 2D list:[[row1], [row2], ...]
- If
Methods:
transpose()- Matrix transposedeterminant()- Calculate determinant (square matrices)inverse()- Matrix inverse (non-singular matrices)adjugate()- Adjugate (adjoint) matrixtrace()- Sum of diagonal elementsrank()- Matrix rankcofactor(row, col)- Cofactor at positionis_square()- Check if squareis_symmetric()- Check if M = M^Tis_singular()- Check if determinant โ 0is_invertible()- Check if non-singularis_orthogonal()- Check if M^T M = I
Operators:
+,-- Matrix addition/subtraction*- Scalar or matrix multiplication/- Scalar division@- Matrix multiplication (preferred)**- Matrix power==,!=,<,>,<=,>=- Comparisons-M- Negationabs()- Frobenius normlen()- Total number of elements[i]- Row access[i] = row- Row assignmentiter()- Iteration over elementsbool()- True if has non-zero elementsround(n)- Round all elements
Matrix Operations Module
Creation Functions:
zeros(rows, cols)- Zero matrixones(rows, cols)- Matrix of onesidentity(size)- Identity matrixdiagonal(values)- Diagonal matrixfrom_rows(rows_list)- Create from row listsfrom_columns(cols_list)- Create from column lists
Advanced Operations:
hadamard_product(m1, m2)- Element-wise multiplicationkronecker_product(m1, m2)- Tensor productrow_echelon_form(m)- Row echelon formreduced_row_echelon_form(m)- RREFconcatenate_horizontal(m1, m2)- Join side-by-sideconcatenate_vertical(m1, m2)- Join top-to-bottom
Property Tests:
is_diagonal(m),is_identity(m),is_upper_triangular(m),is_lower_triangular(m)
Linear Algebra Solvers
Linear Systems:
solve_linear_system(A, b)- Gaussian eliminationsolve_lu(A, b)- Using LU decompositionsolve_cramer(A, b)- Cramer's rulesolve_least_squares(A, b)- Least squares solutionlu_decomposition(A)- Returns (L, U) matrices
Differential Equations:
solve_linear_ode_system_homogeneous(A, x0, t)- Solve dx/dt = Axsolve_linear_ode_system_nonhomogeneous(A, x0, b_func, t)- Solve dx/dt = Ax + b(t)matrix_exponential(A, t)- Compute e^(At)euler_method(A, x0, t_final, steps)- Numerical ODE solverrunge_kutta_4(A, x0, t_final, steps)- RK4 numerical solver
Coordinate Systems
VectorCoordinates Class:
- 2D:
to_polar(),from_polar(),to_complex(),from_complex() - 3D:
to_spherical(),from_spherical(),to_cylindrical(),from_cylindrical() - Generic:
convert(system),convert_from(system, *coords)
Visualization Functions
Vector Plotting:
plot_vector_2d(v, ...)- Plot single 2D vectorplot_vectors_2d(vectors, ...)- Plot multiple 2D vectorsplot_vector_3d(v, ...)- Plot single 3D vectorplot_vectors_3d(vectors, ...)- Plot multiple 3D vectorsplot_vector_field_2d(func, ...)- Plot 2D vector field
Matrix Visualization:
plot_matrix_heatmap(m, ...)- Color-coded matrixplot_matrix_grid(m, ...)- Bar chart representationplot_transformation_2d(m, ...)- Visualize 2D transformationplot_transformation_3d(m, ...)- Visualize 3D transformation
Coordinate Plotting:
plot_coordinate_systems_2d(v)- Cartesian vs Polarplot_coordinate_systems_3d(v)- Cartesian vs Spherical vs Cylindrical
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Development
# Install development dependencies
pip install -e ".[dev]"
# Install with visualization support
pip install -e ".[viz]"
# Run tests
python -m unittest discover -s tests -p "test_*.py"
# Run tests with pytest (if installed)
pytest tests/
# Format code
black src/ tests/ examples/
# Type checking
mypy src/
# Run specific test file
python -m unittest tests.test_matrix_core
Recent Updates
โ v0.1.0 (Current)
- N-dimensional vector support (2D, 3D, 4D+)
- Complete linear systems solver suite
- ODE solver for systems of differential equations
- Coordinate system conversions (Polar, Spherical, Cylindrical, Complex)
- Comprehensive visualization module with matplotlib
- Advanced matrix operations (Hadamard, Kronecker products, RREF)
- Centralized validation and utility modules
- 300+ unit tests with comprehensive coverage
Future Enhancements
- Additional matrix decompositions (QR, SVD, Cholesky)
- Sparse matrix support
- Higher-dimensional tensors
- Complex number support for matrices
- Performance optimizations with optional NumPy backend
- Additional numerical ODE solvers
- Eigenvalue/eigenvector computation for larger matrices
- Interactive visualization widgets
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Inspired by NumPy and other linear algebra libraries
- Built with pure Python for educational purposes
- Comprehensive test coverage for reliability
Contact
Noah Ryan - njryan2005@gmail.com
Project Link: https://github.com/njryan-boou/matpy
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file matpy_linalg-0.2.0.tar.gz.
File metadata
- Download URL: matpy_linalg-0.2.0.tar.gz
- Upload date:
- Size: 54.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91743c9c97435472488e2f59fb18cae08db39ac64d4cff31c77a288d5177d2d7
|
|
| MD5 |
5481b01dcc74769f1cb05f56a0c34676
|
|
| BLAKE2b-256 |
e7a7bcc064b6d6f0b406d00ce75e3cd74bffa43ee7189cafeef9e0311b1d9680
|
File details
Details for the file matpy_linalg-0.2.0-py3-none-any.whl.
File metadata
- Download URL: matpy_linalg-0.2.0-py3-none-any.whl
- Upload date:
- Size: 45.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
266fc286ec5d63fe2e49ef772f3fde0c6abffc6bfeb1b9984d4f5ff6a60c5ec3
|
|
| MD5 |
347e51acbc318cb5bc265ed17614ac4e
|
|
| BLAKE2b-256 |
52bac92239125856dce11344dc1739f29ca8560ed10c001555d646895833ddda
|