C++实现矩阵Matrix类 实现基本运算

本系列文章致力于实现“手搓有限元,干翻Ansys的目标”,基本框架为前端显示使用QT实现交互,后端计算采用Visual Studio C++。

目录

Matrix类

1、public function

1.1、构造函数与析构函数

1.2、获取矩阵数值

1.3、设置矩阵

1.4、矩阵转置、单位化

1.5、矩阵的删除与替换

1.6、矩阵初等变换

1.7、矩阵加法

1.8、矩阵乘法

1.9、行列式相关操作

1.10、矩阵求逆

2、private variable

3、全部源码


Matrix类

矩阵基本类,用于有限元矩阵计算。

1、public function

公共成员函数,调用可实现基本运算

1.1、构造函数与析构函数

构造函数用来初始化矩阵,析构函数用来释放内存。

Matrix.h声明文件:

	/*函数名称:		无参构造函数*/Matrix();/*函数名称:		矩阵有参构造函数,初始化为row行、col列的0矩阵row:			矩阵行数col:			矩阵列数*/Matrix(int row, int col);/*函数名称:		矩阵有参构造函数,初始化为row行、col列、数值为mat的矩阵row:			矩阵行数col:			矩阵列数*mat:			矩阵数值一维数组*/Matrix(int row, int col, double* mat);/*函数名称:		深拷贝构造函数mat:			需要复制的矩阵*/Matrix(const Matrix& mat);/*函数名称:		析构函数*/~Matrix();

Matrix.cpp函数实现文件:

Matrix::Matrix()
{}//初始化矩阵 默认值为0
Matrix::Matrix(int row, int col)
{this->m_Row = row;this->m_Col = col;//开辟内存this->m_Matrix = new double* [row];for (int i = 0; i < row; i++){this->m_Matrix[i] = new double[col] {0.0};}}//初始化矩阵 设定数值
Matrix::Matrix(int row, int col, double *mat)
{this->m_Row = row;this->m_Col = col;//开辟内存this->m_Matrix = new double* [row];for (int i = 0; i < row; i++){this->m_Matrix[i] = new double[col] {0.0};}//矩阵赋值for(int i = 0; i<row; i++){for (int j = 0; j < col; j++){this->m_Matrix[i][j] = mat[i * col + j];}}
}//深拷贝
Matrix::Matrix(const Matrix& mat)
{//行列传递this->m_Row = mat.m_Row;this->m_Col = mat.m_Col;//矩阵深拷贝this->m_Matrix = new double* [this->m_Row];for (int i = 0; i < this->m_Row; i++){this->m_Matrix[i] = new double[this->m_Col];memcpy(this->m_Matrix[i], mat.m_Matrix[i], sizeof(double) * this->m_Col);}
}//析构函数
Matrix::~Matrix()
{//释放矩阵每一行for (int i = 0; i < this->m_Row; i++){if (this->m_Matrix[i] != NULL){delete[]this->m_Matrix[i];this->m_Matrix[i] = NULL;}}//释放矩阵顶点if (this->m_Matrix != NULL){delete[]this->m_Matrix;this->m_Matrix = NULL;}
}

1.2、获取矩阵数值

可以获取矩阵指定位置数值、打印矩阵。

Matrix.h声明文件:

	//*******************获取矩阵*****************///*函数名称:		获取矩阵的第row行、第col列元素数值row:			矩阵行数col:			矩阵列数*/double GetMatrixEle(int row, int col);/*函数名称:		打印矩阵*/void PrintMat();

Matrix.cpp函数实现文件:

//获取矩阵某个元素 某行某列
double Matrix::GetMatrixEle(int row, int col)
{if (row >= this->m_Row){std::cout << "Error: <GetMatrixEle> Input row >= m_Row" << std::endl;return 0.0;}else if (col >= this->m_Col){std::cout << "Error: <GetMatrixEle> Input col >= m_Col" << std::endl;return 0.0;}else{return this->m_Matrix[row][col];}
}//矩阵输出
void Matrix::PrintMat()
{for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){std::cout.setf(std::ios::scientific);		//科学计数法表示std::cout << this->m_Matrix[i][j] << "\t";}std::cout << std::endl;}std::cout << std::endl;
}

测试验证:

测试代码:

#include "Matrix.h"
int main()
{//定义矩阵数值double tempValue[9] = {1.0, 2.0, 3.0,4.0, 5.0, 6.0,7.0, 8.0, 0.0};//创建矩阵Matrix* tempMatrix = new Matrix(3, 3, tempValue);//打印矩阵tempMatrix->PrintMat();system("pause");return 0;
}

应用输出:

1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00请按任意键继续. . .

1.3、设置矩阵

可进行设置矩阵指定位置数值,以及深拷贝矩阵。

Matrix.h声明文件:

	/*函数名称:		设置矩阵第row行、第col列数值row:			矩阵行数col:			矩阵列数value:			设置的矩阵数值*/void SetMatrixEle(int row, int col, double value);/*函数名称:		深拷贝矩阵mat:			需要复制的矩阵*/Matrix CopyMat(const Matrix mat);

Matrix.cpp函数实现文件:

//*******************设置矩阵*****************//
void Matrix::SetMatrixEle(int row, int col, double value)
{if (row >= this->m_Row){std::cout << "Error: <SetMatrixEle> Input row >= m_Row" << std::endl;return;}else if (col >= this->m_Col){std::cout << "Error: <SetMatrixEle> Input col >= m_Col" << std::endl;return;}else{this->m_Matrix[row][col] = value;return;}
}//深拷贝矩阵
Matrix Matrix::CopyMat(const Matrix mat)
{//行列传递this->m_Row = mat.m_Row;this->m_Col = mat.m_Col;//矩阵深拷贝this->m_Matrix = new double* [this->m_Row];for (int i = 0; i < this->m_Row; i++){this->m_Matrix[i] = new double[this->m_Col];memcpy(this->m_Matrix[i], mat.m_Matrix[i], sizeof(double) * this->m_Col);}return *this;
}

测试验证:

测试代码:

int main()
{//定义矩阵数值double tempValue[9] = {1.0, 2.0, 3.0,4.0, 5.0, 6.0,7.0, 8.0, 0.0};//创建矩阵Matrix* tempMatrix = new Matrix(3, 3, tempValue);//打印矩阵std::cout << "数值更改前:" << std::endl;tempMatrix->PrintMat();//更改特定值tempMatrix->SetMatrixEle(1, 1, 10.0);//打印矩阵std::cout << "数值更改后:" << std::endl;tempMatrix->PrintMat();system("pause");return 0;
}

应用输出:

数值更改前:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00数值更改后:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    1.000000e+01    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00请按任意键继续. . .

1.4、矩阵转置、单位化

可进行矩阵转置,单位化,注意返回值类型为自身的引用,可实现链式编程。

Matrix.h声明文件:

	/*函数名称:		矩阵转置,返回的是自身引用,可链式调用*/Matrix& Transpose();/*函数名称:		等维度的单位矩阵,前提是方阵*/Matrix& Uint();

Matrix.cpp函数实现文件:

//矩阵转置
Matrix& Matrix::Transpose()
{Matrix* resMat = new Matrix(this->m_Col, this->m_Row);for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){resMat->m_Matrix[j][i] = this->m_Matrix[i][j];}}return *resMat;
}//求等长度单位矩阵
Matrix& Matrix::Uint()
{//矩阵是否为方阵if (this->m_Col != this->m_Row){std::cout << "Error: <Uint> Row != Col" << std::endl;Matrix* resMat = new Matrix(this->m_Row, this->m_Row);return *resMat;}else{//单位矩阵初始化Matrix* resMat = new Matrix(this->m_Row, this->m_Col);//单位矩阵生成for (int i = 0; i < this->m_Row; i++){resMat->m_Matrix[i][i] = 1.0;}return *resMat;}
}

测试验证:

测试代码:

int main()
{//定义矩阵数值double tempValue[9] = {1.0, 2.0, 3.0,4.0, 5.0, 6.0,7.0, 8.0, 0.0};//创建矩阵Matrix* tempMatrix = new Matrix(3, 3, tempValue);//打印矩阵std::cout << "数值转置前:" << std::endl;tempMatrix->PrintMat();//打印矩阵(注意可链式编程)std::cout << "数值转置后:" << std::endl;tempMatrix->Transpose().PrintMat();system("pause");return 0;
}

应用输出:

数值转置前:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00数值转置后:
1.000000e+00    4.000000e+00    7.000000e+00
2.000000e+00    5.000000e+00    8.000000e+00
3.000000e+00    6.000000e+00    0.000000e+00请按任意键继续. . .

1.5、矩阵的删除与替换

可进行矩阵指定行、列的删除与替换,注意返回值类型为自身的引用,可实现链式编程。

Matrix.h声明文件:

	/*函数名称:		剔除矩阵中以index为行标和列标的行和列,num代表index的大小*index:			矩阵中的行号与列号一维数组num:			index动态数组长度*/Matrix& DeleteMat(int *index, int num);/*函数名称:		剔除矩阵中以index为行标和列标的行和列,num代表index的大小*index:			矩阵中的行号与列号一维动态数组num:			index动态数组长度*/Matrix& DeleteMat(std::vector<int> index, int num);/*函数名称:		剔除矩阵中以index为行标的行,num代表index的大小*index:			矩阵中的行号一维数组num:			index动态数组长度*/Matrix& DeleteRow(int* index, int num);/*函数名称:		剔除矩阵中以index为行标的行,num代表index的大小*index:			矩阵中的行号一维动态数组num:			index动态数组长度*/Matrix& DeleteRow(std::vector<int> index, int num);/*函数名称:		剔除矩阵中以index为列标的列,num代表index的大小*index:			矩阵中的列号一维数组num:			index动态数组长度*/Matrix& DeleteCol(int* index, int num);/*函数名称:		剔除矩阵中以index为列标的列,num代表index的大小*index:			矩阵中的列号一维动态数组num:			index动态数组长度*/Matrix& DeleteCol(std::vector<int> index, int num);//******************矩阵的替换****************///*函数名称:		替换矩阵中行标和列标为 index中的行与列,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的行标和列标的一维数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceMat(int* index, int num, Matrix& mat);/*函数名称:		替换矩阵中行标和列标为 index中的行与列,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的行标和列标的一维动态数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceMat(std::vector<int> index, int num, Matrix& mat);/*函数名称:		替换矩阵中行标为 index中的行,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的行标的一维数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceRow(int* index, int num, Matrix& mat);/*函数名称:		替换矩阵中行标为 index中的行,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的行标的一动态维数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceRow(std::vector<int> index, int num, Matrix& mat);/*函数名称:		替换矩阵中列标为 index中的列,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的列标的一维数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceCol(int* index, int num, Matrix& mat);/*函数名称:		替换矩阵中列标为 index中的列,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的列标的一维动态数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceCol(std::vector<int> index, int num, Matrix& mat);

Matrix.cpp函数实现文件:

//****************矩阵保留与剔除**************//
//剔除矩阵的 index中的行与列,num代表index的大小
Matrix& Matrix::DeleteMat(int* index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row - num, this->m_Col - num);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}else if (index[i] >= this->m_Col){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num-1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[recIndex[iRow]][recIndex[iCol]];}}return *resMat;}Matrix& Matrix::DeleteMat(std::vector<int> index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row - num, this->m_Col - num);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}else if (index[i] >= this->m_Col){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num - 1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[recIndex[iRow]][recIndex[iCol]];}}return *resMat;
}//剔除矩阵的 index中的行,num代表index的大小
Matrix& Matrix::DeleteRow(int* index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row - num, this->m_Col);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num - 1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[recIndex[iRow]][iCol];}}return *resMat;
}Matrix& Matrix::DeleteRow(std::vector<int> index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row - num, this->m_Col);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num - 1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[recIndex[iRow]][iCol];}}return *resMat;
}Matrix& Matrix::DeleteCol(int* index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row, this->m_Col - num);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num - 1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[iRow][recIndex[iCol]];}}return *resMat;
}Matrix& Matrix::DeleteCol(std::vector<int> index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row, this->m_Col - num);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num - 1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[iRow][recIndex[iCol]];}}return *resMat;
}//******************矩阵的替换****************//
//替换矩阵中的行和列 index中的行与列,num代表index的大小
Matrix& Matrix::ReplaceMat(int* index, int num, Matrix& mat)
{//错误判定 方阵if (this->m_Row != this->m_Col){std::cout << "Error: <ReplaceMat> this m_Col != m_Row" << std::endl;return *this;}//检验插入矩阵为方阵if (mat.m_Row != mat.m_Col){std::cout << "Error: <ReplaceMat> mat m_Col != m_Row" << std::endl;return *this;}//检验插入矩阵大小与num保持一致if (mat.m_Col != num){std::cout << "Error: <ReplaceMat> num != mat.m_Col" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <ReplaceMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}else if (index[i] >= this->m_Col){std::cout << "Error: <ReplaceMat> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < num; iRow++){for (int iCol = 0; iCol < num; iCol++){resMat->m_Matrix[index[iRow]][index[iCol]] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}Matrix& Matrix::ReplaceMat(std::vector<int> index, int num, Matrix& mat)
{//错误判定 方阵if (this->m_Row != this->m_Col){std::cout << "Error: <ReplaceMat> this m_Col != m_Row" << std::endl;return *this;}//检验插入矩阵为方阵if (mat.m_Row != mat.m_Col){std::cout << "Error: <ReplaceMat> mat m_Col != m_Row" << std::endl;return *this;}//检验插入矩阵大小与num保持一致if (mat.m_Col != num){std::cout << "Error: <ReplaceMat> num != mat.m_Col" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <ReplaceMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}else if (index[i] >= this->m_Col){std::cout << "Error: <ReplaceMat> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < num; iRow++){for (int iCol = 0; iCol < num; iCol++){resMat->m_Matrix[index[iRow]][index[iCol]] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}//替换矩阵中的行 index中的行,num代表index的大小, mat是需要替换的矩阵
Matrix& Matrix::ReplaceRow(int* index, int num, Matrix& mat)
{//检验插入矩阵大小与num保持一致if (mat.m_Row != num){std::cout << "Error: <ReplaceRow> num != mat.m_Row" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <ReplaceRow> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//当前矩阵列数应与mat列数一致if (this->m_Col != mat.m_Col){std::cout << "Error: <ReplaceRow> this->m_Col != mat.m_Col" << std::endl;return *this;}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < num; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[index[iRow]][iCol] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}Matrix& Matrix::ReplaceRow(std::vector<int> index, int num, Matrix& mat)
{//检验插入矩阵大小与num保持一致if (mat.m_Row != num){std::cout << "Error: <ReplaceRow> num != mat.m_Row" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <ReplaceRow> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//当前矩阵列数应与mat列数一致if (this->m_Col != mat.m_Col){std::cout << "Error: <ReplaceRow> this->m_Col != mat.m_Col" << std::endl;return *this;}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < num; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[index[iRow]][iCol] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}//替换矩阵中的列 index中的列,num代表index的大小, mat是需要替换的矩阵
Matrix& Matrix::ReplaceCol(int* index, int num, Matrix& mat)
{//检验插入矩阵大小与num保持一致if (mat.m_Col != num){std::cout << "Error: <ReplaceCol> mat.m_Col != num" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Col){std::cout << "Error: <ReplaceCol> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//当前矩阵行数应与mat行数一致if (this->m_Row != mat.m_Row){std::cout << "Error: <ReplaceCol> this->m_Row != mat.m_Row" << std::endl;return *this;}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < num; iCol++){resMat->m_Matrix[iRow][index[iCol]] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}Matrix& Matrix::ReplaceCol(std::vector<int> index, int num, Matrix& mat)
{//检验插入矩阵大小与num保持一致if (mat.m_Col != num){std::cout << "Error: <ReplaceCol> mat.m_Col != num" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Col){std::cout << "Error: <ReplaceCol> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//当前矩阵行数应与mat行数一致if (this->m_Row != mat.m_Row){std::cout << "Error: <ReplaceCol> this->m_Row != mat.m_Row" << std::endl;return *this;}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < num; iCol++){resMat->m_Matrix[iRow][index[iCol]] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}

测试验证:

测试代码:

int main()
{//定义矩阵数值double tempValue[9] = {1.0, 2.0, 3.0,4.0, 5.0, 6.0,7.0, 8.0, 0.0};//替换数值double replaceValue[3] = {1.42, 2.54, 9.65};//创建矩阵Matrix* tempMatrix = new Matrix(3, 3, tempValue);Matrix* tempReplaceMatrix = new Matrix(1, 3, replaceValue);int replaceCol[1] = {2};//打印矩阵std::cout << "数值第3行替换前:" << std::endl;tempMatrix->PrintMat();//打印矩阵(注意可链式编程)std::cout << "数值第3行替换后:" << std::endl;tempMatrix->ReplaceRow(replaceCol, 1, *tempReplaceMatrix).PrintMat();//打印矩阵std::cout << "数值第3行删除前:" << std::endl;tempMatrix->PrintMat();//打印矩阵(注意可链式编程)std::cout << "数值第3行删除后:" << std::endl;tempMatrix->DeleteRow(replaceCol, 1).PrintMat();system("pause");return 0;
}

应用输出:

数值第3行替换前:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00数值第3行替换后:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
1.420000e+00    2.540000e+00    9.650000e+00数值第3行删除前:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00数值第3行删除后:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00请按任意键继续. . .

1.6、矩阵初等变换

可实现矩阵的初等变化,注意返回值类型为自身的引用,可实现链式编程。

Matrix.h声明文件:

	//*****************矩阵初等变化***************///*函数名称:		交换矩阵中行标为row0与row1的元素row0:			矩阵行标0row1:			矩阵行标1*/Matrix& SwapRow(int row0, int row1);/*函数名称:		交换矩阵中列标为col0与col1的元素col0:			矩阵列标0col1:			矩阵列标1*/Matrix& SwapCol(int col0, int col1);/*函数名称:		矩阵行加法 rowLocal = rowLocal + rate *rowAddrowLocal:		矩阵行标,被加数rowAdd:			矩阵行标,加数rate:			加数前倍数*/Matrix& AddRow(int rowLocal, int rowAdd, double rate = 1.0);//矩阵加法 某列 + 倍数*某列/*函数名称:		矩阵列加法 colLocal = colLocal + rate * colAddcolLocal:		矩阵列标,被加数colAdd:			矩阵列标,加数rate:			加数前倍数*/Matrix& AddCol(int colLocal, int colAdd, double rate = 1.0);//*******************矩阵加法*****************///*函数名称:		矩阵加法 本矩阵 = 本矩阵 + mat 前提是两个矩阵维度一致mat:			加数矩阵*/Matrix& AddMat(Matrix& mat);

Matrix.cpp函数实现文件:

//*****************矩阵初等变化***************//
Matrix& Matrix::SwapRow(int row0, int row1)
{//错误判定 越界if ((this->m_Row <= row0) || (this->m_Col <= row1)){std::cout << "Error: <SwapRow> Input row0 Or row1 More Than m_Row" << std::endl;return *this;}else if ((0 > row0) || (0 > row1)){std::cout << "Error: <SwapRow> Input row0 Or row1 Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//中转临时变量double temp = 0.0;for (int j = 0; j < resMat->m_Col; j++){temp = resMat->m_Matrix[row0][j];resMat->m_Matrix[row0][j] = resMat->m_Matrix[row1][j];resMat->m_Matrix[row1][j] = temp;}return*resMat;}
}Matrix& Matrix::SwapCol(int col0, int col1)
{//错误判定 越界if ((this->m_Col <= col0) || (this->m_Col <= col1)){std::cout << "Error: <SwapCol> Input col0 Or col1 More Than m_Col" << std::endl;return *this;}else if ((0 > col0) || (0 > col1)){std::cout << "Error: <SwapCol> Input col0 Or col1 Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//中转临时变量double temp = 0.0;for (int i = 0; i < resMat->m_Row; i++){temp = resMat->m_Matrix[i][col0];resMat->m_Matrix[i][col0] = resMat->m_Matrix[i][col1];resMat->m_Matrix[i][col1] = temp;}return*resMat;}
}//矩阵加法 某行 + 倍数*某行
Matrix& Matrix::AddRow(int rowLocal, int rowAdd, double rate)
{if ((this->m_Row <= rowLocal) || (this->m_Row <= rowAdd)){std::cout << "Error: <AddRow> Input rowLocal Or rowAdd More Than m_Row" << std::endl;return *this;}else if ((0 > rowLocal) || (0 > rowAdd)){std::cout << "Error: <AddRow> Input rowLocal Or rowAdd Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//指定行相加for (int j = 0; j < resMat->m_Col; j++){resMat->m_Matrix[rowLocal][j] += rate * resMat->m_Matrix[rowAdd][j];}return *resMat;}
}//矩阵加法 某列 + 倍数*某列
Matrix& Matrix::AddCol(int colLocal, int colAdd, double rate)
{if ((this->m_Col <= colLocal) || (this->m_Col <= colAdd)){std::cout << "Error: <AddCol> Input colLocal Or colAdd More Than m_Col" << std::endl;return *this;}else if ((0 > colLocal) || (0 > colAdd)){std::cout << "Error: <AddCol> Input colLocal Or colAdd Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//指定列相加for (int i = 0; i < resMat->m_Row; i++){resMat->m_Matrix[i][colLocal] += rate * resMat->m_Matrix[i][colAdd];}return *resMat;}
}

测试验证:

测试代码:

int main()
{//定义矩阵数值double tempValue[9] = {1.0, 2.0, 3.0,4.0, 5.0, 6.0,7.0, 8.0, 0.0};//创建矩阵Matrix* tempMatrix = new Matrix(3, 3, tempValue);//打印矩阵std::cout << "************************" << std::endl;std::cout << "数值第1行与第3行交换前:" << std::endl;tempMatrix->PrintMat();//打印矩阵(注意可链式编程)std::cout << "数值第1行与第3行交换后:" << std::endl;tempMatrix->SwapRow(0, 2).PrintMat();//打印矩阵std::cout << "************************" << std::endl;std::cout << "数值第1行与第3行相加前:" << std::endl;tempMatrix->PrintMat();//打印矩阵(注意可链式编程)std::cout << "数值第1行与第3行相加后:" << std::endl;tempMatrix->AddRow(0, 2).PrintMat();system("pause");return 0;
}

应用输出:

************************
数值第1行与第3行交换前:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00数值第1行与第3行交换后:
7.000000e+00    8.000000e+00    0.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
1.000000e+00    2.000000e+00    3.000000e+00************************
数值第1行与第3行相加前:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00数值第1行与第3行相加后:
8.000000e+00    1.000000e+01    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00请按任意键继续. . .

1.7、矩阵加法

实现矩阵基本加法,注意返回值类型为自身的引用,可实现链式编程。

Matrix.h声明文件:

	//*******************矩阵加法*****************///*函数名称:		矩阵加法 本矩阵 = 本矩阵 + mat 前提是两个矩阵维度一致mat:			加数矩阵*/Matrix& AddMat(Matrix& mat);

Matrix.cpp函数实现文件:

//*******************矩阵加法*****************//
Matrix& Matrix::AddMat(Matrix& mat)
{Matrix* ResMat = new Matrix(*this);for (int i = 0; i < ResMat->m_Row; i++){for (int j = 0; j < ResMat->m_Col; j++){ResMat->m_Matrix[i][j] += mat.m_Matrix[i][j];}}return *ResMat;
}

测试验证:

测试代码:

int main()
{//定义矩阵数值double tempValue0[9] = {1.0, 2.0, 3.0,4.0, 5.0, 6.0,7.0, 8.0, 0.0};//定义矩阵数值double tempValue1[9] = {2.0, 5.0, 8.0,1.0, 5.0, 9.0,3.0, 6.0, 7.0};//创建矩阵Matrix* tempMatrix0 = new Matrix(3, 3, tempValue0);Matrix* tempMatrix1 = new Matrix(3, 3, tempValue1);//打印矩阵std::cout << "************************" << std::endl;std::cout << "数值矩阵相加前:" << std::endl;tempMatrix0->PrintMat();//打印矩阵(注意可链式编程)std::cout << "数值矩阵相加后:" << std::endl;tempMatrix0->AddMat(*tempMatrix1).PrintMat();system("pause");return 0;
}

应用输出:

************************
数值矩阵相加前:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00数值矩阵相加后:
3.000000e+00    7.000000e+00    1.100000e+01
5.000000e+00    1.000000e+01    1.500000e+01
1.000000e+01    1.400000e+01    7.000000e+00请按任意键继续. . .

1.8、矩阵乘法

实现矩阵基本乘法,注意返回值类型为自身的引用,可实现链式编程。

Matrix.h声明文件:

	//*******************矩阵乘法*****************///*函数名称:		矩阵乘法 本矩阵 = 本矩阵*num num:			矩阵乘数*/Matrix& MultNum(double num);/*函数名称:		矩阵乘法(运算符重载) 本矩阵 = 本矩阵*num num:			矩阵乘数*/Matrix& operator * (double num);/*函数名称:		矩阵某行乘数值row = row*numnum:			矩阵某列乘数row:			矩阵行标*/Matrix& MultRow(double num, int row);/*函数名称:		矩阵某列乘数值col = col *numnum:			矩阵某列乘数col:			矩阵列标*/Matrix& MultCol(double num, int col);/*函数名称:		矩阵乘法,按照矩阵相乘规则inputMat:		乘数矩阵*/Matrix& MultMat(Matrix& inputMat);

Matrix.cpp函数实现文件:

//*******************矩阵乘法*****************//
//矩阵数乘
Matrix& Matrix::MultNum(double num)
{//结果矩阵初始化Matrix* resMat = new Matrix(this->m_Row, this->m_Col);//乘后矩阵生成for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){resMat->m_Matrix[i][j] = num * this->m_Matrix[i][j];}}return *resMat;
}//运算符重载 矩阵数乘
Matrix& Matrix::operator*(double num)
{//结果矩阵初始化Matrix* resMat = new Matrix(this->m_Row, this->m_Col);//乘后矩阵生成for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){resMat->m_Matrix[i][j] = num * this->m_Matrix[i][j];}}return *resMat;
}//矩阵某行乘数值 行标从0开始计数
Matrix& Matrix::MultRow(double num, int row)
{if (this->m_Row <= row){std::cout << "Error: <MultRow> Input row More Than m_Row" << std::endl;return *this;}else if (0 > row){std::cout << "Error: <MultRow> Input row Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//乘后矩阵生成for (int j = 0; j < this->m_Col; j++){resMat->m_Matrix[row][j] = num * this->m_Matrix[row][j];}return *resMat;}}//矩阵某列乘数值 列标从0开始计数
Matrix& Matrix::MultCol(double num, int col)
{if (this->m_Col <= col){std::cout << "Error: <MultCol> Input col More Than m_Row" << std::endl;return *this;}else if (0 > col){std::cout << "Error: <MultCol> Input col Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//乘后矩阵生成for (int i = 0; i < this->m_Row; i++){resMat->m_Matrix[i][col] = num * this->m_Matrix[i][col];}return *resMat;}
}//矩阵相乘
Matrix& Matrix::MultMat(Matrix& inputMat)
{Matrix *resMat = new Matrix(this->m_Row, inputMat.m_Col);if (this->m_Col != inputMat.m_Row){std::cout << "Matrix Mult Error!" << std::endl;return *resMat;}else{for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < inputMat.m_Col; j++){for (int k = 0; k < this->m_Col; k++){resMat->m_Matrix[i][j] += this->m_Matrix[i][k] * inputMat.m_Matrix[k][j];}}}return *resMat;}
}

测试验证:

测试代码:

int main()
{//定义矩阵数值double tempValue0[9] = {1.0, 2.0, 3.0,4.0, 5.0, 6.0,7.0, 8.0, 0.0};//定义矩阵数值double tempValue1[9] = {2.0, 5.0, 8.0,1.0, 5.0, 9.0,3.0, 6.0, 7.0};//创建矩阵Matrix* tempMatrix0 = new Matrix(3, 3, tempValue0);Matrix* tempMatrix1 = new Matrix(3, 3, tempValue1);//打印矩阵std::cout << "************************" << std::endl;std::cout << "数值矩阵相乘前:" << std::endl;tempMatrix0->PrintMat();//打印矩阵(注意可链式编程)std::cout << "数值矩阵相乘后:" << std::endl;tempMatrix0->MultMat(*tempMatrix1).PrintMat();system("pause");return 0;
}

应用输出:

************************
数值矩阵相乘前:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00数值矩阵相乘后:
1.300000e+01    3.300000e+01    4.700000e+01
3.100000e+01    8.100000e+01    1.190000e+02
2.200000e+01    7.500000e+01    1.280000e+02请按任意键继续. . .

matlab验证:

>> tempMatrix0 = [1 2 3;4 5 6; 7 8 0];
>> tempMatrix1 = [2 5 8;1 5 9; 3 6 7];
>> res = tempMatrix0*tempMatrix1res =13    33    4731    81   11922    75   128

1.9、行列式相关操作

实现行列式计算相关操作。

Matrix.h声明文件:

	//******************行列式相关操作***********************///*函数名称:		求解矩阵对应行列式数值,前提为方阵,按照定义求解,时间复杂度为O(n!*n),一般不用此方法求解*/double Det();/*函数名称:		求解矩阵对应行列式的顺序主子式,前提为方阵,按照定义求解,时间复杂度为O(n!*n),一般不用此方法求解order:			阶数*/double Det(int order);/*函数名称:		矩阵行标为row、列标为col的余子式row:			矩阵行标col:			矩阵列标*/Matrix& ChildMatrix(int row, int col);/*函数名称:		通过高斯列主消元求解矩阵行列式数值,最为常用*/double DetRow();

Matrix.cpp函数实现文件:

//矩阵的行列式数值
double Matrix::Det()
{double res = 0.0;int sign = 1;if (this->m_Row != this->m_Col){//错误判定std::cout << "Error: <Det> Matrix Col != Row" << std::endl;return 0;}else if (this->m_Row <= 1){//程序终止出口return this->m_Matrix[0][0];}else{for (int i = 0; i < this->m_Col; i++){Matrix* temp = &(this->ChildMatrix(0, i));res += sign * this->m_Matrix[0][i] * (temp->Det());sign = -1*sign;delete temp;}}}//矩阵行列式顺序主子式 order阶数
double Matrix::Det(int order)
{if (this->m_Row != this->m_Col){//错误判定std::cout << "Error: <Det> Matrix Col != Row" << std::endl;return 0;}else if (order < 0){std::cout << "Error: <Det>  Input Order Less 0" << std::endl;return 0;}else if (order >= this->m_Row){std::cout << "Error: <Det> Input Order More Than Row" << std::endl;return 0;}else{Matrix tempMat(order + 1, order + 1);for (int i = 0; i < tempMat.m_Col; i++){for (int j = 0; j < tempMat.m_Row; j++){tempMat.m_Matrix[i][j] = this->m_Matrix[i][j];}}return tempMat.Det();}
}//求解余子式
Matrix& Matrix::ChildMatrix(int row, int col)
{if (this->m_Row != this->m_Col){std::cout << "Error: <ChildMatrix> Matrix row != col" << std::endl;return *this;}else if (this->m_Row <= 1){std::cout << "Error: <ChildMatrix> Matrix Row Less 1 " << std::endl;return *this;}else if ((row > this->m_Row) || (col > this->m_Col)){std::cout << "Error: <ChildMatrix> Input Row Or Col More Than Matix Max Row Or Col" << std::endl; return* this; }else{Matrix* resMat = new Matrix(this->m_Row-1, this->m_Col-1);for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){if ((i < row) && (j < col))resMat->m_Matrix[i][j] = this->m_Matrix[i][j];else if((i > row) && (j < col))resMat->m_Matrix[i-1][j] = this->m_Matrix[i][j];else if((i < row) && (j > col))resMat->m_Matrix[i][j - 1] = this->m_Matrix[i][j];else if((i > row) && (j > col))resMat->m_Matrix[i - 1][j - 1] = this->m_Matrix[i][j];}}return *resMat;}
}//列主消元处理为上三角矩阵
double Matrix::DetRow()
{//交换标志位 1代表偶数次交换 -1代表奇数次交换int flagShift = 1;//本矩阵Matrix *localMat = new Matrix(*this);//行列式数值double resDet = 1.0;//*******************通过交换 num1*i + num2*j 实现下三角为0***************//for (int i = 0; i < localMat->m_Row - 1; i++){//记录最大行所在行标int tempMaxRow = i;for (int i1 = i + 1; i1 < localMat->m_Row; i1++){if (abs(localMat->m_Matrix[i1][i]) > abs(localMat->m_Matrix[tempMaxRow][i])){tempMaxRow = i1;}}if (tempMaxRow != i){//std::cout << i << " 行交换" << tempMaxRow << " 行" << std::endl;//进行交换 将当前第i行与第tempMaxRow行进行互换 初等行变换*localMat = localMat->SwapRow(i, tempMaxRow);//记录交换次数flagShift = -flagShift;//localMat->PrintMat();}//此对角线以下的元素通过初等变化为0for (int i2 = i + 1; i2 < localMat->m_Row; i2++){if (localMat->m_Matrix[i2][i] != 0){//std::cout << "<" << localMat->m_Matrix[i][i] << "> *" << i2 << " 行 + <" << -1.0 * (localMat->m_Matrix[i2][i]) << "> *" << i << " 行" << std::endl;*localMat = localMat->AddRow(i2, i, -1.0 * (localMat->m_Matrix[i2][i]) / localMat->m_Matrix[i][i]);//localMat->PrintMat();}}}//计算行列式数值 对角线相乘for (int i = 0; i < localMat->m_Row; i++){resDet = resDet * localMat->m_Matrix[i][i];}//矩阵交换一次就会变号resDet = flagShift * resDet;//清理localMatrixdelete localMat;return resDet;
}

测试验证:

测试代码:

int main()
{//定义矩阵数值double tempValue0[9] = {1.0, 2.0, 3.0,4.0, 5.0, 6.0,7.0, 8.0, 0.0};//创建矩阵Matrix* tempMatrix0 = new Matrix(3, 3, tempValue0);//打印矩阵std::cout << "************************" << std::endl;std::cout << "高斯列主消元过程:" << std::endl;std::cout << tempMatrix0->DetRow() << std::endl;system("pause");return 0;
}

应用输出:

************************
高斯列主消元过程:
0 行交换2 行
7.000000e+00    8.000000e+00    0.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
1.000000e+00    2.000000e+00    3.000000e+00<7.000000e+00> *1 行 + <-4.000000e+00> *0 行
7.000000e+00    8.000000e+00    0.000000e+00
0.000000e+00    4.285714e-01    6.000000e+00
1.000000e+00    2.000000e+00    3.000000e+00<7.000000e+00> *2 行 + <-1.000000e+00> *0 行
7.000000e+00    8.000000e+00    0.000000e+00
0.000000e+00    4.285714e-01    6.000000e+00
0.000000e+00    8.571429e-01    3.000000e+001 行交换2 行
7.000000e+00    8.000000e+00    0.000000e+00
0.000000e+00    8.571429e-01    3.000000e+00
0.000000e+00    4.285714e-01    6.000000e+00<8.571429e-01> *2 行 + <-4.285714e-01> *1 行
7.000000e+00    8.000000e+00    0.000000e+00
0.000000e+00    8.571429e-01    3.000000e+00
0.000000e+00    5.551115e-17    4.500000e+002.700000e+01
请按任意键继续. . .

Matlab验证:

>> tempMatrix0 = [1 2 3;4 5 6; 7 8 0];
>> det(tempMatrix0)ans =27.0000

1.10、矩阵求逆

实现矩阵求逆相关操作

Matrix.h声明文件:

	//*********************矩阵求逆********************///*函数名称:		矩阵求逆,按照定义求解,1/|A|*(A*),时间复杂度为O(n!*n),一般不用此方法*/Matrix& Inverse();/*函数名称:		矩阵求逆,通过行初等变化,高斯列主消元法求解*/Matrix& InverseRow();/*函数名称:		矩阵求逆,只针对于下三角矩阵进行求解*/Matrix& InverseDownTriangle();/*函数名称:		矩阵求逆,只针对于上三角矩阵进行求解*/Matrix& InverseUpTriangle();//矩阵LU分解/*函数名称:		矩阵LU分解LMat:			矩阵分解后的L矩阵UMat:			矩阵分解后的U矩阵*/void ResolveLU(Matrix& LMat, Matrix& UMat);/*函数名称:		矩阵的LUP分解 P*A = L*U 添加了列主消元功能LMat:			矩阵分解后的L矩阵UMat:			矩阵分解后的U矩阵PMat:			矩阵分解后的P矩阵*/void ResolveLUP(Matrix& LMat, Matrix& UMat, Matrix& PMat);

Matrix.cpp函数实现文件:

//矩阵求逆
Matrix& Matrix::Inverse()
{if (abs(this->DetRow()) < MIN_DET){std::cout << "Error: <Inverse> Matrix Det Near 0" << std::endl;return *this;}else{Matrix* resMat = new Matrix(this->m_Row, this->m_Col);for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){Matrix* temp = &(this->ChildMatrix(j, i));resMat->m_Matrix[i][j] = pow(-1.0, (i + j)) / this->DetRow() * (temp->DetRow());delete temp;}}return *resMat;}
}//矩阵求逆 行初等变化
Matrix& Matrix::InverseRow()
{//错误判断if (abs(this->DetRow()) < MIN_DET){std::cout << "Error: <InverseRow> Matrix Det Near 0" << std::endl;return *this;}else if (this->m_Row <= 1){std::cout << "Error: <InverseRow> Size Less 2" << std::endl;return *this;}else{//单位矩阵 与带转换矩阵维度相同的Matrix uint = this->Uint();//结果矩阵 逆矩阵 初始状态与本矩阵相同 为不使本矩阵发生改变Matrix temp(this->m_Row, this->m_Col);Matrix* resMat = new Matrix(temp.Uint());//本矩阵Matrix localMat(*this);//*******************通过交换 num1*i + num2*j 实现下三角为0***************//for (int i = 0; i < localMat.m_Row - 1; i++){//记录最大行所在行标int tempMaxRow = i;for (int i1 = i + 1; i1 < localMat.m_Row; i1++){if (abs(localMat.m_Matrix[i1][i]) > abs(localMat.m_Matrix[tempMaxRow][i])){tempMaxRow = i1;}}if (tempMaxRow != i){//std::cout << i << " 行交换" << tempMaxRow << " 行" << std::endl;//进行交换 将当前第i行与第tempMaxRow行进行互换 初等行变换localMat = localMat.SwapRow(i, tempMaxRow);*resMat = resMat->SwapRow(i, tempMaxRow);//localMat.PrintMat();}//此对角线以下的元素通过初等变化为0for (int i2 = i + 1; i2 < localMat.m_Row; i2++){if (localMat.m_Matrix[i2][i] != 0){//std::cout << "<" << localMat.m_Matrix[i][i] << "> *" << i2 << " 行 + <" << -1.0 * (localMat.m_Matrix[i2][i]) << "> *" << i << " 行" << std::endl;*resMat = resMat->AddRow(i2, i, -1.0 * (localMat.m_Matrix[i2][i]) / localMat.m_Matrix[i][i]);localMat = localMat.AddRow(i2, i, -1.0 * (localMat.m_Matrix[i2][i]) / localMat.m_Matrix[i][i]);//localMat.PrintMat();}}}//错误判断if (localMat.m_Matrix[localMat.m_Row - 1][localMat.m_Col - 1] == 0){std::cout << "Error: <InverseRow> marix[" << localMat.m_Row - 1 << "][" << localMat.m_Col - 1 <<"] == 0" << std::endl;return *this;}//*******************通过 num1*i + num2*j 实现上三角为0***************//for (int i = localMat.m_Row - 1; i > 0; i--){for (int i2 = i - 1; i2 >= 0; i2--){if (localMat.m_Matrix[i2][i] != 0){//std::cout << "<" << localMat.m_Matrix[i][i] << "> *" << i2 << " 行 + <" << -1.0 * (localMat.m_Matrix[i2][i]) << "> *" << i << " 行" << std::endl;*resMat = resMat->AddRow(i2, i, -1.0 * (localMat.m_Matrix[i2][i]) / localMat.m_Matrix[i][i]);localMat = localMat.AddRow(i2, i, -1.0 * (localMat.m_Matrix[i2][i]) / localMat.m_Matrix[i][i]);//localMat.PrintMat();}}}//*******************通过 i*num 实现矩阵为单位矩阵***************//for (int i = 0; i < localMat.m_Row; i++){if (localMat.m_Matrix[i][i] == 0){std::cout << "Error: <InverseRow> matrix[" << i << "]" << "[" << i << "] == 0" << std::endl;return *this;}else{//std::cout << "<" << 1 / localMat.m_Matrix[i][i] << "> *" << i << " 行" << std::endl;*resMat = resMat->MultRow(1 / localMat.m_Matrix[i][i], i);localMat = localMat.MultRow(1 / localMat.m_Matrix[i][i], i);//localMat.PrintMat();}}return *resMat;}
}//矩阵求逆 下三角矩阵
Matrix& Matrix::InverseDownTriangle()
{//错误判断 方阵检测if (this->m_Row != this->m_Col){std::cout << "Error: <InverseDownTriangle> Matrix Col != Row" << std::endl;return *this;}//下三角求逆Matrix* resMat = new Matrix(*this);for (int i = 0; i < resMat->m_Row; i++){for (int j = 0; j <= i; j++){//分段求解 对角线为倒数if (i == j){resMat->m_Matrix[i][j] = 1 / resMat->m_Matrix[i][j];}else{//分段求解 非对角线元素 double tempSum = 0.0;for (int k = j; k <= i - 1; k++){tempSum += resMat->m_Matrix[i][k] * resMat->m_Matrix[k][j];}resMat->m_Matrix[i][j] = -1.0*tempSum / resMat->m_Matrix[i][i];}}}return *resMat;}//矩阵求逆 上三角矩阵
Matrix& Matrix::InverseUpTriangle()
{//错误判断 方阵检测if (this->m_Row != this->m_Col){std::cout << "Error: <InverseUpTriangle> Matrix Col != Row" << std::endl;return *this;}//上三角求逆Matrix* resMat = new Matrix(*this);for (int j = resMat->m_Col-1; j >=0; j--){for (int i = j; i >=0; i--){//分段求解 对角线为倒数if (i == j){resMat->m_Matrix[i][j] = 1 / resMat->m_Matrix[i][j];}else{//分段求解 非对角线元素 double tempSum = 0.0;for (int k = j; k >= i+1; k--){tempSum += resMat->m_Matrix[i][k] * resMat->m_Matrix[k][j];}resMat->m_Matrix[i][j] = -1.0 * tempSum / resMat->m_Matrix[i][i];}}}return *resMat;
}//矩阵LU分解 顺序分解 对于病态矩阵可能存在精度问题
void Matrix::ResolveLU(Matrix& LMat, Matrix& UMat)
{if (this->m_Col != this->m_Row){std::cout << "Error: <ResolveLU> Is Not Square Matrix" << std::endl;return;}//存在性判定 顺序主子式不为0for (int i = 0; i < this->m_Row; i++){if (this->Det(i) == 0){std::cout << "Error: <ResolveLU> order Det = 0" << std::endl;return;}}//LU 分解//L矩阵为单位矩阵LMat = this->Uint();//U矩阵初始化为空矩阵Matrix temp(this->m_Row, this->m_Col);UMat = temp;for (int i = 0; i < this->m_Row; i++){//计算Ufor (int j1 = i; j1 < this->m_Col; j1++){double tempSum1 = 0.0;if (i != 0){for (int j2 = 0; j2 <= i - 1; j2++){tempSum1 += LMat.m_Matrix[i][j2] * UMat.m_Matrix[j2][j1];}}UMat.m_Matrix[i][j1] = this->m_Matrix[i][j1] - tempSum1;}//计算Lfor (int i1 = i; i1 < this->m_Row; i1++){double tempSum2 = 0.0;if (i != 0){for (int j2 = 0; j2 <= i - 1; j2++){tempSum2 += LMat.m_Matrix[i1][j2] * UMat.m_Matrix[j2][i];}}LMat.m_Matrix[i1][i] = (this->m_Matrix[i1][i] - tempSum2)/UMat.m_Matrix[i][i];}}}//矩阵的LUP分解 P*A = L*U 添加了列主消元功能 
//L为主对角线元素为1的下三角矩阵 U为上二角矩阵 P为行交换矩阵 P*A=L*U
void Matrix::ResolveLUP(Matrix& LMat, Matrix& UMat, Matrix& PMat)
{//条件判断 矩阵行列式不为0if (this->Det() == 0){std::cout << "Error: <ResolveLUP> Can't Resolve Matrix To L U P" << std::endl;return;}//初始化 L U PLMat = this->Uint();PMat = this->Uint();UMat = *this;//进行分解计算for (int i = 0; i < UMat.m_Row - 1; i++){//记录最大行所在行标int tempMaxRow = i;for (int i1 = i + 1; i1 < UMat.m_Row; i1++){if (abs(UMat.m_Matrix[i1][i]) > abs(UMat.m_Matrix[tempMaxRow][i])){tempMaxRow = i1;}}//进行交换 将当前第i行与第tempMaxRow行进行互换 初等行变换UMat = UMat.SwapRow(i, tempMaxRow);//L矩阵做出对应交换 先交换<itempMaxRow>列再交换<itempMaxRow>行LMat = LMat.SwapCol(i, tempMaxRow);LMat = LMat.SwapRow(i, tempMaxRow);//P矩阵做出对应变换 交换<itempMaxRow>行PMat = PMat.SwapRow(i, tempMaxRow);//高斯消元 V矩阵消除下三角区域,L矩阵添加下三角区域for (int i1 = i + 1; i1 < UMat.m_Row; i1++){//记录消元系数double deleteVar = UMat.m_Matrix[i1][i] / UMat.m_Matrix[i][i];//L矩阵列填充LMat.m_Matrix[i1][i] = deleteVar;//U矩阵列消除UMat = UMat.MultRow(UMat.m_Matrix[i][i], i1).AddRow(i1, i, -1.0 * UMat.m_Matrix[i1][i]).MultRow(1 / UMat.m_Matrix[i][i], i1);}}return;
}

测试验证:

测试代码:

int main()
{//定义矩阵数值double tempValue0[9] = {1.0, 2.0, 3.0,4.0, 5.0, 6.0,7.0, 8.0, 0.0};//创建矩阵Matrix* tempMatrix0 = new Matrix(3, 3, tempValue0);Matrix* tempMatrix0L = new Matrix(3, 3);Matrix* tempMatrix0U = new Matrix(3, 3);Matrix* tempMatrix0P = new Matrix(3, 3);//打印矩阵std::cout << "************************" << std::endl;std::cout << "矩阵求逆前:" << std::endl;tempMatrix0->PrintMat();std::cout << "矩阵求逆后:" << std::endl;tempMatrix0->InverseRow().PrintMat();std::cout << "求逆验证:" << std::endl;tempMatrix0->MultMat(tempMatrix0->InverseRow()).PrintMat();std::cout << "************************" << std::endl;std::cout << "矩阵LU分解前:" << std::endl;tempMatrix0->PrintMat();std::cout << "矩阵LU分解后:" << std::endl;tempMatrix0->ResolveLUP(*tempMatrix0L, *tempMatrix0U, *tempMatrix0P);std::cout << "矩阵L:" << std::endl;tempMatrix0L->PrintMat();std::cout << "矩阵U:" << std::endl;tempMatrix0U->PrintMat();std::cout << "矩阵P:" << std::endl;tempMatrix0P->PrintMat();system("pause");return 0;
}

应用输出:

************************
矩阵求逆前:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00矩阵求逆后:
-1.777778e+00   8.888889e-01    -1.111111e-01
1.555556e+00    -7.777778e-01   2.222222e-01
-1.111111e-01   2.222222e-01    -1.111111e-01求逆验证:
1.000000e+00    -1.110223e-16   0.000000e+00
-2.220446e-16   1.000000e+00    0.000000e+00
1.776357e-15    -8.881784e-16   1.000000e+00************************
矩阵LU分解前:
1.000000e+00    2.000000e+00    3.000000e+00
4.000000e+00    5.000000e+00    6.000000e+00
7.000000e+00    8.000000e+00    0.000000e+00矩阵LU分解后:
矩阵L:
1.000000e+00    0.000000e+00    0.000000e+00
1.428571e-01    1.000000e+00    0.000000e+00
5.714286e-01    5.000000e-01    1.000000e+00矩阵U:
7.000000e+00    8.000000e+00    0.000000e+00
0.000000e+00    8.571429e-01    3.000000e+00
0.000000e+00    0.000000e+00    4.500000e+00矩阵P:
0.000000e+00    0.000000e+00    1.000000e+00
1.000000e+00    0.000000e+00    0.000000e+00
0.000000e+00    1.000000e+00    0.000000e+00请按任意键继续. . .

matlab验证:

>> tempMatrix0 = [1 2 3; 4 5 6; 7 8 0];
>> tempMatrix0^-1ans =-1.7778    0.8889   -0.11111.5556   -0.7778    0.2222-0.1111    0.2222   -0.1111>> [L, U, P] = lu(tempMatrix0)L =1.0000         0         00.1429    1.0000         00.5714    0.5000    1.0000U =7.0000    8.0000         00    0.8571    3.00000         0    4.5000P =0     0     11     0     00     1     0

2、private variable

私有成员变量

	double** m_Matrix;						//矩阵int m_Row;								//矩阵行数int m_Col;								//矩阵列数

3、全部源码

为了方便大家复制应用,这里直接贴出源码

Matrix.h声明文件:

#ifndef _MATRIX_H_
#define _MATRIX_H_
#include <iostream>
#include <math.h>
#include <vector>//矩阵最大容量
#define MAX_COUNT 500
#define MIN_DET 1e-12				//行列式最小数值class Matrix
{
public://******************************构造函数与析构函数********************************///*函数名称:		无参构造函数*/Matrix();/*函数名称:		矩阵有参构造函数,初始化为row行、col列的0矩阵row:			矩阵行数col:			矩阵列数*/Matrix(int row, int col);/*函数名称:		矩阵有参构造函数,初始化为row行、col列、数值为mat的矩阵row:			矩阵行数col:			矩阵列数*mat:			矩阵数值一维数组*/Matrix(int row, int col, double* mat);/*函数名称:		深拷贝构造函数mat:			需要复制的矩阵*/Matrix(const Matrix& mat);/*函数名称:		析构函数*/~Matrix();//*******************获取矩阵*****************///*函数名称:		获取矩阵的第row行、第col列元素数值row:			矩阵行数col:			矩阵列数*/double GetMatrixEle(int row, int col);//*******************设置矩阵*****************///*函数名称:		设置矩阵第row行、第col列数值row:			矩阵行数col:			矩阵列数value:			设置的矩阵数值*/void SetMatrixEle(int row, int col, double value);/*函数名称:		深拷贝矩阵mat:			需要复制的矩阵*/Matrix CopyMat(const Matrix mat);//********************************矩阵的相关计算**********************************////*******************打印矩阵*****************///*函数名称:		打印矩阵*/void PrintMat();//*****************矩阵基本操作***************///*函数名称:		矩阵转置,返回的是自身引用,可链式调用*/Matrix& Transpose();/*函数名称:		等维度的单位矩阵,前提是方阵*/Matrix& Uint();//****************矩阵保留与剔除**************///*函数名称:		剔除矩阵中以index为行标和列标的行和列,num代表index的大小*index:			矩阵中的行号与列号一维数组num:			index动态数组长度*/Matrix& DeleteMat(int *index, int num);/*函数名称:		剔除矩阵中以index为行标和列标的行和列,num代表index的大小*index:			矩阵中的行号与列号一维动态数组num:			index动态数组长度*/Matrix& DeleteMat(std::vector<int> index, int num);/*函数名称:		剔除矩阵中以index为行标的行,num代表index的大小*index:			矩阵中的行号一维数组num:			index动态数组长度*/Matrix& DeleteRow(int* index, int num);/*函数名称:		剔除矩阵中以index为行标的行,num代表index的大小*index:			矩阵中的行号一维动态数组num:			index动态数组长度*/Matrix& DeleteRow(std::vector<int> index, int num);/*函数名称:		剔除矩阵中以index为列标的列,num代表index的大小*index:			矩阵中的列号一维数组num:			index动态数组长度*/Matrix& DeleteCol(int* index, int num);/*函数名称:		剔除矩阵中以index为列标的列,num代表index的大小*index:			矩阵中的列号一维动态数组num:			index动态数组长度*/Matrix& DeleteCol(std::vector<int> index, int num);//******************矩阵的替换****************///*函数名称:		替换矩阵中行标和列标为 index中的行与列,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的行标和列标的一维数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceMat(int* index, int num, Matrix& mat);/*函数名称:		替换矩阵中行标和列标为 index中的行与列,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的行标和列标的一维动态数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceMat(std::vector<int> index, int num, Matrix& mat);/*函数名称:		替换矩阵中行标为 index中的行,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的行标的一维数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceRow(int* index, int num, Matrix& mat);/*函数名称:		替换矩阵中行标为 index中的行,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的行标的一动态维数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceRow(std::vector<int> index, int num, Matrix& mat);/*函数名称:		替换矩阵中列标为 index中的列,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的列标的一维数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceCol(int* index, int num, Matrix& mat);/*函数名称:		替换矩阵中列标为 index中的列,num代表index的大小, mat是需要替换的矩阵*index:			矩阵中的列标的一维动态数组num:			index动态数组长度mat:			需要替换的矩阵*/Matrix& ReplaceCol(std::vector<int> index, int num, Matrix& mat);//*****************矩阵初等变化***************///*函数名称:		交换矩阵中行标为row0与row1的元素row0:			矩阵行标0row1:			矩阵行标1*/Matrix& SwapRow(int row0, int row1);/*函数名称:		交换矩阵中列标为col0与col1的元素col0:			矩阵列标0col1:			矩阵列标1*/Matrix& SwapCol(int col0, int col1);/*函数名称:		矩阵行加法 rowLocal = rowLocal + rate *rowAddrowLocal:		矩阵行标,被加数rowAdd:			矩阵行标,加数rate:			加数前倍数*/Matrix& AddRow(int rowLocal, int rowAdd, double rate = 1.0);//矩阵加法 某列 + 倍数*某列/*函数名称:		矩阵列加法 colLocal = colLocal + rate * colAddcolLocal:		矩阵列标,被加数colAdd:			矩阵列标,加数rate:			加数前倍数*/Matrix& AddCol(int colLocal, int colAdd, double rate = 1.0);//*******************矩阵加法*****************///*函数名称:		矩阵加法 本矩阵 = 本矩阵 + mat 前提是两个矩阵维度一致mat:			加数矩阵*/Matrix& AddMat(Matrix& mat);//*******************矩阵乘法*****************///*函数名称:		矩阵乘法 本矩阵 = 本矩阵*num num:			矩阵乘数*/Matrix& MultNum(double num);/*函数名称:		矩阵乘法(运算符重载) 本矩阵 = 本矩阵*num num:			矩阵乘数*/Matrix& operator * (double num);/*函数名称:		矩阵某行乘数值row = row*numnum:			矩阵某列乘数row:			矩阵行标*/Matrix& MultRow(double num, int row);/*函数名称:		矩阵某列乘数值col = col *numnum:			矩阵某列乘数col:			矩阵列标*/Matrix& MultCol(double num, int col);/*函数名称:		矩阵乘法,按照矩阵相乘规则inputMat:		乘数矩阵*/Matrix& MultMat(Matrix& inputMat);//******************行列式相关操作***********************///*函数名称:		求解矩阵对应行列式数值,前提为方阵,按照定义求解,时间复杂度为O(n!*n),一般不用此方法求解*/double Det();/*函数名称:		求解矩阵对应行列式的顺序主子式,前提为方阵,按照定义求解,时间复杂度为O(n!*n),一般不用此方法求解order:			阶数*/double Det(int order);/*函数名称:		矩阵行标为row、列标为col的余子式row:			矩阵行标col:			矩阵列标*/Matrix& ChildMatrix(int row, int col);/*函数名称:		通过高斯列主消元求解矩阵行列式数值,最为常用*/double DetRow();//*********************矩阵求逆********************///*函数名称:		矩阵求逆,按照定义求解,1/|A|*(A*),时间复杂度为O(n!*n),一般不用此方法*/Matrix& Inverse();/*函数名称:		矩阵求逆,通过行初等变化,高斯列主消元法求解*/Matrix& InverseRow();/*函数名称:		矩阵求逆,只针对于下三角矩阵进行求解*/Matrix& InverseDownTriangle();/*函数名称:		矩阵求逆,只针对于上三角矩阵进行求解*/Matrix& InverseUpTriangle();//矩阵LU分解/*函数名称:		矩阵LU分解LMat:			矩阵分解后的L矩阵UMat:			矩阵分解后的U矩阵*/void ResolveLU(Matrix& LMat, Matrix& UMat);/*函数名称:		矩阵的LUP分解 P*A = L*U 添加了列主消元功能LMat:			矩阵分解后的L矩阵UMat:			矩阵分解后的U矩阵PMat:			矩阵分解后的P矩阵*/void ResolveLUP(Matrix& LMat, Matrix& UMat, Matrix& PMat);private:double** m_Matrix;						//矩阵int m_Row;								//矩阵行数int m_Col;								//矩阵列数};#endif

Matrix.cpp实现文件:

#include "Matrix.h"//******************************构造函数与析构函数********************************//
Matrix::Matrix()
{}//初始化矩阵 默认值为0
Matrix::Matrix(int row, int col)
{this->m_Row = row;this->m_Col = col;//开辟内存this->m_Matrix = new double* [row];for (int i = 0; i < row; i++){this->m_Matrix[i] = new double[col] {0.0};}}//初始化矩阵 设定数值
Matrix::Matrix(int row, int col, double *mat)
{this->m_Row = row;this->m_Col = col;//开辟内存this->m_Matrix = new double* [row];for (int i = 0; i < row; i++){this->m_Matrix[i] = new double[col] {0.0};}//矩阵赋值for(int i = 0; i<row; i++){for (int j = 0; j < col; j++){this->m_Matrix[i][j] = mat[i * col + j];}}
}//深拷贝
Matrix::Matrix(const Matrix& mat)
{//行列传递this->m_Row = mat.m_Row;this->m_Col = mat.m_Col;//矩阵深拷贝this->m_Matrix = new double* [this->m_Row];for (int i = 0; i < this->m_Row; i++){this->m_Matrix[i] = new double[this->m_Col];memcpy(this->m_Matrix[i], mat.m_Matrix[i], sizeof(double) * this->m_Col);}
}Matrix::~Matrix()
{//释放矩阵每一行for (int i = 0; i < this->m_Row; i++){if (this->m_Matrix[i] != NULL){delete[]this->m_Matrix[i];this->m_Matrix[i] = NULL;}}//释放矩阵顶点if (this->m_Matrix != NULL){delete[]this->m_Matrix;this->m_Matrix = NULL;}
}
//获取矩阵某个元素 某行某列
double Matrix::GetMatrixEle(int row, int col)
{if (row >= this->m_Row){std::cout << "Error: <GetMatrixEle> Input row >= m_Row" << std::endl;return 0.0;}else if (col >= this->m_Col){std::cout << "Error: <GetMatrixEle> Input col >= m_Col" << std::endl;return 0.0;}else{return this->m_Matrix[row][col];}
}//*******************设置矩阵*****************//
void Matrix::SetMatrixEle(int row, int col, double value)
{if (row >= this->m_Row){std::cout << "Error: <SetMatrixEle> Input row >= m_Row" << std::endl;return;}else if (col >= this->m_Col){std::cout << "Error: <SetMatrixEle> Input col >= m_Col" << std::endl;return;}else{this->m_Matrix[row][col] = value;return;}
}Matrix Matrix::CopyMat(const Matrix mat)
{//行列传递this->m_Row = mat.m_Row;this->m_Col = mat.m_Col;//矩阵深拷贝this->m_Matrix = new double* [this->m_Row];for (int i = 0; i < this->m_Row; i++){this->m_Matrix[i] = new double[this->m_Col];memcpy(this->m_Matrix[i], mat.m_Matrix[i], sizeof(double) * this->m_Col);}return *this;
}//*******************打印矩阵*****************//
//矩阵输出
void Matrix::PrintMat()
{for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){std::cout.setf(std::ios::scientific);		//科学计数法表示std::cout << this->m_Matrix[i][j] << "\t";}std::cout << std::endl;}std::cout << std::endl;
}//*****************矩阵基本操作***************//
//矩阵转置
Matrix& Matrix::Transpose()
{Matrix* resMat = new Matrix(this->m_Col, this->m_Row);for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){resMat->m_Matrix[j][i] = this->m_Matrix[i][j];}}return *resMat;
}//求等长度单位矩阵
Matrix& Matrix::Uint()
{//矩阵是否为方阵if (this->m_Col != this->m_Row){std::cout << "Error: <Uint> Row != Col" << std::endl;Matrix* resMat = new Matrix(this->m_Row, this->m_Row);return *resMat;}else{//单位矩阵初始化Matrix* resMat = new Matrix(this->m_Row, this->m_Col);//单位矩阵生成for (int i = 0; i < this->m_Row; i++){resMat->m_Matrix[i][i] = 1.0;}return *resMat;}
}//****************矩阵保留与剔除**************//
//剔除矩阵的 index中的行与列,num代表index的大小
Matrix& Matrix::DeleteMat(int* index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row - num, this->m_Col - num);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}else if (index[i] >= this->m_Col){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num-1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[recIndex[iRow]][recIndex[iCol]];}}return *resMat;}Matrix& Matrix::DeleteMat(std::vector<int> index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row - num, this->m_Col - num);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}else if (index[i] >= this->m_Col){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num - 1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[recIndex[iRow]][recIndex[iCol]];}}return *resMat;
}//剔除矩阵的 index中的行,num代表index的大小
Matrix& Matrix::DeleteRow(int* index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row - num, this->m_Col);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num - 1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[recIndex[iRow]][iCol];}}return *resMat;
}Matrix& Matrix::DeleteRow(std::vector<int> index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row - num, this->m_Col);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num - 1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[recIndex[iRow]][iCol];}}return *resMat;
}Matrix& Matrix::DeleteCol(int* index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row, this->m_Col - num);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num - 1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[iRow][recIndex[iCol]];}}return *resMat;
}Matrix& Matrix::DeleteCol(std::vector<int> index, int num)
{//结果矩阵Matrix* resMat = new Matrix(this->m_Row, this->m_Col - num);int recIndex[MAX_COUNT];int currIndex = 0;//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <DeleteMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//筛选出剔除后行数for (int iRow = 0; iRow < this->m_Row; iRow++){for (int iNum = 0; iNum < num; iNum++){if (iRow == index[iNum]){break;}if (iNum == num - 1){recIndex[currIndex++] = iRow;}}}//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[iRow][iCol] = this->m_Matrix[iRow][recIndex[iCol]];}}return *resMat;
}//******************矩阵的替换****************//
//替换矩阵中的行和列 index中的行与列,num代表index的大小
Matrix& Matrix::ReplaceMat(int* index, int num, Matrix& mat)
{//错误判定 方阵if (this->m_Row != this->m_Col){std::cout << "Error: <ReplaceMat> this m_Col != m_Row" << std::endl;return *this;}//检验插入矩阵为方阵if (mat.m_Row != mat.m_Col){std::cout << "Error: <ReplaceMat> mat m_Col != m_Row" << std::endl;return *this;}//检验插入矩阵大小与num保持一致if (mat.m_Col != num){std::cout << "Error: <ReplaceMat> num != mat.m_Col" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <ReplaceMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}else if (index[i] >= this->m_Col){std::cout << "Error: <ReplaceMat> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < num; iRow++){for (int iCol = 0; iCol < num; iCol++){resMat->m_Matrix[index[iRow]][index[iCol]] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}Matrix& Matrix::ReplaceMat(std::vector<int> index, int num, Matrix& mat)
{//错误判定 方阵if (this->m_Row != this->m_Col){std::cout << "Error: <ReplaceMat> this m_Col != m_Row" << std::endl;return *this;}//检验插入矩阵为方阵if (mat.m_Row != mat.m_Col){std::cout << "Error: <ReplaceMat> mat m_Col != m_Row" << std::endl;return *this;}//检验插入矩阵大小与num保持一致if (mat.m_Col != num){std::cout << "Error: <ReplaceMat> num != mat.m_Col" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <ReplaceMat> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}else if (index[i] >= this->m_Col){std::cout << "Error: <ReplaceMat> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < num; iRow++){for (int iCol = 0; iCol < num; iCol++){resMat->m_Matrix[index[iRow]][index[iCol]] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}//替换矩阵中的行 index中的行,num代表index的大小, mat是需要替换的矩阵
Matrix& Matrix::ReplaceRow(int* index, int num, Matrix& mat)
{//检验插入矩阵大小与num保持一致if (mat.m_Row != num){std::cout << "Error: <ReplaceRow> num != mat.m_Row" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <ReplaceRow> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//当前矩阵列数应与mat列数一致if (this->m_Col != mat.m_Col){std::cout << "Error: <ReplaceRow> this->m_Col != mat.m_Col" << std::endl;return *this;}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < num; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[index[iRow]][iCol] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}Matrix& Matrix::ReplaceRow(std::vector<int> index, int num, Matrix& mat)
{//检验插入矩阵大小与num保持一致if (mat.m_Row != num){std::cout << "Error: <ReplaceRow> num != mat.m_Row" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Row){std::cout << "Error: <ReplaceRow> Input index[" << i << "] = " << index[i] << " >= m_Row" << std::endl;return *this;}}//当前矩阵列数应与mat列数一致if (this->m_Col != mat.m_Col){std::cout << "Error: <ReplaceRow> this->m_Col != mat.m_Col" << std::endl;return *this;}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < num; iRow++){for (int iCol = 0; iCol < resMat->m_Col; iCol++){resMat->m_Matrix[index[iRow]][iCol] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}//替换矩阵中的列 index中的列,num代表index的大小, mat是需要替换的矩阵
Matrix& Matrix::ReplaceCol(int* index, int num, Matrix& mat)
{//检验插入矩阵大小与num保持一致if (mat.m_Col != num){std::cout << "Error: <ReplaceCol> mat.m_Col != num" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Col){std::cout << "Error: <ReplaceCol> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//当前矩阵行数应与mat行数一致if (this->m_Row != mat.m_Row){std::cout << "Error: <ReplaceCol> this->m_Row != mat.m_Row" << std::endl;return *this;}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < num; iCol++){resMat->m_Matrix[iRow][index[iCol]] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}Matrix& Matrix::ReplaceCol(std::vector<int> index, int num, Matrix& mat)
{//检验插入矩阵大小与num保持一致if (mat.m_Col != num){std::cout << "Error: <ReplaceCol> mat.m_Col != num" << std::endl;return *this;}//检验数据有效性for (int i = 0; i < num; i++){//越界判定if (index[i] >= this->m_Col){std::cout << "Error: <ReplaceCol> Input index[" << i << "] = " << index[i] << " >= m_Col" << std::endl;return *this;}}//当前矩阵行数应与mat行数一致if (this->m_Row != mat.m_Row){std::cout << "Error: <ReplaceCol> this->m_Row != mat.m_Row" << std::endl;return *this;}//结果矩阵Matrix* resMat = new Matrix(*this);//加入元素for (int iRow = 0; iRow < resMat->m_Row; iRow++){for (int iCol = 0; iCol < num; iCol++){resMat->m_Matrix[iRow][index[iCol]] = mat.m_Matrix[iRow][iCol];}}return *resMat;
}//*****************矩阵初等变化***************//
Matrix& Matrix::SwapRow(int row0, int row1)
{//错误判定 越界if ((this->m_Row <= row0) || (this->m_Col <= row1)){std::cout << "Error: <SwapRow> Input row0 Or row1 More Than m_Row" << std::endl;return *this;}else if ((0 > row0) || (0 > row1)){std::cout << "Error: <SwapRow> Input row0 Or row1 Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//中转临时变量double temp = 0.0;for (int j = 0; j < resMat->m_Col; j++){temp = resMat->m_Matrix[row0][j];resMat->m_Matrix[row0][j] = resMat->m_Matrix[row1][j];resMat->m_Matrix[row1][j] = temp;}return*resMat;}
}Matrix& Matrix::SwapCol(int col0, int col1)
{//错误判定 越界if ((this->m_Col <= col0) || (this->m_Col <= col1)){std::cout << "Error: <SwapCol> Input col0 Or col1 More Than m_Col" << std::endl;return *this;}else if ((0 > col0) || (0 > col1)){std::cout << "Error: <SwapCol> Input col0 Or col1 Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//中转临时变量double temp = 0.0;for (int i = 0; i < resMat->m_Row; i++){temp = resMat->m_Matrix[i][col0];resMat->m_Matrix[i][col0] = resMat->m_Matrix[i][col1];resMat->m_Matrix[i][col1] = temp;}return*resMat;}
}//矩阵加法 某行 + 倍数*某行
Matrix& Matrix::AddRow(int rowLocal, int rowAdd, double rate)
{if ((this->m_Row <= rowLocal) || (this->m_Row <= rowAdd)){std::cout << "Error: <AddRow> Input rowLocal Or rowAdd More Than m_Row" << std::endl;return *this;}else if ((0 > rowLocal) || (0 > rowAdd)){std::cout << "Error: <AddRow> Input rowLocal Or rowAdd Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//指定行相加for (int j = 0; j < resMat->m_Col; j++){resMat->m_Matrix[rowLocal][j] += rate * resMat->m_Matrix[rowAdd][j];}return *resMat;}
}//矩阵加法 某列 + 倍数*某列
Matrix& Matrix::AddCol(int colLocal, int colAdd, double rate)
{if ((this->m_Col <= colLocal) || (this->m_Col <= colAdd)){std::cout << "Error: <AddCol> Input colLocal Or colAdd More Than m_Col" << std::endl;return *this;}else if ((0 > colLocal) || (0 > colAdd)){std::cout << "Error: <AddCol> Input colLocal Or colAdd Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//指定列相加for (int i = 0; i < resMat->m_Row; i++){resMat->m_Matrix[i][colLocal] += rate * resMat->m_Matrix[i][colAdd];}return *resMat;}
}//*******************矩阵加法*****************//
Matrix& Matrix::AddMat(Matrix& mat)
{Matrix* ResMat = new Matrix(*this);for (int i = 0; i < ResMat->m_Row; i++){for (int j = 0; j < ResMat->m_Col; j++){ResMat->m_Matrix[i][j] += mat.m_Matrix[i][j];}}return *ResMat;
}//*******************矩阵乘法*****************//
//矩阵数乘
Matrix& Matrix::MultNum(double num)
{//结果矩阵初始化Matrix* resMat = new Matrix(this->m_Row, this->m_Col);//乘后矩阵生成for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){resMat->m_Matrix[i][j] = num * this->m_Matrix[i][j];}}return *resMat;
}//运算符重载 矩阵数乘
Matrix& Matrix::operator*(double num)
{//结果矩阵初始化Matrix* resMat = new Matrix(this->m_Row, this->m_Col);//乘后矩阵生成for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){resMat->m_Matrix[i][j] = num * this->m_Matrix[i][j];}}return *resMat;
}//矩阵某行乘数值 行标从0开始计数
Matrix& Matrix::MultRow(double num, int row)
{if (this->m_Row <= row){std::cout << "Error: <MultRow> Input row More Than m_Row" << std::endl;return *this;}else if (0 > row){std::cout << "Error: <MultRow> Input row Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//乘后矩阵生成for (int j = 0; j < this->m_Col; j++){resMat->m_Matrix[row][j] = num * this->m_Matrix[row][j];}return *resMat;}}//矩阵某列乘数值 列标从0开始计数
Matrix& Matrix::MultCol(double num, int col)
{if (this->m_Col <= col){std::cout << "Error: <MultCol> Input col More Than m_Row" << std::endl;return *this;}else if (0 > col){std::cout << "Error: <MultCol> Input col Less 0" << std::endl;return *this;}else{//结果矩阵初始化Matrix* resMat = new Matrix(*this);//乘后矩阵生成for (int i = 0; i < this->m_Row; i++){resMat->m_Matrix[i][col] = num * this->m_Matrix[i][col];}return *resMat;}
}//矩阵相乘
Matrix& Matrix::MultMat(Matrix& inputMat)
{Matrix *resMat = new Matrix(this->m_Row, inputMat.m_Col);if (this->m_Col != inputMat.m_Row){std::cout << "Matrix Mult Error!" << std::endl;return *resMat;}else{for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < inputMat.m_Col; j++){for (int k = 0; k < this->m_Col; k++){resMat->m_Matrix[i][j] += this->m_Matrix[i][k] * inputMat.m_Matrix[k][j];}}}return *resMat;}
}//矩阵的行列式数值
double Matrix::Det()
{double res = 0.0;int sign = 1;if (this->m_Row != this->m_Col){//错误判定std::cout << "Error: <Det> Matrix Col != Row" << std::endl;return 0;}else if (this->m_Row <= 1){//程序终止出口return this->m_Matrix[0][0];}else{for (int i = 0; i < this->m_Col; i++){Matrix* temp = &(this->ChildMatrix(0, i));res += sign * this->m_Matrix[0][i] * (temp->Det());sign = -1*sign;delete temp;}}}//矩阵行列式顺序主子式 order阶数
double Matrix::Det(int order)
{if (this->m_Row != this->m_Col){//错误判定std::cout << "Error: <Det> Matrix Col != Row" << std::endl;return 0;}else if (order < 0){std::cout << "Error: <Det>  Input Order Less 0" << std::endl;return 0;}else if (order >= this->m_Row){std::cout << "Error: <Det> Input Order More Than Row" << std::endl;return 0;}else{Matrix tempMat(order + 1, order + 1);for (int i = 0; i < tempMat.m_Col; i++){for (int j = 0; j < tempMat.m_Row; j++){tempMat.m_Matrix[i][j] = this->m_Matrix[i][j];}}return tempMat.Det();}
}//求解余子式
Matrix& Matrix::ChildMatrix(int row, int col)
{if (this->m_Row != this->m_Col){std::cout << "Error: <ChildMatrix> Matrix row != col" << std::endl;return *this;}else if (this->m_Row <= 1){std::cout << "Error: <ChildMatrix> Matrix Row Less 1 " << std::endl;return *this;}else if ((row > this->m_Row) || (col > this->m_Col)){std::cout << "Error: <ChildMatrix> Input Row Or Col More Than Matix Max Row Or Col" << std::endl; return* this; }else{Matrix* resMat = new Matrix(this->m_Row-1, this->m_Col-1);for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){if ((i < row) && (j < col))resMat->m_Matrix[i][j] = this->m_Matrix[i][j];else if((i > row) && (j < col))resMat->m_Matrix[i-1][j] = this->m_Matrix[i][j];else if((i < row) && (j > col))resMat->m_Matrix[i][j - 1] = this->m_Matrix[i][j];else if((i > row) && (j > col))resMat->m_Matrix[i - 1][j - 1] = this->m_Matrix[i][j];}}return *resMat;}
}//列主消元处理为上三角矩阵
double Matrix::DetRow()
{//交换标志位 1代表偶数次交换 -1代表奇数次交换int flagShift = 1;//本矩阵Matrix *localMat = new Matrix(*this);//行列式数值double resDet = 1.0;//*******************通过交换 num1*i + num2*j 实现下三角为0***************//for (int i = 0; i < localMat->m_Row - 1; i++){//记录最大行所在行标int tempMaxRow = i;for (int i1 = i + 1; i1 < localMat->m_Row; i1++){if (abs(localMat->m_Matrix[i1][i]) > abs(localMat->m_Matrix[tempMaxRow][i])){tempMaxRow = i1;}}if (tempMaxRow != i){//std::cout << i << " 行交换" << tempMaxRow << " 行" << std::endl;//进行交换 将当前第i行与第tempMaxRow行进行互换 初等行变换*localMat = localMat->SwapRow(i, tempMaxRow);//记录交换次数flagShift = -flagShift;//localMat->PrintMat();}//此对角线以下的元素通过初等变化为0for (int i2 = i + 1; i2 < localMat->m_Row; i2++){if (localMat->m_Matrix[i2][i] != 0){//std::cout << "<" << localMat->m_Matrix[i][i] << "> *" << i2 << " 行 + <" << -1.0 * (localMat->m_Matrix[i2][i]) << "> *" << i << " 行" << std::endl;*localMat = localMat->AddRow(i2, i, -1.0 * (localMat->m_Matrix[i2][i]) / localMat->m_Matrix[i][i]);//localMat->PrintMat();}}}//计算行列式数值 对角线相乘for (int i = 0; i < localMat->m_Row; i++){resDet = resDet * localMat->m_Matrix[i][i];}//矩阵交换一次就会变号resDet = flagShift * resDet;//清理localMatrixdelete localMat;return resDet;
}//矩阵求逆
Matrix& Matrix::Inverse()
{if (abs(this->DetRow()) < MIN_DET){std::cout << "Error: <Inverse> Matrix Det Near 0" << std::endl;return *this;}else{Matrix* resMat = new Matrix(this->m_Row, this->m_Col);for (int i = 0; i < this->m_Row; i++){for (int j = 0; j < this->m_Col; j++){Matrix* temp = &(this->ChildMatrix(j, i));resMat->m_Matrix[i][j] = pow(-1.0, (i + j)) / this->DetRow() * (temp->DetRow());delete temp;}}return *resMat;}
}//矩阵求逆 行初等变化
Matrix& Matrix::InverseRow()
{//错误判断if (abs(this->DetRow()) < MIN_DET){std::cout << "Error: <InverseRow> Matrix Det Near 0" << std::endl;return *this;}else if (this->m_Row <= 1){std::cout << "Error: <InverseRow> Size Less 2" << std::endl;return *this;}else{//单位矩阵 与带转换矩阵维度相同的Matrix uint = this->Uint();//结果矩阵 逆矩阵 初始状态与本矩阵相同 为不使本矩阵发生改变Matrix temp(this->m_Row, this->m_Col);Matrix* resMat = new Matrix(temp.Uint());//本矩阵Matrix localMat(*this);//*******************通过交换 num1*i + num2*j 实现下三角为0***************//for (int i = 0; i < localMat.m_Row - 1; i++){//记录最大行所在行标int tempMaxRow = i;for (int i1 = i + 1; i1 < localMat.m_Row; i1++){if (abs(localMat.m_Matrix[i1][i]) > abs(localMat.m_Matrix[tempMaxRow][i])){tempMaxRow = i1;}}if (tempMaxRow != i){//std::cout << i << " 行交换" << tempMaxRow << " 行" << std::endl;//进行交换 将当前第i行与第tempMaxRow行进行互换 初等行变换localMat = localMat.SwapRow(i, tempMaxRow);*resMat = resMat->SwapRow(i, tempMaxRow);//localMat.PrintMat();}//此对角线以下的元素通过初等变化为0for (int i2 = i + 1; i2 < localMat.m_Row; i2++){if (localMat.m_Matrix[i2][i] != 0){//std::cout << "<" << localMat.m_Matrix[i][i] << "> *" << i2 << " 行 + <" << -1.0 * (localMat.m_Matrix[i2][i]) << "> *" << i << " 行" << std::endl;*resMat = resMat->AddRow(i2, i, -1.0 * (localMat.m_Matrix[i2][i]) / localMat.m_Matrix[i][i]);localMat = localMat.AddRow(i2, i, -1.0 * (localMat.m_Matrix[i2][i]) / localMat.m_Matrix[i][i]);//localMat.PrintMat();}}}//错误判断if (localMat.m_Matrix[localMat.m_Row - 1][localMat.m_Col - 1] == 0){std::cout << "Error: <InverseRow> marix[" << localMat.m_Row - 1 << "][" << localMat.m_Col - 1 <<"] == 0" << std::endl;return *this;}//*******************通过 num1*i + num2*j 实现上三角为0***************//for (int i = localMat.m_Row - 1; i > 0; i--){for (int i2 = i - 1; i2 >= 0; i2--){if (localMat.m_Matrix[i2][i] != 0){//std::cout << "<" << localMat.m_Matrix[i][i] << "> *" << i2 << " 行 + <" << -1.0 * (localMat.m_Matrix[i2][i]) << "> *" << i << " 行" << std::endl;*resMat = resMat->AddRow(i2, i, -1.0 * (localMat.m_Matrix[i2][i]) / localMat.m_Matrix[i][i]);localMat = localMat.AddRow(i2, i, -1.0 * (localMat.m_Matrix[i2][i]) / localMat.m_Matrix[i][i]);//localMat.PrintMat();}}}//*******************通过 i*num 实现矩阵为单位矩阵***************//for (int i = 0; i < localMat.m_Row; i++){if (localMat.m_Matrix[i][i] == 0){std::cout << "Error: <InverseRow> matrix[" << i << "]" << "[" << i << "] == 0" << std::endl;return *this;}else{//std::cout << "<" << 1 / localMat.m_Matrix[i][i] << "> *" << i << " 行" << std::endl;*resMat = resMat->MultRow(1 / localMat.m_Matrix[i][i], i);localMat = localMat.MultRow(1 / localMat.m_Matrix[i][i], i);//localMat.PrintMat();}}return *resMat;}
}//矩阵求逆 下三角矩阵
Matrix& Matrix::InverseDownTriangle()
{//错误判断 方阵检测if (this->m_Row != this->m_Col){std::cout << "Error: <InverseDownTriangle> Matrix Col != Row" << std::endl;return *this;}//下三角求逆Matrix* resMat = new Matrix(*this);for (int i = 0; i < resMat->m_Row; i++){for (int j = 0; j <= i; j++){//分段求解 对角线为倒数if (i == j){resMat->m_Matrix[i][j] = 1 / resMat->m_Matrix[i][j];}else{//分段求解 非对角线元素 double tempSum = 0.0;for (int k = j; k <= i - 1; k++){tempSum += resMat->m_Matrix[i][k] * resMat->m_Matrix[k][j];}resMat->m_Matrix[i][j] = -1.0*tempSum / resMat->m_Matrix[i][i];}}}return *resMat;}//矩阵求逆 上三角矩阵
Matrix& Matrix::InverseUpTriangle()
{//错误判断 方阵检测if (this->m_Row != this->m_Col){std::cout << "Error: <InverseUpTriangle> Matrix Col != Row" << std::endl;return *this;}//上三角求逆Matrix* resMat = new Matrix(*this);for (int j = resMat->m_Col-1; j >=0; j--){for (int i = j; i >=0; i--){//分段求解 对角线为倒数if (i == j){resMat->m_Matrix[i][j] = 1 / resMat->m_Matrix[i][j];}else{//分段求解 非对角线元素 double tempSum = 0.0;for (int k = j; k >= i+1; k--){tempSum += resMat->m_Matrix[i][k] * resMat->m_Matrix[k][j];}resMat->m_Matrix[i][j] = -1.0 * tempSum / resMat->m_Matrix[i][i];}}}return *resMat;
}//矩阵LU分解 顺序分解 对于病态矩阵可能存在精度问题
void Matrix::ResolveLU(Matrix& LMat, Matrix& UMat)
{if (this->m_Col != this->m_Row){std::cout << "Error: <ResolveLU> Is Not Square Matrix" << std::endl;return;}//存在性判定 顺序主子式不为0for (int i = 0; i < this->m_Row; i++){if (this->Det(i) == 0){std::cout << "Error: <ResolveLU> order Det = 0" << std::endl;return;}}//LU 分解//L矩阵为单位矩阵LMat = this->Uint();//U矩阵初始化为空矩阵Matrix temp(this->m_Row, this->m_Col);UMat = temp;for (int i = 0; i < this->m_Row; i++){//计算Ufor (int j1 = i; j1 < this->m_Col; j1++){double tempSum1 = 0.0;if (i != 0){for (int j2 = 0; j2 <= i - 1; j2++){tempSum1 += LMat.m_Matrix[i][j2] * UMat.m_Matrix[j2][j1];}}UMat.m_Matrix[i][j1] = this->m_Matrix[i][j1] - tempSum1;}//计算Lfor (int i1 = i; i1 < this->m_Row; i1++){double tempSum2 = 0.0;if (i != 0){for (int j2 = 0; j2 <= i - 1; j2++){tempSum2 += LMat.m_Matrix[i1][j2] * UMat.m_Matrix[j2][i];}}LMat.m_Matrix[i1][i] = (this->m_Matrix[i1][i] - tempSum2)/UMat.m_Matrix[i][i];}}}//矩阵的LUP分解 P*A = L*U 添加了列主消元功能 
//L为主对角线元素为1的下三角矩阵 U为上二角矩阵 P为行交换矩阵 P*A=L*U
void Matrix::ResolveLUP(Matrix& LMat, Matrix& UMat, Matrix& PMat)
{//条件判断 矩阵行列式不为0if (this->Det() == 0){std::cout << "Error: <ResolveLUP> Can't Resolve Matrix To L U P" << std::endl;return;}//初始化 L U PLMat = this->Uint();PMat = this->Uint();UMat = *this;//进行分解计算for (int i = 0; i < UMat.m_Row - 1; i++){//记录最大行所在行标int tempMaxRow = i;for (int i1 = i + 1; i1 < UMat.m_Row; i1++){if (abs(UMat.m_Matrix[i1][i]) > abs(UMat.m_Matrix[tempMaxRow][i])){tempMaxRow = i1;}}//进行交换 将当前第i行与第tempMaxRow行进行互换 初等行变换UMat = UMat.SwapRow(i, tempMaxRow);//L矩阵做出对应交换 先交换<itempMaxRow>列再交换<itempMaxRow>行LMat = LMat.SwapCol(i, tempMaxRow);LMat = LMat.SwapRow(i, tempMaxRow);//P矩阵做出对应变换 交换<itempMaxRow>行PMat = PMat.SwapRow(i, tempMaxRow);//高斯消元 V矩阵消除下三角区域,L矩阵添加下三角区域for (int i1 = i + 1; i1 < UMat.m_Row; i1++){//记录消元系数double deleteVar = UMat.m_Matrix[i1][i] / UMat.m_Matrix[i][i];//L矩阵列填充LMat.m_Matrix[i1][i] = deleteVar;//U矩阵列消除UMat = UMat.MultRow(UMat.m_Matrix[i][i], i1).AddRow(i1, i, -1.0 * UMat.m_Matrix[i1][i]).MultRow(1 / UMat.m_Matrix[i][i], i1);}}return;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/67630.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HTML<form>标签

例子 具有两个输入字段和一个提交按钮的HTML表单&#xff1a; <form action"/action_page.php" method"get"> <label for"fname">First name:</label> <input type"text" id"fname" name"f…

《贪心算法:原理剖析与典型例题精解》

必刷的贪心算法典型例题&#xff01; 算法竞赛&#xff08;蓝桥杯&#xff09;贪心算法1——数塔问题-CSDN博客 算法竞赛&#xff08;蓝桥杯&#xff09;贪心算法2——需要安排几位师傅加工零件-CSDN博客 算法&#xff08;蓝桥杯&#xff09;贪心算法3——二维数组排序与贪心算…

【2024 年度总结】从小白慢慢成长

【2024 年度总结】从小白慢慢成长 1. 加入 CSDN 的契机2. 学习过程2.1 万事开头难2.2 下定决心开始学习2.3 融入技术圈2.4 完成万粉的目标 3. 经验分享3.1 工具的选择3.2 如何提升文章质量3.3 学会善用 AI 工具 4. 保持初心&#xff0c;继续前行 1. 加入 CSDN 的契机 首次接触…

【C++篇】红黑树封装 实现map和set

目录 前言&#xff1a; 一&#xff0c;库中map和set的大致结构 二&#xff0c;模拟实现 2.1&#xff0c;大致框架 2.2&#xff0c;复用红黑树实现insert接口 2.3&#xff0c;迭代器iterator的实现 operator()的实现&#xff1a; operator--()的实现&#xff1a; 对inser…

写作利器:如何用 PicGo + GitHub 图床提高创作效率

你好呀&#xff0c;欢迎来到 Dong雨 的技术小栈 &#x1f331; 在这里&#xff0c;我们一同探索代码的奥秘&#xff0c;感受技术的魅力 ✨。 &#x1f449; 我的小世界&#xff1a;Dong雨 &#x1f4cc; 分享我的学习旅程 &#x1f6e0;️ 提供贴心的实用工具 &#x1f4a1; 记…

RabbitMQ 高级特性

目录 1.消息确认 1.1 消息确认机制 1.2 手动确认方法 1. 2.1肯定确认 1.2.2 否定确认 1.3 SpringBoot 代码示例 1.3.1 配置确认机制 1.3.2 配置队列,交换机,绑定关系 1.3.3 生产者(向 rabbitmq 发送消息) 1.3.4 消费者(消费队列中的信息) 2.持久性 2.1 交换机…

AIGC视频生成模型:Meta的Emu Video模型

大家好&#xff0c;这里是好评笔记&#xff0c;公主号&#xff1a;Goodnote&#xff0c;专栏文章私信限时Free。本文详细介绍Meta的视频生成模型Emu Video&#xff0c;作为Meta发布的第二款视频生成模型&#xff0c;在视频生成领域发挥关键作用。 &#x1f33a;优质专栏回顾&am…

面向对象的程序设计:以对象的方式进行思考

1 理解接口与实现的区别 以上一篇文章的电视机需要插电使用的例子继续来讲解: 对电视而言,插电使用,只需要标准的插座即可,具体的电从哪里来,是火力发电厂,或是太阳能发电,亦或是畜电池逆变供电,电视机是不需要关心的。 发电厂或供电设备属于实现,220V交流电插座属于…

要获取本地的公网 IP 地址(curl ifconfig.me)

文章目录 通过命令行查询&#xff08;适用于 Linux/Mac/Windows&#xff09;Linux/MacWindows 注意事项 要获取本地的公网 IP 地址&#xff0c;可以通过以下简单的方法&#xff1a; 通过命令行查询&#xff08;适用于 Linux/Mac/Windows&#xff09; Linux/Mac 打开终端。输入…

MySQL 数据库 UDF 提权

免责声明 本博客文章仅供教育和研究目的使用。本文中提到的所有信息和技术均基于公开来源和合法获取的知识。本文不鼓励或支持任何非法活动&#xff0c;包括但不限于未经授权访问计算机系统、网络或数据。 作者对于读者使用本文中的信息所导致的任何直接或间接后果不承担任何责…

【数据分享】1929-2024年全球站点的逐年平均气温数据(Shp\Excel\无需转发)

气象数据是在各项研究中都经常使用的数据&#xff0c;气象指标包括气温、风速、降水、湿度等指标&#xff0c;其中又以气温指标最为常用&#xff01;说到气温数据&#xff0c;最详细的气温数据是具体到气象监测站点的气温数据&#xff01;本次我们为大家带来的就是具体到气象监…

【Linux 源码】内核态到用户态

文章目录 1. 由来2. 流程图3. 中断3.1 概念3.2 8259A芯片3.4 中断时的栈处理3.4.1 相同特权级3.4.2 不同特权级 3.5 中断流程3.6 定位中断程序3.7 中断流程步骤总结 4. 源码4.1 move_to_user_mode4.2 0号进程4.3 TSS和LDT在GDT表排布4.4 ldt中的0x17栈段 5. 总结 1. 由来 ​ 首…

雷电9最新版安装Magisk+LSPosd(新手速通)

大家好啊&#xff01;我是NiJiMingCheng 我的博客&#xff1a;NiJiMingCheng 在安卓系统的定制与拓展过程中&#xff0c;获取 ROOT 权限以及安装各类框架是进阶玩家常用的操作&#xff0c;这可以帮助我们实现更多系统层面的个性化功能。今天&#xff0c;我将为大家详细介绍如何…

《Linux服务与安全管理》| 邮件服务器安装和配置

《Linux服务与安全管理》| 邮件服务器安装和配置 目录 《Linux服务与安全管理》| 邮件服务器安装和配置 1.在Server01上安装dns、postfix、dovecot和telnet&#xff0c;并启动 2&#xff0e;在Server01上配置DNS服务器&#xff0c;设置MX资源记录 3&#xff0e;在server1上…

计算机毕业设计Python电商品推荐系统 商品比价系统 电商比价系统 商品可视化 商品爬虫 机器学习 深度学习 京东爬虫 国美爬虫 淘宝爬虫 大数据

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 作者简介&#xff1a;Java领…

【银河麒麟高级服务器操作系统】业务访问慢网卡丢包现象分析及处理过程

了解更多银河麒麟操作系统全新产品&#xff0c;请点击访问 麒麟软件产品专区&#xff1a;product.kylinos.cn 开发者专区&#xff1a;developer.kylinos.cn 文档中心&#xff1a;document.kylinos.cn 交流论坛&#xff1a;forum.kylinos.cn 服务器环境以及配置 【内核版本…

鸿蒙开发中的骨架图:提升用户体验的关键一环

大家好&#xff0c;我是小 z&#xff0c;今天要给大家分享一个提升用户体验的超实用技巧 —— 骨架图&#x1f3af; 文章目录 一、什么是骨架图二、骨架图的作用三、鸿蒙开发中实现骨架图的方法1. 利用 opacity 奠定视觉基础2. animateTo 驱动动态变化3. 二者协同触发与展示 四…

leetcode刷题记录(七十三)——543. 二叉树的直径

&#xff08;一&#xff09;问题描述 543. 二叉树的直径 - 力扣&#xff08;LeetCode&#xff09;543. 二叉树的直径 - 给你一棵二叉树的根节点&#xff0c;返回该树的 直径 。二叉树的 直径 是指树中任意两个节点之间最长路径的 长度 。这条路径可能经过也可能不经过根节点 r…

【esp32小程序】小程序篇02——连接git

一、创建仓库 进入gitee官网&#xff0c;登录&#xff08;如果没有gitee账号的就自行注册一下&#xff09;。 点击号-->新建仓库 填写好必填信息&#xff0c;然后点击“创建” 二、微信开发者工具配置 在微信开发者工具打开我们的项目。按下面的步骤依次点击 三、验证 点…

Linux的基本指令(上) -- 0基础入门

目录 知识点引入 基本指令 ls指令 pwd 命令 cd 指令 touch 指令 stat指令 mkdir 指令 tree指令 rmdir 指令 rm 命令 man 指令 which 指令 alias 指令 echo指令 输出重定向: > 追加重定向&#xff1a;>> cp 指令 知识点引入 1. Linux中路径用 / 作为路径分隔…