C++矩阵操作+,-,*,=,矩阵转置

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/thecentry/article/details/78404710

模板类实现矩阵操作+,-,*,=,矩阵转置//此次添加了转置的模块

#include<iostream>
using namespace std;
template<class T>
class matrix
{
	friend ostream& operator<<(ostream&, const matrix<T>&);
	friend istream& operator >> (istream&, matrix<T>&);
public:
	matrix(int theRows = 0, int theColumns = 0);
	matrix(const matrix<T>&);
	~matrix() { delete[] element; };
	int rows()const { return theRows; };
	int cols()const { return theColumns; };
	T& operator()(int i, int j)const;
	matrix<T>& operator=(const matrix<T>&);
	matrix<T> operator+()const;
	matrix<T> operator+(const matrix<T>&)const;
	matrix<T> operator-()const;
	matrix<T> operator-(const matrix<T>&)const;
	matrix<T> operator*(const matrix<T>&)const;
	matrix<T>& operator+=(const T&);
	void Input();
	void Output();
	void Transport();
private:
	int theRows;
	int theColumns;
	T* element;
};
template<class T>
matrix<T>::matrix(int theRows = 0, int theColumns = 0)
{
	if (theColumns < 0 || theRows < 0)
	{
		throw runtime_error(string{ "Rows and Cols must be >= 0 " });
	}
	if ((theColumns == 0 || theRows == 0) && (theColumns != 0 || theRows != 0))
	{
		throw runtime_error(string{ "Either both or neither rows and columns should be zero " });
	}
	this->theColumns = theColumns;
	this->theRows = theRows;
	element = new T[theColumns * theRows];
}
template<class T>
matrix<T>::matrix(const matrix<T>& m)
{
	theColumns = m.theColumns;
	theRows = m.theRows;
	size_t number = theColumns * theRows;
	element = new T[number];
	copy(m.element, m.element + number, element);
}
template<class T>
matrix<T>& matrix<T>::operator=(const matrix<T>& m)
{
	if (this != &m)
	{
		delete[] element;
		theColumns = m.theColumns;
		theRows = m.theRows;
		size_t number = theColumns * theRows;
		element = new T[number];
		copy(m.element, m.element + number, element);
	}
	return *this;
}
template<class T>
T& matrix<T>::operator()(int i, int j) const
{
	if (i<1 || i>theRows || j<1 || j>theColumns)
	{
		throw runtime_error(string{ "Matrix Index Out Of Bounds " });
	}
	return element[(i - 1)*theColumns + j - 1];
}
template<class T>
matrix<T> matrix<T>::operator+(const matrix<T>& m)const
{
	if (theRows != m.theRows || theColumns != m.theColumns)
	{
		throw runtime_error(string{ "Matrix Size is Out of batch " });
	}
	matrix<T> w(theRows, theColumns);
	size_t number = theColumns*theRows;
	for (int i = 0; i < number; i++)
	{
		w.element[i] = element[i] + m.element[i];
	}
	return w;
}
template<class T>
matrix<T> matrix<T>::operator*(const matrix<T>& m)const
{
	if (theColumns != m.theRows || theRows != m.theColumns)
	{
		throw runtime_error(string{ "Matrix Style is Out of batch " });
	}
	matrix<T> w(theRows, theColumns);
	int ct = 0;
	int cm = 0;
	int cw = 0;
	for (int i = 1; i <= theRows; i++)
	{
		for (int j = 1; j <= theColumns; j++)
		{
			T sum = element[ct] * m.element[cm];
			for (int k = 2; k <= theColumns; k++)
			{
				ct++;
				cm += m.theColumns;
				sum += element[ct] * m.element[cm];
			}
			w.element[cw++] = sum;
			ct -= theColumns - 1;
			cm = j;
		}
		ct += theColumns;
		cm = 0;
	}
	return w;
}
template<class T>
void matrix<T>::Input()
{
	size_t number = theColumns * theRows;
	for (int i = 0; i < number; i++)
	{
		cin >> element[i];
	}
	cout << "Input fanished" << endl;
	return;
}
template<class T>
void matrix<T>::Output()
{
	size_t number = theColumns * theRows;
	for (int i = 0; i < theRows; i++)
	{
		for (int j = 0; j < theColumns; j++)
		{
			cout << element[i*theColumns + j] << " ";
		}
		cout << endl;
	}
	cout << "Output finished " << endl;
	return;
}
template<class T>
void matrix<T>::Transport()
{
	matrix<T> temp(theColumns, theRows);
	size_t number = theColumns * theRows;
	for (int i = 0; i < theRows; i++)
	{
		for (int j = i; j < theColumns; j++)
		{
			temp.element[i*theColumns + j] = this->element[j*theColumns + i];
			temp.element[j*theColumns + i] = this->element[i*theColumns + j];
		}
	}
	*this = temp;
	return;
}
int main()
{
	matrix<int> a(3, 3);
	matrix<int> b(3, 3);
	matrix<int> c(3, 3);
	a.Input();
	b.Input();
	c = a * b;
	c.Transport();
	c.Output();
	return 0;

}


展开阅读全文

Numpy矩阵操作

06-04

<p>rn <br />rn</p>rn<p>rn 20周年限定:唐宇迪老师一卡通!<span style="color:#337FE5;">可学唐宇迪博士全部课程</span>,仅售799元(原价10374元),<span style="color:#E53333;">还送漫威授权机械键盘+CSDN 20周年限量版T恤+智能编程助手!</span> rn</p>rn<p>rn 点此链接购买:rn</p>rn<table>rn <tbody>rn <tr>rn <td>rn <a href="https://edu.csdn.net/topic/teachercard?utm_source=jsk20xqy" target="_blank"><span style="color:#337FE5;">https://edu.csdn.net/topic/teachercard?utm_source=jsk20xqy</span></a> rn </td>rn </tr>rn </tbody>rn</table>rn<p>rn <br />rn</p>rn购买课程后,请扫码进入学习群<span style="font-family:&quot;">,获取唐宇迪老师答疑</span> rn<div>rn <img src="https://img-bss.csdn.net/201908070344327835.jpg" alt="" /> rn</div>rn<p>rn <br />rn</p>rn<p>rn Python数据分析与机器学习实战教程,该课程精心挑选真实的数据集为案例,通过python数据科学库numpy,pandas,matplot结合机器学习库scikit-learn完成一些列的机器学习案例。课程以实战为基础,所有课时都结合代码演示如何使用这些python库来完成一个真实的数据案例。算法与项目相结合,选择经典kaggle项目,从数据预处理开始一步步代码实战带大家入门机器学习。学完该课程即可:rn1.掌握Python数据科学工具包,包括矩阵数据处理与可视化展示。rn2.掌握机器学习算法原理推导,从数学上理解算法是怎么来的以及其中涉及的细节。rn3.掌握每一个算法所涉及的参数,详解其中每一步对结果的影响。rn4.熟练使用Python进行建模实战,基于真实数据集展开分析,一步步完成整个建模实战任务。rn</p>

没有更多推荐了,返回首页