heh heh I'm learning matrix math and so I'm trying to build a matrix class in C++. It's templated. *groans*
Anyway, the idea is simple: provide one matrix class that can use any type for its elements, with any number of rows and columns. It's the implementation that's stumping me.
There are two rules that I'm having trouble with - matrix addition and matrix multiplication.
1. (addition) the matrices are of the same dimensions
2. (multiplication) the inner dimensions of the matrices must be equal (as in: 2x3 * 3x4)
I can't use templated member functions, so I'll have to use global operators (which is better anyway). Here is my basic matrix class definition:
template <typename element_type, unsigned int rows = 4, unsigned int columns = 4>
class matrix
{
public:
element_type array[rows][columns];
};
Not too shabby, eh? Well, check out the operators:
template <typename element_type, unsigned int rows, unsigned int columns>
matrix<element_type, rows, columns> operator +(matrix<element_type, rows, columns>& first, matrix<element_type, rows, columns>& second)
{
matrix<element_type, rows, columns> result;
for( unsigned int x=0; x < rows; x++ )
for( unsigned int y=0; y < columns; y++ )
result[x][y] = first[x][y] + second[x][y];
return( result );
}
template <typename element_type, unsigned int rows, unsigned int columns>
matrix<element_type, rows, columns> operator -(matrix<element_type, rows, columns>& first, matrix<element_type, rows, columns>& second)
{
matrix<element_type, rows, columns> result;
for( unsigned int x=0; x < rows; x++ )
for( unsigned int y=0; y < columns; y++ )
result[x][y] = first[x][y] - second[x][y];
return( result );
}
template <typename element_type, unsigned int first_outer_dimension, unsigned int inner_dimension, unsigned int second_outer_dimension>
matrix<element_type, first_outer_dimension, second_outer_dimension> operator *(matrix<element_type, first_outer_dimension, inner_dimension>& first, matrix<element_type, inner_dimension, second_outer_dimension>& second)
{
matrix<element_type, first_outer_dimension, second_outer_dimension> result;
for( unsigned int x=0; x < rows; x++ )
{
for( unsigned int y=0; y < columns; y++ )
{
element_type sum = 0;
for( unsigned int index=0; index < columns; index++ )
{
sum += first[x][index] * second[index][y];
}
result[x][y] = sum;
}
}
return( result );
}
Now, when I try to do matrix addition and matrix multiplication according to those two rules I listed earlier, I get errors. With addition, the compiler just crashes with an Internal (actually, it should be Infernal) Compiler Error thingy. With multiplication, the compiler complains that it can't resolve the parameter: "second_outer_dimension."
Here's the error for multiplication:
quote:
MSVC Compiler Error
C:\Program Files\Microsoft Visual Studio\MyProjects\Temp22\Temp22.cpp(130) : error C2783: 'class matrix<element_type,first_outer_dimension,inner_dimension> __cdecl operator *(class matrix<element_type,first_outer_dimension,inner_dimension> &,class m
atrix<element_type,first_outer_dimension,inner_dimension> &)' : could not deduce template argument for 'second_outer_dimension'
Oh yeah, please don't worry about optimization until I understand how this thing works...
Thanks!
-
null_pointer
Sabre Multimedia
(edited to re-format template brackets)
Edited by - null_pointer on 7/15/00 3:28:30 PM