[eigen] sparse matrix wrapper flavour |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: [eigen] sparse matrix wrapper flavour
- From: "Gael Guennebaud" <gael.guennebaud@xxxxxxxxx>
- Date: Wed, 25 Jun 2008 23:43:20 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=d3fNmNrv1V3d+hgjGsZFnKMrYQrwXxBUxgyGeJmVb/4=; b=qiBWU1wYhjU43eHvB2nz+HAM86/SGju7G8M82Of5D4RVsdUWrKXg3bfd19dhpSfDZK U44vLqRHFmrqsO/LgcWe7CLaZkgcS4ibvTjFvgfFRgVMxAkX6On101+qXRJxtAZMadEs P1BJa/HNQxfFRQ4VjE/WLPjZ+aoI6HqjfOG7g=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=pvkZ+FmD1sACVxSBiApxyKpZ4qThJ1+RQpuiqvUJRakqqiTzsHE68gwJm8ipOp7trH vtcAHUMZYXS5Bode4FzFIO6TyaVZXLrl5iJwz6wO+LFoyYwnO0jFTpH9jPXCuKgaM55a wMJ3f1/bAyRQOtria8PR6fTFL0eQAhiyArzAk=
Hi,
as you might know there is no ideal sparse matrix format, and
according the write/update access pattern you need, you have to use
different implementations or temporary wrappers.
For instance let's assume you have a compressed sparse matrix
(SparseMatrix): the most efficient to use but you can only fill it in
a fully coherent order. So in order to overcome this limitation you
might copy your current matrix to a temporary sparse matrix which
offer a higher degree of flexibility (but which is also slower) like a
one based on std::map (HashMatrix) :
SparseMatrix m;
// do whatever with m
// after that now you need random access:
{
HashMatrix hm(m);
for (...)
hm(rand(),rand()) = rand();
m = hm;
} // hm gets automatically freed
that works, but it's not very convenient especially when you write
generic code and you don't know which kind of matrix you're dealing
with. (FYI, I identified 4 different access patterns, so I'll
implement 4 different sparse matrix types). So here comes the idea of
generic "SparseSetters" or "SparseWrappers" or whatever name. In
practice I imagine something like that:
{
typename SparseWrapper<MyMatrixType, AccessPattern>::type w(m);
// use w according to the access pattern you requested
}
// at the deletion of the wrapper, m is automatically updated.
So if MyMatrixType already provide the access pattern you requested,
then SparseWrapper<MyMatrixType, AccessPattern>::type will degenerate
to a reference to m. Moreover the wrapper should support all matrix
operations, typically you want to be able to write:
wrapper.col(i) += ...;
To avoid redundancy, I see two approaches to implement such wrappers:
1 - they inherit the corresponding sparse matrix class and simply
overload the constructor, destructor and store a reference to m;
2 - they store an object of the corresponding sparse matrix type and
define operator* and operator-> such that they behave like a pointer.
I think both approaches are more or less equivalent:
(1) allows to overwrite some matrix functions if needed,
(2) implies a pointer like style that emphasis the fact your dealing
with something different
so I think that's just a matter of flavour... any opinion ?
cheers,
gael.