由于C++中没有numpy, 所以自己编写了一个class以实现矩阵
项目依赖:
#include <iostream>
#include <vector>
using namespace std;
代码:
vector<vector<float>>merge(vector<vector<float>>list1, vector<vector<float>>list2)
{
int m, n;
m = list1.size();
n = list2.size();
for (size_t i = 0; i < n; i++)
list1.push_back(list2[i]);
return list1;
}
//摘自 https ://blog.csdn.net/weixin_44679926/article/details/102821763
vector<float>merge(vector<float>list1, vector<float>list2)
{
int m, n;
m = list1.size();
n = list2.size();
for (size_t i = 0; i < n; i++)
list1.push_back(list2[i]);
return list1;
}
vector<vector<float>> Create_Array(vector<int>shape)
{
vector<vector<float>> result;
for (int i = 0; i < shape[0]; i++)
{
vector<float> vec(shape[1],0);
result.push_back(vec);
}
return result;
}
float sum(vector<float>vec)
{
float result = 0;
for (int i = 0; i < vec.size(); i++)
{
result += vec[i];
}
return result;
}
class MyMatrix
{
public:
vector<int> shape;
int ndim;
vector<vector<float>>arr;
MyMatrix(vector<int> shape)
{
this->ndim = shape.size();
this->shape = shape;
this->arr = Create_Array(this->shape);
}
MyMatrix(vector<vector<float>> mat)
{
this->ndim = 2;
vector<int> shape(2,0);
// std::cout << mat.size()<<std::endl;
shape[0] = mat.size();
shape[1] = mat[0].size();
this->shape = shape;
this->arr = Create_Array(this->shape);
for (int i = 0; i < this->shape[0]; i++)
{
for (int j = 0; j < this->shape[1]; j++)
{
this->arr[i][j] = mat[i][j];
}
}
}
MyMatrix copy(vector<vector<float>> mat)
{
MyMatrix a = MyMatrix(mat);
MyMatrix new_matrix = MyMatrix(this->shape);
for (int i = 0; i < a.shape[0]; i++)
{
for (int j = 0; j < a.shape[1]; j++)
{
new_matrix.arr[i][j] = a.arr[i][j];
}
}
return new_matrix;
}
MyMatrix subtract(MyMatrix mat1) // 矩阵相减
{
vector<vector<float>> arr = this->arr;
for (int i = 0; i < this->shape[0]; i++)
{
for (int j = 0; j < this->shape[1]; j++)
{
arr[i][j] -= mat1.arr[i][j];
}
}
return MyMatrix(arr);
}
MyMatrix add(MyMatrix mat1) // 矩阵相加
{
vector<vector<float>> arr = this->arr;
for (int i = 0; i < this->shape[0]; i++)
{
for (int j = 0; j < this->shape[1]; j++)
{
arr[i][j] += mat1.arr[i][j];
}
}
return MyMatrix(arr);
}
MyMatrix scalar_multiply(float a) // 矩阵数乘
{
vector<vector<float>> arr = this->arr;
for (int i = 0; i < this->shape[0]; i++)
{
for (int j = 0; j < this->shape[1]; j++)
{
arr[i][j] = a*arr[i][j];
}
}
return MyMatrix(arr);
}
void copy(MyMatrix a) // 整体赋值
{
for (int i = 0; i < a.shape[0]; i++)
{
for (int j = 0; j < a.shape[1]; j++)
{
this->arr[i][j] = a.arr[i][j];
}
}
}
MyMatrix transpose() // 求转置
{
vector<int> shape(2,0);
shape[0] = this->shape[1];
shape[1] = this->shape[0];
MyMatrix result = MyMatrix(shape);
for (int i = 0; i < result.shape[0]; i++)
{
for (int j = 0; j < result.shape[1]; j++)
{
result.arr[i][j] =this->arr[j][i];
}
}
return result;
}
vector < float > add_reduce(int axis) // 相当于numpy中的__add__.reduce(), 实现逐行/列相加
{
if (axis == 1)
{
vector < float > result;
for (int i = 0; i < this->shape[0]; i++)
{
result.push_back(sum(this->arr[i]));
}
return result;
}
else
{
vector < float > result;
MyMatrix new_matrix =this->transpose();
for (int i = 0; i < this->shape[0]; i++)
{
result.push_back(sum(new_matrix.arr[i]));
}
return result;
}
}
MyMatrix take(vector<int> indices,int axis) // 相当于numpy中的take, 获取指定的行/列
{
vector<vector<float>> result;
if (axis == 0)
{
for (int i = 0; i < indices.size(); i++)
{
result.push_back(this->arr[indices[i]]);
}
return MyMatrix(result);
}
else
{
MyMatrix new_matrix = this->transpose();
for (int i = 0; i < indices.size(); i++)
{
result.push_back(new_matrix.arr[indices[i]]);
}
MyMatrix matrix = MyMatrix(result);
return matrix.transpose();
}
}
MyMatrix mat_merge(MyMatrix mat1, int axis) // 相当于numpy中的concatenate, 实现矩阵拼接
{
if (axis == 0)
{
vector<vector<float>> new_arr = merge(this->arr, mat1.arr);
return MyMatrix(new_arr);
}
else {
MyMatrix transposed_mat = this->transpose();
vector<vector<float>> arr = merge(transposed_mat.arr, mat1.transpose().arr);
MyMatrix new_matrix= MyMatrix(arr);
new_matrix = new_matrix.transpose();
return new_matrix;
}
}
void disp() // 矩阵信息打印
{
std::cout << "----------" << std::endl;
for (int i = 0; i < this->shape[0]; i++)
{
std::cout << "row: " << i << ": ";
disp_vector(this->arr[i]);
}
std::cout << "----------" << std::endl;
}
void disp_vector(vector<float> a)
{
for (int i = 0; i < a.size(); i++)
{
std::cout << a[i] << ' ';
}
std::cout << std::endl;
}
};
测试代码:
//my matrix测试代码:
vector<vector<float>> a(2, vector<float>(2,0));
a[0][0] = 0;
a[0][1] = 1;
a[1][0] = 2;
a[1][1] = 3;
MyMatrix mat = MyMatrix(a);
mat.disp();
mat.add(mat).disp();
mat.subtract(mat).disp();
mat.scalar_multiply(10).disp();
mat.transpose().disp();
vector<int> indices(2, 0);
int axis = 1;
mat.take(indices, axis).disp();
mat.mat_merge(mat, 1).disp();
vector<int> shape(2, 10);
MyMatrix mat1= MyMatrix(shape);
mat1.disp();
2023年4月4日