本文介绍了错误:与"operator *"不匹配(操作数类型为"QGenericMatrix< 4,4,float>"和"QGenericMatrix< 4,3,float>")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个矩阵相乘:

I'm trying to multiply two matrices like this:

float values4x3[] = {
    3, 3, 3,
    1, 1, 1,
    2, 2, 2,
    1, 1, 1
};
QGenericMatrix< 4, 3, float > myMat4x3 (values4x3);

float values4x4[] = {
    3, 3, 3, 3,
    1, 1, 1, 1,
    2, 2, 2, 2,
    1, 1, 1, 1
};
QGenericMatrix< 4, 4, float > myMat4x4 (values4x4);

QGenericMatrix< 4, 3, float > product4x3 = myMat4x4 * myMat4x3;

qDebug() << __func__ << "product4x3 = " << product4x3;

但是,我收到错误消息:

However, I'm receiving the error:


此运算符用于将两个实例相乘QGenericMatrix,但是我很困惑,不确定如何将其与NNxM2M1xNN一起使用.


There is this operator for multiplying two instances of QGenericMatrix, but I'm confused and not sure how to use it with its NNxM2 and M1xNN.

按照@scopchanov的建议,我交换了两个矩阵,如下所示:

As suggested by @scopchanov I swapped the two matrices like this:

QGenericMatrix< 4, 3, float > product4x3 = myMat4x3 * myMat4x4;

现在,错误已解决,结果记录如下:

Now, the error is resolved and the result is logged like this:

qDebug() << __func__ << "product4x3 = " << product4x3;

日志:

product4x3 =  QGenericMatrix<4, 3, float>(
    19        19        19        19
    10        10        10        10
    10        10        10        10
)

上面的结果矩阵实际上是3x4!好吧,这有点令人困惑.

The resulted matrix above is actually 3x4! Well, it is a bit confusing.

推荐答案

原因

在数学中,乘积阶数应为MxP乘以PxN以产生MxN乘积.换句话说,矩阵的内部尺寸必须一致.

Cause

In mathematics the multiplication order should be MxP times PxN to produce MxN product. In other words the inner dimensions of the matrices must agree.

但是, QGenericMatrix<M1, M2, TT> operator* 的文档指出:

However, the documentation for QGenericMatrix<M1, M2, TT> operator* states:

这意味着,为了得到AB的相乘结果,应该先写B然后是A.

This means, that in order to get the result of the multiplication of A and B, one should write first B and then A.

说实话,考虑到我们的习惯,我觉得这很奇怪.

To be honest, I find this rather bizzare, considering what we are used to.

像这样交换矩阵的位置:

Swap the places of the matrices like this:

QGenericMatrix< 4, 3, float > product4x3 = myMat4x3 * myMat4x4;

结果

对于您给出的示例,结果是:

Result

For the example given by you the result is:

MainWindow product4x3 =  QGenericMatrix<4, 3, float>(
        19        19        19        19
        10        10        10        10
        10        10        10        10
)

这篇关于错误:与"operator *"不匹配(操作数类型为"QGenericMatrix&lt; 4,4,float&gt;"和"QGenericMatrix&lt; 4,3,float&gt;")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:38