阅读这篇文章需要掌握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 <cmath>
#include <iostream>
#include <stdlib.h>
#include <cmath>
// 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<rows_num;i++){
for(int j=0;j<cols_num;j++){
p[i][j]= *(a+i*cols_num+j);
}
}
return *this;
}
//+=操作
Matrix& Matrix::operator+=(const Matrix& m)
{
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-=(const Matrix& m)
{
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*=(const Matrix& m)
{
Matrix temp(rows_num, m.cols_num);//若C=AB,则矩阵C的行数等于矩阵A的行数,C的列数等于B的列数。
for (int i = 0; i < temp.rows_num; i++) {
for (int j = 0; j < temp.cols_num; j++) {
for (int k = 0; k < cols_num; k++) {
temp.p[i][j] += (p[i][k] * m.p[k][j]);
}
}
}
*this = temp;
return *this;
}
//实现矩阵的乘法
Matrix Matrix::operator*(const Matrix & m)const{
Matrix ba_M(rows_num,m.cols_num,0.0);
for(int i=0;i<rows_num;i++){
for(int j=0;j<m.cols_num;j++){
for(int k=0;k<cols_num;k++){
ba_M.p[i][j]+=(p[i][k]*m.p[k][j]);
}
}
}
return ba_M;
}
//解方程Ax=b
Matrix Matrix::Solve(const Matrix &A, const Matrix &b)
{
//高斯消去法实现Ax=b的方程求解
for (int i = 0; i < A.rows_num; i++) {
if (A.p[i][i] == 0) {
cout << "请重新输入" << endl;
}
for (int j = i + 1; j < A.rows_num; j++) {
for (int k = i + 1; k < A.cols_num; k++) {
A.p[j][k] -= A.p[i][k] * (A.p[j][i] / A.p[i][i]);
if (abs(A.p[j][k]) < EPS)
A.p[j][k] = 0;
}
b.p[j][0] -= b.p[i][0] * (A.p[j][i] / A.p[i][i]);
if (abs(A.p[j][0]) < EPS)
A.p[j][0] = 0;
A.p[j][i] = 0;
}
}
// 反向代换
Matrix x(b.rows_num, 1);
x.p[x.rows_num - 1][0] = b.p[x.rows_num - 1][0] / A.p[x.rows_num - 1][x.rows_num - 1];
if (abs(x.p[x.rows_num - 1][0]) < EPS)
x.p[x.rows_num - 1][0] = 0;
for (int i = x.rows_num - 2; i >= 0; i--) {
double sum = 0;
for (int j = i + 1; j < x.rows_num; j++) {
sum += A.p[i][j] * x.p[j][0];
}
x.p[i][0] = (b.p[i][0] - sum) / A.p[i][i];
if (abs(x.p[i][0]) < EPS)
x.p[i][0] = 0;
}
return x;
}
//矩阵显示
void Matrix::Show() const {
//cout << rows_num <<" "<<cols_num<< endl;//显示矩阵的行数和列数
for (int i = 0; i < rows_num; i++) {
for (int j = 0; j < cols_num; j++) {
cout << p[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
//实现行变换
void Matrix::swapRows(int a, int b)
{
a--;
b--;
double *temp = p[a];
p[a] = p[b];
p[b] = temp;
}
//计算矩阵行列式的值
double Matrix::det(){
//为计算行列式做一个备份
double ** back_up;
back_up=new double *[rows_num];
for(int i=0;i<rows_num;i++){
back_up[i]=new double[cols_num];
}
for(int i=0;i<rows_num;i++){
for(int j=0;j<cols_num;j++){
back_up[i][j]=p[i][j];
}
}
if(rows_num!=cols_num){
std::abort();//只有方阵才能计算行列式,否则调用中断强制停止程序
}
double ans=1;
for(int i=0;i<rows_num;i++){
//通过行变化的形式,使得矩阵对角线上的主元素不为0
if(abs(p[i][i])<=EPS){
bool flag=false;
for(int j=0;(j<cols_num)&&(!flag);j++){
//若矩阵的一个对角线上的元素接近于0且能够通过行变换使得矩阵对角线上的元素不为0
if((abs(p[i][j])>EPS)&&(abs(p[j][i])>EPS)){
flag=true;
//注:进行互换后,p[i][j]变为p[j][j],p[j][i]变为p[i][i]
//对矩阵进行行变换
double temp;
for(int k=0;k<cols_num;k++){
temp=p[i][k];
p[i][k]=p[j][k];
p[j][k]=temp;
}
}
}
if(flag)
return 0;
}
}
for(int i=0;i<rows_num;i++){
for(int j=i+1;j<rows_num;j++){
for(int k=i+1;k<cols_num;k++){
p[j][k]-=p[i][k]*(p[j][i]*p[i][i]);
}
}
}
for(int i=0;i<rows_num;i++){
ans*=p[i][i];
}
for(int i=0;i<rows_num;i++){
for(int j=0;j<cols_num;j++){
p[i][j]=back_up[i][j];
}
}
return ans;
}
//返回矩阵第i行第j列的数
double Matrix::Point(int i, int j) const{
return this->p[i][j];
}
//求矩阵的逆矩阵
Matrix Matrix::inv(Matrix A){
if(A.rows_num!=A.cols_num){
std::cout<<"只有方阵能求逆矩阵"<<std::endl;
std::abort();//只有方阵能求逆矩阵
}
double temp;
Matrix A_B=Matrix(A.rows_num,A.cols_num);
A_B=A;//为矩阵A做一个备份
Matrix B=eye(A.rows_num);
//将小于EPS的数全部置0
for (int i = 0; i < A.rows_num; i++) {
for (int j = 0; j < A.cols_num; j++) {
if (abs(A.p[i][j]) <= EPS) {
A.p[i][j] = 0;
}
}
}
//选择需要互换的两行选主元
for(int i=0;i<A.rows_num;i++){
if(abs(A.p[i][i])<=EPS){
bool flag=false;
for(int j=0;(j<A.rows_num)&&(!flag);j++){
if((abs(A.p[i][j])>EPS)&&(abs(A.p[j][i])>EPS)){
flag=true;
for(int k=0;k<A.cols_num;k++){
temp=A.p[i][k];
A.p[i][k]=A.p[j][k];
A.p[j][k]=temp;
temp=B.p[i][k];
B.p[i][k]=B.p[j][k];
B.p[j][k]=temp;
}
}
}
if(!flag){
std::cout<<"逆矩阵不存在\n";
std::abort();
}
}
}
//通过初等行变换将A变为上三角矩阵
double temp_rate;
for(int i=0;i<A.rows_num;i++){
for(int j=i+1;j<A.rows_num;j++){
temp_rate=A.p[j][i]/A.p[i][i];
for(int k=0;k<A.cols_num;k++){
A.p[j][k]-=A.p[i][k]*temp_rate;
B.p[j][k]-=B.p[i][k]*temp_rate;
}
A.p[j][i]=0;
}
}
//使对角元素均为1
for(int i=0;i<A.rows_num;i++){
temp=A.p[i][i];
for(int j=0;j<A.cols_num;j++){
A.p[i][j]/=temp;
B.p[i][j]/=temp;
}
}
//std::cout<<"算法可靠性检测,若可靠,输出上三角矩阵"<<std::endl;
//将已经变为上三角矩阵的A,变为单位矩阵
for(int i=A.rows_num-1;i>=1;i--){
for(int j=i-1;j>=0;j--){
temp=A.p[j][i];
for(int k=0;k<A.cols_num;k++){
A.p[j][k]-=A.p[i][k]*temp;
B.p[j][k]-=B.p[i][k]*temp;
}
}
}
std::cout<<"算法可靠性检测,若可靠,输出单位矩阵"<<std::endl;
for(int i=0;i<A.rows_num;i++){
for(int j=0;j<A.cols_num;j++){
printf("%7.4lf\t\t",A.p[i][j]);
}
cout << endl;
}
A=A_B;//还原A
return B;//返回该矩阵的逆矩阵
}
//制造一个单位矩阵
Matrix Matrix::eye(int n){
Matrix A(n,n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j){
A.p[i][j]=1;
}else{
A.p[i][j]=0;
}
}
}
return A;
}
//读取矩阵行列数
int Matrix::row() const{
return rows_num;
}
int Matrix::col() const{
return cols_num;
}
//实现矩阵的转置
Matrix Matrix::T(const Matrix & m)
{ int col_size=m.col();
int row_size=m.row();
Matrix mt(col_size, row_size);
for (int i = 0; i <row_size; i++) {
for (int j = 0; j <col_size; j++) {
mt.p[j][i] = m.p[i][j];
}
}
return mt;
}
//高斯消元法
Matrix Matrix::gaussianEliminate()
{
Matrix Ab(*this);
int rows = Ab.rows_num;
int cols = Ab.cols_num;
int Acols = cols - 1;
int i = 0; //跟踪行
int j = 0; //跟踪列
while (i < rows)
{
bool flag = false;
while (j < Acols && !flag)
{
if (Ab.p[i][j] != 0) {
flag = true;
}
else {
int max_row = i;
double max_val = 0;
for (int k = i + 1; k < rows; ++k)
{
double cur_abs = Ab.p[k][j] >= 0 ? Ab.p[k][j] : -1 * Ab.p[k][j];
if (cur_abs > max_val)
{
max_row = k;
max_val = cur_abs;
}
}
if (max_row != i) {
Ab.swapRows(max_row, i);
flag = true;
}
else {
j++;
}
}
}
if (flag)
{
for (int t = i + 1; t < rows; t++) {
for (int s = j + 1; s < cols; s++) {
Ab.p[t][s] = Ab.p[t][s] - Ab.p[i][s] * (Ab.p[t][j] / Ab.p[i][j]);
if (abs(Ab.p[t][s]) <EPS)
Ab.p[t][s] = 0;
}
Ab.p[t][j] = 0;
}
}
i++;
j++;
}
return Ab;
}
//实现矩阵的输入
istream& operator>>(istream& is, Matrix& m)
{
for (int i = 0; i < m.rows_num; i++) {
for (int j = 0; j < m.cols_num; j++) {
is >> m.p[i][j];
}
}
return is;
}
主程序
#include "matrix.h"
using namespace std;
int main()
{
Matrix A = Matrix(3, 3);
cin >> A;
Matrix b = Matrix(3, 1);
cin >> b;
Matrix x = Matrix::Solve(A, b);
x.Show();
return 0;
}
求赞,求转发
文章浏览阅读1.3w次。转载自 http://www.miui.com/thread-2003672-1-1.html 当手机在刷错包或者误修改删除系统文件后会出现无法开机或者是移动定制(联通合约机)版想刷标准版,这时就会用到线刷,首先就是安装线刷驱动。 在XP和win7上线刷是比较方便的,用那个驱动自动安装版,直接就可以安装好,完成线刷。不过现在也有好多机友换成了win8/8.1系统,再使用这个_mt65驱动
文章浏览阅读1k次。SonarQube是一个代码质量管理平台,可以扫描监测代码并给出质量评价及修改建议,通过插件机制支持25+中开发语言,可以很容易与gradle\maven\jenkins等工具进行集成,是非常流行的代码质量管控平台。通CheckStyle、findbugs等工具定位不同,SonarQube定位于平台,有完善的管理机制及强大的管理页面,并通过插件支持checkstyle及findbugs等既有的流..._sonar的客户端区别
文章浏览阅读3.4k次,点赞2次,收藏27次。神经图灵机是LSTM、GRU的改进版本,本质上依然包含一个外部记忆结构、可对记忆进行读写操作,主要针对读写操作进行了改进,或者说提出了一种新的读写操作思路。神经图灵机之所以叫这个名字是因为它通过深度学习模型模拟了图灵机,但是我觉得如果先去介绍图灵机的概念,就会搞得很混乱,所以这里主要从神经图灵机改进了LSTM的哪些方面入手进行讲解,同时,由于模型的结构比较复杂,为了让思路更清晰,这次也会分开几..._神经图灵机方法改进
文章浏览阅读2.8k次。一、模型迭代方法机器学习模型在实际应用的场景,通常要根据新增的数据下进行模型的迭代,常见的模型迭代方法有以下几种:1、全量数据重新训练一个模型,直接合并历史训练数据与新增的数据,模型直接离线学习全量数据,学习得到一个全新的模型。优缺点:这也是实际最为常见的模型迭代方式,通常模型效果也是最好的,但这样模型迭代比较耗时,资源耗费比较多,实时性较差,特别是在大数据场景更为困难;2、模型融合的方法,将旧模..._模型迭代
文章浏览阅读2.3k次。1、前言上传图片一般采用异步上传的方式,但是异步上传带来不好的地方,就如果图片有改变或者删除,图片服务器端就会造成浪费。所以有时候就会和参数同步提交。笔者喜欢base64图片一起上传,但是图片过多时就会出现数据丢失等异常。因为tomcat的post请求默认是2M的长度限制。2、解决办法有两种:① 修改tomcat的servel.xml的配置文件,设置 maxPostSize=..._base64可以装换zip吗
文章浏览阅读1k次,点赞17次,收藏22次。Opencv自然场景文本识别系统(源码&教程)_opencv自然场景实时识别文字
文章浏览阅读1.3k次。拷贝虚拟机文件时间比较长,因为虚拟机 flat 文件很大,所以要等。脚本完成后,以复制虚拟机文件夹。将以下脚本内容写入文件。_exsi6.7快速克隆centos
文章浏览阅读2k次。本文主要实现基于二度好友的推荐。数学公式参考于:http://blog.csdn.net/qq_14950717/article/details/52197565测试数据为自己随手画的关系图把图片整理成文本信息如下:a b c d e f yb c a f gc a b dd c a e h q re f h d af e a b gg h f bh e g i di j m n ..._本关任务:使用 spark core 知识完成 " 好友推荐 " 的程序。
文章浏览阅读367次。南京大学高级程序设计期末复习总结,c++面向对象编程_南京大学高级程序设计
文章浏览阅读3.1k次,点赞2次,收藏12次。实现朴素贝叶斯分类器,并且根据李航《统计机器学习》第四章提供的数据训练与测试,结果与书中一致分别实现了朴素贝叶斯以及带有laplace平滑的朴素贝叶斯%书中例题实现朴素贝叶斯%特征1的取值集合A1=[1;2;3];%特征2的取值集合A2=[4;5;6];%S M LAValues={A1;A2};%Y的取值集合YValue=[-1;1];%数据集和T=[ 1,4,-1;..._朴素贝叶斯 matlab训练和测试输出
文章浏览阅读1.6k次。Markdown 文本换行_markdowntext 换行
文章浏览阅读6.7w次,点赞2次,收藏37次。win10 2016长期服务版激活错误解决方法:打开“注册表编辑器”;(Windows + R然后输入Regedit)修改SkipRearm的值为1:(在HKEY_LOCAL_MACHINE–》SOFTWARE–》Microsoft–》Windows NT–》CurrentVersion–》SoftwareProtectionPlatform里面,将SkipRearm的值修改为1)重..._错误: 0xc0000022 在运行 microsoft windows 非核心版本的计算机上,运行“slui.ex