This project implements Sparse Matrix Operations in Python. The program supports:
- Reading sparse matrices from text files
- Performing addition, subtraction, and multiplication on sparse matrices
- Storing results in an output file
The SparseMatrix class efficiently stores and manipulates matrices using a dictionary representation to save memory by only keeping track of nonzero elements.
- Sparse Matrix Representation: Stores nonzero values using a dictionary
- Matrix Operations: Supports addition, subtraction, and multiplication
- File Input and Output: Reads matrix data from files and saves results to files
- Command-Line Interface: Users can interactively provide input file paths and choose operations
Sparse_matrix/
│── code/
│ ├── src/
│ │ ├── main.py # Entry point of the program
│ │ ├── sparse_matrix.py # SparseMatrix class implementation
│ │ ├── operations.py # Matrix operations (addition, subtraction, multiplication)
│── sample_inputs/
│ ├── matrixfile1.txt # Sample input matrix
│ ├── easy_sample_04_1.txt # Another sample input matrix
│── output/
│ ├── result.txt # File where the output matrix is stored
│── README.md # Project documentation
This project requires Python 3.7+.
git clone https://github.com/your-username/sparse-matrix.git
cd sparse-matrix/code/srcpython main.py- **Run **`` and enter the file paths for two matrices.
- Choose an operation:
1for addition2for subtraction3for multiplication
- View the result on the console or in the output file.
Example input files (e.g., matrixfile1.txt):
rows=3
cols=3
(0,0,5)
(1,1,8)
(2,2,3)
- Stores elements in a dictionary
{(row, col): value} - Handles file input/output with
save_to_file()and_load_from_file()
add_matrices(mat1, mat2): Adds two matricessubtract_matrices(mat1, mat2): Subtracts two matricesmultiply_matrices(mat1, mat2): Multiplies two matrices
Sparse Matrix Operations
Enter path for first matrix file: sample_inputs/matrixfile1.txt
Enter path for second matrix file: sample_inputs/easy_sample_04_1.txt
Choose operation:
1. Addition
2. Subtraction
3. Multiplication
Enter your choice (1/2/3): 1
✅ Output saved to output/result.txtContents of output/result.txt:
rows=3
cols=3
(0,0,10)
(1,1,16)
(2,2,6)
Feel free to submit issues or pull requests for improvements.
This project is licensed under the MIT License.