[RESOLVED] Assignment on function
Since you can't use [][] to access a matrix you are suggested to use () operator instead.
However a limitation of that is you can't assign:
Code:
m[col][row] = x; // valid
m(col, row) = x; // invalid
which forces us to write such statements as:
Code:
m[( col * cols ) + row] = rhs( col, row );
Is there any way of working around that limitation so you can use
Code:
m( col, row ) = rhs( col, row );
which is more elegant?
It's mainly an academic exercise as I've finished the matrix class and just having a wonder over lunch.
Re: Assignment on function
you can do an assign with the m(x,y) notation.
There should be two implementations of this function. I will assume double
for the example
Code:
double operator()(const size_t row, const size_t col) const;
double& operator()(const size_t row, const size_t col);
Re: Assignment on function
Well apart from the fact I use float and index in using int's ours are identical.
Actual code:
Code:
float& operator()( const int col, const int row )
{
return m[( col * cols ) + row];
}
float operator()( const int col, const int row ) const
{
return m[( col * cols ) + row];
}
However I get the error telling me that the "term does not evaluate to a function".
Re: Assignment on function
well, that should work, so something else is wrong
Re: Assignment on function
You'll have to show us the full code so we can see WHAT isn't evaluating to a function.
BTW, shouldn't it be col + (row * cols) ?
Re: Assignment on function
You need to make them member functions, of course:
Code:
template<typename T>
class Array2D
{
std::vector<T> Data_;
unsigned int Row_;
unsigned int Cols_;
public:
Array2D( unsigned int Rows, unsigned int Cols ) :
Rows_( Rows ),
Cols_( Cols ),
Data_( Rows * Cols )
{
}
const T& operator()( unsigned int Row, unsigned int Col ) const
{
return Data[Row * Cols_ + Col];
}
T& operator()( unsigned int Row, unsigned int Col )
{
return Data[Row * Cols_ + Col];
}
};
Re: Assignment on function
Quote:
Originally Posted by Zaccheus
You'll have to show us the full code so we can see WHAT isn't evaluating to a function.
Okay. How about setting up and identity matrix in the constructor?
Code:
for ( int col=0; col<cols; col++ )
{
for ( int row=0; row<rows; row++ )
m( col, row ) = 0.0f;
m( col, row ) = 1.0f;
}
Quote:
Originally Posted by Zaccheus
BTW, shouldn't it be col + (row * cols) ?
No, I'm pretty sure column-major is (col * cols) + row.
Re: Assignment on function
Quote:
Originally Posted by grahamr (work)
No, I'm pretty sure column-major is (col * cols) + row.
Nope.
Re: Assignment on function
I thought column-major layout is
Code:
[0 4 8 12]
[1 5 9 13]
[2 6 10 14]
[3 7 11 15]
G.
Re: Assignment on function
Oh I see.
I think it is better for each dimension to be contiguous memory.
Re: Assignment on function
Quote:
Originally Posted by grahamr (work)
Okay. How about setting up and identity matrix in the constructor?
Code:
for ( int col=0; col<cols; col++ )
{
for ( int row=0; row<rows; row++ )
m( col, row ) = 0.0f;
m( col, row ) = 1.0f;
}
What is 'm' ? How is it defined/created?
Re: Assignment on function
Quote:
Originally Posted by Zaccheus
What is 'm' ? How is it defined/created?
Re: Assignment on function
if for whatever reason you want to do column major layout (C uses row major layout in memory) then you would do
col*rows + row
Re: Assignment on function
Quote:
Originally Posted by grahamr (work)
'm' should be a 2D matrix class such as in GNiewerth's post.
Re: Assignment on function
Does it matter what it is? It doesn't change the fact that using () to index into the array doesn't allow assignment (or I can't use assignment).