优化了一些算法
#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++矩阵运算的更多相关文章

  1. [Python学习] python 科学计算库NumPy—矩阵运算

    NumPy库的核心是矩阵及其运算. 使用array()函数可以将python的array_like数据转变成数组形式,使用matrix()函数转变成矩阵形式. 基于习惯,在实际使用中较常用array而 ...

  2. [OSG]矩阵运算

    我们都知道,OpenGL规定矩阵使用列主序存储,即glLoadMatrix等函数要求输入的数组是按列主序存储的矩阵.然而,一个很奇怪的事实是,OSG中矩阵存储是使用的标准C二维数组(行主序),并且也是 ...

  3. 基本矩阵运算的Java实现

      一: 矩阵的加法与减法 规则:矩阵的加法与减法要求两个矩阵的行列完全相等,方可以完成两个矩阵的之间的运算. 举例说明如下 二:矩阵的乘法 规则:矩阵的乘法要求两个矩阵符合A(mx k),  B( ...

  4. C#矩阵运算类库

    这个类库是本人参考许多相关资料之后做出的C#矩阵运算类库,因为C#的数值计算库相对比较少,所以希望这个类库能够给大家带来一些帮助. 源码github网址:https://github.com/Josh ...

  5. 【Unity】矩阵运算

    http://www.cnblogs.com/wywnet/p/3585075.html Vector3:  Unity3D中Vector3类定义(只写有用的):  属性:  x,y,z       ...

  6. C++矩阵运算库armadillo配置笔记

    前言 最近在用C++实现神经网络模型,优化算法需要用到矩阵操作,一开始我用的是boost的ublas库,但用着用着感觉很不习惯,接口不够友好.于是上网搜索矩阵运算哪家强,大神们都推荐armadillo ...

  7. C++矩阵运算库推荐

    最近在几个地方都看到有人问C++下用什么矩阵运算库比较好,顺便做了个调查,做一些相关的推荐吧.主要针对稠密矩阵,有时间会再写一个稀疏矩阵的推荐. Armadillo:C++下的Matlab替代品 地址 ...

  8. 实验12:Problem G: 强悍的矩阵运算来了

    这个题目主要是乘法运算符的重载,卡了我好久,矩阵的乘法用3个嵌套的for循环进行,要分清楚矩阵的乘法结果是第一个矩阵的行,第二个矩阵的列所组成的矩阵. 重载+,*运算符时,可以在参数列表中传两个矩阵引 ...

  9. python矩阵运算 不断收集整理

    python矩阵运算 转自:http://blog.sina.com.cn/s/blog_5f234d4701012p64.html Python使用NumPy包完成了对N-维数组的快速便捷操作.使用 ...

  10. 用cudamat做矩阵运算的GPU加速

    1. cudamat简介 cudamat是一个python语言下,利用NVIDIA的cuda sdk 进行矩阵运算加速的库.对于不熟悉cuda编程的程序员来说,这是一个非常方便的GPU加速方案.很多工 ...

随机推荐

  1. UI设计网站参考

    1. https://dribbble.com/colors/6A969A 2. 设计师网站导航:http://hao.uisdc.com/ 3. bootstrap的UI:http://www.bo ...

  2. linux shell中的特殊符号

    该内容,均来自此网址(http://www.92csz.com/study/linux/12.htm).在下只是把那些命令的截图给去了. 你在学习linux的过程中,也许你已经接触过某个特殊符号,例如 ...

  3. Android开源项目发现--- 工具类向下兼容篇(持续更新)

    1. ActionBarSherlock 为Android所有版本提供统一的ActionBar,解决4.0以下ActionBar的适配问题 项目地址:https://github.com/JakeWh ...

  4. Git 、CVS、SVN比较

    Git .CVS.SVN比较 项目源代码的版本管理工具中,比较常用的主要有:CVS.SVN.Git 和 Mercurial  (其中,关于SVN,请参见我先前的博客:SVN常用命令 和 SVN服务器配 ...

  5. PL/SQL游标使用

    游标是用来处理使用SELECT语句从数据库中检索到的多行记录的工具.借助游标的功能,数据库应用程序可以对一组记录逐个进行处理,每次处理一行. 游标是从数据表中提取出来的数据,以临时表的形式存放在内存中 ...

  6. Centos 6.5安装最新版谷歌浏览器-Chrome

    (1)在root下直接运行:yum install --skip-broken google-chrome-stable(2015/6/25更新) (2)网上很多相关到资料,不过都比较繁琐,下面给出一 ...

  7. 一起啃PRML - 1 Introduction 绪论

    一起啃PRML - 1 Introduction @copyright 转载请注明出处 http://www.cnblogs.com/chxer/ 这一部分主要是介绍一下Pattern Recogni ...

  8. 【转】windows常用消息大全(系统消息、通告消息、用户消息)

    原文网址:http://blog.csdn.net/nupt123456789/article/details/7370562 附录A Windows 常用消息大全 表A-1  Windows消息分布 ...

  9. mkimage使用详解

    uboot源代码的tools/目录下有mkimage工具,这个工具可以用来制作不压缩或者压缩的多种可启动映象文件. mkimage在制作映象文件的时候,是在原来的可执行映象文件的前面加上一个0x40字 ...

  10. 游戏开发设计模式之原型模式 & unity3d JSON的使用(unity3d 示例实现)

    命令模式:游戏开发设计模式之命令模式(unity3d 示例实现) 对象池模式:游戏开发设计模式之对象池模式(unity3d 示例实现) 实现原型模式 原型模式带来的好处就是,想要构建生成任意独特对象的 ...