C++实现矩阵类(附代码和功能)

您所在的位置:网站首页 矩阵类java C++实现矩阵类(附代码和功能)

C++实现矩阵类(附代码和功能)

2023-09-01 08:44| 来源: 网络整理| 查看: 265

       阅读这篇文章需要掌握C++类的知识以及线性代数的知识,如果有疑问,可在文章下方评论,作者会尽快回复;本文是在作者阅读了平冈和幸的程序员的数学3:线性代数之后而写,在代码设计上借鉴了书中的方法。

       希望这些代码能够帮助你更好地理解线性代数里提到的矩阵运算,笔者所写的矩阵运算代码,都是初学C++时实现的,并不具有工程应用的价值;真诚的希望读者能够使用更好的矩阵运算库,比如Eigen,OpenCV等,笔者对这两个C++库的理解也是比较深入的;当然,如果读者还了解Python的话,笔者建议学习numpy,numpy的Cpp代码可以在Github上搜索NumCpp。

        初写这份代码时,笔者才刚入C++,如今笔者将在github上上传新的矩阵运算代码NANAhttps://github.com/YuruTu/NANA 

相关文档见地址NANA doxygen生成文档https://yurutu.github.io/NANA/files.html

        具体实现的矩阵功能有: 

 

        最初版代码及其使用        

利用C++的类实现矩阵的运算,可实现矩阵的+-*运算,以及用高斯消去法求解线性方程组Ax=b

2018/10/13新增功能 矩阵的行列变换 高斯消元法得到上三角矩阵 

2018/12/9实现矩阵的逆运算等

预编译头文件

pch.h

#ifndef PCH_H #define PCH_H #include #include #include #include // TODO: 添加要在此处预编译的标头 #endif //PCH_H

 头文件.h

/* author: cclplus date:2018/12/09 if you think it is necessary to reward me, my alipay account number is [email protected] */ #ifndef __MATRIX_CLL_H__ #define __MATRIX_CCL_H__ #include "pch.h" class Matrix { private: int rows_num, cols_num; double **p; void initialize();//初始化矩阵 public: Matrix(int, int); Matrix(int, int, double);//预配分空间 virtual ~Matrix();//析构函数应当是虚函数,除非此类不用做基类 Matrix& operator=(const Matrix&);//矩阵的复制 Matrix& operator=(double *);//将数组的值传给矩阵 Matrix& operator+=(const Matrix&);//矩阵的+=操作 Matrix& operator-=(const Matrix&);//-= Matrix& operator*=(const Matrix&);//*= Matrix operator*(const Matrix & m)const; static Matrix Solve(const Matrix&, const Matrix&);//求解线性方程组Ax=b void Show() const;//矩阵显示 void swapRows(int, int); double det();//求矩阵的行列式 double Point(int i, int j) const; static Matrix inv(Matrix);//求矩阵的逆矩阵 static Matrix eye(int );//制造一个单位矩阵 int row() const; int col() const; static Matrix T(const Matrix & m);//矩阵转置的实现,且不改变矩阵 Matrix gaussianEliminate();//高斯消元法 friend std::istream& operator>>(std::istream&, Matrix&);//实现矩阵的输入 }; #endif

头文件.cpp

/* author: cclplus date : 2018 / 12 / 09 if you think it is necessary to reward me, my alipay account number is [email protected] */ #include "pch.h" #include "matrix.h" using std::endl; using std::cout; using std::istream; const double EPS = 1e-10; void Matrix::initialize() {//初始化矩阵大小 p = new double*[rows_num];//分配rows_num个指针 for (int i = 0; i < rows_num; ++i) { p[i] = new double[cols_num];//为p[i]进行动态内存分配,大小为cols } } //声明一个全0矩阵 Matrix::Matrix(int rows, int cols) { rows_num = rows; cols_num = cols; initialize(); for (int i = 0; i < rows_num; i++) { for (int j = 0; j < cols_num; j++) { p[i][j] = 0; } } } //声明一个值全部为value的矩阵 Matrix::Matrix(int rows, int cols, double value) { rows_num = rows; cols_num = cols; initialize(); for (int i = 0; i < rows_num; i++) { for (int j = 0; j < cols_num; j++) { p[i][j] = value; } } } //析构函数 Matrix::~Matrix() { for (int i = 0; i < rows_num; ++i) { delete[] p[i]; } delete[] p; } //实现矩阵的复制 Matrix& Matrix::operator=(const Matrix& m) { if (this == &m) { return *this; } if (rows_num != m.rows_num || cols_num != m.cols_num) { for (int i = 0; i < rows_num; ++i) { delete[] p[i]; } delete[] p; rows_num = m.rows_num; cols_num = m.cols_num; initialize(); } for (int i = 0; i < rows_num; i++) { for (int j = 0; j < cols_num; j++) { p[i][j] = m.p[i][j]; } } return *this; } //将数组的值传递给矩阵(要求矩阵的大小已经被声明过了) Matrix& Matrix::operator=(double *a){ for(int i=0;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3