有关矩阵的计算

有关矩阵的计算


#include<iostream>
#include<math.h>
#include<iomanip>


using namespace std;


int used[10]={0};
int tmp_array[10];
int tmp_coffeicient[10]={0};
int result=0;


class MATRIX{
private:
    int matrix[10][10]={{0}};
    int tmp_matrix[10][10]={{0}};
    int n,m;
    int det;
    int companion_matrix[10][10];
    int rank;
public:
    MATRIX(int xmatrix[10][10],int xn,int xm);//构造函数
    MATRIX(const MATRIX & A);//复制构造函数
    int cal_det();
    void show_det();
    int cal_companion();
    void show_companion();
    int cal_rank();
    void show_rank();
    void show_matrix();


    int matrix_times(MATRIX B);//矩阵乘法


    int matrix_add(MATRIX B);//矩阵加法


    int matrix_minus(MATRIX B);//矩阵减法


    void times(int k);//数乘矩阵
};


MATRIX::MATRIX(int xmatrix[10][10],int xn,int xm)
{
    this->n=xn;
    this->m=xm;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            this->matrix[i][j]=xmatrix[i][j];
        }
    }
}


MATRIX::MATRIX(const MATRIX & A)
{
    this->n=A.n;
    this->m=A.m;
    for(int i=0;i<this->n;i++)
    {
        for(int j=0;j<this->m;j++)
        {
            this->matrix[i][j]=A.matrix[i][j];
        }
    }
    this->det=A.det;
    this->rank=A.rank;
}


void MATRIX::show_matrix()
{
    for(int i=0;i<this->n;i++)
    {
        for(int j=0;j<this->m;j++)
        {
            cout<<this->tmp_matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}


int MATRIX::matrix_add(MATRIX B)
{
    if((this->n!=B.n)||(this->m!=B.m))
    {
        return 0;
    }
    for(int i=0;i<this->n;i++)
    {
        for(int j=0;j<this->m;j++)
        {
            this->tmp_matrix[i][j]=this->matrix[i][j]+B.matrix[i][j];
        }
    }
    return 1;
}


int MATRIX::matrix_minus(MATRIX B)
{
    if((this->n!=B.n)||(this->m!=B.m))
    {
        return 0;
    }
    for(int i=0;i<this->n;i++)
    {
        for(int j=0;j<this->m;j++)
        {
            this->tmp_matrix[i][j]=this->matrix[i][j]-B.matrix[i][j];
        }
    }
    return 1;
}


void MATRIX::times(int k)
{
    for(int i=0;i<this->n;i++)
    {
        for(int j=0;j<this->m;j++)
        {
            this->tmp_matrix[i][j]=this->matrix[i][j]*k;
        }
    }
}


int MATRIX::matrix_times(MATRIX B)
{
    if(this->m!=B.n)
    {
        return 0;
    }
    int sum=0;
    int r;
    for(int i=0;i<this->n;i++)
    {
        for(int j=0;j<B.m;j++)
        {
            sum=0;
            r=0;
            while(1)
            {
                sum=sum+this->matrix[i][r]*B.matrix[r][j];
                r++;
                if(r==this->m)
                break;
            }
            this->tmp_matrix[i][j]=sum;
        }
    }
    return 1;
}


int cal_sign(int a[10],int n)
{
    int count=0;
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<i;j++)
        {
            if(a[j]>a[i])
            {
                count++;
            }
        }
    }
    if(count%2==0)
    {
        return 1;
    }
    return -1;
}


int cal_time_value(int a[10],int n)
{
    int sum=1;
    for(int i=0;i<n;i++)
    {
        sum=sum*a[i];
    }
    return sum;
}


void funtion_get_det(int a[10][10],int n,int step)
{
    if(step==n)
    {
        int sign=cal_sign(tmp_coffeicient,n);
        result=result+sign*cal_time_value(tmp_array,n);
        return ;
    }
    else
    {
        for(int i=0;i<n;i++)
        {
            if(used[i]==0)
            {
                used[i]=1;
                tmp_array[step]=a[step][i];
                tmp_coffeicient[step]=i;
                funtion_get_det(a,n,step+1);
                used[i]=0;
            }
        }
    }
}


int MATRIX::cal_det()
{
    if(this->m!=this->n)
    {
        return 0 ;
    }
    this->det=0;
    funtion_get_det(this->matrix,this->n,0);
    this->det=result;
    return 1;
}


void MATRIX::show_det()
{
    cout<<this->det<<endl;
}


void companion_change_matrix(int a[10][10],int b[10][10],int n,int x,int y)
{
    int I=0;
    int J=0;
    for(int i=0;i<n;i++)
    {
        if(i==x)continue;
        for(int j=0;j<n;j++)
        {
            if(j==y) continue;
            b[I][J]=a[i][j];
            J++;
        }
        I++;
        J=0;
    }
}


int MATRIX::cal_companion()
{
    if(this->n!=this->m)
    {
        return 0;
    }
    int tmp_matrix[10][10]={{0}};
    for(int i=0;i<this->n;i++)
    {
        for(int j=0;j<this->m;j++)
        {
            for(int l=0;l<10;l++)
            {
                used[l]=0;
                tmp_array[l]=0;
            }
            result=0;
            companion_change_matrix(this->matrix,tmp_matrix,this->n,i,j);
            this->companion_matrix[j][i]=0;
            funtion_get_det(tmp_matrix,n-1,0);
            this->companion_matrix[j][i]=result*pow(-1,i+j);
        }
    }
    return 1;
}


void MATRIX::show_companion()
{
    for(int i=0;i<this->n;i++)
    {
        for(int j=0;j<this->m;j++)
        {
            cout<<this->companion_matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}


int main()
{
    int n,m;
    cout<<"this is a program about matrix"<<endl<<endl<<endl;
    cout<<"please enter the row number of the first matrix"<<endl;
    cin>>n;
    cout<<"please enter the col number of the first matrix"<<endl;
    cin>>m;
    cout<<"please enter the first matrix"<<endl;
    int matrix[10][10]={{0}};
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>matrix[i][j];
        }
    }
    MATRIX A(matrix,n,m);
    int number;
    cout<<"if your calculation needs more than one matrix, please enter 1"<<endl;
    cout<<"if your calculation only needs one matrix, please enter 2"<<endl;
    cin>>number;
    if(number==1)
    {
        cout<<"please enter the row number of the second matrix"<<endl;
        int n0;
        cin>>n0;
        cout<<"please enter the col number of the second matrix"<<endl;
        int m0;
        cin>>m0;
        cout<<"please enter your second matrix"<<endl;
        int matrix0[10][10]={{0}};
        for(int i=0;i<n0;i++)
        {
            for(int j=0;j<m0;j++)
            {
                cin>>matrix0[i][j];
            }
        }
        MATRIX B(matrix0,n0,m0);
        int order;
        cout<<"if you want to multipy them, please enter 1"<<endl;
        cout<<"if you want to add them up, please enter 2"<<endl;
        cout<<"if you want to subtract them, please enter 3"<<endl;
        cin>>order;
        if(order==1)
        {    
            if(A.matrix_times(B)==0)
            {
                cout<<"sorry,these two matrixs can not multipy"<<endl;
            }
            if(A.matrix_times(B)==1)
            {
                cout<<"the result is:"<<endl;
                A.show_matrix();
            }
        }
        if(order==2)
        {
            if(A.matrix_add(B)==0)
            {
                cout<<"sorry,these two matrixs can not add up"<<endl;
            }
            if(A.matrix_add(B)==1)
            {
                cout<<"the result is:"<<endl;
                A.show_matrix();
            }
        }
        if(order==3)
        {
            if(A.matrix_minus(B)==0)
            {
                cout<<"these two matrixs can not subtract"<<endl;
            }
            if(A.matrix_minus(B)==1)
            {
                cout<<"the result is:"<<endl;
                A.show_matrix();
            }
        }
    }
    else if (number==2)
    {
        int order;
        cout<<"if you want to calculate the det of matrix, please enter 1"<<endl;
        cout<<"if you want to calculate the companion matrix, please enter 2"<<endl;
        cout<<"if you want to calculate the rank of matrix, please enter 3"<<endl;
        cin>>order;
        if(order==1)
        {
            if(A.cal_det()==0)
            {
                cout<<"sorry,this matrix can not calculate det"<<endl;
            }
            else
            {
                cout<<"the result is:"<<endl;
                A.show_det();
            }
        }
        if(order==2)
        {
            if(A.cal_companion()==0)
            {
                cout<<"sorry,this matrix can not calculate the companion matrix"<<endl;
            }
            else
            {
                cout<<"the result is:"<<endl;
                A.show_companion();
                A.cal_det();
                cout<<"and the det of the matrix is ";
                A.show_det();
            }
        }
        if(order==3)
        {
            cout<<"sorry, we can not calculate the rank of matrix.This funtion is still needing to be developed."<<endl;
        }
    }
    cout<<endl<<endl<<endl<<"waiting to be improved......"<<endl;
    return 0;
}




猜你喜欢

转载自blog.csdn.net/m0_47055280/article/details/106795749