Boost logo

Ublas :

From: Shawn D. Pautz (sdpautz_at_[hidden])
Date: 2005-10-20 09:31:31


Is there an error in the return type you provide in the first code
snippet below? I don't believe matrix_expression has a value_type. I
assume that one either uses E::value_type, or as you show in the second
code snippet to provide a separate struct that encapsulates the return type.

More generally, it appears that deducing the return type is tricky even
for scalars in the general case. Is the "accepted uBLAS way" to write
these functions with a general return type in order to encapsulate them
in an expression? In other words if I give a simple value_type it will
work, but not necessarily give as efficient an expression?

Thanks for the code examples.


Michael Stevens wrote:

>uBLAS has a much better way to write this kind of function. You should use...
> template <class E>
> typename matrix_expression<E>::value_type determinant (const matrix_expression<E>& A)
> {

>template <class E1, class E2>
>struct prod_expression_result
>{ // Provide ET result E1E2T_type of prod(matrix_expression<E1>,trans(matrix_expression<E2>)
> typedef typename ublas::matrix_unary2_traits<E2, ublas::scalar_identity<typename E2::value_type> >::result_type E2T_type;
> typedef typename ublas::matrix_matrix_binary_traits<typename E1::value_type, E1,
> typename E2T_type::value_type, E2T_type>::result_type E1E2T_type;
>template <class E>
>typename prod_expression_result<E,E>::E1E2T_type
> prod_SPD (const ublas::matrix_expression<E>& X)
> * Symmetric Positive (Semi) Definate product: X*X'
> */
> return prod( X, trans(X));
>As you can see 90% of the code is in deducing the return type. This cannot be done automatically in the current C++ standard.
>Hope this makes sense,
> Michael

Shawn D. Pautz
Radiation Transport Department, MS 1179
Sandia National Laboratories
P.O. Box 5800
Albuquerque, NM 87185
Phone: 505-284-4291
Fax: 505-844-0092
E-Mail: sdpautz_at_[hidden]