全栈工程师开发手册 (作者:栾鹏)
matlab2c动态链接库下载matlab库函数大全
matlab2c基础教程
matlab2c开发全解教程
开发注意事项:
1、目前matlab2c对矩阵的实现仅包含实数型、复数型数据。实数型矩阵使用Matrix定义,复数型矩阵使用CMatrix定义。
2、实数矩阵元素int、float元素类型会自动转为double。 复数元素类型为c++标准复数std::complex使用typedef别名定义为Complex
3、本应用开发环境为vs2012,建议使用c++11编译器
4、借助广大网友的力量,动态链接库会实时更新实现更多的函数功能,欢迎下载最新版。
5、开发中请尽量规范代码编写,调用函数使用Matlab2c::xx函数,避免与其他库同名函数、同名变量的冲突
matlab2c调用方法:
1、下载动态链接库
2、将Matlab2c.dll拷贝到exe同目录下
3、将Matlab2c.h、Matlab2c.lib放到项目头文件目录下
4、在cpp文件中引入下面的代码
#include "Matlab2c.h"
#pragma comment(lib,"Matlab2c.lib")
using namespace Matlab2c;
c++实现Matlab矩阵Matrix类
使用c++实现Matlab实数矩阵Matrix类和复数矩阵CMatrix,是今后c++实现Matlab函数功能的基础,保留了Matlab中矩阵复制、赋值,加减乘数算数运算,()运算,正负号运算,以及矩阵使用类似Matlab中的生成方式,子矩阵读取,矩阵的扩充,变换等。建议首先查看matlab2c基础教程
在文章首部下载matlab2c动态链接库即可下载到该h文件。
#pragma once //防止文件被多重包含
#include <stdio.h>
#include <stdlib.h>
#include <string> //字符串
#include <fstream> //文件流
#include <iostream> //数据流
#include <memory> //内存操作
#include <regex> //正则表达式
#include <exception> //异常
#include <stdexcept> //异常
#include <stdexcept> //异常类型
#include <iterator> //迭代器
#include <array> //可获取大小的固定数组
#include <vector> //数据结构 连续分布的动态数组
#include <algorithm> //STL算法
#include <numeric> //数值计算算法
#include <math.h> //数值计算算法
#include <complex>
#include <functional> //函数指针,将函数指针作为自定义函数参数 实现设置回调函数
#include <time.h> //时间类型
#include <stdarg.h> //各种参数形式
#include <algorithm> //min函数和max函数
#include <iomanip> //为了能固定输出小数位的个数
#include <random> //随机数库
#include <complex> //引入复数
using namespace std;
//自定义命名空间
namespace Matlab2c
{
class CMatrix;
class Matrix;
typedef std::complex<double> Complex;
const float FLOATERROR = 1.0e-6F; //最小浮点数,浮点比较误差
const double DOUBLEERROR = 1.0e-15; //最小double,比较误差
const long double LONGDOUBLEERROR = 1.0e-30; //long double比较误差
const double GoldNo = 0.618033399; //黄金分割常数(1.0-0.381966)
const double inf = DBL_MAX; //无穷大
const double pi = 3.1415926535898; //圆周率
const double PI = 3.1415926535898; //圆周率
const double eps = DOUBLEERROR; //精度容许误差
//内联函数,用于在出现逻辑错误时,弹出信息
inline void throw_logic_error(string str){
throw logic_error(str);
}
//声明调用字符串
void trim_string(string &s)
{
if (!s.empty())
{
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
}
}
void split_char(const std::string& yuan, std::vector<std::string>& arr, const std::string& split)
{
std::string::size_type pos1, pos2;
pos2 = yuan.find_first_of(split);
pos1 = 0;
while(std::string::npos != pos2)
{
if (pos1!=pos2)
arr.push_back(yuan.substr(pos1, pos2-pos1));
pos1 = pos2 + 1;
pos2 = yuan.find_first_of(split, pos1); //find_first_of方法查询任意一个字符首次出现的位置,允许了使用多个分割字符
}
if(pos1 != yuan.length()) //分割剩余的最后一个字符串
arr.push_back(yuan.substr(pos1));
}
void split_string(const std::string& yuan, std::vector<std::string>& arr, const std::string& split)
{
std::string::size_type pos1, pos2;
pos2 = yuan.find(split);
pos1 = 0;
while(std::string::npos != pos2)
{
if (pos1!=pos2)
arr.push_back(yuan.substr(pos1, pos2-pos1));
pos1 = pos2 + split.size();
pos2 = yuan.find(split, pos1); //find_first_of方法查询任意一个字符首次出现的位置,允许了使用多个分割字符
}
if(pos1 != yuan.length())
arr.push_back(yuan.substr(pos1));
}
//比较两double浮点数相等
bool FloatEqual(double lhs, double rhs)
{
if (std::abs(lhs - rhs) < Matlab2c::DOUBLEERROR)
return true;
else
return false;
}
//比较两double浮点数不相等
bool FloatNotEqual(double lhs, double rhs)
{
if (std::abs(lhs - rhs) >= Matlab2c::DOUBLEERROR)
return true;
else
return false;
}
//定义矩阵类
class Matrix{
//公共数据
public:
double* data;
int row;
int column;
//构造函数,构建空矩阵
Matrix(){
row=0;column=0;data=nullptr;
}
//由doube转化为1x1矩阵
Matrix(double item){
row=1;column=1;this->data=new double[row*column];
data[0]=item;
}
//构建矩阵,不赋值
Matrix(int row,int column){
this->row=row;
this->column=column;
this->data=new double[row*column];
}
//构建值矩阵,矩阵中每个元素都等于给定值
Matrix(int row,int column,double data){
this->row=row;
this->column=column;
this->data=new double[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=data;
}
//根据数组构建矩阵,复制元素
Matrix(int row,int column,double *data){
this->row=row;
this->column=column;
this->data=new double[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=data[i];
}
//根据数组构建矩阵,复制元素
Matrix(double *data,int row,int column){
this->row=row;
this->column=column;
this->data=new double[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=data[i];
}
//根据向量构建矩阵
template<class T>
Matrix(vector<T> a){
this->row=a.size();
this->column=1;
this->data=new double[row*column];
for (int i=0;i<a.size();i++)
this->data[i] = (double)a[i];
}
//根据向量构建矩阵
template<class T>
Matrix(vector<T> a,int row,int column){
if (a.size()<row*column)
{
throw_logic_error("无法根据向量vector生成矩阵");
return;
//*this=NULL;
}
this->row=row;
this->column=column;
this->data=new double[row*column];
for (int i=0;i<a.size();i++)
this->data[i] = (double)a[i];
}
//根据向量构建矩阵
Matrix(vector<vector<double>> a){
if (a.size()>0 && a[0].size()>0)
{
this->row=a.size();
this->column=a[0].size();
this->data=new double[row*column];
for (int i=0;i<row;i++)
for (int j=0;j<column;j++)
this->data[i*column+j] = a[i][j];
}
}
//通过matlab字符串构造矩阵
Matrix(string str){ //去除两边空格 去除两边[] 按;分割 按,或空格分割 按:分割
Matrix a= str2matrix(str);
this->row=a.row;
this->column=a.column;
this->data=new double[row*column];
for (int i=0;i<a.row*a.column;i++)
this->data[i]=a.data[i];
}
Matrix str2matrix(string str){ //根据一个字符串判断是否 \[([0-9\-\.:]*[ ,]*;*)+\] 基本元素为0-9.:-组成 行内连接使用空格或,行间连接使用;最外层为[]
trim_string(str); //去掉首尾空格
if (str[0]=='[' && str[str.length()-1]==']') //首尾是否为[]
{
str = str.substr(1,str.length()-2);
Matrix p = str2matrix(str);
return p;
}
else if(str.find(';')!= string::npos){ //是否包含; 是否有行
vector<string> strarr1;
split_char(str,strarr1,";"); //分割每行
Matrix p;
for (int i=0;i<strarr1.size();i++)
{
Matrix temp=str2matrix(strarr1[i]); //根据每一行数据字符串,获取每一行的数据
p.append_bottom(temp); //增加一行数据
}
return p;
}else if (str.find_first_of(", ")!= string::npos) //是否包含 ,是否有多列
{
vector<string> strarr2;
split_char(str,strarr2,", "); //分割每列
Matrix p;
for (int i=0;i<strarr2.size();i++)
{
Matrix temp=str2matrix(strarr2[i]); //根据每一列数据字符串,获取每一列的数据
p.append_right(temp); //增加一列数据
}
return p;
}else if (str.find(':')!= string::npos) //判断每个元素是否是增量元素
{
vector<string> strarr3;
split_char(str,strarr3,":");
if (strarr3.size()==3)//如果包含首元素、步长、尾元素
{
double begindata=atof(strarr3[0].c_str());
double jiangedata = atof(strarr3[1].c_str());
int num = (int)((atof(strarr3[2].c_str())-begindata)/jiangedata)+1;
double* date_temp = new double[num];
for (int i=0;i<num;i++)
{
date_temp[i]= begindata+jiangedata*i;
}
return Matrix(1,num,date_temp); //分析增量矩阵,返回成行向量,按道理也可以返回成列矩阵
}
if (strarr3.size()==2)//如果包含首元素,尾元素,默认步长为1
{
double begindata=atof(strarr3[0].c_str());
int num = (int)((atof(strarr3[1].c_str())-begindata))+1;
double* date_temp = new double[num];
for (int i=0;i<num;i++)
{
date_temp[i]= begindata+i;
}
return Matrix(1,num,date_temp); //分析增量矩阵,返回成行向量,按道理也可以返回成列矩阵
}
}else
{
return Matrix(1,1,atof(str.c_str())); //如果是纯数字就转化为double
}
}
//复制构造函数
Matrix(const Matrix& another){
this->row=another.row;
this->column=another.column;
this->data=new double[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=another.data[i];
}
//重载赋值运算符,保留在matlab中赋值等于深复制的效果
Matrix& operator=(const Matrix& another){
if(this==&another)
return *this;
this->row=another.row;
this->column=another.column;
if(data !=nullptr)
delete[] data;
this->data=new double[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=another.data[i];
return *this;
}
//重载()运算符,下标从0开始的习惯,读取时调用
double operator()(int i,int j) const
{
if (i>this->row-1 || i<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
if (j>this->column-1 || j<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
return data[i*column+j];
}
//重载()运算符,下标从0开始的习惯,写入时调用
double& operator()(int i,int j)
{
if (i>this->row-1 || i<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
if (j>this->column-1 || j<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
return data[i*column+j];
}
//重载()运算符,下标从0开始的习惯,读取时调用
double operator()(int i) const
{
if (i>this->row*this->column-1 || i<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
return data[i];
}
//重载()运算符,下标从0开始的习惯
double& operator()(int i){
if (i>this->row*this->column-1 || i<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
return data[i];
}
//重载()运算符,下标从0开始的习惯,读取子矩阵,读取时调用
Matrix operator()(int row1,int row2,int column1,int column2) const //包含所有下标行或列
{
Matrix p(row2-row1+1,column2-column1+1);
for (int i=row1;i<row2+1;i++)
for (int j=column1;j<column2+1;j++)
p(i-row1,j-column1) = this->data[i*column+j];
return p;
}
//根据字符串读取子矩阵,不能对子矩阵赋值
Matrix operator()(string str){
Matrix rowcontrol,columncontrol; //行控制数列,和列控制数列
trim_string(str); //去掉首尾空格
if(str==":")
return Matrix(*this);
int index=str.find_first_of(",");
if (index>-1) //如果包含,表明同时控制行和列
{
string strtemp1=str.substr(0,str.find_first_of(",")); //查询行控制字符串
trim_string(strtemp1); //去掉首尾空格
if (strtemp1==":") //表示整行或整列。不做处理
{
rowcontrol = Matrix(1,row);
for (int i=0;i<row;i++)
rowcontrol(i)=i;
}
else
rowcontrol = Matrix(strtemp1);
string strtemp2=str.substr(str.find_first_of(",")+1); //查询列控制字符串
trim_string(strtemp2); //去掉首尾空格
if (strtemp2==":") //表示整行或整列。不做处理
{
columncontrol = Matrix(1,column);
for (int i=0;i<column;i++)
columncontrol(i)=i;
}
else
columncontrol = Matrix(strtemp2);
Matrix p(rowcontrol.row*rowcontrol.column,columncontrol.row*columncontrol.column);
for (int i=0;i<rowcontrol.row*rowcontrol.column;i++)
{
for (int j=0;j<columncontrol.row*columncontrol.column;j++){
p(i,j) = this->data[(int)(rowcontrol.data[i]*column+columncontrol.data[j])];
}
}
return p;
}
else //不包含,输入按向量使用,取向量局部数据
{
columncontrol = Matrix(str);
Matrix p(1,columncontrol.row*columncontrol.column);
for (int j=0;j<columncontrol.row*columncontrol.column;j++){
p(j) = this->data[(int)(columncontrol.data[j])];
}
return p;
}
}
//转化函数
//operator double() const
//{
// return data[0];
//}
//重载加法运算符
Matrix operator+(double xi){
Matrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p.data[i] = data[i]+xi;
return p;
}
//重载加法运算符
Matrix& operator+=(double xi){
for (int i=0;i<row*column;i++)
this->data[i] += xi;
return *this;
}
//重载加法运算符
Matrix operator+(Matrix& another){
if(!another.data)
return *this;
if (row!=another.row || column!=another.column)
throw_logic_error ("矩阵不匹配");
Matrix p(row,column);
for (int i=0;i<row*column;i++)
p(i)=data[i]+another(i);
return p;
}
//重载加法运算符
Matrix& operator+=(Matrix& another){
if(!another.data)
return *this;
if (row!=another.row || column!=another.column)
throw_logic_error ("矩阵不匹配");
for (int i=0;i<row*column;i++)
this->data[i]+=another.data[i];
return *this;
}
//重载减法运算符
Matrix operator-(double xi){
Matrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p.data[i] = data[i]-xi;
return p;
}
//重载减法运算符
Matrix& operator-=(double xi){
for (int i=0;i<row*column;i++)
this->data[i] -= xi;
return *this;
}
//重载减法运算符
Matrix operator-(Matrix& another){
if(!another.data)
return *this;
if (row!=another.row || column!=another.column)
throw_logic_error ("矩阵不匹配");
Matrix p(row,column);
for (int i=0;i<row*column;i++)
p.data[i]=data[i]-another.data[i];
return p;
}
//重载减法运算符
Matrix& operator-=(Matrix& another){
if(!another.data)
return *this;
if (row!=another.row || column!=another.column)
throw_logic_error ("矩阵不匹配");
for (int i=0;i<row*column;i++)
this->data[i]-=another.data[i];
return *this;
}
//重载乘法运算符
Matrix operator*(double xi){
Matrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p.data[i] = this->data[i]*xi;
return p;
}
//重载乘法运算符
Matrix& operator*=(double xi){
for (int i=0;i<row*column;i++)
this->data[i] *= xi;
return *this;
}
//重载乘法运算符
Matrix operator*(Matrix& another){
if(!another.data || !this->data)
throw_logic_error("矩阵乘法数据为空");
if (column!=another.row)
throw_logic_error ("矩阵不匹配");
Matrix p(row,another.column);
for (int i=0;i<p.row;i++)
for (int j=0;j<p.column;j++)
{
double sum=0;
for (int t=0;t<column;t++) //第一个矩阵的第i行乘以第二个矩阵的第j列
sum+=data[i*column+t]*another(t,j);
p(i,j) = sum;
}
return p;
}
//重载除法运算符
Matrix operator/(double xi){
Matrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p.data[i] = data[i]/xi;
return p;
}
//重载除法运算符
Matrix& operator/=(double xi){
for (int i=0;i<row*column;i++)
this->data[i] /= xi;
return *this;
}
//重载矩阵等号==运算符
bool operator==(Matrix& another){
if (this->row!= another.row)
return false;
if (this->column != another.column)
return false;
for (int i = 0; i < row*column; i ++)
if (FloatNotEqual(this->data[i],another.data[i]))
return false;
return true;
}
//重载矩阵等号!=运算符
bool operator!=(Matrix& another){
if (this->row!= another.row)
return true;
if (this->column != another.column)
return true;
for (int i = 0; i < row*column; i ++)
if (FloatNotEqual(this->data[i],another.data[i]))
return true;
return false;
}
//重载一元操作符:正号
Matrix operator +() const
{
return *this; //不用作任何处理,维持原状
}
//重载一元操作符:负号
Matrix operator -() const
{
Matrix p(row,column);
for (int i=0;i<row*column;i++)
p.data[i]=-this->data[i];
return p;
}
//矩阵转置,不改变源矩阵
Matrix T()
{
Matrix p(column,row);
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(j,i)=(*this)(i,j);
return p;
}
//矩阵左扩展,改变了源矩阵
void append_left(Matrix& a){
if (a.row==0)
return;
if (this->row==0){
this->row = a.row;
this->column=a.column;
this->data = new double[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=a.data[i];
return;
}
int new_column=a.column+column;
if (a.row==this->row)
{
double* datanew=new double[row*new_column];
for (int i=0;i<a.row;i++)
{
for (int j=0;j<a.column;j++)
datanew[i*new_column+j]=a(i,j);
for (int j=0;j<column;j++)
datanew[i*new_column+j+a.column]=this->data[i*column+j];
}
this->column=new_column;
delete[] this->data;
this->data = datanew;
}else
{
throw_logic_error("合并矩阵行数不等");
}
}
//向量首部添加元素
void append_left(double x){
Matrix a(1,1,x);
if(column==1)
this->append_top(a);
else if(row==1)
this->append_left(a);
}
//矩阵右扩展,改变了源矩阵
void append_right(Matrix& a){
if (a.row==0)
return;
if (this->row==0){
this->row = a.row;
this->column=a.column;
this->data = new double[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=a.data[i];
return;
}
int new_column=a.column+column;
if (a.row==this->row)
{
double* datanew=new double[row*new_column];
for (int i=0;i<row;i++)
{
for (int j=0;j<column;j++)
datanew[i*new_column+j]=this->data[i*column+j];
for (int j=0;j<a.column;j++)
datanew[i*new_column+j+column]=a(i,j);
}
this->column=new_column;
delete[] this->data;
this->data = datanew;
}else
{
throw_logic_error("合并矩阵行数不等");
}
}
//向量尾部添加元素
void append_right(double x){
Matrix a(1,1,x);
if(column==1)
this->append_bottom(a);
else if(row==1)
this->append_right(a);
}
//矩阵上扩展,改变了源矩阵
void append_top(Matrix& a){
if (a.row==0)
return;
if (this->row==0){
this->row = a.row;
this->column=a.column;
this->data = new double[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=a.data[i];
return;
}
int new_row=a.row+row;
if (a.column==this->column)
{
double* datanew=new double[new_row*column];
for (int i=0;i<a.row;i++)
for (int j=0;j<a.column;j++)
datanew[i*column+j]=a(i,j);
for (int i=0;i<row;i++)
for (int j=0;j<column;j++)
datanew[(i+a.row)*column+j]=this->data[i*column+j];
this->row=new_row;
delete[] this->data;
this->data = datanew;
}else
{
throw_logic_error("合并矩阵列数不等");
}
}
//向量首部添加元素
void append_top(double x){
Matrix a(1,1,x);
if(column==1)
this->append_top(a);
else if(row==1)
this->append_left(a);
}
//矩阵下扩展,改变了源矩阵
void append_bottom(Matrix& a){
if (a.row==0)
return;
if (this->row==0){
this->row = a.row;
this->column=a.column;
this->data = new double[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=a.data[i];
return;
}
int new_row=a.row+row;
if (a.column==this->column)
{
double* datanew=new double[new_row*column];
for (int i=0;i<row;i++)
for (int j=0;j<column;j++)
datanew[i*column+j]=this->data[i*column+j];
for (int i=0;i<a.row;i++)
for (int j=0;j<a.column;j++)
datanew[(i+row)*column+j]=a(i,j);
this->row=new_row;
delete[] this->data;
this->data = datanew;
}else
{
throw_logic_error("合并矩阵列数不等");
}
}
//向量尾部添加元素
void append_bottom(double x){
Matrix a(1,1,x);
if(column==1)
this->append_bottom(a);
else if(row==1)
this->append_right(a);
}
//删除指定行,改变了源矩阵
void remove_row(int rowindex) //删除指定行
{
if (rowindex>=row || rowindex<0)
{
throw_logic_error("删除行范围越界");
return;
}
double* datetemp = data;
int newrow=row-1;
data = new double[newrow*column];
for (int i=0;i<rowindex;i++)
for (int j=0;j<this->column;j++)
this->data[i*column+j] = datetemp[i*column+j];
for (int i=rowindex;i<newrow;i++)
for (int j=0;j<this->column;j++)
this->data[i*column+j] = datetemp[(i+1)*column+j];
row=newrow;
delete[] datetemp;
}
//删除指定行,改变了源矩阵
void remove_row(int row1,int row2) //删除多行,包含两个下标
{
if (row1>=row || row2>=row || row1<0||row2<0)
{
throw_logic_error("删除行范围越界");
return;
}
double* datetemp = data;
int newrow=row-row2+row1-1;
data = new double[newrow*column];
for (int i=0;i<row1;i++)
for (int j=0;j<this->column;j++)
this->data[i*column+j] = datetemp[i*column+j];
for (int i=row1;i<newrow;i++)
for (int j=0;j<this->column;j++)
this->data[i*column+j] = datetemp[(i+row2-row1+1)*column+j];
row=newrow;
delete[] datetemp;
}
//删除指定列,改变了源矩阵
void remove_column(int columnindex) //删除指定列
{
if (columnindex>=column || columnindex<0)
{
throw_logic_error("删除列范围越界");
return;
}
double* datetemp = data;
int newcolumn=column-1;
data = new double[row*newcolumn];
for (int i=0;i<row;i++)
{
for (int j=0;j<columnindex;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j];
for (int j=columnindex;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j+1];
}
column=newcolumn;
delete[] datetemp;
}
//删除指定列,改变了源矩阵
void remove_column(int column1,int column2) //删除多列,包含两个下标
{
if (column1>=column || column2>=column || column1<0||column2<0)
{
throw_logic_error("删除列范围越界");
return;
}
double* datetemp = data;
int newcolumn=column-column2+column1-1;
data = new double[row*newcolumn];
for (int i=0;i<row;i++)
{
for (int j=0;j<column1;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j];
for (int j=column1;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j+column2-column1+1];
}
column=newcolumn;
delete[] datetemp;
}
//删除指定的行和列
void remove_row_column(int rowindex,int columnindex)
{
if (columnindex>=column || columnindex<0 || rowindex<0 || rowindex>=row)
{
throw_logic_error("删除行列范围越界");
return;
}
double* datetemp = data;
int newcolumn=column-1;
int newrow=row-1;
data = new double[newrow*newcolumn];
for (int i=0;i<rowindex;i++)
{
for (int j=0;j<columnindex;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j];
for (int j=columnindex;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j+1];
}
for (int i=rowindex;i<newrow;i++)
{
for (int j=0;j<columnindex;j++)
this->data[i*newcolumn+j] = datetemp[(i+1)*column+j];
for (int j=columnindex;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[(i+1)*column+j+1];
}
column=newcolumn;
row =newrow;
delete[] datetemp;
}
//删除多行和多列
void remove_row_column(int row1,int row2,int column1,int column2)
{
if (column1>=column || column1<0 || row1<0 || row1>=row || column2>=column || column2<0 || row2<0 || row2>=row)
{
throw_logic_error("删除行列范围越界");
return;
}
double* datetemp = data;
int newcolumn=column-column2+column1-1;
int newrow=row-row2+row1-1;
data = new double[newrow*newcolumn];
for (int i=0;i<row1;i++)
{
for (int j=0;j<column1;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j];
for (int j=column1;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j+column2-column1+1];
}
for (int i=row1;i<newrow;i++)
{
for (int j=0;j<column1;j++)
this->data[i*newcolumn+j] = datetemp[(i+row2-row1+1)*column+j];
for (int j=column1;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[(i+row2-row1+1)*column+j+column2-column1+1];
}
column=newcolumn;
row =newrow;
delete[] datetemp;
}
//替换局部矩阵,改变了源矩阵,
void replace(Matrix &a,int row1,int column1)
{
int endrow=std::min(this->row,a.row+row1);
int endcolumn=std::min(this->column,a.column+column1);
for (int i=row1;i<endrow;i++)
for (int j=column1;j<endcolumn;j++)
this->data[i*column+j] = a((i-row1),j-column1);
}
//矩阵元素遍历操作,需要传入遍历函数,函数传入元素值,所属行,所属列,源矩阵,返回替换值。不改变源矩阵
Matrix map(function<double(double item,int row,int column,Matrix& src)> fun){
Matrix p(this->row,this->column);
for (int i=0;i<this->row;i++)
for (int j=0;j<this->column;j++)
p(i,j)=fun(this->data[i*column+j],i,j,*this);
return p;
}
//定义一个按行遍历函数(固定格式),每一行得到一个值,遍历结果为一个列向量。
//输入参数:前一个元素,当前元素,当前元素所属行,当前元素所属列,源矩阵
//迭代从第二项开始,prev初始值为第一项,cur初始值为第二项。计算值自动传给下一函数的prev,返回最后一次迭代产生的值
Matrix map_row(function<double(double prev,double cur,int row,int column,Matrix& src)> fun){
Matrix p(this->row,1);
for (int i=0;i<this->row;i++)
{
double result = this->data[i*column];
for (int j=1;j<this->column;j++)
result = fun(result,this->data[i*column+j],i,j,*this);
p(i) = result;
}
return p;
}
//定义一个按列遍历函数(固定格式),每一列得到一个值
//输入参数:前一个元素,当前元素,当前元素所属行,当前元素所属列,源矩阵
//迭代从第二项开始,prev初始值为第一项,cur初始值为第二项。计算值自动传给下一函数的prev,返回最后一次迭代产生的值
Matrix map_column(function<double(double prev,double cur,int row,int column,Matrix& src)> fun){
Matrix p(1,this->column);
for (int j=0;j<this->column;j++)
{
double result = this->data[j];
for (int i=1;i<this->row;i++)
result = fun(result,this->data[i*column+j],i,j,*this);
p(j) = result;
}
return p;
}
//点运算
Matrix dot(Matrix& a,string operation)
{
if (row!=a.row || column!=a.column)
{
throw_logic_error("点运算矩阵不匹配");
return NULL;
}
Matrix p(row,column);
if (operation=="+")
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(i,j)=(*this)(i,j)+a(i,j);
else if (operation=="-")
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(i,j)=(*this)(i,j)-a(i,j);
else if (operation=="*")
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(i,j)=(*this)(i,j)*a(i,j);
else if (operation=="/")
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(i,j)=(*this)(i,j)/a(i,j);
else if (operation=="\\")
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(i,j)=a(i,j)/(*this)(i,j);
return p;
}
//查询是否是向量
bool isVector()
{
if (row==1||column==1)
return true;
return false;
}
//字符串形式表示
string toString(){
stringstream back;
for (int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
back<< fixed << setprecision(4)<<data[i*column+j];
back<<" ";
}
if(i!=row-1)
back<<"\n";
}
return back.str();
}
//按行优先转化为series
double* toSeries(){
double* back = new double[row*column];
for (int i=0;i<column*row;i++)
back[i] = data[i];
return back;
}
//转化为二位数组
double** toArray(){
double** back = new double*[column];
int n=0;
for (int i=0;i<row;i++){
back[i]=new double[column];
for(int j=0;j<column;j++)
back[i][j] = data[i*column+j];
}
return back;
}
//按行优先转化为一维vector
vector<double> toVector(){
int t=0;
vector<double> back(row*column);
for (int i=0;i<row;i++)
for (int j=0;j<column;j++)
back[t++]=data[i*column+j];
return back;
}
//按行优先转化为二维vector
vector<vector<double>> toVector2(){
int rowindex=0;
int columnindex=0;
vector<vector<double>> back(row);
for (int i=0;i<row;i++)
{
columnindex=0;
vector<double> newrow(column);
for (int j=0;j<column;j++)
newrow[columnindex++]=data[i*column+j];
back[rowindex++]=newrow;
}
return back;
}
//析构函数,不能使用析构函数的原因在于很多函数局部类型返回需要保留data数据,不能使局部类型析构造成接收对象不可用。但是如果重写了赋值运算符,可以加上,因为在函数内,先赋值,在析构局部变量
~Matrix(){
row=0;column=0;
if(data !=nullptr){
delete[] data;
data=nullptr;
}
}
};
//定义复数矩阵类
_DLL_API class CMatrix{
//公共数据
public:
Complex* data;
int row;
int column;
//构造函数,构建空矩阵
CMatrix(){
row=0;column=0;data=nullptr;
}
//由Complex转化为1x1矩阵
CMatrix(Complex item){
row=1;column=1;this->data=new Complex[row*column];
data[0]=item;
}
//由实矩阵转化为复数矩阵
CMatrix(Matrix& a){
row=a.row;
column=a.column;
this->data=new Complex[row*column];
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
data[i*column+j]=Complex(a(i,j),0);
}
//由实部和虚部组成复数矩阵
CMatrix(Matrix& real,Matrix& imag){
row=real.row;
column=real.column;
this->data=new Complex[row*column];
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
data[i*column+j]=Complex(real(i,j),imag(i,j));
}
//构建矩阵,不赋值
CMatrix(int row,int column){
this->row=row;
this->column=column;
this->data=new Complex[row*column];
}
//构建值矩阵,矩阵中每个元素都等于给定值
CMatrix(int row,int column,Complex data){
this->row=row;
this->column=column;
this->data=new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=data;
}
//根据数组构建矩阵,复制元素
CMatrix(int row,int column,double *x,double *y){
this->row=row;
this->column=column;
this->data=new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=Complex(x[i],y[i]);
}
//根据数组构建矩阵,复制元素
CMatrix(double *x,double *y,int row,int column){
this->row=row;
this->column=column;
this->data=new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=Complex(x[i],y[i]);
}
//根据数组构建矩阵,复制元素
CMatrix(int row,int column,Complex *data){
this->row=row;
this->column=column;
this->data=new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=data[i];
}
//根据数组构建矩阵,复制元素
CMatrix(Complex *data,int row,int column){
this->row=row;
this->column=column;
this->data=new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=data[i];
}
//根据向量构建矩阵
CMatrix(vector<Complex> a){
this->row=a.size();
this->column=1;
this->data=new Complex[row*column];
for (int i=0;i<a.size();i++)
this->data[i] = a[i];
}
//根据向量构建矩阵
CMatrix(vector<Complex> a,int row,int column){
if (a.size()<row*column)
{
throw_logic_error("无法根据向量vector生成矩阵");
return;
//*this=NULL;
}
this->row=row;
this->column=column;
this->data=new Complex[row*column];
for (int i=0;i<a.size();i++)
this->data[i] = a[i];
}
//根据向量构建矩阵
CMatrix(vector<vector<Complex>> a){
if (a.size()>0 && a[0].size()>0)
{
this->row=a.size();
this->column=a[0].size();
this->data=new Complex[row*column];
for (int i=0;i<row;i++)
for (int j=0;j<column;j++)
this->data[i*column+j] = a[i][j];
}
}
//复制构造函数
CMatrix(const CMatrix& another){
this->row=another.row;
this->column=another.column;
this->data=new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=another.data[i];
}
//重载赋值运算符,保留在matlab中赋值等于深复制的效果
CMatrix& operator=(const CMatrix& another){
if(this==&another)
return *this;
this->row=another.row;
this->column=another.column;
if(data !=nullptr) //必须新释放之前的内存,不然内存泄漏了
delete[] data;
this->data=new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=another.data[i];
return *this;
}
//重载()运算符,下标从0开始的习惯,读取时调用
Complex operator()(int i,int j) const
{
if (i>this->row-1 || i<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
if (j>this->column-1 || j<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
return data[i*column+j];
}
//重载()运算符,下标从0开始的习惯,写入时调用
Complex& operator()(int i,int j)
{
if (i>this->row-1 || i<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
if (j>this->column-1 || j<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
return data[i*column+j];
}
//重载()运算符,下标从0开始的习惯,读取时调用
Complex operator()(int i) const
{
if (i>this->row*this->column-1 || i<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
return data[i];
}
//重载()运算符,下标从0开始的习惯
Complex& operator()(int i){
if (i>this->row*this->column-1 || i<0)
throw_logic_error("读取矩阵元素,超出矩阵范围");
return data[i];
}
//重载()运算符,下标从0开始的习惯,读取子矩阵,读取时调用
CMatrix operator()(int row1,int row2,int column1,int column2) const //包含所有下标行或列
{
CMatrix p(row2-row1+1,column2-column1+1);
for (int i=row1;i<row2+1;i++)
for (int j=column1;j<column2+1;j++)
p(i-row1,j-column1) = this->data[i*column+j];
return p;
}
//重载加法运算符
CMatrix operator+(Complex xi){
CMatrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p.data[i] = data[i]+xi;
return p;
}
//重载加法运算符
CMatrix& operator+=(Complex xi){
for (int i=0;i<row*column;i++)
this->data[i] += xi;
return *this;
}
//重载加法运算符
CMatrix operator+(CMatrix& another){
if(!another.data)
return *this;
if (row!=another.row || column!=another.column)
throw_logic_error ("矩阵不匹配");
CMatrix p(row,column);
for (int i=0;i<row*column;i++)
p(i)=data[i]+another(i);
return p;
}
//重载加法运算符
CMatrix& operator+=(CMatrix& another){
if(!another.data)
return *this;
if (row!=another.row || column!=another.column)
throw_logic_error ("矩阵不匹配");
for (int i=0;i<row*column;i++)
this->data[i]+=another.data[i];
return *this;
}
//重载减法运算符
CMatrix operator-(Complex xi){
CMatrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p.data[i] = data[i]-xi;
return p;
}
//重载减法运算符
CMatrix& operator-=(Complex xi){
for (int i=0;i<row*column;i++)
this->data[i] -= xi;
return *this;
}
//重载减法运算符
CMatrix operator-(CMatrix& another){
if(!another.data)
return *this;
if (row!=another.row || column!=another.column)
throw_logic_error ("矩阵不匹配");
CMatrix p(row,column);
for (int i=0;i<row*column;i++)
p.data[i]=data[i]-another.data[i];
return p;
}
//重载减法运算符
CMatrix& operator-=(CMatrix& another){
if(!another.data)
return *this;
if (row!=another.row || column!=another.column)
throw_logic_error ("矩阵不匹配");
for (int i=0;i<row*column;i++)
this->data[i]-=another.data[i];
return *this;
}
//重载乘法运算符
CMatrix operator*(Complex xi){
CMatrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p.data[i] = this->data[i]*xi;
return p;
}
//重载乘法运算符
CMatrix& operator*=(Complex xi){
for (int i=0;i<row*column;i++)
this->data[i] *= xi;
return *this;
}
//重载乘法运算符
CMatrix operator*(CMatrix& another){
if(!another.data || !this->data)
throw_logic_error("矩阵乘法数据为空");
if (column!=another.row)
throw_logic_error ("矩阵不匹配");
CMatrix p(row,another.column);
for (int i=0;i<p.row;i++)
for (int j=0;j<p.column;j++)
{
Complex sum=0;
for (int t=0;t<column;t++) //第一个矩阵的第i行乘以第二个矩阵的第j列
sum+=data[i*column+t]*another(t,j);
p(i,j) = sum;
}
return p;
}
//重载除法运算符
CMatrix operator/(Complex xi){
CMatrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p.data[i] = data[i]/xi;
return p;
}
//重载除法运算符
CMatrix& operator/=(Complex xi){
for (int i=0;i<row*column;i++)
this->data[i] /= xi;
return *this;
}
//重载矩阵等号==运算符
bool operator==(CMatrix& another){
if (this->row!= another.row)
return false;
if (this->column != another.column)
return false;
for (int i = 0; i < row*column; i ++)
if (FloatNotEqual(this->data[i].real(),another.data[i].real()) || FloatNotEqual(this->data[i].imag(),another.data[i].imag()))
return false;
return true;
}
//重载矩阵等号!=运算符
bool operator!=(CMatrix& another){
if (this->row!= another.row)
return true;
if (this->column != another.column)
return true;
for (int i = 0; i < row*column; i ++)
if (FloatNotEqual(this->data[i].real(),another.data[i].real()) || FloatNotEqual(this->data[i].imag(),another.data[i].imag()))
return true;
return false;
}
//重载一元操作符:正号
CMatrix operator +() const
{
return *this; //不用作任何处理,维持原状
}
//重载一元操作符:负号
CMatrix operator -() const
{
CMatrix p(row,column);
for (int i=0;i<row*column;i++)
p.data[i]=-this->data[i];
return p;
}
//矩阵转置,不改变源矩阵
CMatrix T()
{
CMatrix p(column,row);
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(j,i)=(*this)(i,j);
return p;
}
//矩阵左扩展,改变了源矩阵
void append_left(CMatrix& a){
if (a.row==0)
return;
if (this->row==0){
this->row = a.row;
this->column=a.column;
this->data = new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=a.data[i];
return;
}
int new_column=a.column+column;
if (a.row==this->row)
{
Complex* datanew=new Complex[row*new_column];
for (int i=0;i<a.row;i++)
{
for (int j=0;j<a.column;j++)
datanew[i*new_column+j]=a(i,j);
for (int j=0;j<column;j++)
datanew[i*new_column+j+a.column]=this->data[i*column+j];
}
this->column=new_column;
delete[] this->data;
this->data = datanew;
}else
{
throw_logic_error("合并矩阵行数不等");
}
}
//向量首部添加元素
void append_left(Complex x){
CMatrix a(1,1,x);
if(column==1)
this->append_top(a);
else if(row==1)
this->append_left(a);
}
//矩阵右扩展,改变了源矩阵
void append_right(CMatrix& a){
if (a.row==0)
return;
if (this->row==0){
this->row = a.row;
this->column=a.column;
this->data = new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=a.data[i];
return;
}
int new_column=a.column+column;
if (a.row==this->row)
{
Complex* datanew=new Complex[row*new_column];
for (int i=0;i<row;i++)
{
for (int j=0;j<column;j++)
datanew[i*new_column+j]=this->data[i*column+j];
for (int j=0;j<a.column;j++)
datanew[i*new_column+j+column]=a(i,j);
}
this->column=new_column;
delete[] this->data;
this->data = datanew;
}else
{
throw_logic_error("合并矩阵行数不等");
}
}
//向量尾部添加元素
void append_right(Complex x){
CMatrix a(1,1,x);
if(column==1)
this->append_bottom(a);
else if(row==1)
this->append_right(a);
}
//矩阵上扩展,改变了源矩阵
void append_top(CMatrix& a){
if (a.row==0)
return;
if (this->row==0){
this->row = a.row;
this->column=a.column;
this->data = new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=a.data[i];
return;
}
int new_row=a.row+row;
if (a.column==this->column)
{
Complex* datanew=new Complex[new_row*column];
for (int i=0;i<a.row;i++)
for (int j=0;j<a.column;j++)
datanew[i*column+j]=a(i,j);
for (int i=0;i<row;i++)
for (int j=0;j<column;j++)
datanew[(i+a.row)*column+j]=this->data[i*column+j];
this->row=new_row;
delete[] this->data;
this->data = datanew;
}else
{
throw_logic_error("合并矩阵列数不等");
}
}
//向量首部添加元素
void append_top(Complex x){
CMatrix a(1,1,x);
if(column==1)
this->append_top(a);
else if(row==1)
this->append_left(a);
}
//矩阵下扩展,改变了源矩阵
void append_bottom(CMatrix& a){
if (a.row==0)
return;
if (this->row==0){
this->row = a.row;
this->column=a.column;
this->data = new Complex[row*column];
for (int i=0;i<row*column;i++)
this->data[i]=a.data[i];
return;
}
int new_row=a.row+row;
if (a.column==this->column)
{
Complex* datanew=new Complex[new_row*column];
for (int i=0;i<row;i++)
for (int j=0;j<column;j++)
datanew[i*column+j]=this->data[i*column+j];
for (int i=0;i<a.row;i++)
for (int j=0;j<a.column;j++)
datanew[(i+row)*column+j]=a(i,j);
this->row=new_row;
delete[] this->data;
this->data = datanew;
}else
{
throw_logic_error("合并矩阵列数不等");
}
}
//向量尾部添加元素
void append_bottom(Complex x){
CMatrix a(1,1,x);
if(column==1)
this->append_bottom(a);
else if(row==1)
this->append_right(a);
}
//删除指定行,改变了源矩阵
void remove_row(int rowindex) //删除指定行
{
if (rowindex>=row || rowindex<0)
{
throw_logic_error("删除行范围越界");
return;
}
Complex* datetemp = data;
int newrow=row-1;
data = new Complex[newrow*column];
for (int i=0;i<rowindex;i++)
for (int j=0;j<this->column;j++)
this->data[i*column+j] = datetemp[i*column+j];
for (int i=rowindex;i<newrow;i++)
for (int j=0;j<this->column;j++)
this->data[i*column+j] = datetemp[(i+1)*column+j];
row=newrow;
delete[] datetemp;
}
//删除指定行,改变了源矩阵
void remove_row(int row1,int row2) //删除多行,包含两个下标
{
if (row1>=row || row2>=row || row1<0||row2<0)
{
throw_logic_error("删除行范围越界");
return;
}
Complex* datetemp = data;
int newrow=row-row2+row1-1;
data = new Complex[newrow*column];
for (int i=0;i<row1;i++)
for (int j=0;j<this->column;j++)
this->data[i*column+j] = datetemp[i*column+j];
for (int i=row1;i<newrow;i++)
for (int j=0;j<this->column;j++)
this->data[i*column+j] = datetemp[(i+row2-row1+1)*column+j];
row=newrow;
delete[] datetemp;
}
//删除指定列,改变了源矩阵
void remove_column(int columnindex) //删除指定列
{
if (columnindex>=column || columnindex<0)
{
throw_logic_error("删除列范围越界");
return;
}
Complex* datetemp = data;
int newcolumn=column-1;
data = new Complex[row*newcolumn];
for (int i=0;i<row;i++)
{
for (int j=0;j<columnindex;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j];
for (int j=columnindex;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j+1];
}
column=newcolumn;
delete[] datetemp;
}
//删除指定列,改变了源矩阵
void remove_column(int column1,int column2) //删除多列,包含两个下标
{
if (column1>=column || column2>=column || column1<0||column2<0)
{
throw_logic_error("删除列范围越界");
return;
}
Complex* datetemp = data;
int newcolumn=column-column2+column1-1;
data = new Complex[row*newcolumn];
for (int i=0;i<row;i++)
{
for (int j=0;j<column1;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j];
for (int j=column1;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j+column2-column1+1];
}
column=newcolumn;
delete[] datetemp;
}
//删除指定的行和列
void remove_row_column(int rowindex,int columnindex)
{
if (columnindex>=column || columnindex<0 || rowindex<0 || rowindex>=row)
{
throw_logic_error("删除行列范围越界");
return;
}
Complex* datetemp = data;
int newcolumn=column-1;
int newrow=row-1;
data = new Complex[newrow*newcolumn];
for (int i=0;i<rowindex;i++)
{
for (int j=0;j<columnindex;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j];
for (int j=columnindex;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j+1];
}
for (int i=rowindex;i<newrow;i++)
{
for (int j=0;j<columnindex;j++)
this->data[i*newcolumn+j] = datetemp[(i+1)*column+j];
for (int j=columnindex;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[(i+1)*column+j+1];
}
column=newcolumn;
row =newrow;
delete[] datetemp;
}
//删除多行和多列
void remove_row_column(int row1,int row2,int column1,int column2)
{
if (column1>=column || column1<0 || row1<0 || row1>=row || column2>=column || column2<0 || row2<0 || row2>=row)
{
throw_logic_error("删除行列范围越界");
return;
}
Complex* datetemp = data;
int newcolumn=column-column2+column1-1;
int newrow=row-row2+row1-1;
data = new Complex[newrow*newcolumn];
for (int i=0;i<row1;i++)
{
for (int j=0;j<column1;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j];
for (int j=column1;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[i*column+j+column2-column1+1];
}
for (int i=row1;i<newrow;i++)
{
for (int j=0;j<column1;j++)
this->data[i*newcolumn+j] = datetemp[(i+row2-row1+1)*column+j];
for (int j=column1;j<newcolumn;j++)
this->data[i*newcolumn+j] = datetemp[(i+row2-row1+1)*column+j+column2-column1+1];
}
column=newcolumn;
row =newrow;
delete[] datetemp;
}
//替换局部矩阵,改变了源矩阵,
void replace(CMatrix &a,int row1,int column1)
{
int endrow=std::min(this->row,a.row+row1);
int endcolumn=std::min(this->column,a.column+column1);
for (int i=row1;i<endrow;i++)
for (int j=column1;j<endcolumn;j++)
this->data[i*column+j] = a((i-row1),j-column1);
}
//矩阵元素遍历操作,需要传入遍历函数,函数传入元素值,所属行,所属列,源矩阵,返回替换值。不改变源矩阵
CMatrix map(function<Complex(Complex item,int row,int column,CMatrix& src)> fun){
CMatrix p(this->row,this->column);
for (int i=0;i<this->row;i++)
for (int j=0;j<this->column;j++)
p(i,j)=fun(this->data[i*column+j],i,j,*this);
return p;
}
//定义一个按行遍历函数(固定格式),每一行得到一个值,遍历结果为一个列向量。
//输入参数:前一个元素,当前元素,当前元素所属行,当前元素所属列,源矩阵
//迭代从第二项开始,prev初始值为第一项,cur初始值为第二项。计算值自动传给下一函数的prev,返回最后一次迭代产生的值
CMatrix map_row(function<Complex(Complex prev,Complex cur,int row,int column,CMatrix& src)> fun){
CMatrix p(this->row,1);
for (int i=0;i<this->row;i++)
{
Complex result = this->data[i*column];
for (int j=1;j<this->column;j++)
result = fun(result,this->data[i*column+j],i,j,*this);
p(i) = result;
}
return p;
}
//定义一个按列遍历函数(固定格式),每一列得到一个值
//输入参数:前一个元素,当前元素,当前元素所属行,当前元素所属列,源矩阵
//迭代从第二项开始,prev初始值为第一项,cur初始值为第二项。计算值自动传给下一函数的prev,返回最后一次迭代产生的值
CMatrix map_column(function<Complex(Complex prev,Complex cur,int row,int column,CMatrix& src)> fun){
CMatrix p(1,this->column);
for (int j=0;j<this->column;j++)
{
Complex result = this->data[j];
for (int i=1;i<this->row;i++)
result = fun(result,this->data[i*column+j],i,j,*this);
p(j) = result;
}
return p;
}
//点运算
CMatrix dot(CMatrix& a,string operation)
{
if (row!=a.row || column!=a.column)
{
throw_logic_error("点运算矩阵不匹配");
return NULL;
}
CMatrix p(row,column);
if (operation=="+")
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(i,j)=(*this)(i,j)+a(i,j);
else if (operation=="-")
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(i,j)=(*this)(i,j)-a(i,j);
else if (operation=="*")
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(i,j)=(*this)(i,j)*a(i,j);
else if (operation=="/")
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(i,j)=(*this)(i,j)/a(i,j);
else if (operation=="\\")
for (int i=0;i<row;i++)
for(int j=0;j<column;j++)
p(i,j)=a(i,j)/(*this)(i,j);
return p;
}
//查询是否是向量
bool isVector()
{
if (row==1||column==1)
return true;
return false;
}
//实部矩阵
Matrix real(){
Matrix p(row,column);
for (int i=0;i<p.row*p.column;i++){
p(i) = data[i].real();
}
return p;
}
//虚部矩阵
Matrix imag(){
Matrix p(row,column);
for (int i=0;i<p.row*p.column;i++){
p(i) = data[i].imag();
}
return p;
}
//模值矩阵
Matrix abs(){
Matrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p(i)=(double)std::sqrt((this->data[i].imag())*(this->data[i].imag())+(this->data[i].real())*(this->data[i].real()));
return p;
}
//角度矩阵
Matrix angle(){
Matrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p(i)=(double)std::atan((this->data[i].imag())/(this->data[i].real()));
return p;
}
//共轭矩阵
CMatrix conj(){
CMatrix p(row,column);
for (int i=0;i<p.row*p.column;i++)
p(i)=Complex(this->data[i].real(),-(this->data[i].imag()));
return p;
}
//字符串形式表示
string toString(){
stringstream back;
for (int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
if(data[i*column+j].imag()<0)
back<< fixed << setprecision(4)<<data[i*column+j].real()<<data[i*column+j].imag()<<"i";
else
{
back<< fixed << setprecision(4)<<data[i*column+j].real()<<"+"<<data[i*column+j].imag()<<"i";
}
back<<" ";
}
if(i!=row-1)
back<<"\n";
}
return back.str();
}
//按行优先转化为series
Complex* toSeries(){
Complex* back = new Complex[row*column];
for (int i=0;i<column*row;i++)
back[i] = data[i];
return back;
}
//转化为二位数组
Complex** toArray(){
Complex** back = new Complex*[column];
int n=0;
for (int i=0;i<row;i++){
back[i]=new Complex[column];
for(int j=0;j<column;j++)
back[i][j] = data[i*column+j];
}
return back;
}
//按行优先转化为一维vector
vector<Complex> toVector(){
int t=0;
vector<Complex> back(row*column);
for (int i=0;i<row;i++)
for (int j=0;j<column;j++)
back[t++]=data[i*column+j];
return back;
}
//按行优先转化为二维vector
vector<vector<Complex>> toVector2(){
int rowindex=0;
int columnindex=0;
vector<vector<Complex>> back(row);
for (int i=0;i<row;i++)
{
columnindex=0;
vector<Complex> newrow(column);
for (int j=0;j<column;j++)
newrow[columnindex++]=data[i*column+j];
back[rowindex++]=newrow;
}
return back;
}
//析构函数,不能使用析构函数的原因在于很多函数局部类型返回需要保留data数据,不能使局部类型析构造成接收对象不可用。但是如果重写了赋值运算符,可以加上,因为在函数内,先赋值,在析构局部变量
~CMatrix(){
row=0;column=0;
if(data !=nullptr){
delete[] data;
data=nullptr;
}
}
};
}