📄 矩阵运算.txt
字号:
1,矩阵运算规则: (1)只有同型矩阵才能相加减,矩阵可与一个标量相加减; (2)只有有相邻公共阶的两矩阵才能相乘 Cm...(4)左除(\)运算两矩阵的行数相等,右除(/)运算两矩 阵的列数相等. 2,数组运算规则: 参与数组运算的数组必须...
1/**//***
2*
3* author:XieXiaokui
5* purpose:Defines functions for matrix
6*
7***/
8#pragma once
9
10#include <iostream>
11#include <fstream>
12
13#include <string>
14#include <sstream>
15#include <algorithm>
16#include <functional>
17#include <numeric>
18#include <iterator>
19#include <cassert>
20
21#include "MatrixException.h"
22
23using namespace std;
24
25class Matrix
26{
27public:
28
29 // Constructors
30 explicit Matrix();
31 explicit Matrix(int size);
32 Matrix(int row,int col);
33 Matrix(const Matrix& m);
34
35 // Destructor
36 ~Matrix();
37
38 // Assignment operators
39 Matrix& operator= (const Matrix& m);
40
41 // Value extraction method
42 int GetRow() const;
43 int GetCol() const;
44
45 // Subscript operator
46 double operator()(int i,int j)const; //subscript operator to get individual elements
47 double& operator()(int i,int j); //subscript operator to set individual elements
48
49 // Unary operators
50 Matrix operator+() const; //unary negation operator
51 Matrix operator-() const; //unary negation operator
52
53 //Binary operators
54 Matrix operator+(const Matrix& m) const;
55 Matrix operator-(const Matrix& m) const;
56 Matrix operator*(const Matrix& m) const;
57// Matrix operator*(double d)const;
58 Matrix operator/(const Matrix& m) const;
59 Matrix operator/(double d) const;
60 Matrix operator^(int pow) const;
61
62 bool operator== (const Matrix& m) const; //logical equal-to operator
63 bool operator!= (const Matrix& m) const; //logical not-equal-to operator
64
65 friend Matrix operator* (double d,const Matrix& m);
66 friend Matrix operator/ (double d,const Matrix& m);
67
68
69
70 // Combined assignment - calculation operators
71 Matrix& operator +=(const Matrix& m) const;
72 Matrix& operator -=(const Matrix& m) const;
73 Matrix& operator *=(const Matrix& m) const;
74 Matrix& operator *=(double d) const;
75 Matrix& operator /=(const Matrix& m) const;
76 Matrix& operator /=(double d) const;
77 Matrix& operator ^=(int pow) const;
78
79 // Miscellaneous -methods
80 void SetZero() ; //zero matrix:零阵
81 void SetUnit() ; //unit matrix:单位阵
82 void SetSize(int size) ; //rsizing matrix
83 void SetSize(int row,int col) ; //resizing matrix
84
85 // Utility methods
86 Matrix Solve(const Matrix& m)const; //
87 Matrix Adjoin() const; //adjoin matrix:伴随矩阵
88 double Determinant() const; //determinant:行列式
89 double Norm() const; //norm:模
90 Matrix Inverse() const; //inverse:逆阵
91 Matrix Transpose() const; //transpose:转置
92 double Cofactor() const; //confactor
93 double Condition() const; //the condition number of a matrix
94 int Pivot(int row) const; // partial pivoting
95
96 //primary change
97 Matrix& Exchange(int i,int j);// 初等变换 对调两行:ri<-->rj
98 Matrix& Multiple(int index,double k); //初等变换 第index 行乘以k
99 Matrix& MultipleAdd(int index,int src,double k); //初等变换 第src行乘以k加到第index行
100
101
102 // Type of matrices
103 bool IsSquare() const; //determine if the matrix is square:方阵
104 bool IsSingular() const; //determine if the matrix is singular奇异阵
105 bool IsDiagonal() const; //determine if the matrix is diagonal对角阵
106 bool IsScalar() const; //determine if the matrix is scalar数量阵
107 bool IsUnit() const; //determine if the matrix is unit单位阵
108 bool IsZero() const; //determine if the matrix is zero零矩阵
109 bool IsSymmetric() const; //determine if the matrix is symmetric对称阵
110 bool IsSkewSymmetric() const; //determine if the matrix is skew-symmetric斜对称阵
111 bool IsUpperTriangular() const; //determine if the matrix is upper-triangular上三角阵
112 bool IsLowerTriangular() const; //determine if the matrix is lower-triangular下三角阵
113
114 // output stream function
115 friend ostream& operator<<(ostream& os,const Matrix& m);
116
117 // input stream function
118 friend istream& operator>>(istream& is,Matrix& m);
119
120 //conert to string
121 string ToString() const;
122
123protected:
124
125 //delete the matrix
126 void Create(int row,int col);
127 void Clear();
128
129private:
130
131 const static double epsilon;
132
133 double** m_data;
134 size_t m_row;
135 size_t m_col;
136
137};实现文件
1/**//***
2*
3*
4* author:XieXiaokui
5* purpose:Defines functions for matrix
6*
7***/
8#include "stdafx.h"
9
10#include <iostream>
11#include <fstream>
12
13#include <string>
14#include <sstream>
15#include <algorithm>
16#include <functional>
17#include <numeric>
18#include <iterator>
19#include <cmath>
20#include <cassert>
21
22#include "matrix.h"
23
24using namespace std;
25
26const double Matrix::epsilon = 1e-7;
27
28// constructor
29
30Matrix::Matrix():m_row(0),m_col(0),m_data(0)
31{
32}
33
34
35// constructor
36Matrix::Matrix(int size):m_row(size),m_col(size)
37{
38 if(size<=0)
39 {
40 m_row=0;
41 m_col=0;
42 m_data=0;
43
44 ostringstream oss;
45 oss<<"In Matrix::Matrix(int size) size "<<size<<" <=0.Please check it.";
46 throw oss.str();
47
48 }
49
50 m_data=new double*[size];
51
52 for(int i=0;i<size;i++)
53 {
54 m_data[i]=new double[size];
55 }
56
57}
58
59// constructor
60Matrix::Matrix(int row,int col):m_row(row),m_col(col)
61{
62 if(row<=0)
63 {
64 m_row=0;
65 m_col=0;
66 m_data=0;
67
68 ostringstream oss;
69 oss<<"In Matrix::Matrix(int row,int col),row "<<row<<" <=0.Please check it.";
70 throw oss.str();
71 }
72 if(col<=0)
73 {
74 m_row=0;
75 m_col=0;
76 m_data=0;
77
78 ostringstream oss;
79 oss<<" In Matrix::Matrix(int row,int col),col "<<col<<" <=0.Pleasecheck it.";
80 throw oss.str();
81 }
82
83 m_data=new double*[row];
84 for(int i=0;i<row;i++)
85 {
86 m_data[i]=new double[col];
87 }
88}
89
90//copy constructor
91Matrix::Matrix(const Matrix& m):m_row(m.m_row),m_col(m.m_col)
92{
93 m_data = new double*[m_row];
94 for(int i=0;i<m_row;i++)
95 {
96 m_data[i] = new double[m_col];
97 }
98
99 for(int i=0;i<m_row;i++)
100 {
101 copy(m.m_data[i],m.m_data[i]+m_col,m_data[i]);
102 }
103
104}
105
106Matrix::~Matrix()
107{
108 if(m_data != 0)
109 {
110 for(int i=0;i<m_row;i++)
111 {
112 delete[] m_data[i];
113 }
114 delete[] m_data;
115 }
116}
117
118void Matrix::SetZero()
119{
120 for(int i=0;i<m_row;i++)
121 fill(m_data[i],m_data[i]+m_col,0);
122}
123
124void Matrix::SetUnit()
125{
126 for(int i=0;i<m_row;i++)
127 for(int j=0;j<m_col;j++)
128 m_data[i][j] = (i==j)?1:0;
129}
130
131void Matrix::SetSize(int size)
132{
133 Clear();
134 Create(size,size);
135}
136
137
138void Matrix::SetSize(int row,int col)
139{
140 Clear();
141 Create(row,col);
142}
143
144
145void Matrix::Clear()
146{
147
148 if(m_data != 0)
149 {
150 for(int i=0;i<m_row;i++)
151 {
152 delete[] m_data[i];
153 }
154 delete[] m_data;
155 }
156
157 m_row=0;
158 m_col=0;
159 m_data=0;
160}
161
162/**//*
163Matrix& Matrix::Create(int size)
164{
165 if(size<=0)
166 {
167 ostringstream oss;
168 oss<<"In Matrix::Create(int size),size "<<size<<" <=0.Please check it.";
169
170 throw oss.str();
171 }
172
173 if(m_data != 0)
174 {
175 for(int i=0;i<m_row;i++)
176 {
177 delete[] m_data[i];
178 }
179 delete[] m_data;
180 }
181
182 m_row=size;
183 m_col=size;
184
185 m_data=new double*[size];
186 for(int i=0;i<size;i++)
187 {
188 m_data[i]=new double[size];
189 }
190
191 for(int i=0;i<size;i++)
192 {
193 for(int j=0;j<size;j++)
194 {
195 m_data[i][j] = ((i==j)?1:0);
196 }
197 }
198
199 return *this;
200}
201*/
202
203void Matrix::Create(int row,int col)
204{
205 if(row<=0)
206 {
207 ostringstream oss;
208 oss<<"In Matrix::Create(int row,int col),row "<<row<<" <=0.Please check it.";
209 throw oss.str();
210 }
211 if(col<=0)
212 {
213 ostringstream oss;
214 oss<<"In Matrix::Create(int row,int col),col "<<col<<" <=0.Please check it.";
215 throw oss.str();
216 }
217
218 if(m_data != 0)
219 {
220 for(int i=0;i<m_row;i++)
221 {
222 delete[] m_data[i];
223 }
224 delete[] m_data;
225 }
226
227 m_row=row;
228 m_col=col;
229 m_data=new double*[row];
230 for(int i=0;i<row;i++)
231 {
232 m_data[i]=new double[col];
233 }
234}
235
236
237int Matrix::GetRow() const
238{
239 return m_row;
240}
241
242int Matrix::GetCol() const
243{
244 return m_col;
245}
246
247//transpose 转置
248Matrix Matrix::Transpose() const
249{
250 Matrix ret(m_col,m_row);
251 for(int i=0;i<m_row;i++)
252 {
253 for(int j=0;j<m_col;j++)
254 {
255 ret.m_data[j][i] = m_data[i][j];
256 }
257 }
258 return ret;
259}
260
261int Matrix::Pivot(int row) const
262{
263 int index=row;
264
265 for(int i=row+1;i<m_row;i++)
266 {
267 if(m_data[i][row] > m_data[index][row])
268 index=i;
269 }
270
271 return index;
272}
273
274
275Matrix& Matrix::Exchange(int i,int j) // 初等变换:对调两行ri<-->rj
276{
277 if(i<0 || i>=m_row)
278 {
279 ostringstream oss;
280 oss<<"In void Matrix::Exchange(int i,int j) ,i "<<i<<" out of bounds.Please check it.";
281 throw oss.str();
282 }
283
284 if(j<0 || j>=m_row)
285 {
286 ostringstream oss;
287 oss<<"In void Matrix::Exchange(int i,int j) ,j "<<j<<" out of bounds.Please check it.";
288 throw oss.str();
289 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -