This Python program allows users to perform basic operations on matrices of up to 4x4 in size. The operations include matrix addition, subtraction, multiplication, determinant calculation, and finding the inverse of a square matrix.
- Matrix Addition: Add two matrices of the same size (up to 4x4).
- Matrix Subtraction: Subtract one matrix from another of the same size (up to 4x4).
- Matrix Multiplication: Multiply two compatible matrices (up to 4x4).
- Determinant Calculation: Compute the determinant of a square matrix (up to 4x4).
- Matrix Inversion: Find the inverse of a square matrix (up to 4x4), if it exists.
- Python 3.6 or higher.
- No external libraries are required.
-
Clone the repository or copy the program file.
-
Run the program using Python:
python matrix_operations.py
-
Follow the on-screen instructions to choose an operation and input matrix values.
- The maximum matrix size is 4x4.
- Matrix elements must be integers.
- For determinant and inverse calculations, the input matrix must be square.
- If the determinant of a matrix is zero, its inverse does not exist.
The program includes robust error handling to ensure a smooth user experience. Here are the scenarios it handles:
-
Invalid Input Format:
- If the user enters a non-integer value or the wrong input format, the program will prompt the user to try again.
-
Matrix Size Constraints:
- If the user attempts to input a matrix larger than 4x4, an error message will be displayed:
"Ukuran matriks maksimal adalah 4x4."
- If the user attempts to input a matrix larger than 4x4, an error message will be displayed:
-
Incompatible Matrix Sizes:
- For addition and subtraction, both matrices must have the same size. If they don't, the program will display an error message.
- For multiplication, the number of columns in the first matrix must equal the number of rows in the second matrix. If not, an error message will be shown.
-
Zero Determinant:
- If the determinant of a square matrix is zero, the program will inform the user that the matrix has no inverse.
-
Overflow Prevention:
- If the user enters very large or unreasonable numbers, the program will catch and handle potential overflow issues gracefully.
-
Graceful Exit:
- If the user interrupts the program using
Ctrl+Cor wants to exit, the program will handle it gracefully by displaying a friendly exit message.
- If the user interrupts the program using
Input:
Pilih operasi:
1. Penjumlahan Matriks
2. Pengurangan Matriks
3. Perkalian Matriks
4. Determinan Matriks
5. Invers Matriks
6. Keluar
Masukkan pilihan: 1
Masukkan ukuran matriks (n m): 2 2
Masukkan elemen matriks A:
[1][1]: 1
[1][2]: 2
[2][1]: 3
[2][2]: 4
Masukkan elemen matriks B:
[1][1]: 5
[1][2]: 6
[2][1]: 7
[2][2]: 8
Output:
Hasil Penjumlahan Matriks:
6 8
10 12
This project is licensed under the MIT License.
Developed by Wyatt Matthew.