C实现矩阵加减乘除运算



1、矩阵的加减乘除求逆运算的概念:

  (1)矩阵概念

      有m n个数排列成一个mn 列,并括以方括弧(或圆括弧)的数表称为mn 列矩阵。

  (2)矩阵加法: 

   (3)矩阵乘法:

  (4)矩阵的求逆运算

  (5)矩阵的除法:

    分成两种(1)A\B=inverse(A)*B  (2)B/A=B*inverse(A),理解上可能有误,不过是按照这两种方式来运算的。。

2、要求:

  要求很简单:编写一个实现矩阵(向量)的+ - * / 求逆运算的类(女友的一个作业题)

3、实现代码

  

View Code
复制代码
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #define col 3
  4 #define row 3
  5 class matrix//类的定义
  6 {
  7 private:
  8     double m[col][row];//矩阵设置为私有的,
  9 public:
 10     matrix(){}//无参数的构造函数
 11     matrix(double a[col][row]);//有参数的构造函数
 12     matrix Add(matrix &b);//加法运算声明
 13     matrix Sub(matrix &b);//减法运算声明
 14     matrix Mul(matrix &b);//乘法运算声明
 15     matrix Div(matrix &b);//除法运算声明
 16     matrix Inverse();//求逆运算声明
 17     ~matrix();//析构函数声明
 18     void display();//显示函数声明
 19 };
 20 matrix::matrix(double a[col][row])//构造函数的定义
 21 {
 22     int i,j;
 23     for(i=0;i<col;i++)
 24         for(j=0;j<row;j++)
 25             m[i][j]=a[i][j];
 26 }
 27 matrix matrix::Add(matrix &b)//加法运算
 28 {
 29     int i,j;
 30     matrix*c=(matrix*)malloc(sizeof(matrix));
 31     for(i=0;i<col;i++)
 32         for(j=0;j<row;j++)
 33             c->m[i][j]=m[i][j]+b.m[i][j];
 34     return(*c);
 35 }
 36 matrix matrix::Sub(matrix &b)//减法运算
 37 {
 38     int i,j;
 39     matrix*c=(matrix*)malloc(sizeof(matrix));
 40     for(i=0;i<col;i++)
 41         for(j=0;j<row;j++)
 42             c->m[i][j]=m[i][j]-b.m[i][j];
 43     return *c;
 44 }
 45 matrix matrix::Mul(matrix &b)//乘法运算
 46 {
 47     int i,j,k;
 48     double sum=0;
 49     matrix*c=(matrix*)malloc(sizeof(matrix));
 50     for(i=0;i<col;i++)
 51     {
 52         for(j=0;j<row;j++)
 53         {
 54             for(k=0;k<row;k++)
 55                 sum+=m[i][k]*(b.m[k][j]);
 56             c->m[i][j]=sum;
 57             sum=0;
 58         }
 59     }
 60     return(*c);
 61 }
 62 matrix matrix::Div(matrix &b)//除法运算
 63 {
 64     //除法直接求解,参见主函数    
 65     matrix c;
 66     return(c);
 67 }
 68 matrix matrix::Inverse()//求逆运算
 69 {                       //参考博客:http://www.cnblogs.com/rollenholt/articles/2050662.html
 70     int i,j,k,M=col,N=2*col;
 71     double b[col][col*2];
 72     matrix*c=(matrix*)malloc(sizeof(matrix));
 73     for(i=0;i<M;i++)     //赋值        
 74         for(j=0;j<M;j++)                    
 75             b[i][j]=m[i][j];      
 76     for(i=0;i<M;i++)    //扩展      
 77         for(j=M;j<N;j++)        
 78         {             
 79             if(i==(j-M))                             
 80                 b[i][j]=1;                      
 81             else                           
 82                 b[i][j]=0;                     
 83         }  
 84     /***************下面进行求逆运算*********/
 85         for(i=0;i<M;i++)    
 86         {         
 87             if(b[i][i]==0)    
 88             {            
 89                 for(k=i;k<M;k++)             
 90                 {                 
 91                     if(b[k][i]!=0)            //作者的博客里面此处为b[k][k],貌似是不正确的,
 92                                               //因为这对比如说是{0,0,1,1,0,1,0,1,1}的矩阵就会判断为不可逆,                    
 93                     {                         //而实际上该矩阵是可逆的,这里应该是作者笔误,待进一步求证        
 94                         for(int j=0;j<N;j++)                     
 95                         {                         
 96                             double temp;                        
 97                             temp=b[i][j];                       
 98                             b[i][j]=b[k][j];                      
 99                             b[k][j]=temp;                    
100                         }                 
101                         break;                 
102                     }            
103                 }            
104                 if(k==M)
105                 {
106                     printf("该矩阵不可逆!\n"); 
107                     exit(0);
108                 }
109             }        
110             for(j=N-1;j>=i;j--)                  
111                 b[i][j]/=b[i][i];   
112             
113             for(k=0;k<M;k++)     
114             {         
115                 if(k!=i)      
116                 {              
117                     double temp=b[k][i];         
118                     for(j=0;j<N;j++)                              
119                         b[k][j]-=temp*b[i][j];             
120                 }       
121             }   
122         } 
123     /**********************导出结果******************/
124         for(i=0;i<M;i++)           
125             for(j=3;j<N;j++)        
126                 c->m[i][j-3]=b[i][j];    
127     return (*c);
128 }
129 
130 matrix::~matrix()
131 {}
132 void matrix::display()
133 {
134     int i,j;
135     for(i=0;i<col;i++)
136     {
137         for(j=0;j<row;j++)
138             printf("%f  ",m[i][j]);
139         printf("\n");
140     }
141 }
142 void main()
143 {
144     double a[3][3]={{1,0,1},{0,1,1},{0,3,1}};
145     double b[3][3]={{0,0,1},{1,0,1},{0,1,0}};
146     matrix ma(a),mb(b),mc;
147     int flag;
148     printf("----------------------------------------------------\n请选择要进行的操作:\n1、打印\t2、加法");
149     printf("\t3、减法\n4、乘法\t5、除法\t6、求逆\n7、退出\n");
150     printf("-----------------------------------------------------\n");
151     scanf("%d",&flag);
152     while((flag==1)||(flag==2)||(flag==3)||(flag==4)||(flag==5)||(flag==6)||(flag==7))
153     {
154         if(flag==1)
155         {
156             printf("矩阵a为:\n");
157             ma.display();
158             printf("矩阵b为:\n");
159             mb.display();
160         }
161         if(flag==2)//矩阵加法运算
162         {
163             printf("矩阵加法运算结果:\n");
164             mc=ma.Add(mb);
165             mc.display();
166         }
167         else if(flag==3)//矩阵减法运算
168         {
169                 printf("矩阵减法运算结果:\n");
170             mc=ma.Sub(mb);
171             mc.display();
172         }
173         else if(flag==4)//矩阵乘法运算
174         {
175                 printf("矩阵乘法运算结果:\n");
176             mc=ma.Mul(mb);
177             mc.display();
178         }
179         else if(flag==5)//矩阵除法运算
180         {
181             printf("矩阵除法运算结果:\n");
182             printf("矩阵的除法分成两类:\n 1、A\\B=inverse(A)*B \n 2、B/A=B*inverse(A)\n");
183             printf("采用第1类,则a\\b的结果为:\n");
184             mc=ma.Inverse();
185             mc=mc.Mul(mb);
186             mc.display();
187             printf("采用第2类,则a/b的结果为:\n");
188             mc=mb.Inverse();
189             mc=ma.Mul(mc);
190             mc.display();
191         }
192         else if (flag==6)//矩阵求逆运算
193         {
194             printf("矩阵a求逆运算结果为:\n");
195             mc=ma.Inverse();
196             mc.display();
197 
198             printf("矩阵b求逆运算结果为:\n");
199             mc=mb.Inverse();
200             mc.display();
201         }
202         else {exit(0);}
203     printf("----------------------------------------------------\n请选择要进行的操作:\n1、打印\t2、加法");
204     printf("\t3、减法\n4、乘法\t5、除法\t6、求逆\n7、退出\n");
205     printf("-----------------------------------------------------\n");
206     scanf("%d",&flag);
207     }
208 }
复制代码
  • 6
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的矩阵类,包括加减乘除运算实现: ```c++ #include<iostream> #include<vector> using namespace std; class Matrix { private: int rows; //行数 int cols; //列数 vector<vector<double>> data; //矩阵数据 public: Matrix(int rows, int cols, double init = 0.0) { this->rows = rows; this->cols = cols; data.resize(rows); for (int i = 0; i < rows; i++) { data[i].resize(cols, init); } } Matrix operator+(Matrix &other) { if (this->rows != other.rows || this->cols != other.cols) { cerr << "Error: Matrix dimensions must match." << endl; exit(1); } Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.data[i][j] = this->data[i][j] + other.data[i][j]; } } return result; } Matrix operator-(Matrix &other) { if (this->rows != other.rows || this->cols != other.cols) { cerr << "Error: Matrix dimensions must match." << endl; exit(1); } Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.data[i][j] = this->data[i][j] - other.data[i][j]; } } return result; } Matrix operator*(Matrix &other) { if (this->cols != other.rows) { cerr << "Error: Matrix dimensions must match." << endl; exit(1); } Matrix result(this->rows, other.cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < other.cols; j++) { double sum = 0.0; for (int k = 0; k < cols; k++) { sum += this->data[i][k] * other.data[k][j]; } result.data[i][j] = sum; } } return result; } Matrix operator/(double scalar) { Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.data[i][j] = this->data[i][j] / scalar; } } return result; } void print() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cout << data[i][j] << " "; } cout << endl; } } }; int main() { Matrix a(2, 3, 1.0); Matrix b(3, 2, 2.0); Matrix c = a + a; Matrix d = a - a; Matrix e = a * b; Matrix f = a / 2.0; c.print(); d.print(); e.print(); f.print(); return 0; } ``` 在矩阵类中,重载了加减乘除运算符,分别对应矩阵加减乘除操作。其中,加减操作需要保证两个矩阵的维度相同,乘法操作需要保证左矩阵的列数等于右矩阵的行数,除法操作则是将矩阵中的每个元素除以一个标量。 在 `main` 函数中,创建了两个矩阵 `a` 和 `b`,分别为 $2 \times 3$ 和 $3 \times 2$ 的矩阵。通过对矩阵进行加减乘除操作,得到了新的矩阵 `c`、`d`、`e` 和 `f`,并分别输出到屏幕上。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值