Skip to content

Instantly share code, notes, and snippets.

@0xD9D0
Last active April 22, 2024 15:58
Show Gist options
  • Save 0xD9D0/b44c109e3d26afcaa27fb01a1897a0a3 to your computer and use it in GitHub Desktop.
Save 0xD9D0/b44c109e3d26afcaa27fb01a1897a0a3 to your computer and use it in GitHub Desktop.
Matrix manipulation in C++

The prgoram defines a class matrix with private data members rows, columns of type int, and **p a double pointer of type float. The class has the public member functions findNorm() which finds the Euclidean norm of a matrix, dem() which finds the determinant of a matrix, inverse() which finds the inverse of a matrix, solve() which solve a system of simultaneous linear equations, and a default parametrized constructor. Overload the operators +, == operators as members of the class, and the operators *, extraction >>, insertion << as non-members of the class.

#include"matrix.h"
int main() {
matrix A, B, b, x;
cout << "Enter the A matrix" << endl;
cin >> A;
cout << "Enter the B matrix" << endl;
cin >> B;
cout << "Enter the b vector" << endl;
cin >> b;
cout << "The two matrices addition" << endl;
cout << A + B << endl;
cout << "The two matrices multiplication"
<< endl;
cout << A * B << endl;
cout << "The matrix determinant" << endl;
cout << A.dim() << endl;
cout << "The matrix inverse" << endl;
cout<<A.inverse()<<endl;
cout << "The solution of A x = b" << endl;
cout << A.solve(b) << endl;
return 0;
}
#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
#include <iostream>
#include<cassert>
#include <iomanip>
#include<cmath>
using namespace std;
class matrix {
friend ostream& operator<<(ostream &, const matrix&);
friend istream& operator>>(istream &, matrix&);
friend matrix operator*(const matrix&, const matrix&);
private:
int rows, columns;
float **p;
public:
void findNorm()const;
matrix inverse()const;
matrix solve(matrix b)const;
double dim()const;
matrix(int=1, int=1);
matrix operator+(const matrix&) const;
bool operator==(const matrix&) const;
}
;
#endif // MATRIX_H_INCLUDED
#include"matrix.h"
double matrix::dim()const{
int row, column, step;
double mult;
matrix A(rows,columns);
for (int i=0;i<rows;i++)
for (int j=0;j<columns;j++)
A.p[i][j]=p[i][j];
for(step=0; step<rows-1; step++)
{
for(row=step+1; row<rows; row++)
{
mult = A.p[row][step] / A.p[step][step];
for (column=step; column<rows; column++)
A.p[row][column]-=mult*A.p[step][column];
}
}
mult = 1;
for(row=0; row<rows; row++)
mult = mult * A.p[row][row];
return mult;
}
matrix matrix::inverse()const
{
matrix B(rows,columns),C(columns,columns*2);
int row, column, step, i;
double mult;
for(row=0; row<rows; row++)
for(column=0; column<rows; column++)
C.p[row][column] = p[row][column];
for(i=0; i<rows; i++)
C.p[i][rows+i] = 1;
for(step=0; step<rows-1; step++)
{
for(row=step+1; row<rows; row++)
{
mult = C.p[row][step] / C.p[step][step];
for (column=step;column<2*rows; column++)
C.p[row][column]-= mult*C.p[step][column];
}
}
for(step=1; step<=rows-1; step++)
{
for(row=rows-step-1; row>=0; row--)
{
mult = C.p[row][rows-step] / C.p[rows-step][rows-step];
for (column=rows; column<2*rows; column++)
C.p[row][column]-=
mult*C.p[rows-step][column];
}
}
for(row=0; row<rows; row++)
for(column=0; column<rows; column++)
B.p[row][column] = C.p[row][rows+column] / C.p[row][row];
return B;
}
ostream &operator<<( ostream &coout, const matrix& n ) {
for(int i=0; i<n.rows; i++) {
for(int j=0; j<n.columns; j++)
coout << setw(14) << n.p[i][j];
coout << endl;
}
return coout;
}
istream &operator>>( istream &ciin, matrix &n ) {
delete [] n.p;
cout<<"Enter the number of Rows and Columns: ";
ciin>>n.rows>>n.columns;
n.p = new float *[n.rows];
for(int i=0; i<n.rows; i++) {
n.p[i] = new float[n.columns];
}
cout << "Enter the " << n.rows << " rows of the matrix:" << endl;
for(int i=0; i<n.rows; i++) {
cout << "Enter the " << n.columns << " elements of row number " << i+1 << ": ";
for(int j=0; j<n.columns; j++)
ciin >> n.p[i][j];
}
return ciin;
}
matrix matrix::operator+(const matrix& n) const {
int i, j;
matrix tempMatrix(n.rows, n.columns);
for(i=0; i<n.rows; i++) {
for(j=0; j<n.columns; j++) {
tempMatrix.p[i][j] = p[i][j] + n.p[i][j];
}
}
return tempMatrix;
};
matrix operator*(const matrix& A, const matrix& B) {
float sum;
int i, j, k, n, m, l;
n = A.rows;
m = A.columns;
l = B.columns;
matrix tempMatrix(n, l);
for(i=0; i<n; i++) {
for(j=0; j<l; j++) {
sum = 0;
for(k=0; k<m ; k++)
sum = sum + A.p[i][k] * B.p[k][j];
tempMatrix.p[i][j] = sum;
}
}
return tempMatrix;
}
matrix::matrix(int r, int c) {
int i, j;
rows = r;
columns = c;
p = new float *[rows];
assert(p != NULL);
for(i=0; i<rows; i++) {
p[i] = new float[columns];
assert(p[i] != NULL);
}
for(i=0; i<rows; i++)
for(j=0; j<columns; j++)
p[i][j] = 0;
}
matrix matrix::solve(matrix b)const{
int row, column, step, iStep;
double sum, maxElement, temp, mult;
matrix x(rows,1),A(columns,columns+1);
for(row=0; row<rows; row++)
{
for(column=0; column<rows; column++)
A.p[row][column] = p[row][column];
A.p[row][rows] = b.p[row][0];
}
for (step=0; step<rows-1; step++)
{
maxElement=A.p[step][step];
iStep=step;
for ( row=step+1; row<rows; row++ )
if(fabs(maxElement)< fabs(A.p[row][step]))
{
maxElement=A.p[row][step];
iStep=row;
}
for (column=step; column<=rows; column++)
{
temp=A.p[step][column];
A.p[step][column]=A.p[iStep][column];
A.p[iStep][column]=temp;
}
for (row=step+1; row<rows; row++)
{
mult=A.p[row][step]/A.p[step][step];
for (column=step; column<=rows; column++)
A.p[row][column]-= mult*A.p[step][column];
}
}
x.p[rows-1][0]=A.p[rows-1][rows]/A.p[rows-1][rows-1];
for (row=rows-2; row>=0; row--)
{
sum=0;
for (column=row+1; column<rows; column++)
sum+= A.p[row][column]*x.p[column][0];
x.p[row][0]=(A.p[row][rows] - sum)/A.p[row][row];
}
return x;
}
@LowLevelCodingCH
Copy link

Just make a matrix be:
std::vector<std::vector>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment