### Throwing values to Matrix Class?

Hello professionals,

I tried throwing values to these Matrix44 class function but I failed...

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` `````` #include #include template class Matrix44 { public: Matrix44() {} const T* operator [] (uint8_t i) const { return m[i]; } T* operator [] (uint8_t i) { return m[i]; } // initialize the coefficients of the matrix with the coefficients of the identity matrix T m[4][4] = { { 1,0,0,0 },{ 0,1,0,0 },{ 0,0,1,0 },{ 0,0,0,1 } }; //Multiplying two-dimensional array matrices 4x4.[not yet called] Matrix44 operator * (const Matrix44& rhs) const { Matrix44 mult; for (uint8_t i = 0; i < 4; ++i) { for (uint8_t j = 0; j < 4; ++j) { mult[i][j] = m[i][0] * rhs[0][j] + m[i][1] * rhs[1][j] + m[i][2] * rhs[2][j] + m[i][3] * rhs[3][j]; } } return mult; } }; typedef Matrix44 Matrix44f; int main() { std::cout << "Multiplying 2 matrices"; std::cout << Matrix44({ 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 }); //error system("pause"); return 0; } ``````

What should I edit with my statement in line 38?

For example:

If I will multiply the matrices M1*M2 like:

M1 = | 1 2 3 4 |
| 1 2 3 4 |
| 1 2 3 4 |
| 1 2 3 4 |

M2 = | 1 2 3 4 |
| 1 2 3 4 |
| 1 2 3 4 |
| 1 2 3 4 |

I would like to ask how will I successfully throw these values to the Matrix class function above to get the answer:

M3 = | 10 20 30 40 |
| 10 20 30 40 |
| 10 20 30 40 |
| 10 20 30 40 |
Last edited on
I would like to ask if do I have to initialize a set of array values to rhs as well?
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````#include #include #include template< typename T > struct Matrix44 { // inplicitly defaulted default consteructor static constexpr std::size_t N = 4 ; using row_type = T[N] ; // a row is an array of N T row_type& operator[] ( std::size_t row_num ) noexcept { return m[row_num] ; } const row_type& operator[] ( std::size_t row_num ) const noexcept { return m[row_num] ; } T m[N][N] {} ; // initialise to all T{} // note: within the class template definition, the template name Matrix44 (without any template arguments) // means Matrix44 (in jargon, this is known as 'the injected-class-name) // the following line is equivalent to: static Matrix44 identity() // return an identity matrix static Matrix44 identity() // return an identity matrix { return { { { T{1}, T{}, T{}, T{} }, { T{}, T{1}, T{}, T{} }, { T{}, T{}, T{1}, T{} }, { T{}, T{}, T{}, T{1} } } }; } // overloaded as a non member function /* note: to overload as a member function, declare Matrix44 operator* ( const Matrix44& b ) const and change 'a' to '(*this)': result[i][j] += (*this)[i][k] * b[k][j]; */ friend Matrix44 operator* ( const Matrix44& a, const Matrix44& b ) { Matrix44 result ; // note: initialised to all zeroes for( std::size_t i = 0 ; i a { { { 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 } } } ; std::cout << a << '\n' ; std::cout << a * Matrix44::identity() << '\n' ; // multiply with identity matrix std::cout << a*a << '\n' ; // multiply with itself Matrix44< std::complex > b { { { 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 } } } ; using namespace std::literals ; for( std::size_t i = 0 ; i < b.N ; ++i ) for( auto& cmplx : b[i] ) cmplx += 0.1i ; std::cout << b << '\n' ; std::cout << b * Matrix44< std::complex >::identity() << '\n' ; // multiply with identity matrix std::cout << b*b << '\n' ; // multiply with itself }``````

http://coliru.stacked-crooked.com/a/fc3915d31ca96234
Thank you @JLBorges. :)
Topic archived. No new replies allowed.