MTL: creation of matrix elements

View: New views
2 Messages — Rating Filter:   Alert me  

MTL: creation of matrix elements

by Michael Smolsky-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you for your insight, Peter.

By looking at your code (contiguous_memory_block.hpp, generic_array<..>::alloc(..)) I've noticed, that you don't initialize matrix elements on allocation. That method seems to be called from some methods of dense2D, such as resize(..). If that is the case, the library won't work with any class, that has a non-trivial default constructor.

Is this by design?

In order to cover classes with non-trivial default constructors (STL has no problem with this), you might want to invoke in-place operator new on the newly allocated memory in generic_array<..>::alloc(..) and perhaps other places as well. If the default constructor of the element type is trivial, the compiler will probably optimize this call out and you won't loose in performance.

Michael.

----- Original Message -----
From: "Peter Gottschling"
To: "General Matrix Template Library \(MTL\) list"
Subject: Re: MTL: Loop-free submatrix extraction
Date: Sat, 22 Dec 2007 17:24:03 +0100

Hi again, Michael,

For dense2D and morton_dense there is a function sub_matrix (shame on me that there is no documentation yet).  You can use it like:
b= sub_matrix(a, begin_row, end_row, begin_column, end_column);

As you see it only supports intervals of rows and columns not arbitrary sets.  The reason is that this can be realized efficiently with a view.

If you are going to write a function I would do something like (only sketch not tested code):

template <typename Matrix, typename RowVector, typename ColVector>
Matrix list_sub_matrix(const Matrix& matrix, const RowVector& row_vector, const ColVector& col_vector)
{
  Matrix s(size(row_vector), size(col_vector));
  {
    matrix::inserter<Matrix>   ins(s);
    for (size_t r= 0; r < size(row_vector); r++)
       for (size_t c= 0; c < size(col_vector); c++)
            ins(r, c) << matrix[row_vector[r]][col_vector[c]];
  }
  return s;
}

Returning a matrix is quite expensive at the moment but will be okay once I've implemented move semantics.  I hope I haven't overseen a detail otherwise you can asked again.  Once you have it done and tested I'd like to put it in the repository.

Good luck and Merry Christmas,
Peter




--
Want an e-mail address like mine?
Get a free e-mail account today at www.mail.com!

_______________________________________________
This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/

Re: MTL: creation of matrix elements

by Neal Becker :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sunday 23 December 2007, Michael Smolsky wrote:
> Thank you for your insight, Peter.
>
> By looking at your code (contiguous_memory_block.hpp,
> generic_array<..>::alloc(..)) I've noticed, that you don't initialize
> matrix elements on allocation. That method seems to be called from some
> methods of dense2D, such as resize(..). If that is the case, the library
> won't work with any class, that has a non-trivial default constructor.
>

If you look at the current boost::ublas, you'll find e.g.:

        unbounded_array (size_type size, const ALLOC &a = ALLOC()):
            alloc_(a), size_ (size) {
          if (size_) {
              data_ = alloc_.allocate (size_);
              if (! detail::has_trivial_constructor<T>::value) {
                  for (pointer d = data_; d != data_ + size_; ++d)
                      alloc_.construct(d, value_type());
 
also:
    namespace detail {

        // specialisation which define whether a type has a trivial
constructor
        // or not. This is used by array types.
        template<typename T>
        struct has_trivial_constructor : public
boost::has_trivial_constructor<T> {};

        template<typename T>
        struct has_trivial_destructor : public
boost::has_trivial_destructor<T> {};

        template<typename FLT>
        struct has_trivial_constructor<std::complex<FLT> > : public
boost::true_type {};
       
        template<typename FLT>
        struct has_trivial_destructor<std::complex<FLT> > : public
boost::true_type {};

    }

I believe this is a good approach
_______________________________________________
This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/