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


      1 #include"iostream"
      2 #include"time.h"
      3 using namespace std;
      4 class matrix{
      5 private:
      6     double **a;
      7     int list;
      8     int line;
      9 public:
     10     matrix(int line,int list);
     11     void creatmatrix(double *a1);    //通过一维数组创建矩阵
     12     void cinmatrix();                //通过输入创建矩阵
     13     void show();                    //显示矩阵
     14     matrix& operator*(matrix &m);    //计算矩阵
     15     matrix& operator*(double b);
     16     matrix& operator+(matrix &m);
     17     matrix& operator-(matrix &m);
     18     double determinant();            //计算方阵的行列式
     19     matrix& adjoint();                //伴随矩阵
     20     matrix& inverse();                //逆矩阵
     21     void info(){
     22         if(line != list){
     23             return;
     24         }
     25         cout<<"方阵的行列式:"<<determinant()<<endl;
     26         cout<<"伴随矩阵:
    ";
     27         adjoint().show();
     28         cout<<"逆矩阵:
    ";
     29         inverse().show();
     30     }
     31 };
     32 //3 3 1 2 1 2 1 0 1 0 1
     33 //3 3 2 1 1 1 4 -4 1 0 2
     34 //4 4 1 1 1 1 2 4 3 1 4 16 9 1 8 64 27 1
     35 int main(){
     36     void randtest();
     37     void cinmatrix(int n = 1);
     38     randtest();                    //随机生成矩阵
     39     //cinmatrix();                //输入矩阵:行数 列数 所有值
     40     return 0;
     41 }
     42 void randtest(){
     43     const int n = 120;
     44     matrix m1(3,3),m2(3,3);
     45     double a[n];
     46     srand(time(NULL));
     47     for(int i = 0;i < n;i++){
     48         a[i] = rand()%6;
     49     }
     50     m1.creatmatrix(a);
     51     m2.creatmatrix(a);
     52     m1.show();
     53     m1.info();
     54     //m2.show();
     55     //(m1 * m2).show();
     56 }
     57 void cinmatrix(int n){
     58     int line,list;
     59     cout<<"输入矩阵的行,列和各个值,空格隔开"<<endl;
     60     cin>>line>>list;
     61     matrix m1(line,list);
     62     matrix *m2;
     63     m1.cinmatrix();
     64     if(n == 2){
     65         cin>>line>>list;
     66         m2 = new matrix(line,list);
     67         m2->cinmatrix();
     68     }
     69     m1.show();
     70     m1.info();
     71     if(n == 2){
     72         m2->show();
     73         (m1 * *m2).show();
     74     }
     75 }
     76 matrix::matrix(int line,int list){
     77     this->list = list;
     78     this->line = line;
     79     a = new double*[line];
     80     for(int i = 0;i < line;i++){
     81         a[i] = new double[list];
     82     }
     83 }
     84 
     85 void matrix::creatmatrix(double *a1){
     86     for(int i = 0;i < line;i++){
     87         for(int j = 0;j < list;j++){
     88             a[i][j] = a1[i * list + j];
     89         }
     90     }
     91 }
     92 void matrix::cinmatrix(){
     93     for(int i = 0;i < line;i++){
     94         for(int j = 0;j < list;j++){
     95             cin>>a[i][j];
     96         }
     97     }
     98 }
     99 void matrix::show(){
    100     cout.precision(3);
    101     for(int i = 0;i < line;i++){
    102         cout<<"|	";
    103         for(int j = 0;j < list;j++){
    104             cout<<a[i][j]<<"	";
    105         }
    106         cout<<"|"<<endl;
    107     }
    108     cout<<endl;
    109 }
    110 
    111 matrix& matrix::operator+(matrix &m){
    112     matrix *m1 = new matrix(this->line,m.list);
    113     if(list != m.list||line != m.line){
    114         cout<<"error"<<endl;
    115         return *m1;
    116     }
    117     for(int i = 0;i < line;i++){
    118         for(int j = 0;j < m.list;j++){
    119             m1->a[i][j] = a[i][j] + m.a[i][j];
    120         }
    121     }
    122     return *m1;
    123 }
    124 
    125 matrix& matrix::operator-(matrix &m){
    126     matrix *m1 = new matrix(this->line,m.list);
    127     if(list != m.list||line != m.line){
    128         cout<<"error"<<endl;
    129         return *m1;
    130     }
    131     for(int i = 0;i < line;i++){
    132         for(int j = 0;j < m.list;j++){
    133             m1->a[i][j] = a[i][j] - m.a[i][j];
    134         }
    135     }
    136     return *m1;
    137 }
    138 
    139 matrix& matrix::operator*(matrix &m){
    140     matrix *m1 = new matrix(this->line,m.list);
    141     if(list != m.line){
    142         cout<<"error"<<endl;
    143         return *m1;
    144     }
    145     for(int i = 0;i < line;i++){
    146         for(int j = 0;j < m.list;j++){
    147             double sum = 0;
    148             for(int w = 0,h = 0;w < this->list;w++,h++){
    149                 sum += this->a[i][w] * m.a[h][j];
    150             }
    151             m1->a[i][j] = sum;
    152         }
    153     }
    154     return *m1;
    155 }
    156 
    157 matrix& matrix::operator *(double b){
    158     matrix *m1 = new matrix(line,list);
    159     for(int i = 0;i < line;i++){
    160         for(int j = 0;j < list;j++){
    161             m1->a[i][j] = a[i][j] * b;
    162         }
    163     }
    164     return *m1;
    165 }
    166 
    167 double matrix::determinant(){
    168     if(list!=line){
    169         cout<<"error"<<endl;
    170         return 0;
    171     }
    172     if(list==1){
    173         return a[0][0];
    174     }
    175     else{
    176         double result = 0;
    177         matrix b(line - 1,list - 1);
    178         for(int j = 0;j < list;j++){
    179             int i2 = 0,j2 = 0;
    180             for(int i1 = 1;i1 < line;i1++){
    181                 for(int j1 = 0;j1 < list;j1++){
    182                     if(j1 != j){                    //划去第一行第j1列,创建一个新矩阵
    183                         b.a[i2][j2++] = a[i1][j1];
    184                         if(j2 == b.list){
    185                             j2 = 0;
    186                             i2++;
    187                         }
    188                     }
    189                 }
    190             }
    191             if(j%2==0){
    192                 result += a[0][j] * b.determinant();
    193             }
    194             else{
    195                 result -= a[0][j] * b.determinant();
    196             }
    197         }
    198         return result;
    199     }
    200 }
    201 matrix& matrix::adjoint(){                //伴随矩阵
    202     matrix *m = new matrix(line,list);
    203     matrix m1(line - 1,list - 1);
    204     if(line != list){
    205         cerr<<"out of space!"<<endl;
    206         return *m;
    207     }
    208     for(int i = 0;i < line;i++){
    209         for(int j = 0;j < list;j++){
    210             int i2 = 0,j2 = 0;
    211             for(int i1 = 0;i1 < line;i1++){
    212                 for(int j1 = 0;j1 < list;j1++){
    213                     if(i1 != i && j1 != j){
    214                         m1.a[i2][j2++] = a[i1][j1];
    215                         if(j2 == m1.list){
    216                             i2++;
    217                             j2 = 0;
    218                         }
    219                     }
    220                 }
    221             }
    222             if((j + i) % 2 == 0){
    223                 m->a[j][i] = m1.determinant();
    224             }
    225             else{
    226                 m->a[j][i] = -1 * m1.determinant();
    227             }
    228         }
    229     }
    230     return *m;
    231 }
    232 
    233 matrix& matrix::inverse(){
    234     matrix m = this->adjoint();
    235     return m * (1 / this->determinant());
    236 }
  • 相关阅读:
    练手
    课余时间娱乐下
    2017-2-19,作业
    JavaScript(下)
    JavaScript(上)
    Day15:网络编程-HTTP
    小知识:静态导入
    多线程知识点:锁
    多线程知识点:同步
    Day16:反射技术
  • 原文地址:https://www.cnblogs.com/oleolema/p/9028412.html
Copyright © 2020-2023  润新知