Re: Generic "expression template" library?
On Feb 10, 4:20 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
Noah Roberts wrote:
Important thing to note here is that you've a copying
operator here. Any time * appears in an expression there's
a temporary.
The expression template version will instead do something like this:
matrix_multiplication operator * (mt const& a, mt const& b)
{
return matrix_multiplication(a,b);
}
matrix_multiplication then has a conversion operator to
convert to matrix_type that does each of the necessary
operations for each element accessed.
I'm not sure if I understood correctly, but are you saying
that with the templates it's basically possible to perform
lazy evaluation of the operations involved which, with complex
expressions, will minimize the amount of temporaries created?
with templates or polymorphic classes, sort of. The number of
temporaries stays the same, but the temporaries are of very
small types (e.g. two pointers to already existing objects),
rather than enormous matrixes. The maxtrix_add class might look
something like:
template< typename T, typename LHS, typename RSH >
class maxtrix_add
{
public:
maxtrix_add( LHS const& lhs, RHS const& rhs )
: lhs( lhs )
, rhs( rhs )
{
assert( lhs.getX() == rhs.getX() ) ;
assert( lhs.getY() == rhs.getY() ) ;
}
T get( size_t i, size_t j ) const
{
return lhs.get( i, j ) + rhs.get( i, j ) ;
}
private:
LHS const& lhs ;
RHS const& rhs ;
} ;
template< typename T, typename LHS, typename RHS >
matrix_add< T, LHS, RHS >
operator+(
LHS const& lhs,
RHS const& rhs )
{
return matrix_add< T, LHS, RHS >( lhs, rhs ) ;
}
and matrix_multiply:
template< typename T, typename LHS, typename RHS >
class matrix_multiply
{
public:
matrix_multiply( LHS& lhs, RHS& rhs )
: lhs( lhs )
, rhs( rhs )
{
assert( lhs.getX() == rhs.getY() ) ;
}
T get( size_t i, size_t j ) const
{
T result = 0 ;
for ( size_t k = 0 ; k != lhs.getX() ; ++ k ) {
result += lhs.get( i, k ) * rhs.get( k, j ) ;
}
return result ;
}
private:
LHS const& lhs ;
RHS const& rhs ;
} ;
template< typename T, typename LHS, typename RHS >
matrix_multipy< T, LHS, RHS >
operator*(
LHS const& lhs,
RHS const& rhs )
{
return matrix_multipy< T, LHS, RHS >( lhs, rhs ) ;
}
The intermediate matrixes in a complicated expression are never
constructed. (This is obviously just an example. You'ld have
to do something to ensure that the template operators are only
found when you're actually dealing with matrices---when I
learned this idiom, we used virtual functions, with the
operators taking references to the base class, and there was no
problem.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34