c++矩阵运算
优化了一些算法
#pragma once
#include <iostream>
#include <iomanip>
#include <string> #define OVERFLOWED 1E-12
class Matrix
{
public:
Matrix(int m, int n); //构建一个m*n的全零矩阵
Matrix(int n); //构建一个n*n的单位矩阵
Matrix(const Matrix &); //拷贝构造函数,深拷贝
Matrix(double* items, int m, int n);//根据数组拷贝一个矩阵
~Matrix();
static Matrix FromFile(std::string file);
int getRowNum() const; //返回矩阵的行数
int getColNum() const; //返回矩阵的列数 Matrix Trans() const; //将矩阵转置 double get(int i, int j) const; //返回矩阵第i行j列元素
void set(int i, int j, double val); //设置矩阵第i行j列元素 Matrix operator +(const Matrix &m); //两个矩阵相加
Matrix operator -(const Matrix &m); //两个矩阵相减
Matrix operator *(const Matrix &m); //两个矩阵相乘
Matrix operator *(const double f); //矩阵乘以常数
Matrix& operator=(const Matrix& m);
Matrix Inverse(); friend std::ostream& operator <<(std::ostream &os, const Matrix &m); private:
double *item; //指向矩阵首元素
int rowNum; //矩阵行数
int colNum; //矩阵列数 private:
//矩阵初等行变换
//如果j=-1,则对i扩大multiply倍
//如果j在取值范围内,则将第i行扩大multiply倍加到j行
void RowSwap(int i, int j, double multiply);
//交换两行
void RowSwap(int i, int j);
void FlowOver();
};
#include "Matrix.h"
#include <vector>
#include <cmath>
#include <fstream>
#include <sstream> using namespace std;
Matrix::Matrix(int m, int n)
{
if (m < || n < )
{
cout << "矩阵大小不能为负\n";
return;
}
rowNum = m;
colNum = n;
item = new double[m*n];
for (int i = ; i < m*n; i++)
{
item[i] = ;
}
} //也可用二维数组初始化
Matrix::Matrix(double* items, int m, int n)
{
rowNum = m;
colNum = n;
item = new double[m*n];
for (int i = ; i < colNum*rowNum; i++)
{
item[i] = items[i];
}
}
Matrix::Matrix(int n)
{
rowNum = colNum = n;
item = new double[n*n];
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
if (i == j)
set(i, j, 1.0);
else
set(i, j, );
}
}
}
Matrix::Matrix(const Matrix &M)
{
colNum = M.colNum;
rowNum = M.rowNum;
//这里不能对指针直接赋值,复制对求逆、转置等操作会影响原矩阵
item = new double[colNum*rowNum];
for (int i = ; i < colNum*rowNum; i++)
{
item[i] = M.item[i];
}
}
Matrix& Matrix::operator=(const Matrix & M)
{
colNum = M.colNum;
rowNum = M.rowNum;
if (item != nullptr) delete[] item;
item = new double[colNum*rowNum];
for (int i = ; i < colNum*rowNum; i++)
{
item[i] = M.item[i];
}
return *this;
} Matrix Matrix::FromFile(std::string file)
{
ifstream read(file);
if (!read.is_open())
{
cout << "Matrix::未能打开文件\n";
}
int rows = ;
string line;
vector<double> nums;
while (getline(read, line))
{
istringstream record(line);
double num = 0.0;
while (record >> num) nums.push_back(num);
rows++;
}
return Matrix(&(*nums.begin()), rows, nums.size() / rows);
} Matrix::~Matrix()
{
delete[] item;
}
double Matrix::get(int i, int j) const
{
return item[i*colNum + j];
}
void Matrix::set(int i, int j, double value)
{
item[i*colNum + j] = value;
}
void Matrix::RowSwap(int i, int j, double multiply)
{
if (j == -)
{
for (int k = ; k < colNum; k++)
{
set(i, k, multiply*get(i, k));
}
}
else
{
for (int k = ; k < colNum; k++)
{
set(j, k, multiply*get(i, k) + get(j, k));
}
}
}
void Matrix::RowSwap(int i, int j)
{
Matrix _copy = *this;
for (int k = ; k < colNum; k++)
{
double swap = _copy.get(j, k);
set(j, k, _copy.get(i, k));
set(i, k, swap);
}
}
Matrix Matrix::Trans() const
{
Matrix _copy = *this;
_copy.rowNum = this->colNum;
_copy.colNum = this->rowNum;
for (int i = ; i < _copy.rowNum; i++)
{
for (int j = ; j < _copy.colNum; j++)
{
_copy.set(i, j, get(j, i));
}
}
return _copy;
}
int Matrix::getRowNum() const
{
return rowNum;
}
int Matrix::getColNum() const
{
return colNum;
}
ostream& operator <<(ostream &os, const Matrix &m)
{
for (int i = ; i < m.rowNum; i++)
{
for (int j = ; j < m.colNum; j++)
os << std::setw() << std::fixed << std::setprecision() << m.get(i, j) << " ";
os << "\n";
}
os.flush();
return os;
}
Matrix Matrix::operator +(const Matrix &m)
{
if (m.colNum != colNum || m.rowNum != rowNum) return *this;
Matrix _copy = *this;
for (int i = ; i < rowNum; i++)
{
for (int j = ; j < colNum; j++)
{
_copy.set(i, j, get(i, j) + m.get(i, j));
}
}
return _copy;
}
Matrix Matrix::operator -(const Matrix &m)
{
if (m.colNum != colNum || m.rowNum != rowNum) return *this;
Matrix _copy = *this;
for (int i = ; i < rowNum; i++)
{
for (int j = ; j < colNum; j++)
{
_copy.set(i, j, get(i, j) - m.get(i, j));
}
}
return _copy;
}
Matrix Matrix::operator *(const double f)
{
Matrix _copy = *this;
for (int i = ; i < rowNum; i++)
{
for (int j = ; j < colNum; j++)
{
_copy.set(i, j, get(i, j)*f);
}
}
return _copy;
}
Matrix Matrix::operator *(const Matrix &m)
{
if (colNum != m.rowNum)
{
cout << "无法相乘!";
return *this;
}
Matrix _copy(rowNum, m.getColNum());
for (int i = ; i < rowNum; i++)
{
for (int j = ; j < m.colNum; j++)
{
double sum = ;
for (int k = ; k < m.rowNum; k++)
{
sum += get(i, k)*m.get(k, j);
}
_copy.set(i, j, sum);
}
}
return _copy;
}
Matrix Matrix::Inverse()
{
Matrix _copy = *this;
//变换结果
Matrix result(colNum);
if (colNum != rowNum)
{
cout << "矩阵不可逆!" << endl;
return *this;
}
for (int i = ; i < rowNum; i++)
{
int MaxRow = i;
//首先找到第i列的绝对值最大的数,并将该行和第i行互换
double max = abs(_copy.get(i, i));
for (int j = i; j < colNum; j++)
{
if (abs(_copy.get(j, i))>max)
{
max = abs(_copy.get(j, i));
MaxRow = j;
}
}
//交换j,i两行
if (MaxRow != i)
{
result.RowSwap(i, MaxRow);
_copy.RowSwap(i, MaxRow);
}
//将第i行做初等行变换,将第一个非0元素化为1
double r = 1.0 / _copy.get(i, i);
_copy.RowSwap(i, -, r);
result.RowSwap(i, -, r);
//消元
for (int j = ; j < rowNum; j++)
{
if (j == i) continue;
r = -_copy.get(j, i);
_copy.RowSwap(i, j, r);
result.RowSwap(i, j, r);
}
}
//result.FlowOver();
return result;
}
void Matrix::FlowOver()
{
for (int i = ; i < rowNum;i++)
{
for (int j = ; j < colNum;j++)
{
if (abs(get(i, j)) <= OVERFLOWED) set(i, j, );
}
}
}
c++矩阵运算的更多相关文章
- [Python学习] python 科学计算库NumPy—矩阵运算
NumPy库的核心是矩阵及其运算. 使用array()函数可以将python的array_like数据转变成数组形式,使用matrix()函数转变成矩阵形式. 基于习惯,在实际使用中较常用array而 ...
- [OSG]矩阵运算
我们都知道,OpenGL规定矩阵使用列主序存储,即glLoadMatrix等函数要求输入的数组是按列主序存储的矩阵.然而,一个很奇怪的事实是,OSG中矩阵存储是使用的标准C二维数组(行主序),并且也是 ...
- 基本矩阵运算的Java实现
一: 矩阵的加法与减法 规则:矩阵的加法与减法要求两个矩阵的行列完全相等,方可以完成两个矩阵的之间的运算. 举例说明如下 二:矩阵的乘法 规则:矩阵的乘法要求两个矩阵符合A(mx k), B( ...
- C#矩阵运算类库
这个类库是本人参考许多相关资料之后做出的C#矩阵运算类库,因为C#的数值计算库相对比较少,所以希望这个类库能够给大家带来一些帮助. 源码github网址:https://github.com/Josh ...
- 【Unity】矩阵运算
http://www.cnblogs.com/wywnet/p/3585075.html Vector3: Unity3D中Vector3类定义(只写有用的): 属性: x,y,z ...
- C++矩阵运算库armadillo配置笔记
前言 最近在用C++实现神经网络模型,优化算法需要用到矩阵操作,一开始我用的是boost的ublas库,但用着用着感觉很不习惯,接口不够友好.于是上网搜索矩阵运算哪家强,大神们都推荐armadillo ...
- C++矩阵运算库推荐
最近在几个地方都看到有人问C++下用什么矩阵运算库比较好,顺便做了个调查,做一些相关的推荐吧.主要针对稠密矩阵,有时间会再写一个稀疏矩阵的推荐. Armadillo:C++下的Matlab替代品 地址 ...
- 实验12:Problem G: 强悍的矩阵运算来了
这个题目主要是乘法运算符的重载,卡了我好久,矩阵的乘法用3个嵌套的for循环进行,要分清楚矩阵的乘法结果是第一个矩阵的行,第二个矩阵的列所组成的矩阵. 重载+,*运算符时,可以在参数列表中传两个矩阵引 ...
- python矩阵运算 不断收集整理
python矩阵运算 转自:http://blog.sina.com.cn/s/blog_5f234d4701012p64.html Python使用NumPy包完成了对N-维数组的快速便捷操作.使用 ...
- 用cudamat做矩阵运算的GPU加速
1. cudamat简介 cudamat是一个python语言下,利用NVIDIA的cuda sdk 进行矩阵运算加速的库.对于不熟悉cuda编程的程序员来说,这是一个非常方便的GPU加速方案.很多工 ...
随机推荐
- hadoop 常用配置项
core-site.xml name value Description fs.default.name hdfs://hadoopmaster:9000 定义HadoopMaster的URI ...
- Python3.x和Python2.x的区别-转
这个星期开始学习Python了,因为看的书都是基于Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下3.x和 ...
- c# 函数练习;结构体、枚举类型
* 结构体 1.就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样. 注意:枚举类型和结构体都属于值类型. 2.定义的方法: struct student { public in ...
- What does the number on the visual studio solution icon represent?
The numbers correspond to the internal version numbers of various editions of Visual Studio http://e ...
- HTML,CSS编码规范
不管有多少人共同参与同一项目,一定要确保每一行代码都像是同一个人编写的. HTML 语法 对于属性的定义,确保全部使用双引号,绝不要使用单引号. 为每个 HTML 页面的第一行添加标准模式(stand ...
- WordPress Simple Login Registration插件’username‘参数跨站脚本漏洞
漏洞名称: WordPress Simple Login Registration插件’username‘参数跨站脚本漏洞 CNNVD编号: CNNVD-201308-519 发布时间: 2013-0 ...
- 搜索提示時jquery的focusout和click事件沖突問題完美解决
在主流的搜索引擎上搜索時,輸入內容,往往會彈出智能提示.輸入框为input,智能提示區域为suggest.接下來一般有兩種操作: 1.選擇某一提示,則把內容复制到input中 ...
- HDOJ1181变形课 深搜回溯
变形课 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submissi ...
- UNION、EXCEPT和INTERSECT操作查询结果
对查询结果进行合并.剔除.取重操作可以通过UNION.EXCEPT和INTERSECT实现 任意一种操作都要满足以下两个条件: 1.字段的数量和顺序一致 2.对应字段的数据类型相兼容 一.UNION ...
- 自己生成非官方iPhone toolchain的头文件
如果你已经搭建好非官方iPhone toolchain开发包,如果缺少某些头文件,可以用以下方法自己生成. 首先下载class-dump: http://www.codethecode.com/pro ...