C++实现矩阵,继承vector

基于vector>实现,因为是作神经网络的的,所以直接搞成double了。

1. 重载 * 作为矩阵叉乘的符号

2.重载 / 作为矩阵点乘的符号

3.还有矩阵 加法+ 减法-

4.矩阵转置transposition()

 

使用说明:

// 使用说明
//{
//	int row = 10;
//	int col = 10;
//	//矩阵是浮点型滴
//	// 1.创建一个10 * 10的矩阵
//	Matrix matrix(row, col);
//
//	// 2.赋值
//	matrix[1][1] = 20;
//
//	// 3.创建一个10 * 10的矩阵 并且默认0-1的随机值 
//	Matrix matrix2(row, col, true);
//}
//

 

vector版本

#pragma once
#include 
#include 

class MATRIX_ :
	public std::vector>
{
public:
	MATRIX_()
	{
	}
	MATRIX_(int row_, int col_, bool rand_=false)
	{
		if (row_ < 1 || col_ < 1)
			throw "ERR row < 1 || col < 1";

		this->row = row_;
		this->col = col_;
		this->resize(row);
		std::vector>::iterator iter;
		for (iter = this->begin(); iter < this->end(); iter++)
		{
			iter->resize(col);
			if (rand_)
			{
				std::vector::iterator it;
				for (it = iter->begin(); it < iter->end(); it++)
				{
					*it = double(rand()) / RAND_MAX;
				}
			}
		}

	}

	MATRIX_ operator*(MATRIX_ &m2)
	{
		if (this->col != m2.row)
		{
			printf("%d\n", this->col);
			printf("%d\n", m2.row);
			throw "ERR this->col != m2.row";

		}

		MATRIX_ my_result(this->row, m2.col); //500*1

		for (int i = 0; i < my_result.row; i++)
		{
			for (int j = 0; j < my_result.col; j++)
			{
				for (int m = 0; m < this->col; m++)
				{
					my_result[i][j] += (*this)[i][m] * m2[m][j];
				}
			}
		}

		return my_result;
	}

	// 用除号表示数乘运算
	MATRIX_ operator/(MATRIX_ m2)
	{
		if (this->col != m2.col || this->row != m2.row)
			throw "this->col != m2.col || this->row != m2.row";

		MATRIX_ my_result(this->row, this->col);
		for (int i = 0; i < this->row; i++)
		{
			for (int j = 0; j < m2.col; j++)
			{
				my_result[i][j] += (*this)[i][j] * m2[i][j];
			}
		}
		return my_result;
	}

	MATRIX_ operator*(double num)
	{

		MATRIX_ my_result(this->row, this->col);
		for (int i = 0; i < this->row; i++)
		{
			for (int j = 0; j < this->col; j++)
			{
				my_result[i][j] += (*this)[i][j] * num;
			}
		}
		return my_result;
	}
	
	MATRIX_ operator+(MATRIX_ m2)
	{
		if (this->col != m2.col || this->row != m2.row)
		{
			printf("ERR this->col != m2.col || this->row != m2.row\n");
			throw "this->col != m2.col || this->row != m2.row";
		}

		MATRIX_ my_result(this->row, this->col);
		for (int i = 0; i < this->row; i++)
		{
			for (int j = 0; j < this->col; j++)
			{
				my_result[i][j] = (*this)[i][j] + m2[i][j];
			}
		}
		return my_result;
	}

	MATRIX_ operator-(MATRIX_ m2)
	{
		if (this->col != m2.col || this->row != m2.row)
			throw "this->col != m2.col || this->row != m2.row";

		MATRIX_ my_result(this->row, this->col);
		for (int i = 0; i < this->row; i++)
		{
			for (int j = 0; j < this->col; j++)
			{
				my_result[i][j] = (*this)[i][j] - m2[i][j];
			}
		}
		return my_result;
	}

	MATRIX_ transposition()
	{
		MATRIX_ my_result(this->col, this->row);
		for (int i = 0; i < this->col; i++)
		{
			for (int j = 0; j < this->row; j++)
			{
				my_result[i][j] = (*this)[j][i];
			}
		}
		return my_result;
	}

	void print()
	{
		printf("\n---------row:%d,col:%d---------\n",row,col);
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				printf("%f  |  ", (*this)[i][j]);
			}
			printf("\n");
		}
		printf("------------------------\n");
	}
public:
	int row = 0;
	int col = 0;
};

typedef MATRIX_ Matrix;

 

你可能感兴趣的:(C++实现矩阵,继承vector)