Code
1
#ifndef MATRIX_H
2
#define MATRIX_H
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
#include<iostream>
5
#include<cstdlib>
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
using namespace std;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
class Matrix;
10
ostream& operator <<(ostream& output, const Matrix& m);
11
istream& operator >>(istream& input , Matrix& m);
12
const bool operator ==(const Matrix& M1, const Matrix& M2);
13![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
const Matrix operator +(const Matrix& M1, const Matrix& M2);
15
const Matrix operator -(const Matrix& M1, const Matrix& M2);
16
Matrix operator *(const Matrix& M1, const Matrix& M2);
17
Matrix operator *(const double k, const Matrix& M1);
18
const Matrix operator ~(const Matrix& M1);//the transpose of the matrix
19
Matrix operator /(const Matrix& M1, double k);
20![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
class Matrix
22![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
23
public:
24
Matrix();
25
Matrix(int n);
26
Matrix(int row, int col);
27
Matrix(int row, int col, double *a);
28
Matrix(const Matrix& other);
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
~Matrix();
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
int GetRow() const;
33
int GetCol() const;
34
double GetData(int i, int j) const;
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
Matrix& operator =(const Matrix& other);
37
friend ostream& operator <<(ostream& output, const Matrix& m);
38
friend istream& operator >>(istream& input , Matrix& m);
39
friend const bool operator ==(const Matrix& M1, const Matrix& M2);
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
friend const Matrix operator +(const Matrix& M1, const Matrix& M2);
42
friend const Matrix operator -(const Matrix& M1, const Matrix& M2);
43
friend Matrix operator *(const Matrix& M1, const Matrix& M2);
44
friend Matrix operator *(const double k, const Matrix& M2);
45
friend const Matrix operator ~(const Matrix& M1);//
46
friend Matrix operator /(const Matrix& M1, double k);//overload the / for matrix's divide.
47
//i.e.each component of matrix divide by k.
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
double& operator ()(int i, int j);//we set ()quote in ordor to use () as a left-hand value.
51
//return a vector which from M1(rcBegin:rcEnd, rc2) or M1(rc1, rcBegin:rcEnd)
52
Matrix& GetColVectorOfMatrix(int c, int rBegin, const Matrix& M1, Matrix& v);//int rBegin, int rEnd,
53
Matrix& Get2DataOfCol(int c, int r1, int r2, const Matrix& M1, Matrix& v);
54
//get two entries of a certain column i.e. (r1,c),(r2,c).
55
Matrix& GetSubMatrix(int r1, int r2, int c1, int c2, Matrix& M1, Matrix& m);
56
Matrix& SetSubMatrix(int r1, int r2, int c1, int c2, Matrix& M1, Matrix& m);
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
Matrix& SwapRow(int i, int j, Matrix& M1);//exchange the two rows which are select.
59
Matrix& SwapCol(int i, int j, Matrix& M1);//exchange the two columns which are select.
60
int* MaxRowCol(int RCn[2], const Matrix& M1, const int flag);//RCn for the Row and column number.
61
//flag=1 representive for do not consider the diagonal entries.
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
double Norm1(const Matrix& M1);//solve the norm1.
64
double NormInfinite(const Matrix& M1);//solve the infinite-norm
65
double Norm2Frobenius(const Matrix& M1);//solve the Frobenius-norm for matrix and 2-norm
66
//for vector.
67
double ConditionNum1(const Matrix& M1);//solve the condition of matrix with norm1.
68
double ConditionNumInf(const Matrix& M1);//solve the condition of matrix with infinite-norm.
69
double DotVector(const Matrix& M1, const Matrix& M2);
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
Matrix& inverse(Matrix& M1);//by the Gauss-Jordon Emlimination Method with the all-big-povit.
72
Matrix& LU(Matrix& M1);//by the Gauss Emlimination Method.
73
Matrix HouseholderTranf(Matrix& x);//return v ,and the householder matrix equal to I-2*w*w'
74
//which w=v/norm2(v).
75
Matrix GivensTranf(Matrix& v);//return a vector which c=vr(0,0) and s=vr(1,0).
76
Matrix QRHouseholder(Matrix& M1);
77
Matrix* QRHouseholderReturnQR(Matrix& M1, Matrix *qr);//return Q and R.
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
double PowerMethod(Matrix& M1, Matrix& x0, const double tol);//power method return a biggest eigenvalue
80
double InversePower(Matrix& M1, Matrix& x0, const double tol);//InversePower method return a smallest eigenvalue
81
Matrix JacobiSolInv(Matrix& M1, const double tol);//return all of the eigenvalue of matrix.
82
//the matrix need to be square and symmetry.
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
private:
85
int row;
86
int col;
87
double *M;
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
};
90
#endif
上面是近期写的一个矩阵类。
实现的功能主要是先编写最基本的运算,然后在增加新算法的时候才对其基本功能进行增加。