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

最低0.47元/天 解锁文章
5万+

被折叠的 条评论
为什么被折叠?



