• c++实现矩阵类矩阵行列式,伴随矩阵,逆矩阵


      1 //Matrix ver1.0
      2 //只支持矩阵内部(方阵)的运算
      3 #include<iostream>
      4 #include<math.h>
      5 using namespace std;
      6 class matrix
      7 {
      8     double**num;
      9     int **e;//单位矩阵
     10     int r;
     11     int c;
     12     unsigned int *array;//为全排序原矩阵列标提供初始顺序模板
     13     unsigned int*arr;//为全排序伴随矩阵列标提供初始顺序模板
     14     int**b;//存放原矩阵列标
     15     int j;//计数变量,原矩阵的全排列
     16     int po;//计数变量,伴随矩阵的全排列
     17 
     18     double **comp;//伴随矩阵
     19     double**s;//存放每一个num的余子式
     20     int**sb;//存放每一个余子式的列标
     21 
     22 public:
     23     double caculate();
     24     matrix(int n, int m);//nrow
     25     void setmat();
     26     bool checkcomp() { if (po)return true; return false; }
     27     void showm();
     28     void showinver(double k);
     29     void swap(unsigned int* array, int i, int j);
     30     void FullArray(int n, int index);
     31     int func(int n) { if (n == 0)return 1; else return n*func(n - 1); }
     32     int ni(int a[], int n);
     33     void sets(int a, int b);
     34     double cacu(int a, int b);
     35     void setcomp();
     36     void showcomp();
     37     void fullArray(int n, int index = 0);
     38     void shows();
     39     void showsb();
     40 
     41 };
     42 void matrix::shows()//r
     43 {
     44     for (int i = 0; i < r - 1; i++)
     45     {
     46         for (int j = 0; j < c - 1; j++)
     47             cout << s[i][j] << " ";
     48         cout << endl;
     49     }
     50 }
     51 void matrix::showcomp()
     52 {
     53     for (int i = 0; i < r; i++)
     54     {
     55         for (int j = 0; j < c; j++)
     56             cout << comp[i][j] << " ";
     57         cout << endl;
     58     }
     59 }
     60 void matrix::setcomp()
     61 {
     62     for (int i = 0; i < r; i++)
     63         for (int j = 0; j < c; j++)
     64             comp[i][j] = pow(-1, j + i)*cacu(i, j);
     65 
     66 }
     67 double matrix::cacu(int a, int b)//计算每一位的代数余子式
     68 {
     69     sets(a, b);
     70     double sum = 0;
     71     double *p = new double[c - 1];
     72     for (int i = 0; i < func(c - 1); i++)
     73     {
     74         p[i] = 1;
     75         for (int j = 0; j < c - 1; j++)
     76             p[i] *= s[j][sb[i][j]];
     77         p[i] = p[i] * pow(-1, ni(sb[i], c - 1));
     78         sum += p[i];
     79     }
     80     return sum;
     81 }
     82 void  matrix::sets(int a, int b)//把(a,b)的余子式存入**s
     83 {
     84     int t = 0;
     85     int *cun = new int[(c - 1)*(r - 1)];
     86     for (int i = 0; i < r; i++)
     87         for (int j = 0; j < c; j++)
     88         {
     89             if (i != a&&j != b)
     90             {
     91                 cun[t] = num[i][j];
     92                 t++;
     93             }
     94         }
     95     for (int i = 0; i < r - 1; i++)
     96         for (int j = 0; j < c - 1; j++)
     97             s[i][j] = cun[i*(r - 1) + j];
     98 }
     99 void  matrix::showm()//r
    100 {
    101     for (int i = 0; i < r; i++)
    102     {
    103         for (int j = 0; j < c; j++)
    104             cout << num[i][j] << " ";
    105         cout << endl;
    106     }
    107 }
    108 int matrix::ni(int a[], int n)//逆序数
    109 {
    110     int s = 0;
    111     for (int j = n - 1; j >= 0; j--)
    112         for (int i = 0; i < j; i++)
    113         {
    114             if (a[j] < a[i])
    115                 s++;
    116         }
    117     return s;
    118 }
    119 void matrix::swap(unsigned int*array, int i, int j)//r
    120 {
    121     int t = array[i];
    122     array[i] = array[j];
    123     array[j] = t;
    124 }
    125 void  matrix::showsb()
    126 {
    127     for (int i = 0; i < func(r - 1); i++)
    128     {
    129         for (int j = 0; j < c - 1; j++)
    130             cout << sb[i][j];
    131         cout << endl;
    132     }
    133 }
    134 void  matrix::fullArray(int n, int index)//对于伴随矩阵全排列
    135 {
    136     if (index >= n)
    137     {
    138         for (int i = 0; i <n; ++i)
    139         {
    140             sb[po][i] = arr[i];
    141         }
    142         po++;
    143         //return;
    144     }
    145 
    146     for (int i = index; i < n; ++i)
    147     {
    148         swap(arr, index, i);
    149         fullArray(n, index + 1);
    150         swap(arr, index, i);
    151     }
    152 }
    153 void  matrix::FullArray(int n, int index)//r
    154 {
    155     if (index >= n)
    156     {
    157         for (int i = 0; i <n; ++i)
    158         {
    159             b[j][i] = array[i];//把全排列存入b[][]中
    160         }
    161         j++;
    162     }
    163 
    164     for (int i = index; i < n; ++i)
    165     {
    166         swap(array, index, i);
    167         FullArray(n, index + 1);
    168         swap(array, index, i);
    169     }
    170 }
    171 
    172 void matrix::showinver(double k)
    173 {
    174     for (int i = 0; i < r; i++)
    175     {
    176         for (int j = 0; j < c; j++)
    177             cout << comp[i][j] / k << " ";
    178         cout << endl;
    179     }
    180 }
    181 matrix::matrix(int n, int m) :r(n), c(m)//r
    182 {
    183     j = 0;
    184     array = new unsigned int[c];
    185     for (int i = 0; i < c; i++)
    186         array[i] = i;
    187     arr = new unsigned int[c - 1];
    188     for (int i = 0; i < c - 1; i++)
    189         arr[i] = i;
    190     num = new double*[n];
    191     b = new int*[func(c)];
    192     for (int i = 0; i < r; i++)
    193         num[i] = new double[c];
    194     for (int i = 0; i < func(c); i++)
    195         b[i] = new int[c];
    196     e = 0;
    197 
    198     sb = 0;
    199     s = 0;
    200     po = 0;
    201     if (r == c)
    202     {
    203         sb = new int*[func(c - 1)];
    204         for (int i = 0; i < func(c - 1); i++)
    205             sb[i] = new int[c - 1];
    206         s = new double*[r - 1];
    207         for (int i = 0; i < r; i++)
    208             s[i] = new double[c - 1];
    209         comp = new double*[r];
    210 
    211         for (int i = 0; i < r; i++)
    212             comp[i] = new double[c];
    213 
    214 
    215         e = new int*[r];
    216         for (int i = 0; i < r; i++)
    217             e[i] = new int[c];
    218         for (int i = 0; i < r; i++)
    219             for (int j = 0; j < c; j++)
    220             {
    221                 if (i == j)
    222                     e[i][j] = 1;
    223                 else
    224                     e[i][j] = 0;
    225             }
    226     }
    227 }
    228 void matrix::setmat()
    229 {
    230     cout << "请按行输入你的矩阵:" << endl;
    231     for (int i = 0; i < r; i++)
    232         for (int j = 0; j < c; j++)
    233             cin >> num[i][j];
    234     //num[i][j] = i*r + j;
    235 }
    236 double matrix::caculate()//计算原矩阵行列式
    237 {
    238     FullArray(c, 0);
    239     double sum = 0;
    240     double *p = new double[c];
    241     for (int i = 0; i < func(r); i++)
    242     {
    243         p[i] = 1;
    244         for (int j = 0; j < c; j++)
    245             p[i] *= num[j][b[i][j]];
    246         p[i] = p[i] * pow(-1, ni(b[i], c));
    247         sum += p[i];
    248     }
    249     return sum;
    250 }
    251 void main()
    252 {
    253     int C, R;
    254     int choice = 100;
    255     matrix *p = 0;
    256     double k;//储存行列式的值
    257     while (choice)
    258     {
    259         cout << "1:创建一个矩阵对象
    2:输出矩阵行列式的值
    3:输出伴随矩阵
    4:输出逆矩阵
    5:结束" << endl;
    260         cout << "请输入你的选择" << endl;
    261         cin >> choice;
    262         switch (choice)
    263         {
    264         case 1:
    265             cout << "请输入矩阵的行与列:" << endl;
    266             cin >> C >> R;
    267             p = new matrix(C, R);
    268             p->setmat();
    269             break;
    270         case 2:
    271             if (C == R)
    272             {
    273                 k = p->caculate();
    274                 cout << "矩阵行列式的值:" << k << endl;
    275             }
    276             else
    277                 cout << "非方阵,无法计算" << endl;
    278             break;
    279         case 3:
    280             if (C == R)
    281             {
    282                 p->fullArray(C - 1, 0);//先给下标全排序好,存入相应数组
    283                 p->setcomp();
    284                 p->showcomp();
    285             }
    286             else
    287                 cout << "非方阵,无法计算" << endl;
    288             break;
    289         case 4:
    290             if (C == R)
    291                     if (p->checkcomp())
    292                         p->showinver(k);
    293                     else
    294                         cout << "请先计算伴随矩阵。" << endl;
    295             else
    296                 cout << "非方阵,无法计算" << endl;
    297             break;
    298         case 5:
    299             choice = 0;
    300             break;
    301         }
    302     }
    303 }

    =====================6.18update&BUG已改===========================

  • 相关阅读:
    InnoDB和MyISAM区别总结
    jquery判断checkbox是否被选中
    手机网站重构经验分享(S60V3篇)
    域名A记录、MX记录、CNAME、TTL
    ubuntu apache2配置详解(含虚拟主机配置方法)
    IP分片攻击
    Linux+Apache+Mysql+PHP典型配置
    apache配置优化测试
    PHP 正则表达式资料
    人人网UGC技术广播站
  • 原文地址:https://www.cnblogs.com/yuelien/p/5373823.html
Copyright © 2020-2023  润新知