Total members 11895 |It is currently Sat Dec 21, 2024 5:11 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This is C++ project allow you the following operations on the matrices :
    1. Add Rows and Columns.
    2. Multiply two matrices.
    3. Transpose of Matrix.
    4. Determinant of Matrix.
    5. Inverse of Matrix (Any Size) :lol:
    6. Solve System of equations.



cpp code
#include <iostream.h>
#include<iomanip.h>
#include "matrix.h"
///////////////////////////////////constructor//////////
matrix::matrix()
{
for(int i=0;i<50;i++)
for(int j=0;j<50;j++)
{
matrix1[i][j]=0;
matrix2[i][j]=0;
sum_of_two_matrix[i][j]=0;
}
f_coloumn=0;
f_row=0;
s_coloumn=0;
s_row=0;
}
///////////////////////////////////set_dimentions/////////
void matrix::set_dimentions()
{
cout<<"ENTER The Dimentions Of Two Matrix (Row,Coloumn):"<<endl;
cout<<"First Matrix :"<<endl;
cin>>f_row;
cin>>f_coloumn;
cout<<"Second Matrix:"<<endl;
cin>>s_row;
cin>>s_coloumn;
}
///////////////////////////////////create_matrix///////////
void matrix::create_matrix ()
{
cout<<"\aFirst Matrix "<<endl;
for(int i=0;i<f_row;i++)
for(int j=0;j<f_coloumn;j++)
{
cin>>matrix1[i][j];
}

cout<<"\aSecond Matrix "<<endl;
for(int k=0;k<s_row;k++)
for(int l=0;l<s_coloumn;l++)
{
cin>>matrix2[k][l];
}


}
///////////////////////////////////display_matrix////////////////// /
void matrix::display()
{
cout<<"////////////////////////////////////YOUR MATRICES/////////////////////////////// "<<endl;
cout<<" First Matrix :"<<endl;
cout<<"----------------\n"<<endl;

for(int i=0;i<f_row;i++)
{
cout<<"\t\t";
cout<<"|";
for(int j=0;j<f_coloumn;j++)
{
cout<<" "<<matrix1[i][j]<<" ";
}
cout<<"|\a"<<endl<<endl;
}
/////////////////////////////////////////
cout<<"\n Second Matrix :"<<endl;
cout<<"-----------------\n"<<endl;

for(int k=0;k<s_row;k++)
{
cout<<"\t\t";
cout<<"|";
for(int l=0;l<s_coloumn;l++)
{
cout<<" "<<matrix2[k][l]<<" ";
}
cout<<"|\a"<<endl<<endl;
}
}
///////////// //////////add_rows_coloumns////////////
void matrix::add_rows_coloumns(int choice)
{
if(choice==1)
{
f_row+=1;
int serial,counter=0;
int row=f_row-1;
cout<<"ENTER SERIAL OF ROW TO ADD BEFORE"<<endl;
cin>>serial;
cout<<"ENTER NEW ROW "<<f_coloumn<<"ELEMENT ONLY"<<endl;
for(int i=row;i>=0;i--)
{
if(i!=serial-2)
{
counter++;
}
}
while(counter!=0)
{
for(int j=0;j<f_coloumn;j++)
matrix1[row+1][j]=matrix1[row][j];
row--;
counter--;
}
for(i=0;i<f_coloumn;i++)
cin>>matrix1[serial-1][i];
}
/////////////////////////////////////////////////////////
if(choice==2)
{
f_coloumn+=1;
int serial,counter=0;
int coloumn=f_coloumn-1;
cout<<"ENTER SERIAL OF COLOUMN TO ADD BEFORE"<<endl;
cin>>serial;
cout<<"ENTER NEW COLOUMN "<<f_row<<"ELEMENT ONLY"<<endl;
for(int i=coloumn;i>=0;i--)
{
if(i!=serial-2)
{
counter++;
}
}
while(counter!=0)
{
for(int j=0;j<f_row;j++)
matrix1[j][coloumn+1]=matrix1[j][coloumn
];
coloumn--;
counter--;
}
for(i=0;i<f_row;i++)
cin>>matrix1[i][serial-1];
}
/////////////////////////////////////////////////////////
if(choice==3)
{
int serial;
int add=0;
cout<<"ENTER SERIAL OF ROW TO ADD VECTROR TO IT "<<endl;
cin>>serial;
cout<<"ENTER THE VECTOR "<<f_coloumn<<"ELEMENT ONLY"<<endl;
for(int i=0;i<f_coloumn;i++)
{
cin>>add;
matrix1[serial-1][i]+=add;
}
}
/////////////////////////////////////////////////////////
if(choice==4)
{
int serial;
int add=0;
cout<<"ENTER SERIAL OF COLOUMN TO ADD VECTROR TO IT "<<endl;
cin>>serial;
cout<<"ENTER THE VECTOR "<<f_row<<"ELEMENT ONLY"<<endl;
for(int i=0;i<f_row;i++)
{
cin>>add;
matrix1[i][serial-1]+=add;
}
}
/////////////////////////////////////////////////////////
if(choice==5)

{
s_row+=1;
int serial,counter=0;
int row=s_row-1;
cout<<"ENTER SERIAL OF ROW TO ADD BEFORE"<<endl;
cin>>serial;
cout<<"ENTER NEW ROW "<<s_coloumn<<"ELEMENT ONLY"<<endl;
for(int i=row;i>=0;i--)
{
if(i!=serial-2)
{
counter++;
}
}
while(counter!=0)
{
for(int j=0;j<s_coloumn;j++)
matrix2[row+1][j]=matrix2[row][j];
row--;
counter--;
}
for(i=0;i<s_coloumn;i++)
cin>>matrix2[serial-1][i];
}
/////////////////////////////////////////////////////////
if(choice==6)
{
s_coloumn+=1;
int serial,counter=0;
int coloumn=s_coloumn-1;
cout<<"ENTER SERIAL OF COLOUMN TO ADD BEFORE"<<endl;
cin>>serial;
cout<<"ENTER NEW COLOUMN "<<s_row<<"ELEMENT ONLY"<<endl;
for(int i=coloumn;i>=0;i--)
{
if(i!=serial-2)
{
counter++;
}
}
while(counter!=0)
{
for(int j=0;j<s_row;j++)
matrix2[j][coloumn+1]=matrix2[j][coloumn];
coloumn--;
counter--;
}
for(i=0;i<s_row;i++)
cin>>matrix2[i][serial-1];

}
/////////////////////////////////////////////////////////
if(choice==7)
{
int serial;
int add=0;
cout<<"ENTER SERIAL OF ROW TO ADD VECTROR TO IT "<<endl;
cin>>serial;
cout<<"ENTER THE VECTOR "<<s_coloumn<<"ELEMENT ONLY"<<endl;
for(int i=0;i<s_coloumn;i++)
{
cin>>add;
matrix2[serial-1][i]+=add;
}
}
/////////////////////////////////////////////////////////
if(choice==8)
{

int serial;
int add=0;
cout<<"ENTER SERIAL OF COLOUMN TO ADD VECTROR TO IT "<<endl;
cin>>serial;
cout<<"ENTER THE VECTOR "<<s_row<<"ELEMENT ONLY"<<endl;
for(int i=0;i<s_row;i++)
{
cin>>add;
matrix2[i][serial-1]+=add;
}
}
}

/////////////////////////////////////////transpose////////////////////////////////////////
void matrix::transpose (int choice)
{
if(choice==1)
{
float temp1;
int temp2;
for(int i=0;i<=f_row;i++)
for(int j=0;j<=f_coloumn;j++)
{
temp1=matrix1[i][j+i+1];
matrix1[i][j+i+1]=matrix1[j+i+1][i];
matrix1[j+i+1][i]=temp1;
}
temp2=f_row;
f_row=f_coloumn;
f_coloumn=temp2;
}
if(choice==2)
{
float temp1;
int temp2;
for(int i=0;i<=s_row;i++)
for(int j=0;j<=s_coloumn;j++)
{
temp1=matrix2[i][j+i+1];
matrix2[i][j+i+1]=matrix2[j+i+1][i];
matrix2[j+i+1][i]=temp1;
}
temp2=s_row;
s_row=s_coloumn;
s_coloumn=temp2;
}
}
/////////////////////////////////////
void matrix::Multplay_Two_Matrixes()
{
for(int i=0;i<f_row;i++)
for(int j=0;j<s_coloumn;j++)
multplay_matrix[i][j]=0;



for(i=0;i<f_row;i++)
{

for(int j=0;j<s_coloumn;j++)
{

for(int k=0;k<s_coloumn;k++)
{
multplay_matrix[i][j]+=matrix1[i][k]*matrix2[k][j];

}

}
}



cout<<"*********************** ******"<<endl;
for(i=0;i<f_row;i++)
{
cout<<'|';
for(int j=0;j<s_coloumn;j++)
cout<<setw(7)<<" "<< multplay_matrix[i][j]<<" ";
cout<<'|'<<endl;
}
cout<<"********************* **************"<<endl;
}
void matrix::LU_decomposition()
{

float temp[50][50];
float temp_matrix[50][50];

float unit;

cout<<"Choose matrix(1) or matrix (2): "<<endl;
cin>>choose;
if(choose==1)
{
if(f_row!=f_coloumn){cout<<"It must be a square matrix "<<endl;return;}
for(int i=0;i<f_row;i++)
for(int j=0;j<f_coloumn;j++)
temp_matrix[i][j]=matrix1[i][j];
lenth=f_row;
}
else
{
if(s_row!=s_coloumn){cout<<"It must be a square matrix "<<endl;return;}
for(int i=0;i<s_row;i++)
for(int j=0;j<s_coloumn;j++)
temp_matrix[i][j]=matrix2[i][j];
lenth=s_row;
}


////////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////Algorithm to find LU-decompostion
/////////////////////////////////////////////////////
for(int i=0;i<lenth;i++)
for(int j=0;j<lenth;j++)
{
matrixL[i][j]=0;
matrixU[i][j]=0;
temp[i][j]=0;
}
for( i=0;i<lenth;i++)
{
if(i>0)
{
for(int k=i;k<lenth;k++)
{
for(int r=i;r<lenth;r++)
{
temp_matrix[k][r]-=temp_matrix[k][i-1]*temp_matrix[i-1][r];
}

}
cout<<"----------------------------"<<endl;
temp[i][i]=temp_matrix[i][i];

}
else

temp[i][i]= temp_matrix[i][i];
///////////////////////////////////
unit=temp[i][i];

for(int j=0;j<lenth;j++)
{
if(j>i)
{
temp[j][i]=(float)temp_matrix[j][i]/unit;
temp_matrix[j][i]=(float)temp_matrix[j][i]/unit;

}
else
matrixU[j][i]=(float)temp_matrix[j][i];





}



}
//////////////////////////////////////////
////////////////////////////////////////////
///// ///////////Evaluating matrix-L-///////////////////
/////////////////////////////////
for(i=0;i<lenth;i++)
{
matrixL[i][i]=1;
for(int j=0;j<lenth;j++)
{
if(j<i)
matrixL[i][j]=temp[i][j];

}

}
//////////////////////////Evaluating matrix-U-/////
/////////////////////////////////////////////
for(i=0;i<lenth;i++)
{

for(int j=0;j<lenth;j++)
{
if(j>i)
matrixL[i][j]=temp[i][j];

}
}
///////////////////Display matrix-L-///

cout<<"Matrix L is :"<<endl;
for(i=0;i<lenth;i++)
{
cout<<'|';
for(int j=0;j<lenth;j++)
cout<<setw(7)<<" "<< matrixL[i][j]<<" ";
cout<<'|'<<endl;
}
///////////////////Display matrix-U-///////

cout<<"Matrix U is :"<<endl;
for(i=0;i<lenth;i++)
{
cout<<'|';
for(int j=0;j<lenth;j++)
cout<<setw(7)<<" "<< matrixU[i][j]<<" ";
cout<<'|'<<endl;
}
cout<<"-----------------------------------"<<endl;
cout<<"A=LU : "<<endl;
float A[50][50];
for(i=0;i<lenth;i++)
{

for(int j=0;j<lenth;j++)
{
A[i][j]=0;
for(int k=0;k<lenth;k++)
{

A[i][j]+=matrixL[i][k]*matrixU[k][j];

}

}
}
//////
cout<<"*******************************************************"<<endl;
for(i=0;i<lenth;i++)
{
cout<<'|';
for(int j=0;j<lenth;j++)
cout<<setw(7)<<" "<< A[i][j]<<" ";
cout<<'|'<<endl;
}
cout<<"************************************************"<<endl;



}

void matrix::Determent()
{
LU_decomposition();
float Determent(1);
for(int i=0;i<lenth;i++)
{
Determent*=matrixU[i][i];

}
cout<<"The Determent is : " <<Determent<<endl;

}
void matrix::GetInverse()
{
//////////////////// Algorthim to find inverse of matrix using A=LU /////
LU_decomposition();
float z[50][50];
for(int i=0;i<lenth;i++)

for(i=0;i<lenth;i++)
{
for(int j=0;j<lenth;j++)
z[i][j]=0;
}


float sum(0);
for(int k=0;k<lenth;k++)
{
z[k][k]=1;
for( i=1;i<lenth;i++)
{
sum=0;
for(int j=0;j<i;j++)
{

sum+=-(z[j][k]*matrixL[i][j]);


}

if(i==k)z[i][k]=sum+1;
else
z[i][k]=sum;
}
inverse[lenth-1][k]=(float)z[lenth-1][k]/matrixU[lenth-1][lenth-1];
for( i=lenth-2;i>=0;i--)
{
sum=0;
for(int j=lenth-1;j>i;j--)
{
sum+=-(inverse[j][k]*matrixU[i][j]);

}
sum=z[i][k]+sum;
inverse[i][k]=(float)sum/matrixU[i][i];
}
}

//////Print on the screen the inverse Matrix///////
cout<<"**********************Inverse Matrix************************"<<endl;
for(i=0;i<lenth;i++)
{
cout<<'|';
for(int j=0;j<lenth;j++)
cout<<setw(7)<<" "<< inverse[i][j]<<" ";
cout<<'|'<<endl;
}
////Multiplay the matrix and its Inverse matrix


double A[50][50]={0};
for(i=0;i<lenth;i++)
{
for(int j=0;j<lenth;j++)
{
for(int k=0;k<lenth;k++)
{
A[i][j]+=matrix1[i][k]*inverse[k][j];
}
}
}
//Display on the screen the results of Muliplay of the matrix and unit matrix //
}
void matrix::Sovle_system_of_equation()
{
GetInverse();

float b[50][1];
float A[50][50]={0};
cout<<"The right hand side (Matrix .b.): "<<endl;

for(int i=0;i<lenth;i++)
cin>>b[i][0];
if(choose==1)
for(i=0;i<lenth;i++)
{
for(int j=0;j<lenth;j++)
{

for(int k=0;k<lenth;k++)
{
A[i][j]+= inverse[i][k]*b[k][j];

}

}
}
else
for(i=0;i<lenth;i++)
{
for(int j=0;j<1;j++)
{
for(int k=0;k<lenth;k++)
{
A[i][j]+=inverse[i][k]*b[k][j];
}

}
}
for(i=0;i<lenth;i++)
{
cout<<"X"<<i+1<<" = "<<A[i][0]<<endl;

}
}


matrix.h
cpp code
class matrix
{
private:
float matrix1[50][50];
float matrix2[50][50];
float inverse[50][50];
float sum_of_two_matrix[50][50];
float multplay_matrix[50][50];
float matrixL[50][50],matrixU[50][50];
int f_coloumn,f_row,s_coloumn,s_row;
int lenth;
int choose;
public:
matrix();
void set_dimentions();
void create_matrix();
void display();
void Determent();
void Multplay_Two_Matrixes();
void GetInverse();
void Sovle_system_of_equation();
void add_rows_coloumns(int);
void LU_decomposition();
void transpose(int);

};


main.cpp
cpp code
#include <iostream.h>
#include "matrix.h"
#include <windows.h>
int main()
{
matrix m;
int choice;
char c,b;
cout<<"\t\t\t ________________________________ \n"<<endl;
cout<<"\t\t\t| OPERATIONS ON MATRICES |"<<endl;
cout<<"\t\t\t|________________________________| \n"<<endl;
m.set_dimentions ();
cout<<"\n\nEnter the matrix now "<<endl;
m.create_matrix ();
system("cls");
do
{
m.display ();
cout<<"\nCHOOSE ONE OF THE FOLLOWING OPERATIONS\n "<<endl;
cout<<"1)Add rows and coloumns \n2)Multiply \n3)Transpose of a matrix \n4)Determenant.\n5)Inverse of a matrix. \n6)Solve system of equations\n\n";
cin>>choice;
switch (choice)
{
case 1:
do
{
system("cls");
m.display ();
cout<<"NOW -> CHOOSE ONE OF THE FOLLOWING \n\n"<<endl;
//////////////operation on first matrix//////////////
cout<<"1)add new row to MATRIX 1 "<<endl;
cout<<"2)add new coloumn to MATRIX 1 "<<endl;
cout<<"3)add vector to row in MATRIX 1 "<<endl;
cout<<"4)add vector to coloumn in MATRIX 1 \n"<<endl;
//////////////operation on second matrix//////////////
cout<<"5)add new row to MATRIX 2 "<<endl;
cout<<"6)add new coloumn to MATRIX 2 "<<endl;
cout<<"7)add vector to row in MATRIX 2 "<<endl;
cout<<"8)add vector to coloumn in MATRIX 2\n"<<endl;
cin>>choice;
switch(choice)
{
case 1:

m.add_rows_coloumns(choice);
m.display ();

break;
case 2:
m.add_rows_coloumns(choice);
m.display ();
break;
case 3:
m.add_rows_coloumns(choice);
m.display ();
break;
case 4:
m.add_rows_coloumns(choice);
m.display ();
break;
case 5:
m.add_rows_coloumns(choice);
m.display ();
break;
case 6:
m.add_rows_coloumns(choice);
m.display ();
break;
case 7:
m.add_rows_coloumns(choice);
m.display ();
break;
case 8:
m.add_rows_coloumns(choice);
m.display ();
break;
}
cout<<" ADD ROW ,COLOUMN,VECTOR ON YOUR MATRICES ( Y / N ) "<<endl;
cin>>b;
if(b=='n'||b=='N')
system("cls");

}while(b=='y'||b=='Y');
break;
case 2:
m.Multplay_Two_Matrixes();

break;
case 3:
cout<<"TRANSPOSE MATRIX NUMBER 1 OR 2 ?"<<endl;
cin>>choice;
m.transpose (choice);
m.display();
break;
case 4:
m.Determent();break;
break;
case 5:
m.GetInverse();break;
case 6:
m.Sovle_system_of_equation();break;
default:
cout<<" PLEASE CHOOSE FROM 1 TO 6 "<<endl;
break;
}
cout<<" CHOOSE ANOTHER OPERATION ON YOUR MATRIX ( Y / N ) "<<endl;
cin>>c;
if(c=='y'||c=='Y')
system("cls");
}while(c=='y'||c=='Y');

return 0;
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Matrix Operations
 Matrix multiplication     -  
 print element in 2d matrix     -  
 Matrix Multiplication using java     -  
 Matrix multiplication (Product) using C++     -  
 Sparse matrix using a class     -  
 How to do a array matrix program     -  
 create Sparse Matrix in java     -  
 C++ Boolean Operations     -  
 Character Operations     -  
 Operations on Sucxent++     -  



Topic Tags

C++ Arrays
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com