A Python package for solving linear equations and performing 2x2 and 3x3 matrix operations
Project description
🧮 EqSolver
A comprehensive Python package for matrix operations and solving linear equations
Features • Installation • Quick Start • Documentation • Contributing
✨ Features
🧮 Matrix Operations
- ➕ Addition and subtraction
- ✖️ Matrix and scalar multiplication
- ➗ Matrix and scalar division
- 🔢 Determinant calculation
- 🔄 Adjoint and inverse matrix computation
🔢 Linear Equation Solver
- 📐 Solve 2×2 and 3×3 systems
- 🎯 Cramer's Rule and Matrix Inversion
- 📝 Parse equations from strings
- 🎲 Supports fractions for exact results
🛡️ Robust Error Handling
- ❌ Invalid or singular matrices
- 🚫 Division by zero
- ⚠️ Inconsistent or malformed equations
⚙️ Installation
Quick Install
pip install eqsolver
Development Installation
git clone https://github.com/Hammail-Riaz/eqsolver.git
cd eqsolver
pip install -e .
🚀 Quick Start
from eqsolver import Matrices2x2, LinearEquationsSolver
# Create a 2x2 matrix calculator
calc = Matrices2x2()
# Define matrices
A = [[2, 3], [1, 4]]
B = [[5, 2], [3, 1]]
# Perform operations
result = calc.add(A, B)
print(result) # [[7.0, 5.0], [4.0, 5.0]]
# Solve linear equations
solver = LinearEquationsSolver("2x + 4y = 5, 3x - 3y = -1")
solution = solver.solve('cramer')
for var, val in solution:
print(f"{var} = {val}")
# Output:
# x = 11/18
# y = 17/18
📘 Documentation
Table of Contents
🧩 2×2 Matrix Operations
from eqsolver import Matrices2x2
calc = Matrices2x2()
A = [[2, 3], [1, 4]]
B = [[5, 2], [3, 1]]
| Operation | Example | Output |
|---|---|---|
| Addition | calc.add(A, B) |
[[7.0, 5.0], [4.0, 5.0]] |
| Subtraction | calc.subtract(A, B) |
[[-3.0, 1.0], [-2.0, 3.0]] |
| Multiplication | calc.multiply(A, B) |
Matrix product |
| Scalar Multiply | calc.multiply(A, num=2) |
All elements × 2 |
| Determinant | calc.determinant(A) |
5.0 |
| Adjoint | calc.adjoint(A) |
[[4.0, -3.0], [-1.0, 2.0]] |
| Inverse | calc.multiplicative_inverse(A) |
[[0.8, -0.6], [-0.2, 0.4]] |
🧮 3×3 Matrix Operations
from eqsolver import Matrices3x3
calc = Matrices3x3()
C = [[1, 2, 3], [0, 1, 4], [5, 6, 0]]
D = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] # Identity matrix
| Operation | Example | Output |
|---|---|---|
| Addition | calc.add(C, D) |
[[2, 2, 3], [0, 2, 4], [5, 6, 1]] |
| Subtraction | calc.subtract(C, D) |
Matrix difference |
| Determinant | calc.determinant(C) |
1.0 |
| Adjoint | calc.adjoint(C) |
[[-24, 18, 5], [20, -15, -4], [-5, 4, 1]] |
| Inverse | calc.multiplicative_inverse(C) |
[[-24, 18, 5], [20, -15, -4], [-5, 4, 1]] |
🔍 Linear Equation Solver
Solve systems of linear equations with ease!
Example: 2-Variable System
from eqsolver import LinearEquationsSolver
# Define your equations as a string
solver = LinearEquationsSolver("2x + 4y = 5, 3x - 3y = -1")
# Solve using Cramer's Rule
solution = solver.solve('cramer')
for var, val in solution:
print(f"{var} = {val}")
Output:
x = 11/18
y = 17/18
Example: 3-Variable System
equations = "x + 2y + 3z = 6, 2x - y + z = 3, 3x + y - z = 4"
solver = LinearEquationsSolver(equations)
# Solve using matrix inversion
solution = solver.solve('inverse')
for var, val in solution:
print(f"{var} = {val}")
Get Coefficient Matrix
solver = LinearEquationsSolver("2x + 4y = 5, 3x - 3y = -1")
A, b = solver.A() # Returns coefficient matrix and constants vector
⚠️ Error Handling
EqSolver provides comprehensive error handling for common issues:
Singular Matrix
from eqsolver import Matrices3x3, InvalidMatrixError
calc = Matrices3x3()
singular = [[1, 2, 3], [2, 4, 6], [3, 6, 9]] # Linearly dependent rows
try:
inv = calc.multiplicative_inverse(singular)
except InvalidMatrixError as e:
print(f"Error: {e}")
Output:
Error: Matrix is singular and cannot be inverted.
Division by Zero
try:
result = calc.divide(A, num=0)
except ZeroDivisionError as e:
print(f"Error: {e}")
🧠 API Reference
Matrices2x2 and Matrices3x3
Both classes share the same interface:
Methods
| Method | Parameters | Returns | Description |
|---|---|---|---|
add(matrix1, matrix2) |
Two matrices | Matrix | Element-wise addition |
subtract(matrix1, matrix2) |
Two matrices | Matrix | Element-wise subtraction |
multiply(matrix1, matrix2, num) |
Two matrices or matrix + scalar | Matrix | Matrix multiplication or scalar multiplication |
divide(matrix, num) |
Matrix and scalar | Matrix | Scalar division |
determinant(matrix) |
Matrix | Float | Calculate determinant |
adjoint(matrix) |
Matrix | Matrix | Calculate adjoint (adjugate) |
multiplicative_inverse(matrix) |
Matrix | Matrix | Calculate inverse matrix |
LinearEquationsSolver
Constructor
LinearEquationsSolver(equations_str: str)
Parameters:
equations_str: Comma-separated string of equations (e.g.,"2x + 3y = 5, x - y = 1")
Methods
| Method | Parameters | Returns | Description |
|---|---|---|---|
A() |
None | Tuple[Matrix, Vector] | Returns coefficient matrix and constants vector |
solve(method) |
'cramer' or 'inverse' |
List[Tuple[str, str]] | Solves equations and returns variable-value pairs |
InvalidMatrixError
Custom exception raised for:
- Singular matrices (determinant = 0)
- Invalid matrix dimensions
- Malformed input
🤝 Contributing
We welcome contributions! Here's how you can help:
- Fork the repository
- Create a feature branch
git checkout -b feature/amazing-feature
- Commit your changes
git commit -m "Add amazing feature"
- Push to the branch
git push origin feature/amazing-feature
- Open a Pull Request
📜 License
This project is licensed under the MIT License – see the LICENSE file for details.
👨💻 Author
Hammail Riaz
- 🐙 GitHub: @Hammail-Riaz
- 📧 Email: hammailriaz.dev@gmail.com
🕓 Changelog
Version 1.0.0
- ✅ Initial stable release as
eqsolver - ✅ Added 2×2 and 3×3 matrix operations
- ✅ Added linear equation solver with Cramer's Rule and Matrix Inversion
- ✅ Comprehensive error handling
- ✅ Complete documentation and examples
❤️ Acknowledgments
- Built with Python 3.8+
- Inspired by linear algebra education needs
- Thanks to all contributors and the open-source community
🆘 Support
Need help? Have suggestions?
- 📝 Open an issue
- 📧 Email: hammailriaz.dev@gmail.com
Made with ❤️ for mathematics and Python enthusiasts
⭐ Star this repo if you find it helpful!
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 pyeqsolver-1.0.0.tar.gz.
File metadata
- Download URL: pyeqsolver-1.0.0.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4988ed4aa426e456eefba6e1e15488b39cfceab10e15dfb7df68ad9e9c0a55d
|
|
| MD5 |
06e8468153654e789701b6865a65c7bc
|
|
| BLAKE2b-256 |
1a6f39bab286d754cd9b3a102a91b71e65a0e482052d9a0b84306713019b0957
|
File details
Details for the file pyeqsolver-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pyeqsolver-1.0.0-py3-none-any.whl
- Upload date:
- Size: 21.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a18c709284ed695d96d8978cdbf6d3885444136250b7f7217b672137074b7979
|
|
| MD5 |
0a705a5615d0635c0cc03b9debc6284c
|
|
| BLAKE2b-256 |
a5897bae0dbdc9b26d4f3531745868cf0262fc4846aee14eeed9f1ac80a8e171
|