
Ublas : 
From: Gunter Winkler (guwi17_at_[hidden])
Date: 20061120 05:37:01
On Sunday 19 November 2006 00:13, Manoj Rajagopalan wrote:
> template<typename E>
> void initialize(matrix_expression<E> A,
> typename E::value_type const& value)
> {
> ...
> }
Dealing with matrix expression directly is tricky, because of the CRTP usage.
1) You always have to use references to a matrix_expression as function
parameter, because the (default) copy constructor of matrix_expression does
not copy anything. (matrix_expression has no member variables and thus its
size is zero!)
template<typename E>
void initialize(matrix_expression<E>& A,
typename E::value_type const& value)
{
E& A_ref = A(); // this is the "official" way (just hiding the cast)
A_ref = scalar_matrix<typename E::value_type>(A_ref.size1(), A_ref.size2(),
value);
}
2) Sometimes you have to explicitly create instances (because you need
something to refer to and you can't have a direct instance of a
matrix_expression<X>)
matrix_range< matrix<int> > sub_A(A,range(1,3),range(1,3));
initialize(sub_A, 8);
In short:
Never instantiate matrix_expression because it contains no data.
Longer:
matrix_expression exists only because you need some common type
for all expressions. The price you pay is to use references everywhere.
mfg
Gunter