Boost logo

Ublas :

From: Maik Beckmann (maikbeckmann_at_[hidden])
Date: 2007-06-07 05:32:53


Am Mittwoch, 6. Juni 2007 22:45:17 schrieb Georg Baum:

> > What is a proper way, if any, to adapt a ublas (dense) vector around a
> > legacy pointer to data?
>
> Something like this should work:
>
> #include <boost/version.hpp>
> #if BOOST_VERSION >= 103400
> typedef boost::numeric::ublas::carray_adaptor<double> array_adaptor;
> #else
> typedef boost::numeric::ublas::array_adaptor<double> array_adaptor;
> #endif
> typedef vector<double, rcarray_adaptor> array_vector;

Hello

Lately had to work with Matlab's (you all know it) mex files and the
subroutine mechanism provided by MSC.Adams
( http://www.mscsoftware.com/products/adams.cfm?Q=396&Z=39 ),
a multibody system simulation software.

In both cases functions return pointers to vectors and matrices which:
    - underlying memory not owned by my code
    -Am Mittwoch, 6. Juni 2007 22:45:17 schrieb Georg Baum:

> > What is a proper way, if any, to adapt a ublas (dense) vector around a
> > legacy pointer to data?
>
> Something like this should work:
>
> #include <boost/version.hpp>
> #if BOOST_VERSION >= 103400
> typedef boost::numeric::ublas::carray_adaptor<double> array_adaptor;
> #else
> typedef boost::numeric::ublas::array_adaptor<double> array_adaptor;
> #endif
> typedef vector<double, rcarray_adaptor> array_vector;

Hello

Lately had to work with Matlab's (you all know it) mex files and the
subroutine mechanism provided by MSC.Adams
( http://www.mscsoftware.com/products/adams.cfm?Q=396&Z=39 ),
a multibody system simulation software.

In both cases functions return pointers to vectors and matrices which:
    - underlying storage not owned by my code
    - ... thus fix-sized

I modified Gunter Winkler's storage adapters and added a new vector and a new
matrix type (see attachment). Now They work as follows:

    typedef boost::ublas::adaption_vector<double> my_adaption_vector_type;
    double* ext_vec = 3rd_get_the_pointer(3rd_vector_handle);
    std::size_t vec_size = 3rd_get_the_size(3rd_vec_handle);
    my_adaption_vector_type my_adaption_vector(ext_vec, vec_size);

    my_adaption_vector(2) = 10; // assignment to an element

    tyepdef boost::ublas::adaption_matrix<double, ublas::column_major>
        my_adaption_matrix_type;
    double* ext_mat = 3rd_get_the_pointer(3rd_matrix_handle);
    std::size_t mat_size1 = 3rd_get_the_size1(3rd_matrix_handle);
    std::size_t mat_size2 = 3rd_get_the_size2(3rd_matrix_handle);
    my_adaption_matrix_type my_adaption_matrix(ext_mat, mat_size1, mat_size2);

    my_adaption_matrix(2,2) = 10; // assignment to an element

This code worked well for me, but isn't well tested because I was under time
pressure.

The adaption_vector overloads the operator=:
    // works ony if my_adaption_vector.size() == ublas_vector.size()
    my_adaption_vector = ublas_vector;

Since I didn't need adaption_matrix's operator= it isn't implemented yet.

Maybe it's useful for some of you.

MfG Maik Beckmann

I modified Gunter Winkler's storage adapters and added a new vector and a new
matrix type. Now They work as follows:

    typedef boost::ublas::adaption_vector<double> my_adaption_vector_type;
    double* ext_vec = 3rd_get_the_pointer(3rd_vector_handle);
    std::size_t vec_size = 3rd_get_the_size(3rd_vec_handle);
    my_adaption_vector_type my_adaption_vector(ext_vec, vec_size);

    my_adaption_vector(2) = 10; // assignment to an element

    tyepdef boost::ublas::adaption_matrix<double, ublas::column_major>
        my_adaption_matrix_type;
    double* ext_mat = 3rd_get_the_pointer(3rd_matrix_handle);
    std::size_t mat_size1 = 3rd_get_the_size1(3rd_matrix_handle);
    std::size_t mat_size2 = 3rd_get_the_size2(3rd_matrix_handle);
    my_adaption_matrix_type my_adaption_matrix(ext_mat, mat_size1, mat_size2);

    my_adaption_matrix(2,2) = 10; // assignment to an element

This code worked well for me, but isn't well tested because I was under time
pressure.

The adaption_vector overloads the operator=:
    // works ony if my_adaption_vector.size() == ublas_vector.size()
    my_adaption_vector = ublas_vector;

Since I didn't need adaption_matrix's operator= it isn't implemented yet.

Maybe it's useful for some of you.

MfG Maik Beckmann