自己的矩阵类乘法运算符 [英] Own matrix class multiply operator

查看:121
本文介绍了自己的矩阵类乘法运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个IntegerMatrix类来添加我自己的方法来处理矩阵.现在,我编写了这样的函数:

I wrote an IntegerMatrix class to add my own methods to work with matrices. Now I've written a function like this:

IntegerMatrix** IntegerMatrix::multiplyMatrix(IntegerMatrix** table2)

(这是一个双指针,因为我持有指向4x4 2D数组的大量指针.)所以我可以简单地做到这一点:

(It's a double pointer because I'm holding a huge array of pointers to 4x4 2D arrays.) so I simply could do this:

matrix1.multplyMatrix(matrix2)

一个小问题是*没有为我自己的班级定义.所以我想让这个运算符重载,我可以做这样的事情:

One little problem is the * isn't defined for my own class. So I thought to overload this operator that I could do something like this:

sum += this->table[i][k] * table2[k][j];

但是如何在重载运算符中获得正确的ik,其定义如下:

But how can I get the right i and k in the overloaded operator, which is defined like this:

IntegerMatrix IntegerMatrix::operator*(const IntegerMatrix & k);

我现在不知道的唯一问题是如何获取正确的值?

The only problem I can't figure out right now is how to get the right values ?

我已经重写了这个,现在我有了:

I've rewrote this and now I have:

IntegerMatrix IntegerMatrix::operator*(const IntegerMatrix & table2)
{
    int i, j, k;
    int sum;
    IntegerMatrix * result = new IntegerMatrix(SIZE);

    for (i = 0; i < SIZE; i++) {
        for (j = 0; j < SIZE; j++) {
            sum = 0;
            for (k = 0; k < SIZE; k++) {
                sum += this->table[i][k] * table2[k][j];
            }
            result[i][j] = sum;
        }
    }
    return *result;

}

这只是[]上的一个错误:

That gives me just an error on the [] :

Binary '[' : 'IntegerMatrix' does not define this operator or a conversiont o a type acceptable to the predefined operator.

推荐答案

我不明白您的问题,但这是有关矩阵乘法法线工作原理的简短演示:

I don't understand your question, but here's a brief demo of how matrix multiplication normall works:

class IntegerMatrix {
    int table[3][3];

public:
      IntegerMatrix& operator*=(const IntegerMatrix& rhs) {
           //multiply table by rhs.table, store in data.
           return *this;
      }
};
IntegerMatrix operator*(IntegerMatrix lhs, const IntegerMatrix& rhs)
{return lhs*=rhs;} //lhs is a copy, so we can alter and return it

为您编辑

您有验证码

FOR YOUR EDIT

You have the code

IntegerMatrix * result = new IntegerMatrix(SIZE); //pointer to an IntegerMatrix
...
result[i][j] = sum; //assign sum to the jth index of matrix # i

实际上,我认为您想要

result->table[i][j] = sum; //sum to the ixj index of the result matrix.

此外,您的函数是泄漏的,因为您有new,但没有delete.这很容易解决,因为您不需要新的. (您是来自Java还是C#背景?)

Also, your function is leaky, because you have a new, but no delete. This is easy to fix in your case, since you don't need the new. (Are you from a Java or C# background?)

IntegerMatrix result(SIZE);
...
        result[i][j] = sum;
...
return result;

与上述所有内容无关,您实际上可能希望为Integer Matrix提供[]运算符.

Unrelated to all of the above, you might actually want to provide a [] operator for your Integer Matrix.

class row {
    int* data;
    int size;
public:
    row(int* d, int s) :data(d), size(s) {}
    int& operator[](int offset) {
        assert(offset<size);
        return data[offset];
    }
};

row operator[](int column) {
    assert(column<SIZE); 
    return row(table[column], SIZE);
}

这将使您可以编写:

IntegerMatrix result;
result[i][j] = sum;

这篇关于自己的矩阵类乘法运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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