【问题标题】:C++, overload * for matrix multiplicationC++,重载 * 用于矩阵乘法
【发布时间】:2016-11-16 14:59:58
【问题描述】:

我在尝试重载乘法运算符 * 以进行矩阵乘法时遇到了很多麻烦。我已经定义了一个矩阵类

#ifndef MMATRIX_H
#define MMATRIX_H
#include <vector>
#include <cmath>

// Class that represents a mathematical matrix
class MMatrix
{
public:
// constructors
MMatrix() : nRows(0), nCols(0) {}
MMatrix(int n, int m, double x = 0) : nRows(n), nCols(m), A(n * m, x)
{}

// set all matrix entries equal to a double
MMatrix &operator=(double x)
{
    for (int i = 0; i < nRows * nCols; i++) 
        A[i] = x;
return *this;
}

// access element, indexed by (row, column) [rvalue]
double operator()(int i, int j) const
{
    return A[j + i * nCols];
}

// access element, indexed by (row, column) [lvalue]
double &operator()(int i, int j)
{
    return A[j + i * nCols];
}


// size of matrix
int Rows() const { return nRows; }
int Cols() const { return nCols; }

// operator overload for matrix * vector. Definition (prototype) of member class
MVector operator*(const MMatrix& A);

private:
unsigned int nRows, nCols;
std::vector<double> A;
};
#endif

这是我尝试的运算符重载

inline MMatrix operator*(const MMatrix& A, const MMatrix& B)
{
MMatrix m(A), c(m.Rows(),m.Cols(),0.0);
for (int i=0; i<m.Rows(); i++)
{
    for (int j=0; j<m.Cols(); j++)
    {
        for (int k=0; k<m.Cols(); k++)
        {
            c(i,j)+=m(i,k)*B(k,j);
        }
    }
}
return c;

}

我确信元素的实际乘法没有任何问题。

我得到的错误来自我试图将两个矩阵相乘的主 .cpp 文件 C=A*B;我得到了这个错误,

错误:“operator=”不匹配(操作数类型为“MMatrix”和“MVector”)

【问题讨论】:

  • MVector operator*(const MMatrix&amp; A); 什么是 MVector?
  • 您在类中声明了一个成员operator* 重载,它返回一个MVector,但随后定义了一个全局operator* 重载,它返回一个MMatrix。选择一种返回类型,然后选择是要实现类成员还是全局函数。
  • 好的,MVector 正在为我的矩阵向量乘法返回一个向量,但它工作正常。我现在已经把 MMatrix MMatrix::operator*(const MMatrix & A) 放在课堂上,虽然我不确定我现在如何进行乘法

标签: c++ operator-overloading


【解决方案1】:

有两种方法可以重载operator*:

MMatrix MMatrix::operator*(MMatrix); //or const& or whatever you like
MMatrix operator*(MMatrix, MMatrix);

这些都是有效的,但语义略有不同。

为使您的定义与您的声明相匹配,请将定义更改为:

MMatrix MMatrix::operator*(const MMatrix & A)
{
    //The two matrices to multiple are (*this) and A
    MMatrix c(Rows(),A.Cols(),0.0);
    for (int i=0; i < Rows(); i++)
    {
        for (int j=0; j < A.Cols(); j++)
        {
            for (int k=0; k < Cols(); k++)
            {
                c(i,j) += (*this)(i,k)*A(k,j);
            }
        }
    }
    return c;
}

至于您看到的错误,您似乎在您的类中声明了运算符采用矩阵并返回一个向量。您可能打算改为返回一个矩阵。该错误告诉您不能将MVector 分配给MMatrix。

【讨论】:

  • 我现在已按照您的建议将原型放入课程中。 MVector 返回矩阵向量乘法。我不确定的是我现在如何将矩阵相乘。这是如何工作的?
  • @wildboy 你问矩阵相乘的公式是什么?有很多在线代码示例,您可以参考如何实现矩阵乘法。
  • 您提到将定义从 MMatrix operator*(const MMatrix& A, const MMatrix& B) 更改为 MMatrix MMatrix::operator*(const MMatrix & A)。所以我对如何只用一个矩阵进行乘法感到困惑?
  • @wildboy 您正在定义一个成员函数,在这种情况下,* 左侧的矩阵 可以通过 this 指针访问。
  • 如何使用“this”指针来引用 lhs 上矩阵的特定元素?
【解决方案2】:

我相信,你需要定义拷贝构造函数和拷贝赋值:

MMatrix(const MMatrix& other);
MMatrix& operator=(const MMatrix& other);

移动构造函数和赋值也不重要:

MMatrix(MMatrix&& other);
MMatrix& operator=(MMatrix&& other);

你的 MVector 也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2012-12-09
    相关资源
    最近更新 更多