Professional Documents
Culture Documents
Class
Class
#include <conio.h>
#include <math.h>
#include <iostream>
using namespace std;
#include <iomanip>
//
//
//
//
//
//~~~~~~~LOP MA TRAN~~~~~~~~~~~~~~~~
class MAT{
public:
int u,v;
float **r;
MAT();
MAT(int u1,int u2);
friend int Rank(const MAT & x);// tnh c hng ca A :Rank(A)
friend float Det(const MAT & x);// tnh c Det(A)
friend MAT operator + (const MAT& x1,const MAT& x2);// vit c A+B
friend MAT operator - (const MAT& x1,const MAT& x2);// vit c A-B
friend MAT operator * (const MAT& x1,const MAT& x2);// vit c B*A
friend MAT operator ^ (const MAT& x,const int & n);// vit c A^n
friend istream & operator >> (istream & In,MAT & x);// vit c
cin>>A
friend ostream & operator << (ostream & Out,const MAT & x);// vit
c cout<<A
friend MAT kalman(const MAT & a,const MAT & b);
friend MAT operator * (const MAT & x,const float & n); // vit c
A*2
friend MAT operator / (const MAT & x,const float & n); // vit c A/2
friend MAT operator * (const float & n, const MAT & x); // vit c
2*A
};
//~~~~~~~~~~DINH NGHIA HAM BAN+HAM THANH PHAN CUA LOP~~~~~~~~~~~~
//***************************************************************
MAT::MAT() {
this->u=0;this->v=0;this->r=NULL;
}
//**********************************
MAT::MAT(int u1,int v1) {
this->u=u1;this->v=v1;
this->r = new float* [u1+1];
for(int i=1;i<=u1+1;i++){
this->r[i]= new float [v1+1];
for(int j=1;j<v1+1;j++)
this->r[i][j]=0;
}
}
//***********************************
int Rank(const MAT & x){
int Rank,i,j,m,n;
float tg;
Rank=x.u;
for(i=1;i<=x.u;i++)
for(j=1;j<=x.v;j++){
if(x.r[i][j]==0&&i==j){
for(m=i+1;m<=x.u;m++)
if(x.r[m][j]!=0){
for(n=1;n<=x.u;n++){
tg=x.r[m][n];
x.r[m][n]=x.r[i][n];
x.r[i][n]=tg;
}
break;
}
}
else break;
}
for(i=1;i<=x.u;i++)
for(j=1;j<=x.v;j++){
if(x.r[i][j]!=0&&i==j) m=i+1;
while(m<=x.u){
float k=x.r[m][j]/x.r[i][j];
for(n=1;n<=x.v;n++)
x.r[m][n]=x.r[m][n]-k*x.r[i][n];
m++;
}
}
for(i=1;i<=x.u;i++){
for(j=1;j<=x.v;j++)
if(x.r[i][j]!=0)
break;
else
if(j==x.v)
Rank--;
}
return Rank;
}
//***********************************
MAT operator + (const MAT & x1,const MAT & x2){
if(x1.u!=x2.u||x1.v!=x2.v){
cout << "\n Do 2 ma tran khong cung cap! Hay nhap lai!";
getch();
}
else {
MAT d(x1.u,x1.v);
for(int i=1;i<x1.u+1;i++)
for(int j=1;j<x1.v+1;j++)
d.r[i][j]=x1.r[i][j]+x2.r[i][j];
return d;
}
}//***********************************
MAT operator - (const MAT & x1,const MAT & x2){
if(x1.u!=x2.u||x1.v!=x2.v){
cout << "\n Do 2 ma tran khong cung cap! Hay nhap lai!";
getch();
}
else {
MAT d(x1.u,x1.v);
for(int i=1;i<x1.u+1;i++)
for(int j=1;j<x1.v+1;j++)
d.r[i][j]=x1.r[i][j]-x2.r[i][j];
return d;
}
}
//***********************************
MAT operator * (const MAT & x1,const MAT & x2){
if(x1.v!=x2.u){
cout << "\n Khong thuc hien duoc do kich thuoc 2 ma tran ko hop
le! Hay nhap lai!";
getch();
}
else
{
MAT d(x1.u,x2.v);
for(int i=1;i<x1.u+1;i++)
for(int j=1;j<x2.v+1;j++){
for(int k=1;k<x1.v+1;k++)
d.r[i][j]+=x1.r[i][k]*x2.r[k][j];
}
return d;
}
}
//***********************************
MAT operator * (const MAT & x,const float & n){
MAT d(x.u,x.v);
for(int i=1;i<x.u+1;i++)
for(int j=1;j<x.v+1;j++)
d.r[i][j]=x.r[i][j]*n;
return d;
}
//***********************************
MAT operator * (const float & n, const MAT & x){
MAT d(x.u,x.v);
for(int i=1;i<x.u+1;i++)
for(int j=1;j<x.v+1;j++)
d.r[i][j]=x.r[i][j]*n;
return d;
}
//***********************************
MAT operator / (const MAT & x,const float & n){
MAT d(x.u,x.v);
for(int i=1;i<x.u+1;i++)
for(int j=1;j<x.v+1;j++)
d.r[i][j]=x.r[i][j]/n;
return d;
}
//***********************************
MAT operator ^ (const MAT & x,const int & n) {
MAT d(x.u,x.v);
for (int i=1; i <x.u+1; i++)
for (int j=1;j<x.v+1;j++)
d.r[i][j]=x.r[i][j];
for (int k=2; k <=n; k++)
d=d*d;
return d;
}
//***********************************
tran
tran
tran
tran
tran
a+b
a-b
a*b
a^2
a/2
=";
=";
=";
=";
=";
Nhap 2 ma tran
a va b
cung cap
//***********************************
cout << "\n Do you want continue? Y/N
cin >> t;
}while (t=='y'||t=='Y');
}
/*
cout << "\n Nhap bac cua he " <<"\n";
cin >> n;
u=kalman(a,b);
cout << "\n Ma tran Kalman: " <<"\n" <<u;
if (Rank(u)!=n)
cout << "\n He KHONG Dieu Khien Duoc !";
else
cout << "\n He Dieu Khien Duoc!";
*/
\n";