 # Ublas :

Subject: Re: [ublas] Numeric traits for ublas bounded_vector and matrix
From: Rutger ter Borg (rutger_at_[hidden])
Date: 2010-02-19 08:28:59

Jesse Perla wrote:

> I think I mean sizes, but let me be more specific. Here are the
> metafunctions I implemented (poorly and specifically for ublas):
>
> is_bounded_vector< ublas::bounded_vector<double, 3> >::value == true
> is_bounded_vector< const ublas::bounded_vector<double, 3>& >::value ==
> true is_bounded_vector< ublas::vector<double> >::value == false
> is_bounded_vector< garbage_type >::value == false
> is_bounded_matrix< ublas::bounded_matrix<double, 3, 3> >::value == true
> is_bounded_matrix< ublas::matrix<double> >::value == false
> is_bounded_matrix< garbage_type >::value == false

I guess you actually mean "has_static_size" or "is_fully_static" or
something like that. You have those cases, like the matrix of the eigen
library, which permit you to specify one static size (e.g., the row size),
and one dynamic. If we're going to introduce a meta-function, it should
handle all cases in a clean way somehow.

> static_vector_size< ublas::bounded_vector<double, 3> >::value == 3
> static_vector_size< const ublas::bounded_vector<double, 3>& >::value == 3
> static_vector_size< ublas::bounded_vector<double, 3> >::value == 3
> static_matrix_rows< ublas::bounded_matrix<int, 3, 2> >::value == 3
> static_matrix_columns< ublas::bounded_matrix<int, 3, 2> >::value == 2

This is incorrect. Static sizes != bounded sizes. The template argument used
by ublas is to specify the storage allocation. I.e., the matrices / vectors
are bounded by those sizes, but not sized to those dimensions. E.g., you can
do

ublas::bounded_vector<double, 3> a( 1 );

which is a vector of length 1, but with pre-allocated storage of 3.

> What I didn't implement but was considering was what should happen with
> these metafunctions when the type isn't statically allocated. Why not
> just have it fail? Because otherwise you may get into the messy world
> of lazy template instantiation when getting traits from various types...
>
> static_vector_size< ublas::vector<double> >::value ==
> numeric_limits<std::size_t>::NaN?
>
> static_matrix_rows< ublas::matrix<double> >::value == NaN?
> static_matrix_columns< ublas::matrix<double> >::value == NaN?
> static_matrix_rows< ublas::vector<double> >::value //This should fail
>
> Why is this stuff useful? Besides static_asserts/enable_if, it also lets
> you have specialized algorithms for stack allocated vectors.
>

I don't see why you couldn't do static asserts / specialize algorithms on

typename result_of::size_row< ublas::matrix<double> >::type ==
std::ptrdiff_t
typename result_of::size_row< double >::type == mpl::int_<10>

, and ...

result_of::size_row< double >::type::value // compiles
result_of::size_row< ublas::matrix<double> >::type::value // error

Please let me know if you think something is not possible with the current
bindings and/or if you need some kind of easy meta-function added for your
enable_if stuff (or other magic).

Cheers,

Rutger