基于C++的矩阵模板类

```cpp
#ifndef CALCULATEMATRIX_H
#define CALCULATEMATRIX_H

#include<iostream>
#include<assert.h>
template <class Type>
class myMatrix
{
	typedef myMatrix<Type> _matrix;

public:
	myMatrix(int nrows, int ncols);
	myMatrix(const myMatrix<Type> & _Matrix);
	~myMatrix();

	void initialize(const Type**rhs, int nrow, int ncol);//自定义初始化
	myMatrix<Type> reverse();//转置
	myMatrix<Type> Inverse();//求逆
	int getRow();//获取行
	int getCol();//获取列


	const Type operator()(int nrow, int ncol)const;
	Type& operator()(int nrow, int ncol);
	 
	friend myMatrix<Type> operator + (const myMatrix<Type>& lmatrix, const myMatrix<Type>&rmatrix)
	{
		if (lmatrix.nSizeCol != rmatrix.nSizeCol || lmatrix.nSizeRow != rmatrix.nSizeRow)
		{
			//行列必须一致
			cerr << "[err]   error col's number or row's number!\n";
			exit(-1);
		}

		myMatrix<Type> matrix(lmatrix.nSizeRow, lmatrix.nSizeCol);

		for (int i = 0; i < lmatrix.nSizeRow; i++)
		{
			for (int j = 0; j < lmatrix.nSizeCol; j++)
			{
				matrix.ppdMatrix[i][j] = lmatrix.ppdMatrix[i][j] + rmatrix.ppdMatrix[i][j];
			}
		}
		return matrix;
	};

	friend _matrix operator - (const _matrix& lmatrix, const _matrix&rmatrix)
	{
		if (lmatrix.nSizeCol != rmatrix.nSizeCol || lmatrix.nSizeRow != rmatrix.nSizeRow)
		{
			//行列必须一致
			cerr << "[err]   error col's number or row's number!\n";
			exit(-1);
		}

		myMatrix<Type> matrix(lmatrix.nSizeRow, lmatrix.nSizeCol);

		for (int i = 0; i < lmatrix.nSizeRow; i++)
		{
			for (int j = 0; j < lmatrix.nSizeCol; j++)
			{
				matrix.ppdMatrix[i][j] = lmatrix.ppdMatrix[i][j] - rmatrix.ppdMatrix[i][j];
			}
		}
		return matrix;
	};

	friend _matrix operator * (const _matrix& lmatrix, const _matrix&rmatrix)
	{
		if (lmatrix.nSizeCol != rmatrix.nSizeRow)
		{
			//行列对应
			cerr << "[err]   error col's number or row's number!\n";
			exit(-1);
		}

		myMatrix<Type> matrix(lmatrix.nSizeRow, rmatrix.nSizeCol);
		for (int i = 0; i < lmatrix.nSizeRow; i++)
		{
			for (int j = 0; j < rmatrix.nSizeCol; j++)
			{
				Type _value = 0;
				for (int k = 0; k < rmatrix.nSizeRow; k++)
				{
					double a = lmatrix.ppdMatrix[i][k];
					a= rmatrix.ppdMatrix[k][j];
					_value += lmatrix.ppdMatrix[i][k] * rmatrix.ppdMatrix[k][j];
				}
				matrix.ppdMatrix[i][j] = _value;
			}
		}
		return matrix;
	};
	myMatrix<Type>& operator=(const myMatrix<Type>& curmatrix);
	int nSizeRow;//行
	int nSizeCol;//列
	Type**ppdMatrix;//矩阵

protected:

private:

};

template<class Type>
inline myMatrix<Type>::myMatrix(int nrows, int ncols)
{
	//初始化为0

	if (nrows < 0 || ncols < 0)
	{
		cerr << "[err]   error col's number or row's number!\n";
		exit(-1);
	}
	else
	{
		nSizeRow = nrows;
		nSizeCol = ncols;
		ppdMatrix = new Type*[nSizeRow];
		for (int i = 0; i < nSizeRow; i++)
		{
			ppdMatrix[i] = new Type[nSizeCol];
			memset(ppdMatrix[i], 0, nSizeCol * sizeof(Type));
		}

	}

}

template<class Type>
inline myMatrix<Type>::myMatrix(const myMatrix<Type>& _Matrix)
{ 
	//为临时对象构造函数
	 
	if (_Matrix.nSizeRow < 0 || _Matrix.nSizeCol < 0)
	{
		cerr << "[err]   error col's number or row's number!\n";
		exit(-1);
	}
	else
	{
		nSizeRow = _Matrix.nSizeRow;
		nSizeCol = _Matrix.nSizeCol;
		ppdMatrix = new Type*[nSizeRow];
		for (int i = 0; i < nSizeRow; i++)
		{
			ppdMatrix[i] = new Type[nSizeCol];
		}
		int i = 0, j = 0;
		for (i = 0; i < nSizeRow; i++)
		{
			for (j = 0; j < nSizeCol; j++)
			{
				ppdMatrix[i][j] = _Matrix(i, j);
			}
		}

	}
}

template<class Type>
myMatrix<Type>::~myMatrix()
{
	for (int i = 0; i < nSizeRow; i++)
	{
		delete[]ppdMatrix[i];
	}
	delete[]ppdMatrix;
	ppdMatrix = NULL;
	/*delete matrix;*/
}

template<class Type>
inline const Type myMatrix<Type>::operator()(int nrow, int ncol) const
{
	//获取矩阵指定位置值
	if (nSizeRow <= nrow || nSizeCol <= ncol)
	{
		cerr << "[err]   error col's number or row's number!\n";
		exit(-1);
	}
	return (Type)ppdMatrix[nrow][ncol];  
}

template<class Type>
inline Type & myMatrix<Type>::operator()(int nrow, int ncol)
{
	//更改矩阵指定位置值
	if (nSizeRow <= nrow || nSizeCol <= ncol)
	{
		cerr << "[err]   error col's number or row's number!\n";
		exit(-1);
	}
	return (Type&)ppdMatrix[nrow][ncol];
}

template<class Type>
inline void myMatrix<Type>::initialize(const Type ** rhs, int nrow, int ncol)
{
	//自定义初始化
	if (nrow < 0 || ncol < 0)
	{
		cerr << "[err]   error col's number or row's number!\n";
		exit(-1);
	}
	else
	{
		nSizeRow = nrows;
		nSizeCol = ncols;
		ppdMatrix = new Type*[nSizeRow];
		int i = 0, j = 0;
		for (i= 0; i < nSizeRow; i++)
		{
			ppdMatrix[i] = new Type[nSizeCol];
			memset(ppdMatrix[i], 0, nSizeCol * sizeof(Type));
		}
		for (i = 0; i < nrow; i++)
		{
			for (j = 0; j < ncol; j++)
			{
				ppdMatrix[i][j] = rhs[i][j];
			}
		}
	}
}

template<class Type>
inline myMatrix<Type> myMatrix<Type>::reverse()
{
	//转置
	myMatrix<Type> _matrix2(nSizeCol, nSizeRow);
	int i, j;
	for (i = 0; i < _matrix2.nSizeRow; i++)
	{
		for (j = 0; j < _matrix2.nSizeCol; j++)
		{
			_matrix2(i,j) =ppdMatrix[j][i];
		}
	}
	return _matrix2;
	 

}

template<class Type>
inline myMatrix<Type> myMatrix<Type>::Inverse()
{
    //Cholesky法求解
	//求逆
	if (nSizeCol != nSizeRow)
	{
		cerr << "[err]   The number of rows and columns must be equal !\n";
		exit(-1);
	}
	myMatrix<Type> _matrixT(nSizeRow, nSizeCol);
	myMatrix<Type> _matrixR(nSizeRow, nSizeCol);

	//calculate T
	int i = 0, j = 0, k = 0;
	Type _Tki = 0;
	_matrixT(0, 0) = sqrt(ppdMatrix[0][0]);
	for (j = 1; j < nSizeCol; j++)
	{
		//calculate the first row value of _matrixT 计算矩阵T的第一行
		_matrixT(0, j) = ppdMatrix[0][j] / _matrixT(0, 0);
	}
	for (i = 1; i < nSizeRow; i++)
	{
		_Tki = 0;
		for (k = 0; k <= i - 1; k++)
		{
			_Tki += _matrixT(k, i)*_matrixT(k, i);
		}
		//calculate Tii 计算Tii
		_Tki = ppdMatrix[i][i] - _Tki;
		_matrixT(i, i) = sqrt(_Tki);

		for (j = i + 1; j < nSizeCol; j++)
		{
			_Tki = 0;
			for (k = 0; k <= i - 1; k++)
			{
				_Tki += _matrixT(k, i)*_matrixT(k, j);
			}
			//calculate Tij   计算Tij
			_matrixT(i, j) = (ppdMatrix[i][j] - _Tki) / _matrixT(i, i);
		}
	}
	//calculate R  计算矩阵R
	for (i = 0; i < nSizeRow; i++)
	{
		//calculate Rii  计算Rii
		_matrixR(i, i) = 1 / _matrixT(i, i);
	}
	for (i = nSizeRow - 2; i >= 0; i--)
	{
		for (j = i + 1; j < nSizeCol; j++)
		{
			_Tki = 0;
			for (k = i + 1; k <= j; k++)
			{
				_Tki += _matrixT(i, k)*_matrixR(k, j);
			}
			//calculate Rij   计算Rij
			_matrixR(i, j) = (-1 * _Tki) / _matrixT(i, i);
		}
	}

	myMatrix<double> matrix_N = _matrixR * _matrixR.reverse();
	return matrix_N;
}


template<class Type>
inline int myMatrix<Type>::getRow()
{
	//获取行
	return nSizeRow;
}

template<class Type>
inline int myMatrix<Type>::getCol()
{
	//获取列
	return nSizeCol;
}

 

template<class Type>
inline myMatrix<Type>& myMatrix<Type>::operator=(const myMatrix<Type>& curmatrix)
{
	for (int i = 0; i < curmatrix.nSizeRow; i++)
	{
		for (int j = 0; j < curmatrix.nSizeCol; j++)
		{
			this->ppdMatrix[i][j] = curmatrix.ppdMatrix[i][j];
		}
	}

	return *this;
}

#endif

调用

myMatrix<double> A(3, 3),B(3,3), C(3, 3);//构建
A(1, 2) = -1;//赋值
A.getCol();//获取列数
A.getRow();//获取行数
A.reverse();//转置
A.Inverse();//求逆(只适用正定矩阵)

C = A + B;
C = A * B;
C = A.Inverse() - B.reverse();
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值