This project provides a set of functions to perform various matrix operations including addition, subtraction, multiplication, transpose, minor, cofactor, adjoint, determinant, and inverse. The project is divided into two main parts: the main script that handles user interaction and the module containing the matrix functions.
Note
This is an improved version of my old program for matrix calculation that i did when i was starting to learn python 2 years ago, it was 1700+ lines of code(including the main and multiple module files combined)! and it was limited to only 3 by 3 matrices, so i really wanted to improve it, so 2 years later here i am after improving it from 1700+ lines of code to just 137 lines and it works with all sizes of matrices(Maybe depends on the device) but yea thats it, maybe i will try again after some time to shortening it and also try using new methods that I learn as I continue in this journey to learn more and try new things.
- Python 3.x
Install Python, if you don't have python 3.x already installed.
-
Clone the repository using the following in cmd/shell or just download the files.
git clone https://github.com/MTCodes01/Matrix-Calculator.git
-
Ensure the
Matrix Calculator.py
andModule/Mfunc.py
are in the correct directories.
-
Open a terminal or command prompt.
-
Navigate to the directory containing
Matrix Calculator.py
.cd C:/path/to/your/directory
-
Run the program using the command:
python Matrix Calculator.py
-
Follow the on-screen instructions to perform matrix operations.
The main script handles user interaction and calls the appropriate functions from the Mfunc module based on user input.
- When prompted, enter "Y" to start the program.
- Choose the desired matrix operation by entering the corresponding number or name.
- Enter the dimensions and elements of the matrices as prompted.
- The result of the operation will be displayed.
- Addition
- Difference (Subtraction)
- Multiplication
- Transpose
- Minor
- Cofactor
- Adjoint
- Determinant
- Inverse
- Exit
This module contains all the functions necessary to perform the matrix operations.
inp(m1: int, n1: int, m2: int = 0, n2: int = 0)
: Prompts the user for matrix input. Ifm2
andn2
are provided, it takes input for two matrices; otherwise, it takes input for a single matrix.out(lst: list[list[int]])
: Prints the matrix in a formatted way.ADD(T: tuple[list[list[int]], list[list[int]]])
: Adds two matrices element-wise.SUB(T: tuple[list[list[int]], list[list[int]]])
: Subtracts the second matrix from the first element-wise.MUL(T: tuple[list[list[int]], list[list[int]]])
: Multiplies two matrices using matrix multiplication rules.TRA(lst: list[list[int]]])
: Transposes the given matrix.MIN(lst: list[list[int]], row: int = -1, col: int = -1)
: Calculates the minor of a matrix. Ifrow
andcol
are provided, it calculates the minor of the matrix at that position; otherwise, it returns the matrix of minors.COF(lst: list[list[int]], row: int = -1, col: int = -1)
: Calculates the cofactor of a matrix. Ifrow
andcol
are provided, it calculates the cofactor of the matrix at that position; otherwise, it returns the matrix of cofactors.DET(lst: list[list[int]]])
: Calculates the determinant of a matrix.ADJ(lst: list[list[int]]])
: Calculates the adjoint (adjugate) of a matrix.INV(lst: list[list[int]]])
: Calculates the inverse of a matrix, if the determinant is non-zero. Raises aZeroDivisionError
if the determinant is zero.
Enter "Y" to start!: Y
1) Addition
2) Difference
3) Multiplication
4) Transpose
5) Minor
6) Cofactor
7) Adjoint
8) Determinant
9) Inverse
10) Exit
Enter your option: 1
Type 'm' by 'n' order of your Matrix:
m: 2
n: 2
A11: 1
A12: 2
A21: 3
A22: 4
B11: 5
B12: 6
B21: 7
B22: 8
6 8
10 12
Enter "Y" to start!: Y
1) Addition
2) Difference
3) Multiplication
4) Transpose
5) Minor
6) Cofactor
7) Adjoint
8) Determinant
9) Inverse
10) Exit
Enter your option: 2
Type 'm' by 'n' order of your Matrix:
m: 2
n: 2
A11: 5
A12: 6
A21: 7
A22: 8
B11: 1
B12: 2
B21: 3
B22: 4
4 4
4 4
Enter "Y" to start!: Y
1) Addition
2) Difference
3) Multiplication
4) Transpose
5) Minor
6) Cofactor
7) Adjoint
8) Determinant
9) Inverse
10) Exit
Enter your option: 3
Type 'm' by 'n' order of your Matrix:
m1: 2
n1: 2
m2: 2
n2: 2
A11: 1
A12: 2
A21: 3
A22: 4
B11: 5
B12: 6
B21: 7
B22: 8
19 22
43 50
Enter "Y" to start!: Y
1) Addition
2) Difference
3) Multiplication
4) Transpose
5) Minor
6) Cofactor
7) Adjoint
8) Determinant
9) Inverse
10) Exit
Enter your option: 4
Type 'm' by 'n' order of your Matrix:
m: 2
n: 2
A11: 1
A12: 2
A21: 3
A22: 4
1 3
2 4
Enter "Y" to start!: Y
1) Addition
2) Difference
3) Multiplication
4) Transpose
5) Minor
6) Cofactor
7) Adjoint
8) Determinant
9) Inverse
10) Exit
Enter your option: 5
Type 'm' by 'n' order of your Matrix:
m: 2
n: 2
A11: 1
A12: 2
A21: 3
A22: 4
4 3
2 1
Enter "Y" to start!: Y
1) Addition
2) Difference
3) Multiplication
4) Transpose
5) Minor
6) Cofactor
7) Adjoint
8) Determinant
9) Inverse
10) Exit
Enter your option: 6
Type 'm' by 'n' order of your Matrix:
m: 2
n: 2
A11: 1
A12: 2
A21: 3
A22: 4
4 -3
-2 1
Enter "Y" to start!: Y
1) Addition
2) Difference
3) Multiplication
4) Transpose
5) Minor
6) Cofactor
7) Adjoint
8) Determinant
9) Inverse
10) Exit
Enter your option: 8
Type 'm' by 'n' order of your Matrix:
m: 2
n: 2
A11: 1
A12: 2
A21: 3
A22: 4
-2
Enter "Y" to start!: Y
1) Addition
2) Difference
3) Multiplication
4) Transpose
5) Minor
6) Cofactor
7) Adjoint
8) Determinant
9) Inverse
10) Exit
Enter your option: 7
Type 'm' by 'n' order of your Matrix:
m: 2
n: 2
A11: 1
A12: 2
A21: 3
A22: 4
4 -2
-3 1
Enter "Y" to start!: Y
1) Addition
2) Difference
3) Multiplication
4) Transpose
5) Minor
6) Cofactor
7) Adjoint
8) Determinant
9) Inverse
10) Exit
Enter your option: 9
Type 'm' by 'n' order of your Matrix:
m: 2
n: 2
A11: 1
A12: 2
A21: 3
A22: 4
-2.0 1.0
1.5 -0.5