C++,用于矩阵乘法的重载 * [英] C++, overload * for matrix multiplication

查看:133
本文介绍了C++,用于矩阵乘法的重载 *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a great deal of trouble trying to overload the multiplication operator * for matrix multiplication. I've defined a matrix class

#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

这是我尝试的运算符重载

And here is my attempted operator overload

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;

}

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

I'm sure that there is nothing wrong with the actual multiplication of elements.

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

The error that I get is from my main .cpp file where I have tried to multiply two matrices together C=A*B; and I get this error,

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

error: no match for 'operator=' (operand types are 'MMatrix' and 'MVector')

推荐答案

重载operator*有两种方式:

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

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

These are both valid, but different with slightly different semantics.

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

For your definition to match your declaration change the definition to:

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.

As for the error you're seeing, it seems in your class you declared the operator to take a matrix and return a vector. You probably meant to return a matrix instead. The error is telling you that you can't assign a MVector to a MMatrix.

这篇关于C++,用于矩阵乘法的重载 *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆