A simple example of reconstructing-c++ implementation matrix _c language

Source: Internet
Author: User
Tags constructor pow

Refactoring-c++ A simple instance of the implementation matrix

#include <iostream> #include <cmath> using namespace std;   Double cofactor (double* detptr,int rank,int t);      Algebraic cofactor type Double Valdet (double *detptr, int rank); The row-and-column template <class t> void Exchange (t& t1,t& T2) {T temp;temp=t1;t1=t2;t2=temp;}//Exchange class Squarematr
ix     Class matrix{Public:friend class Squarematrix;        Coordinate conversion function Edible Matrix () {m=n=mn=0;}       The default constructor Matrix (int mt,int nt);    Tectonic matrix matrices (const matrix& MTRX);  Copy constructor Matrix (int mt,int nt,double* a);      Array initialization matrix Transposemtrx ();      Transpose matrix//Elementary transformation void Exchangerow (int r1,int r2,int c=0);        Swap line void MultiRow (int r,int k,int c=0);  Number multiplication line void addmultirow (int r1,int r2,int k=1,int c=0);      R1+=K*R2 void exchangecolumn (int c1,int c2,int r=0);        Swap column void multicolumn (int c,int k,int r=0);  The number multiply column void addmulticolumn (int c1,int c2,int k=1,int r=0);

  C1+=k*c2  matrix& operator = (const matrix& MTRX); 
    Assignment constructor friend istream& operator>> (istream& input,matrix& Mtrx); Friend ostream& operator<< (ostream& output,matrix& Mtrx);  Output matrix friend Matrix operator* (matrix& m1,matrix& m2);
    Matrix multiplication protected:int m;
    int n;
    int mn;
Double* matrixptr;

}; Class Squarematrix:public matrix{Public:squarematrix (): Matrix () {}//default constructor Squarematrix (int mt): M       Atrix (MT,MT) {}; Constructor Squarematrix (int mt,double* a): Matrix (mt,mt,a) {};         Array initialization matrix Squarematrix (const matrix& MTRX);    Matrix to Phalanx conversion Squarematrix Transposesqrmtrx ();    Squarematrix Adjugatesqrmatrix of the rotating phalanx ();    Adjoint matrix Squarematrix Inversesqrmatrix (); 
                              Inverse matrix friend istream& operator>> (istream& input,squarematrix& Mtrx); Input matrix friend Squarematrix operator * (squarematrix& sm1,squarematrix& SM2);                  Matrix multiplication double Getdet ();

The value of the determinant is private:};
  Matrix::matrix (int mt,int nt) {//Initialize m*n matrix m=mt;n=nt;mn=m*n;
Matrixptr=new DOUBLE[MN];
  } matrix::matrix (const matrix& MTRX) {//copy constructor m=mtrx.m;n=mtrx.n;mn=mtrx.mn;
  Matrixptr=new DOUBLE[MN];
for (int i=0;i<mn;i++) matrixptr[i]=mtrx.matrixptr[i];
  Matrix::matrix (int mt,int nt,double* a) {//array initialization m*n matrix m=mt;n=nt;mn=m*n;
  Matrixptr=new DOUBLE[MN];
for (int i=0;i<mn;i++) matrixptr[i]=a[i]; } istream& operator>> (istream& input,matrix& Mtrx) {//Overload >> if (!mtrx.m) {cout<< "Ente"
    R the M,n of Matrix: ";
    input>>mtrx.m>>mtrx.n;
    MTRX.MN=MTRX.M*MTRX.N;
    Mtrx.matrixptr=new DOUBLE[MTRX.MN];
  cout<< "Enter the Matrix:" <<endl;
  Else cout<< "Enter a" <<mtrx.m<< ' * ' <<mtrx.n<< "Matrix:" <<endl; for (int i=0;i<mtrx.mn;i++) input>>mtrx.matrixptr[I];
return input;
  Matrix Matrix::transposemtrx () {//transpose matrices Mtrx (n,m);
  for (int i=0;i<n;i++) for (int j=0;j<m;j++) mtrx.matrixptr[m*i+j]=matrixptr[n*j+i];
return Mtrx; } void Matrix::exchangerow (int r1,int r2,int c) {//Interchange line, default c=0 for (int i=c;i<n;i++) Exchange (matrixptr[n*r1+i
],matrixptr[n*r2+i]);

} void Matrix::multirow (int r,int k,int c) {//number multiplication line, default c=0 for (int i=c;i<n;i++) matrixptr[n*r+i]*=k;} void Matrix::addmultirow (int r1,int r2,int k,int c) {//R1+=K*R2, default k=1,c=0 for (int i=c;i<n;i++) matrixptr[n*r1+i
]+=matrixptr[n*r2+i]*k; } void Matrix::exchangecolumn (int c1,int c2,int R) {//Interchange column, default r=0 for (int i=r;i<m;i++) Exchange (matrixptr[n*i+
C1],MATRIXPTR[N*I+C2]); 

} void Matrix::multicolumn (int c,int k,int R) {//multiplier column, default k=1,r=0 for (int i=r;i<m;i++) matrixptr[n*i+c]*=k;} void Matrix::addmulticolumn (int c1,int c2,int k,int r) {//c1+=k*c2, default r=0 for (int i=r;i<m;i++) MATRIXPTR[n*i+c1]+=matrixptr[n*i+c2]*k;
  } matrix& matrix::operator= (const matrix& MTRX) {//overload = M=mtrx.m;n=mtrx.n;mn=m*n;
  Matrixptr=new DOUBLE[MN];
  for (int i=0;i<mn;i++) matrixptr[i]=mtrx.matrixptr[i];
return *this;
  } ostream& operator<< (ostream& output,matrix& Mtrx) {//Overload << output<<endl;
    for (int i=0;i<mtrx.m;i++) {for (int j=0;j<mtrx.n;j++) output<<mtrx.matrixptr[mtrx.n*i+j]<< ';
  output<<endl;
  } output<<endl;
return output;
  Matrix operator * (matrix& m1,matrix& m2) {//overload * Matrix M3 (M1.M,M2.N);
      for (int i=0;i<m3.m;i++) for (int j=0;j<m3.n;j++) {double val=0;
      for (int k=0;k<m2.m;k++) VAL+=M1.MATRIXPTR[M1.N*I+K]*M2.MATRIXPTR[M2.N*K+J];
    M3.matrixptr[m3.n*i+j]=val;
return m3; //I Am Meng Meng's split line-------------------------------------------------------Squarematrix::squarematrix (const matrix& MTRX) {//constructor m=n=mtrx.m;Mn=m*n;matrixptr=new DOUBLE[MN];
for (int i=0;i<mn;i++) matrixptr[i]=mtrx.matrixptr[i]; } istream& operator>> (istream& input,squarematrix& Mtrx) {//Overload >> if (!MTRX.M) {COUT&LT;&L t; "
    Enter the M of Squarematrix: ";
    input>>mtrx.m;
    MTRX.N=MTRX.M;MTRX.MN=MTRX.M*MTRX.N;
    Mtrx.matrixptr=new DOUBLE[MTRX.MN];
  cout<< "Enter the Squarematrix:" <<endl;
  Else cout<< "Enter a" <<mtrx.m<< "Order Squarematrix:" <<endl;
  for (int i=0;i<mtrx.mn;i++) input>>mtrx.matrixptr[i];
return input; 

} Squarematrix Squarematrix::transposesqrmtrx () {//Transpose Phalanx return Squarematrix (*this). Transposemtrx ());
  Squarematrix Squarematrix::adjugatesqrmatrix () {//adjoint matrix Squarematrix ASM (m);
  for (int i=0;i<mn;i++) asm.matrixptr[i]=cofactor (matrixptr,m,i);
  Asm=asm.transposesqrmtrx ();
return ASM;
  Squarematrix Squarematrix::inversesqrmatrix () {//Inverse matrix double det=getdet ();
   if (det==0) { cerr<< "This is a singular matrix!"  <<endl; 
  Judgment singular matrix return 0;
  Squarematrix ASM (M), ISM (m);
  Asm=adjugatesqrmatrix ();
  for (int i=0;i<mn;i++) Ism.matrixptr[i]=asm.matrixptr[i]/det;
return to ISM;
  } squarematrix operator * (squarematrix& sm1,squarematrix& sm2) {//overload * Squarematrix SM3 (SM1.M);
      for (int i=0;i<sm3.m;i++) for (int j=0;j<sm3.n;j++) {double val=0;
      for (int k=0;k<sm2.m;k++) VAL+=SM1.MATRIXPTR[SM1.N*I+K]*SM2.MATRIXPTR[SM2.N*K+J];
    Sm3.matrixptr[sm3.n*i+j]=val;
return SM3;

Double Squarematrix::getdet () {//determinant return Valdet (matrixptr,m);}
  It's an eruption. Split line------------------------------------------double Valdet (double *detptr, int rank) {double val=0;
  if (rank==1) return detptr[0];
    for (int i=0;i<rank;i++)//Compute cofactor saved in nextdetptr[] {double *nextdetptr=new double[(rank-1) * (rank-1)]; for (int j=0;j<rank-1;j++) for (int k=0;k<i;k++) Nextdetptr[j* (rank-1) +k]=detptr[(j+1) *rank+k];
    for (int j=0;j<rank-1;j++) for (int k=i;k<rank-1;k++) nextdetptr[j* (rank-1) +k]=detptr[(j+1) *rank+k+1];
  Val+=detptr[i]*valdet (nextdetptr,rank-1) *pow ( -1.0,i);
return Val; Double cofactor (double* detptr,int rank,int t) {//Compute algebra cofactor double *nextdetptr=new double[(rank-1) * (rank-1)]
  ; for (int i=0,j=0;i<rank*rank;i++) if (i>= (T/rank) *rank&&i< (t/rank) *rank+rank| |!  ((t-i)%rank));
      If I and T peers or the same row else{nextdetptr[j]=detptr[i];
    j + +;
return Valdet (nextdetptr,rank-1) *pow ( -1.0,t/rank+t%rank);
  int main () {cout<<endl<< "test driver-------------------" <<endl; 
  /* cout<<endl<< "input any matrix-------------------" <<endl;
  
  Matrix m1;cin>>m1;cout<<m1;
  cout<<endl<< "input arbitrary phalanx-------------------" <<endl;
  
  Squarematrix sm1;cin>>sm1;cout<<sm1; cout<<endl<< "Input 3*2 matrix--------------------" <<endl
  
  Matrix m2 (3,2);cin>>m2;cout<<m2;
  cout<<endl<< "Input 2-order phalanx--------------------" <<endl;
  Squarematrix SM2 (2);cin>>sm2;cout<<sm2;
  * * cout<<endl<< "array initialization matrix-----------------" <<endl;
  Double a1[6]={1,2,3,7,8,9};
  
  Matrix m3 (2,3,A1);cout<<m3;
  cout<<endl<< "Array initialization matrix-----------------" <<endl;
  Double a2[4]={3,4,5,6}; 
  
  Squarematrix sm3 (2,A2);cout<<sm3;
  cout<<endl<< "copy constructed matrix/matrix--------------" <<endl; Matrix m4;m4=m3;
  Matrix M5 (M3);
  cout<<m4<<m5; Squarematrix SM4;SM4=SM3;
  Squarematrix SM5 (SM3); 
  
  cout<<sm4<<sm5;
  cout<<endl<< "Matrix/Phalanx multiplication------------------" <<endl;
  Double a3[6]={1,0,3,2,1,0},a4[9]={4,1,0,-1,1,3,2,0,1};
  Matrix M6 (2,3,A3), M7 (3,3,A4);
  
  Matrix m8=m6*m7;cout<<m8;
  Double a5[4]={1,2,2,3},a6[4]={2,3,4,1};
  Squarematrix SM6 (2,A5), SM7 (2,A6);
  
  Squarematrix sm8 (SM6*SM7);cout<<sm8; Cout<<endl<< "Matrix conversion to square-----------------" <<endl;
  
  Squarematrix sm9 (M7);cout<<m7<<sm9;
  cout<<endl<< "Transpose matrix/Phalanx------------------" <<endl;
  Matrix M9 (M6.transposemtrx ());
  cout<<m6<<m9;
  Squarematrix Sm10=sm9.transposesqrmtrx (); 
  
  cout<<sm9<<sm10;
  cout<<endl<< "Elementary Transformation-----------------------" <<endl;
  cout<<m3<<m4;
  M4.exchangerow (0,1,2);cout<<m3<<m4;
  M4.exchangerow (0,1);cout<<m4;
  M4.exchangecolumn (0,2);cout<<m4;
  M4.multirow (1,2);cout<<m4;
  M4.multicolumn (1,2,1);cout<<m4;
  M4.addmultirow (0,1);cout<<m4;
  M4.addmulticolumn (0,2,2,1);cout<<m4;
  cout<<sm3<<sm4;
  
  Sm4.exchangerow (0,1);cout<<sm3<<sm4;
  cout<<endl<< "determinant value of square matrix-----------------" <<endl; 
  
  Cout<<sm3<<sm3.getdet () <<endl;
  cout<<endl<< "Inverse matrix-------------------------" <<endl; Squarematrix sm11=sm3.inversesQrmatrix ();cout<<sm11;
  Squarematrix sm12=sm3*sm11;cout<<sm12;
return 0; }

The above refactoring-c++ to achieve a simple example of the matrix is to share the whole of the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.