This program performs various operations on sparse matrices, including addition, subtraction, multiplication, and transposition. It reads matrices from input files, processes them, and saves the results to output files.
- Load sparse matrices from text files
- Perform matrix operations:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Transposition (T)
- Save results of operations to output files
- Interactive command-line interface with error handling
- Displays matrix dimensions and non-zero elements count
- Python 3.11 or higher
- Required Python package:
colorama
- Clone the repository:
git clone https://github.com/Yvantrey/sparse-matrix.git cd sparse-matrix - Install the required package:
pip install colorama
.
├── src
│ ├── main.py # Main script with CLI interface
│ ├── sparse_matrix.py # SparseMatrix class
│ ├── addition.py # Addition functionality
│ ├── subtraction.py # Subtraction functionality
│ ├── multiplication.py # Multiplication functionality
│ ├── transpose.py # Transposition functionality
├── sample_inputs
│ ├── matrix1.txt # Example input matrix
│ ├── matrix2.txt # Example input matrix
├── results # Stores output files
└── README.md
- Navigate to the
srcdirectory:cd src - Run the script:
python main.py
- Follow the on-screen instructions to select and execute matrix operations.
- Each line represents a row of the matrix.
- Elements in a row are separated by spaces.
- Example input file (
matrix1.txt):5 0 0 8 0 0 3 0 2 0 0 0
The results are saved in the results directory:
addition_result.txtsubtraction_result.txtmultiply_result.txttranspose_result_matrix1.txttranspose_result_matrix2.txt
main.py: Provides an interactive CLI for operations.sparse_matrix.py: Defines theSparseMatrixclass for handling sparse matrices.addition.py: Implements matrix addition.subtraction.py: Implements matrix subtraction.multiplication.py: Implements matrix multiplication.transpose.py: Implements matrix transposition.
This project is licensed under the MIT License. See the LICENSE file for details.
- Colorama for colored terminal text.
For questions or suggestions, contact: [y.rugamba@alustudent.com] Thank you!