Re: [eigen] Iterators with dense matrices: (was: Areas of eigen3 variation and using eigen2/3 in generic code)

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]



On Sun, Jan 3, 2010 at 7:37 PM, Paul C. Leopardi <paul.leopardi@xxxxxxxxxxxx> wrote:
On Monday 04 January 2010 06:02:43 Benoit Jacob wrote:
> 2010/1/3 Jesse Perla <jesseperla@xxxxxxxxx>:
> Indeed, we do not have any iterators in Eigen for dense matrices and
> vectors. Do you have a use case for that? So far, index-based access
> has been all we have needed, as we were 100% focused on linear
> algebra.

Hi all,
Sorry to break into the discussion at this point, but I am also thinking of
migrating from uBLAS to Eigen. My GluCat code ( http://glucat.sf.net )
compiles with an optional macro definition, GLUCAT_USE_DENSE_MATRICES
which is used as follows.

I think that Paul's example  is a perfect one.  I want to be able to rapidly fly through dense or sparse matrices with iterator arithmetic instead of jumping to indices.

As for other examples of where iterators are important for vectors (1xN or Nx1), there are too many to count... The std idiom is pretty pervasive in everything I do, and I know if I start using  Vector4f, etc. as a container I will want to use the algorithms.

But to give a few examples:

//std::copy for getting data into a vec from another container.
ublas::vector<double> ublas_vec(4);
std::vector<double> std_vec(4)
Vector4i eig_vec;
std::copy(ublas_vec.begin(), ublas_vec.end(), eig_vec..begin());

//Or with future ranges inside of STD it could look something like:
std::copy(std_vec, std::begin(eig_vec));


//Or as another example, a rough generic geometric mean function that works on the Container Concept.
template<typename Vector>
typename Vector::value_type geometric_mean(const Vector& vec)
{
typedef typename Vector::value_type value_type;
return std::pow(std::accumulate(vec.begin(), vec.end(), value_type(1), std::multiplies<value_type>()), 1.0/vec.size());; //nth root of reduction over multiplication
}

As for other algorithms I use all the time: std::accumulate for a reduction, std's binary search, etc.

Not to mention, that many parallel libraries are following the std iterator patterns (as one example, see the par_for in http://msdn.microsoft.com/en-us/magazine/dd434652.aspx)

Note that with ublas, etc. a Vector is a different concept than a Matrix, so it makes sense to have it act like a normal std Container (e.g. size(), .begin(), .end()). So it would need a little metaprogramming work to think through.

One suggestion from boost::multi_array is that their .begin() returns 1 dimension less. So there a 3d tensor returns an iterator over matrices, calling .begin() on the matrix returns an iterator over either rows or columns, and calling .begin() to get an iterator on a row/column returns an iterator over the scalar values. This isn't exactly what Paul is talking about, but I think it is possible to get the concepts talking.




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/