-
overload *
Hi
I am trying to overload * for matrix multiplications. For some reason the overload doesn't work for more than two consecutive operations (ie. M*T*V).
Code:
// Overload * for ob1
matrix_square operator*(const matrix_square &left, const matrix_square &right)
{
int i;
matrix_square temp;
for(i = 0; i < Grids; i++){
temp.ptr00[i] = left.ptr00[i]*right.ptr00[i] + left.ptr01[i]*right.ptr10[i] + left.ptr02[i]*right.ptr20[i];
temp.ptr01[i] = left.ptr00[i]*right.ptr01[i] + left.ptr01[i]*right.ptr11[i] + left.ptr02[i]*right.ptr21[i];
temp.ptr02[i] = left.ptr00[i]*right.ptr02[i] + left.ptr01[i]*right.ptr12[i] + left.ptr02[i]*right.ptr22[i];
temp.ptr10[i] = left.ptr10[i]*right.ptr00[i] + left.ptr11[i]*right.ptr10[i] + left.ptr12[i]*right.ptr20[i];
temp.ptr11[i] = left.ptr10[i]*right.ptr01[i] + left.ptr11[i]*right.ptr11[i] + left.ptr12[i]*right.ptr21[i];
temp.ptr12[i] = left.ptr10[i]*right.ptr02[i] + left.ptr11[i]*right.ptr12[i] + left.ptr12[i]*right.ptr22[i];
temp.ptr20[i] = left.ptr20[i]*right.ptr00[i] + left.ptr21[i]*right.ptr10[i] + left.ptr22[i]*right.ptr20[i];
temp.ptr21[i] = left.ptr20[i]*right.ptr01[i] + left.ptr21[i]*right.ptr11[i] + left.ptr22[i]*right.ptr21[i];
temp.ptr22[i] = left.ptr20[i]*right.ptr02[i] + left.ptr21[i]*right.ptr12[i] + left.ptr22[i]*right.ptr22[i];
}
temp.zeros();
return temp;
}
where ptr20 etc are elements of a matrix. Any advise please.
-
-
Well that code looks fine, assuming it is declared as static or friend. Define "doesn't work".
What is the definition of Grids, and zeros(), and what other code from matrix_square can you post that might help?
-
Is Grids a global var? Not too good solution for C++
All your matrixes are 3x3? Have you heared about vectors?
ptrXY variables arritating me a little.
temp.zeros(); - hmmm, are you zeroing all you calculated results before returning them? Or it is just a bad naming?
-
It's working fine now for some strange reason. Do tell me about vectors is it from standard C++ library?
-