Re: [eigen] [Sparse] cwise op |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
Hi,
On Wed, 17 Nov 2010 10:27:25 +0100, Gael Guennebaud
<gael.guennebaud@xxxxxxxxx> wrote:
Hi,
this is already the default behavior, otherwise the result would be
dense, and that would make no sense to use a sparse representation.
That's also why there are currently only a very few cwise binary
operator exposed.
There is also one exception for coeff wise operator* for which we
skip
each pair of elements where one is a zero. Currently, this is
specific
to operator*, but using traits, we could extend it to operator &&
for
boolean matrices and perhaps a few others...
gael
Yes, I understand that you provide the cwiseXXX with the right
traversals, but I think it would be important to allow custom operation
with
binaryExpr() to select also the right traversal for any binary
operator.
Traits could give the cancellation property giving the best traversal.
Btw, with regard to Sparse traversal, I must be missing something
obvious
because I fail to understand why I cannot implement a write of a sparse
into
another sparse (as in block() of dense matrices) as in [0]
Any enlightenment welcome :)
Best Regards,
Bernard
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Sparse>
// write src into dest from pos (r,c)
template<typename Scalar1, typename Scalar2>
void doWrite(Eigen::SparseMatrix<Scalar1> & src, int r, int c,
Eigen::SparseMatrix<Scalar2>& dest)
{
typedef typename Eigen::SparseMatrix<Scalar1>::InnerIterator
srcIt_t;
typedef Eigen::SparseMatrix<Scalar2> dest_t;
for (std::size_t k=0; k != src.outerSize(); ++k)
{
for (srcIt_t it(src,k); it; ++it)
{
dest.insert( it.row()+r, it.col()+c)= it.value();
}
}
dest.finalize();
}
int main(int argc, char* argv[])
{
Eigen::SparseMatrix<double>
ac(4,10);//a/*DenseMatrix::Random(2,2).sparseView()*/);//4,4);
ac.insert(0,1)=1.;
ac.insert(2,9)=3.;
ac.insert(3,4)= 2;
ac.finalize();
Eigen::SparseMatrix<double> dest(4,20);
std::cerr<<"write:\n"<<ac<<std::endl;
doWrite(ac, 0, 0, dest);
std::cout<<dest<<std::endl;
doWrite(ac, 0, 10, dest);
std::cout<<dest<<std::endl;
return 0;
}
outputs :
write:
Nonzero entries:
(1,0) (2,3) (3,2)
Column pointers:
0 0 1 1 1 2 2 2 2 2 $
Nonzero entries:
(1,1) (3,9) (2,4)
Column pointers:
0 1 1 2 $
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 3
0 0 0 0 2 0 0 0 0 0
Nonzero entries:
(1,0) (2,3) (3,2)
Column pointers:
0 0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 $
Nonzero entries:
(1,1) (3,9) (2,4)
Column pointers:
0 1 1 2 $
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0
0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Nonzero entries:
(1,0) (2,3) (3,2) (2,3) (3,2) (0,0)
Column pointers:
0 0 1 1 1 2 2 2 2 2 3 3 4 3 3 4 4 4 4 4 $
Nonzero entries:
(1,1) (3,9) (3,19) (2,4) (2,11) (2,14)
Column pointers:
0 1 1 3 $
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 3
0 0 0 0 2 0 0 0 0 0 0 2 0 0 2 0 0 0 0 0 ←fail ☹