简体   繁体   English

来自两个实例(矩阵)的运算符 *= 给出了不好的结果

[英]Operator*= from two instances (Matrix) gives bad results

I have a class Matrix, and I'm trying to implement the method operator*=, which i use to make the product of two instances(matrix): m1*=m2.我有一个类 Matrix,我正在尝试实现方法 operator*=,我用它来制作两个实例(矩阵)的乘积:m1*=m2。

I tried both method with friend and two parameter, and without friend and 1 parameter, but in both cases the results it's bad.我用朋友和两个参数尝试了这两种方法,没有朋友和 1 个参数,但在这两种情况下,结果都很糟糕。 Only with one parameter and the this use gives me results similar to the right (not always).只有一个参数,这个使用给了我类似于正确的结果(并非总是如此)。

Tried with friend and 2 parameter, and without friend and 1 parameter.尝试使用朋友和 2 个参数,没有朋友和 1 个参数。 Tried returning directly the first matrix, m1, also creating a temporary matrix m3尝试直接返回第一个矩阵 m1,同时创建一个临时矩阵 m3

My private members:我的私人会员:

int N, M;      
T** data;
bool Init(const int N_, const int M_);
void Clear();

(i'm using init to initialize the matrix/bidimensional array): (我使用 init 来初始化矩阵/二维数组):

bool Matrix::Init(const int N_, const int M_) {
  this->Clear(); //dealloco comunque prima di inizializzarla
  N = N_;
  M = M_;
  if (N <= 0 || M <= 0) {
      cerr << "Non posso generare una matrixe: " << N <<"x"<< M << endl;
      return false;
  }
  else if (N > 0 && M > 0) {
      data = new T*[N];
      for (int i = 0; i < N; i++) {
        data[i] = new T[M];
      }
  }
return true;

} }

My operator *= mathod (no friend, 1 parameter):我的运算符 *= mathod(没有朋友,1 个参数):

Matrix& Matrix::operator*=(const Matrix& m2) {

    float operation = 0;
    int N_ = (this->N < m2.N) ? this->N : m2.N;
    int M_ = (this->M < m2.M) ? this->M : m2.M;
    Matrix m3(N_, M_);

    if (this->N != m2.M || this->M != m2.N) {
        this->Set(0, 0, flag_stop);
    }
    else {

        for (int i = 0; i < this->N; ++i) {
            for (int j = 0; j < m2.M; ++j) {
                for (int k = 0; k < this->M; ++k) {
                    operation = operation + (this->Get(i,k) * m2.Get(k,j)) ;
                    //cout << " Cout m1 su "<< i<< ","<<k<<":"<< this->Get(i,k) << "\t " << " Cout m2: "<< m2.Get(k,j) << endl;
                    this->Set(i, j, operation);
                }

                //cout << operation << "\t";
                operation = 0;
            }
            operation = 0;
        }
    }
    return *this;
}

In the main, when i try to use the operator*=:主要是,当我尝试使用运算符 *= 时:

Matrix m1(i1,j1);
Matrix m2(i2,j2);

//operator*=
    cout << endl;
    m1*=m2;
    int N_ = (m1.GetN() < m2.GetN()) ? m1.GetN() : m2.GetN();
    int M_ = (m1.GetM() < m2.GetM()) ? m1.GetM() : m2.GetM();

    for (int i = 0; i < N_; ++i) {
        for (int j = 0; j < M_; ++j) {
            cout << m1.Get(i,j) << "\t";
        }
        cout << endl;
    }

It's all the day that i try, but the results are not rights i also tried with m1[2][2] and m2[2][2], with m1[3][2] and m2[2][3], etc... but nothing.我一整天都在尝试,但结果不是我也尝试过 m1[2][2] 和 m2[2][2],m1[3][2] 和 m2[2][3] ,等等......但什么都没有。 Someone have had a similar problem?有人遇到过类似的问题吗?

Hoping to have right product of two matrixes, but i have, at the major times, big numbers (expected 5, obtained 30), or the first column right numbers, and the second not希望有两个矩阵的正确乘积,但我在主要时间有大数字(预期为 5,获得 30),或者第一列正确的数字,第二个不是

The reason for mistakes you report seems to be the multiplication algorithm itself.您报告的错误的原因似乎是乘法算法本身。 Basically, your multiplication code is as follows:基本上,您的乘法代码如下:

for (int i = 0; i < this->N; ++i) {
    for (int j = 0; j < m2.M; ++j) {
        for (int k = 0; k < this->M; ++k) {
            operation = operation + (this->Get(i,k) * m2.Get(k,j)) ;
            this->Set(i, j, operation);
        }
        operation = 0;
    }
}

Your algorithm modifies the original matrix just in the process of the calculation ( this->Set() call) so when you call this->Get(i,k) for any i < j you obtain not an original value of the first matrix at i th row and k th column but a value that was already modified by this->Set() call.您的算法仅在计算过程中修改原始矩阵( this->Set()调用),因此当您为任何i < j调用this->Get(i,k) ,您获得的不是第一个矩阵的原始值在第i行和第k列,但是这个值已经被this->Set()调用修改了。 This apparently leads to wrong results.这显然会导致错误的结果。

In order to solve this you must ensure that you use original matrix values for your calculations, for example, by making a copy of the original matrix or (more optimal) of the currently modified row of the original matrix.为了解决这个问题,您必须确保使用原始矩阵值进行计算,例如,通过制作原始矩阵的副本或(更优化)原始矩阵当前修改行的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模运算符(%)给出不同的结果 - Modulo Operator (%) gives divergent results 三元运算符在return语句中给出意外结果 - Ternary operator gives unexpected results in return statement OpenCV卷积矩阵给出异常结果 - Opencv convolution matrix gives unusual results 取消引用运算符给出的结果与带有 void* 的数组偏移运算符不同 - Dereference operator gives different results than array offset operator with void* 当我彼此减去两个矩阵时,c ++中的重载运算符(&lt;&lt;)cout无法正常工作 - overloading operator (<<) cout in c++ is not working when i Subtract two matrix from each other 合并两个文本文件会产生奇怪的结果 - Merging two text files gives wierd results 为什么这两种情况给出相同的结果? - Why these two cases gives the same results? 在lambda上使用条件运算符调用std :: any_of会产生意外结果 - Calling std::any_of with conditional operator on lambda gives unexpected results 重载ostream运算符以从文件中打印矩阵 - Overloading ostream operator to print matrix from file 在两台计算机上使用浮动的相同代码会产生两种不同的结果 - Same code using floats on two computers gives two different results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM