c++ - Matrix multiplication algorithm -


i have mat2, mat3 , mat4 classes denote 2x2, 3x3 , 4x4 matrices, respectively. trying implement multiplication algorithm overloading *= operator. taking mat4 example, here declaration:

mat4& operator*=(const mat4 &m); 

the multiplication algorithm return reference calling object. means return reference this. implementation of multiplication algorithm defined thus:

mat4& mat4::operator *=(const mat4& m) {     (uint = 0; < row(); i++)     {         (uint j = 0; j < col(); j++)         {             (uint k = 0; k < m.col(); k++)             {                 data[i][j] += (data[i][k] * m.data[k][j]);             }         }     }      return *this; } 

where uint typedef of unsigned int. because operator overload class function, there's no need have lhs matrix instead provide rhs matrix call m. functions row() , col() in case return 4 since mat4x4; these functions part of mat4 class. attribute data 2-dimensional array of floats fixed size. problem algorithm doesn't produce correct result. example:

mat4 m1(1.0, 0.0, 0.0, 30.0,         0.0, 1.0, 0.0, 30.0,         0.0, 0.0, 1.0, 30.0,         0.0, 0.0, 0.0, 1.0);  mat4 m2(23.0, 21.0, 0.0, 1.0,         10.0, 9.0, 1.0, 0.0,         1.0, 2.0, 0.0, 0.0,         3.0, 2.0, 9.0, 9.0);  auto result = m1 * m2;  cout << result << endl; 

just know, overloaded << operator make couts matrices possible , easy. aware, matrices column-major because intend use them opengl. m1.data[3][2] means 4th column, 3rd row. result hardly supposed be:

mat4 =  [      24   5040   5040    54     10   2110   2110    40     1    212    213     31     0    0      0       1 ] 

my issue multiplication algorithm doesn't work , produces correct result. how go correcting algorithm produces correct result result of matrix multiplication?

i overload * operator so:

mat4 mat4::operator *(const mat4& m) const {     mat4 result = *this;     result *= m;     return result; } 

this utilises *= operator overload. unfortunately * operator overload won't work either.

your problems mess data (you read , write same variable during calculations) in data field of (*this) have wrong data.

you need use temp matrix , copy data after calculations:

mat2& operator*=(const mat2 &m) {     // set elements of temp_data 0. important     array<array<int, 2>,2> temp_data = {{{0,0},{0,0}}};     (uint = 0; < row(); i++)     {         (uint j = 0; j < col(); j++)         {             (uint k = 0; k < m.col(); k++)             {                 temp_data[i][j] += (data[i][k] * m.data[k][j]);             }         }     }      this->data = temp_data;     return *this; } 

example of use:

int main() {     array<array<int, 2>,2> data1 = {{{1,0},{0,1}}};     array<array<int, 2>,2> data2 = {{{1,2},{3,4}}};     mat2 m1;     m1.data = data1;     mat2 m2;     m2.data = data2;     m1 *= m2;     (auto el: m1.data) {         (auto ele : el) {             cout << ele << " ";         }         cout << endl;     }     return 0; } 

output:

1 2                                                  3 4  

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -