65.9K
CodeProject 正在变化。 阅读更多。
Home

一个使用 2D 动态数组声明的模板化矩阵类,包含矩阵最需要的数学函数以及标准 C++ 中所需的所有运算符(不使用任何 vc 命令)

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.30/5 (10投票s)

2007年2月22日

CPOL

3分钟阅读

viewsIcon

38287

downloadIcon

707

此程序由 Shahin Namini 编写

引言

这个矩阵类是一个模板类,可以初始化为任何类型的数字类型(int、double、float 等)。它包括逆矩阵和行列式函数(编写代码并不容易),以及矩阵类所需的所有运算符,例如两个矩阵相乘或将数字乘以矩阵。它能够是任何大小,而且易于使用。

指令

上传的文件 check.zip 包含一个文件,展示了如何初始化矩阵以及运算符或函数如何工作。 请注意,括号可能是最重要的运算符。它允许您访问数组元素,如

matrix1(2,0)=5;

Matrix 类的成员函数

这些是该类的所有成员函数和运算符

template<class T>
class Matrix
{
public

  1. Matrix(int Row_Size,int Column_Size)
  2. Matrix() >
  3. Matrix(const Matrix<T> &)
  4. ~Matrix(void)
  5. void Set_Element(int Row_,int Column,const T& Value)
  6. T Get_Element(int Row ,int Column) const
  7. bool operator!() const;
  8. const Matrix<T> & operator=(const Matrix<T> &)
  9. const Matrix<T> & operator=(const T&)
  10. bool operator==(const Matrix<T> &) const
  11. bool operator==(const T & ) const
  12. bool operator!=(const Matrix<T> & second) const
  13. bool operator!=(const T & number)
  14. T &operator() (int Row,int Column)
  15. Matrix<T> operator+(const T &)
  16. Matrix<T> operator+(const Matrix<T> &)
  17. const Matrix<T> &operator+=(const T&)
  18. const Matrix<T> & operator+=(const Matrix<T> &)
  19. Matrix<T> operator-(const T&)
  20. Matrix<T> operator - (const Matrix<T> &)
  21. const Matrix<T> & operator -=(const T & )
  22. const Matrix<T> & operator -=(Matrix<T> & )
  23. Matrix<T> operator*(const T&)
  24. Matrix<T> operator* (const Matrix<T> &)
  25. const Matrix<T> & operator*=(const T&)
  26. const Matrix<T> & operator*=(const Matrix<T> &)
  27. void Transposed();>
  28. Matrix<T> GetTransposed() const
  29. bool Inverse();>
  30. Matrix<T> GetInversed() const
  31. double Det() const
  32. void Set_Size(int RowSize,int ColumnSize)
  33. const int& Get_ColumnSize() const
  34. const int & Get_RowSize() const
私有的
  • int m_RowSize
  • int m_ColumnSize
  • T**m_Data

};

注释


注意:Inverse 的返回参数(一个布尔值)显示矩阵是否可逆,如果可逆,则求矩阵的逆矩阵


注意:我已经在 vc 2005 中编写了此代码,但它不是托管代码。如果在 vc6 中编译它,您会看到一些错误,例如 i 被重新声明。这是因为 vc6 有点不同,当您这样声明 i 时:for (int i=0;i<5;i++){ /*语句*/}

在 for 语句之后 i 被销毁,但在 vc6 中即使在 for 语句之后它也是一个声明的变量。但是通过调试 vc 中的此类错误很容易将其更改为 vc6。你可以自己做。


注意:我唯一没有自己编写的代码是行列式,因为我找到了由 Paul Bourke 编写的非常棒的代码。要查找原始代码,请转到 http://local.wasp.uwa.edu.au/~pbourke/other/determinant/

类中使用的一些技术

为了声明一个,我必须指出使用 new 命令声明元素数组 (m_Data) 的方式

假设给出了 m_RowSize & m_ColumnSize

m_Data=new T* [m_RowSize];

for(int i=0;i<m_RowSize;i++)

m_ColumnSize=new T [m_ColumnSize];

希望你使用并享受它

© . All rights reserved.