Last active
April 20, 2025 13:16
-
-
Save barbarbar338/b66e2a404abb3a7f7c4d542204a3626a to your computer and use it in GitHub Desktop.
Taking the Inverse of a Matrix Using Gauss-Jordan Method - Prepared for AGU EEE Coma Capsule Integrated Project#1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
/* | |
used vectors instead of arrays. | |
It's easier to work with vectors | |
because of their dynamic size. | |
~ barbarbar338 | |
*/ | |
#include <vector> | |
using namespace std; | |
void printMatrix(const vector<vector<double>> &matrix) | |
{ | |
for (const auto &row : matrix) | |
{ | |
cout << "| "; | |
for (const auto &elem : row) | |
{ | |
cout << elem << "\t"; | |
} | |
cout << "|" << endl; | |
} | |
} | |
int main() | |
{ | |
int n; // matrix size | |
int choice; // user's choice (1, input matrix elements manually; 2, generate random numbers) | |
cout << "This program finds the inverse of a square matrix using Gauss-Jordan elimination."; | |
// ask for use input to create nxn matrix | |
cout << "Enter the square matrix size (n => nxn): "; | |
cin >> n; | |
cout << n << "x" << n << " matrix will be created. Would you like to enter all the elements by yourself (1) or generate random numbers (2)? (1/2): "; | |
cin >> choice; | |
// create matrix | |
vector<vector<double>> matrix(n, vector<double>(n, 0.0)); | |
if (choice == 2) | |
{ | |
// Create random elements for the matrix | |
srand(time(NULL)); | |
for (int i = 0; i < n; i++) | |
for (int k = 0; k < n; k++) | |
matrix[i][k] = rand() % 100; | |
} | |
else | |
{ | |
// Get all elements from the user | |
for (int i = 0; i < n; i++) | |
{ | |
cout << "Enter the elements of the " << i + 1 << ". row:"; | |
for (int k = 0; k < n; k++) | |
cin >> matrix[i][k]; | |
} | |
} | |
// Pretty-print the matrix in a table | |
cout << "The matrix is:" << endl; | |
printMatrix(matrix); | |
// Create the nxn identity matrix | |
vector<vector<double>> identity(n, vector<double>(n, 0.0)); | |
for (int i = 0; i < n; i++) | |
identity[i][i] = 1.0; | |
// Pretty-print the identity matrix in a table | |
cout << "The identity matrix is:" << endl; | |
printMatrix(identity); | |
// Create the augmented [A I] matrix | |
vector<vector<double>> augmented(n, vector<double>(n * 2, 0.0)); | |
for (int i = 0; i < n; i++) | |
for (int k = 0; k < n; k++) | |
augmented[i][k] = matrix[i][k]; | |
for (int i = 0; i < n; i++) | |
for (int k = n; k < n * 2; k++) | |
augmented[i][k] = identity[i][k - n]; | |
// Pretty-Print the augmented matrix | |
cout << "The augmented matrix is:" << endl; | |
printMatrix(augmented); | |
// Gauss-Jordan elimination to find the inverse matrix | |
for (int i = 0; i < n; i++) | |
{ | |
// Scale the current row to make the diagonal element 1 | |
double scale = augmented[i][i]; | |
for (int j = 0; j < 2 * n; j++) | |
augmented[i][j] /= scale; | |
// Eliminate non-diagonal elements in the current column | |
for (int j = 0; j < n; j++) | |
{ | |
if (j != i) | |
{ | |
double factor = augmented[j][i]; | |
for (int k = 0; k < 2 * n; k++) | |
augmented[j][k] -= factor * augmented[i][k]; | |
} | |
} | |
} | |
// Extract the inverse matrix from the augmented matrix | |
vector<vector<double>> inverse(n, vector<double>(n, 0.0)); | |
for (int i = 0; i < n; i++) | |
for (int j = 0; j < n; j++) | |
inverse[i][j] = augmented[i][j + n]; | |
// Pretty-Print the inverse matrix | |
cout << "The inverse matrix is:" << endl; | |
printMatrix(inverse); | |
cout << "This program is prepared by Barış DEMİRCİ & Nuri İNCEÖZ for AGU EEE COMA Capsule Integrated Project 1"; | |
cout << "Thanks for using our program <3" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment