Boost logo

Ublas :

From: Gunter Winkler (guwi17_at_[hidden])
Date: 2006-03-16 08:14:20

On Wednesday 15 March 2006 16:03, Alion Sci wrote:
> Below is the code I need help understanding. It is a function template that
> computes a product. What is confusing me is that I don't know what
> vector_matrix_binary_traits() and scalar_multiplies() do. I also don't
> understand, what BOOST_UBLAS_INLINE is supposed to accomplish. And lastly,
> what is value_type and expression_type ?

I'll try to explain the lines.

> matrix_expression.hpp
> .
> // (outer_prod (v1, v2)) [i] [j] = v1 [i] * v2 [j]
> template<class E1, class E2>
> typename vector_matrix_binary_traits<E1, E2, scalar_multiplies<typename
> E1::value_type, typename E2::value_type> >::result_type

the vector_matrix_binary_traits types are used to map the types of the 2
operands of a binary operation (of 2 vectors) to a result type. The (partial)
specialization of this type is just a few lines above. E1 and E2 are the
types of the operands, the third argument is the operations performed on the
elements of the operands. Here the are multiplied (because they are expected
to be scalars).

> outer_prod (const vector_expression<E1> &e1,
> const vector_expression<E2> &e2) {
> BOOST_STATIC_ASSERT (E1::complexity == 0 && E2::complexity == 0);

> typedef typename vector_matrix_binary_traits<E1, E2,
> scalar_multiplies<typename E1::value_type, typename E2::value_type>
> >::expression_type expression_type;

Here the second information about the operation is used: The type of the
requested expression. This is the key to the expression template mechanism to
only produce types that describe the expression to be computed, but not to do
any computation, yet.

> return expression_type (e1 (), e2 ());
> }

In this case the returned type is vector_matrix_binary< ... > . This type
knows that it combines e1(i) and e2(j) to result(i,j) using the given scalar
operation (scalar_multiplies).

in short: outer_prod(...) returns an instance of vector_matrix_binary
and outer_prod(...) (i,j) returns one value of the resulting matrix. Thus the
actual computation starts only if the result is really requested.