This project provides a set of operations for 2D matrices implemented in C++. The operations include:
- Matrix Addition
- Matrix Subtraction
- Matrix Multiplication
- Determinant Calculation
- Matrix Inversion
-
Clone the repository:
git clone https://github.com/KristiantoJohan/2D-Matrix-Operations.git
-
Navigate to the project directory:
cd 2D-Matrix-Operations
-
Compile the code:
g++ -o matrix_operations main.cpp matrix.cpp
Run the compiled program:
./matrix_operations
Add two matrices of the same dimensions.
Subtract one matrix from another, both of the same dimensions.
Multiply two matrices if the number of columns in the first matrix is equal to the number of rows in the second matrix.
Calculate the determinant of a square matrix.
Calculate the inverse of a square matrix if it is invertible.
Below are examples of how each operation can be used within the program.
Matrix A = {{1, 2}, {3, 4}};
Matrix B = {{5, 6}, {7, 8}};
Matrix C = addMatrices(A, B);
// C is {{6, 8}, {10, 12}}
Matrix A = {{5, 6}, {7, 8}};
Matrix B = {{1, 2}, {3, 4}};
Matrix C = subtractMatrices(A, B);
// C is {{4, 4}, {4, 4}}
Matrix A = {{1, 2}, {3, 4}};
Matrix B = {{5, 6}, {7, 8}};
Matrix C = multiplyMatrices(A, B);
// C is {{19, 22}, {43, 50}}
Matrix A = {{1, 2}, {3, 4}};
double det = determinant(A);
// det is -2.0
Copy code
Matrix A = {{4, 7}, {2, 6}};
Matrix invA = inverse(A);
// invA is {{0.6, -0.7}, {-0.2, 0.4}}
Contributions are welcome! Please open an issue or submit a pull request for any improvements or additions.
Ensure your matrices are valid (e.g., dimensions match for the required operations). Error handling is included for invalid operations, such as attempting to invert a non-invertible matrix.