⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.h

📁 自己设计的一个小型矩阵运算类(泛化)
💻 H
字号:
// 2003/04/14
// name : Matrix.h
// construct a simple Matrix
// updated latest on 04/15/2003
// update on 05/06/2003

#ifndef MATRIX_H
#define MATRIX_H

#include <cassert>
#include <iostream>
using namespace std;

template <typename T>
class Matrix 
{
public:
	Matrix(int =defaultRowSize, int =defaultColumnSize);  // default constructor
	Matrix(int, int, T*);		// constructor
	Matrix(const Matrix&);		// copy constructor
	~Matrix();				// destructor
	Matrix& operator=(const Matrix&);		// assignment operator
	T& operator()(int, int);
	const T& operator()(int, int) const;
	friend ostream& operator<<(ostream&, const Matrix&);
	bool friend operator==(const Matrix&, const Matrix&);
	bool friend operator!=(const Matrix&, const Matrix&);
	friend const Matrix operator+(Matrix&, Matrix&);
	friend const Matrix operator-(Matrix&, Matrix&);
	friend const Matrix operator*(Matrix&, Matrix&);
	friend const Matrix operator*(T, Matrix&);
	friend const Matrix operator*(Matrix&, T);
	friend const Matrix operator+=(Matrix&, Matrix&);
	friend const Matrix operator-=(Matrix&, Matrix&);
	friend const Matrix operator*=(Matrix&, Matrix&);
	friend const Matrix operator*=(Matrix&, T);

	friend Matrix& transverse(Matrix&);		// 矩阵转置
	//	friend T getMatrixValue(Matrix&);		// 若为等阶矩阵,则求其对应的行列式的值
	T getElem(int, int) const;		// 取得矩阵中某个位置的元素
	bool setElem(int, int, T);
	// 逐步扩充
private:
	T **im;				// implementation....
	int rows;
	int cols;
	static const int defaultRowSize;
	static const int defaultColumnSize;
	void initMatrix(T*);	// init with a row
	void initMatrix(T**);		// init with a matrix;
	void initData(int r, int c);

};


template <typename T>
Matrix<T>::Matrix(int r, int c)
{	// default constructor
	initData(r, c);
	initMatrix(static_cast<T*>(0));
}

template <typename T>
Matrix<T>::Matrix(int r, int c, T* array) 
{	// constructor
	initData(r, c);
	initMatrix(array);
}

template <typename T>
Matrix<T>::Matrix(const Matrix<T>& rhs) 
{		// copy constructor
	initData(rhs.rows, rhs.cols);
	initMatrix(rhs.im);
}

template <typename T>
void Matrix<T>::initMatrix(T* array) 
{
	for (int i = 0; i < rows; ++i)
		for (int j = 0; j < cols; ++j)
			if (array)
				im[i][j] = *array++;
			else
				im[i][j] = 0;
}

template <typename T>
void Matrix<T>::initMatrix(T** matrix) 
{
	for (int r = 0; r < rows; ++r)
		for (int c = 0; c < cols; ++c)
			im[r][c] = matrix[r][c];
}

template <typename T>
void Matrix<T>::initData(int r, int c) 
{
	rows = r;
	cols = c;
	im = new T* [rows];
	for (int i = 0; i < rows; ++i)
		im[i] = new T[cols];
}

template <typename T>
Matrix<T>::~Matrix() 
{
	for (int r = 0; r < rows; ++r)
		delete [] im[r];
	delete [] im;
}

template <typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& rhs) 
{
	if (this != &rhs) 
	{
		if (rows != rhs.rows || cols != rhs.cols) 
		{
			Matrix<T>::~Matrix();		// deallocate old matrix
			initData(rhs.rows, rhs.cols);
		}
		initMatrix(rhs.im);
	}

	return *this;
}

template <typename T>
T& Matrix<T>::operator()(int r, int c) 
{
	assert(r >= 0 && r < rows && c >= 0 && c < cols);
	return im[r][c];
}

template <typename T>
const T& Matrix<T>::operator()(int r, int c) const 
{
	assert(r >= 0 && r < rows && c >= 0 && c < cols);
	return im[r][c];
}

template <typename T>
ostream& operator<<(ostream& os, const Matrix<T>& mt) 
{
	for (int r = 0; r < mt.rows; ++r) 
	{
		for (int c = 0; c < mt.cols; ++c)
			os << mt(r, c) << ' ';
		os << '\n';
	}

	return os;
}

template <typename LT, typename RT>
bool operator==(const Matrix<LT>& lmtr, const Matrix<RT>& rmtr) 
{
	if (lmtr.rows == rmtr.rows && lmtr.cols == rmtr.cols) 
	{
		for (int r = 0; r < lmtr.rows; ++r)
			for (int c = 0; c <lmtr.cols; ++c)
				if (lmtr.im[r][c] != rmtr.im[r][c])
					return false;
	}
	else
		return false;

	return true;
}

template <typename LT, typename RT>
bool operator!=(const Matrix<LT>& lmtr, const Matrix<RT>& rmtr) 
{
	if (lmtr==rmtr)
		return false;
	else
		return true;
}

template<typename T>
Matrix<T>& transverse(Matrix<T>& lhs) 
{
	Matrix<T> _matrix(lhs);
	for (int r=0; r<lhs.rows; ++r)
		for (int c = 0; c<lhs.cols; ++c)
			_matrix.im[c][r] = lhs.im[r][c];	// change rows with columns
	lhs = _matrix;

	return lhs;
}

template<typename T>
const Matrix<T> operator+(Matrix<T>& lhs, Matrix<T>& rhs) 
{
	if ( lhs.rows!=rhs.rows ||  lhs.cols != rhs.cols )
	{
		cout << "the range of two Matrix is not equal" << endl;
		exit(1);
	}
	else
	{
		Matrix<T> _matrix(lhs.rows, lhs.cols);

		for (int r = 0; r < lhs.rows; ++r)
			for(int c = 0; c < lhs.cols; ++c)
			{
				_matrix.im[r][c] = lhs.im[r][c] + rhs.im[r][c];
			}

		return _matrix;
	}
}

template<typename T>
const Matrix<T> operator-(Matrix<T>& lhs, Matrix<T>& rhs)
{
	if ( lhs.rows!=rhs.rows ||  lhs.cols != rhs.cols )
	{
		cout << "the range of two Matrix is not equal" << endl;
		exit(1);
	}
	else
	{
		Matrix<T> _matrix(lhs.rows, lhs.cols);

		for (int r = 0; r < lhs.rows; ++r)
			for(int c = 0; c < lhs.cols; ++c)
			{
				_matrix.im[r][c] = lhs.im[r][c] - rhs.im[r][c];
			}

			return _matrix;
	}
}

template<typename T>
const Matrix<T> operator*(Matrix<T>&lhs, Matrix<T>&rhs)
{
	if ( lhs.cols != rhs.rows )
	{
		cout << "The colum of the Left is NOT equal to the row of the Right!" << endl;
		exit(1);
	}
	else
	{
		Matrix<T> _matrix(lhs.rows, rhs.cols);
		for (int lr = 0; lr < lhs.rows; ++lr)			// lhs的行
		{
			
			for (int rc = 0; rc < rhs.cols; ++rc)
			{
				T _tmp = static_cast<T> (0);  // 用于存贮 lhs 行与 rhs 列相乘的结果
				for (int lc_rr = 0; lc_rr < lhs.cols; ++lc_rr)	
				{
					_tmp += lhs.im[lr][lc_rr] * rhs.im[lc_rr][rc];
				}
				_matrix.im[lr][rc] = _tmp;
			}
		}

		return _matrix;
	}
}

template<typename T>
const Matrix<T> operator*(T l_value, Matrix<T>& rhs)
{
	Matrix<T> _matrix(rhs.rows, rhs.cols);
	
	for (int r = 0; r < rhs.rows; ++r)
		for(int c = 0; c < rhs.cols; ++c)
		{
			_matrix.im[r][c] = l_value * rhs.im[r][c];
		}

	return _matrix;
}

template<typename T>
const Matrix<T> operator*(Matrix<T>& lhs, T r_value)
{
	Matrix<T> _matrix(lhs.rows, lhs.cols);

	for (int r = 0; r < lhs.rows; ++r)
		for(int c = 0; c < lhs.cols; ++c)
		{
			_matrix.im[r][c] = r_value * lhs.im[r][c];
		}

		return _matrix;
}

template<typename T>
const Matrix<T> operator+=(Matrix<T>& lhs, Matrix<T>& rhs)
{
	lhs = lhs + rhs;

	return lhs;
}

template<typename T>
const Matrix<T> operator-=(Matrix<T>& lhs, Matrix<T>& rhs)
{
	lhs = lhs - rhs;

	return rhs;
}

template<typename T>
const Matrix<T> operator*=(Matrix<T>& lhs, Matrix<T>& rhs)
{
	lhs = lhs * rhs;

	return lhs;
}

template<typename T>
const Matrix<T> operator*=(Matrix<T>& lhs, T r_value)
{
	lhs = lhs * r_value;

	return lhs;
}


/* template<typename T>
T getMatrixValue(Matrix<T>& _matrix)
{
	if ( _matrix.rows != _matrix.cols )
	{
		cout << "row != column, cannot canculate!" << endl;
	}
	T _value = static_cast<T> (0);

	T _range = _matrix.rows;

	T _positive_num = static_cast<T> (0);
	T _negative_num = static_cast<T> (0);

	for ( int c = 0; c <_range; ++c)
	{
		T _ltmp = static_cast<T> (1);
		for (int i = 0; i < _range; ++c)
		{
			_ltmp *= _matrix.im[(c+i)%_range][(c+i)%_range];
		}
		_positive_num += _ltmp;
	}

	for (int _c = _range-1; _c >= 0; --_c)
	{
		T __ltmp = static_cast<T> (1);
		for (int _i = _range-1; _i >= 0; --_i)
		{
			__ltmp *= _matrix.im
		}
	}	
}		*/

template<typename T>
T Matrix<T>::getElem(int _r, int _c) const
{
	if ( _r<0 || _r>=rows || _c<0 || _c>=cols )
	{
		cout << "Not in range!" << endl;
	}
	else
	{
		return this->im[_r][_c];
	}
}

template<typename T>
bool Matrix<T>::setElem(int _r, int _c, T _value)
{
	if ( _r<0 || _r>=rows || _c<0 || _c>=cols )
	{
		cout << "Not in range!" << endl;
		return false;
	}
	else
	{
		this->im[_r][_c] = _value;
		return true;
	}
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -