线性代数中矩阵的简单计算

Posted O了吗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线性代数中矩阵的简单计算相关的知识,希望对你有一定的参考价值。

#include"iostream"
#include"time.h"
using namespace std;
class matrix{
private:
    double **a;
    int list;
    int line;
public:
    matrix(int line,int list);
    void creatmatrix(double *a1);    //通过一维数组创建矩阵
    void cinmatrix();                //通过输入创建矩阵
    void show();                    //显示矩阵
    matrix& operator*(matrix &m);    //计算矩阵
    matrix& operator*(double b);
    matrix& operator+(matrix &m);
    matrix& operator-(matrix &m);
    double determinant();            //计算方阵的行列式
    matrix& adjoint();                //伴随矩阵
    matrix& inverse();                //逆矩阵
    void info(){
        if(line != list){
            return;
        }
        cout<<"方阵的行列式:"<<determinant()<<endl;
        cout<<"伴随矩阵:\n";
        adjoint().show();
        cout<<"逆矩阵:\n";
        inverse().show();
    }
};
//3 3 1 2 1 2 1 0 1 0 1
//3 3 2 1 1 1 4 -4 1 0 2
//4 4 1 1 1 1 2 4 3 1 4 16 9 1 8 64 27 1
int main(){
    void randtest();
    void cinmatrix(int n = 1);
    randtest();                    //随机生成矩阵
    //cinmatrix();                //输入矩阵:行数 列数 所有值
    return 0;
}
void randtest(){
    const int n = 120;
    matrix m1(3,3),m2(3,3);
    double a[n];
    srand(time(NULL));
    for(int i = 0;i < n;i++){
        a[i] = rand()%6;
    }
    m1.creatmatrix(a);
    m2.creatmatrix(a);
    m1.show();
    m1.info();
    //m2.show();
    //(m1 * m2).show();
}
void cinmatrix(int n){
    int line,list;
    cout<<"输入矩阵的行,列和各个值,空格隔开"<<endl;
    cin>>line>>list;
    matrix m1(line,list);
    matrix *m2;
    m1.cinmatrix();
    if(n == 2){
        cin>>line>>list;
        m2 = new matrix(line,list);
        m2->cinmatrix();
    }
    m1.show();
    m1.info();
    if(n == 2){
        m2->show();
        (m1 * *m2).show();
    }
}
matrix::matrix(int line,int list){
    this->list = list;
    this->line = line;
    a = new double*[line];
    for(int i = 0;i < line;i++){
        a[i] = new double[list];
    }
}

void matrix::creatmatrix(double *a1){
    for(int i = 0;i < line;i++){
        for(int j = 0;j < list;j++){
            a[i][j] = a1[i * list + j];
        }
    }
}
void matrix::cinmatrix(){
    for(int i = 0;i < line;i++){
        for(int j = 0;j < list;j++){
            cin>>a[i][j];
        }
    }
}
void matrix::show(){
    cout.precision(3);
    for(int i = 0;i < line;i++){
        cout<<"|\t";
        for(int j = 0;j < list;j++){
            cout<<a[i][j]<<"\t";
        }
        cout<<"|"<<endl;
    }
    cout<<endl;
}

matrix& matrix::operator+(matrix &m){
    matrix *m1 = new matrix(this->line,m.list);
    if(list != m.list||line != m.line){
        cout<<"error"<<endl;
        return *m1;
    }
    for(int i = 0;i < line;i++){
        for(int j = 0;j < m.list;j++){
            m1->a[i][j] = a[i][j] + m.a[i][j];
        }
    }
    return *m1;
}

matrix& matrix::operator-(matrix &m){
    matrix *m1 = new matrix(this->line,m.list);
    if(list != m.list||line != m.line){
        cout<<"error"<<endl;
        return *m1;
    }
    for(int i = 0;i < line;i++){
        for(int j = 0;j < m.list;j++){
            m1->a[i][j] = a[i][j] - m.a[i][j];
        }
    }
    return *m1;
}

matrix& matrix::operator*(matrix &m){
    matrix *m1 = new matrix(this->line,m.list);
    if(list != m.line){
        cout<<"error"<<endl;
        return *m1;
    }
    for(int i = 0;i < line;i++){
        for(int j = 0;j < m.list;j++){
            double sum = 0;
            for(int w = 0,h = 0;w < this->list;w++,h++){
                sum += this->a[i][w] * m.a[h][j];
            }
            m1->a[i][j] = sum;
        }
    }
    return *m1;
}

matrix& matrix::operator *(double b){
    matrix *m1 = new matrix(line,list);
    for(int i = 0;i < line;i++){
        for(int j = 0;j < list;j++){
            m1->a[i][j] = a[i][j] * b;
        }
    }
    return *m1;
}

double matrix::determinant(){
    if(list!=line){
        cout<<"error"<<endl;
        return 0;
    }
    if(list==1){
        return a[0][0];
    }
    else{
        double result = 0;
        matrix b(line - 1,list - 1);
        for(int j = 0;j < list;j++){
            int i2 = 0,j2 = 0;
            for(int i1 = 1;i1 < line;i1++){
                for(int j1 = 0;j1 < list;j1++){
                    if(j1 != j){                    //划去第一行第j1列,创建一个新矩阵
                        b.a[i2][j2++] = a[i1][j1];
                        if(j2 == b.list){
                            j2 = 0;
                            i2++;
                        }
                    }
                }
            }
            if(j%2==0){
                result += a[0][j] * b.determinant();
            }
            else{
                result -= a[0][j] * b.determinant();
            }
        }
        return result;
    }
}
matrix& matrix::adjoint(){                //伴随矩阵
    matrix *m = new matrix(line,list);
    matrix m1(line - 1,list - 1);
    if(line != list){
        cerr<<"out of space!"<<endl;
        return *m;
    }
    for(int i = 0;i < line;i++){
        for(int j = 0;j < list;j++){
            int i2 = 0,j2 = 0;
            for(int i1 = 0;i1 < line;i1++){
                for(int j1 = 0;j1 < list;j1++){
                    if(i1 != i && j1 != j){
                        m1.a[i2][j2++] = a[i1][j1];
                        if(j2 == m1.list){
                            i2++;
                            j2 = 0;
                        }
                    }
                }
            }
            if((j + i) % 2 == 0){
                m->a[j][i] = m1.determinant();
            }
            else{
                m->a[j][i] = -1 * m1.determinant();
            }
        }
    }
    return *m;
}

matrix& matrix::inverse(){
    matrix m = this->adjoint();
    return m * (1 / this->determinant());
}

 

以上是关于线性代数中矩阵的简单计算的主要内容,如果未能解决你的问题,请参考以下文章

线性代数中矩阵相乘如何计算啊

Python中怎样计算3%乘100

Eigen学习之简单线性方程与矩阵分解

入门必备线性代数与编程语言能摩擦出怎样的火花?

线性代数辅助程序

用python检查矩阵的计算