Re: [eigen] Reusing a sparse matrix for SparseLU
• To: eigen <eigen@xxxxxxxxxxxxxxxxxxx>
• Subject: Re: [eigen] Reusing a sparse matrix for SparseLU
• From: Gael Guennebaud <gael.guennebaud@xxxxxxxxx>
• Date: Mon, 3 Dec 2018 12:59:24 +0100
• Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=YDgNk2gM56F6i8g6xeVDk2XXHo/nAaxGVzEnmX2q0so=; b=RXRpiDWGQLUj17YuRCPPBiLmwAGyvtwugqCF31TOTKCFCIr5OV6c9ggJf1GCjDrJPb lnooX3ZA5FsmiemAr2iDM9EZNNYlyl9jNPknOoFkqfPhHgc1GsYFQ7NOqO96kg0wWxBU iQjM0o5CTPWd1N2T1R9ysLGWyVWGkYESzGllfs/ZZnLOtM3k6TpZzNPZzM8NiTSIvtMR 3AY7oYx/Ret24v35QIUEV1BHZtELdYV6FnrI+x64vhHW4eOS0ziqKQQloOnZmzxNwz6i MZegGhUu5zQEcVePf9x05HpQFFAGEDoeQb/185lksNSDF9Gt+YdwKUDH51agxz5/ONtb rYUg==

Hi,

I'm thinking about 3 different ways:

1 - the easiest:

for(...) A.coeffRef(i,j) = new_val(i,j);

but it costs 1 binary search per element

2 - via an iterator over the non-zeros:

for (int k=0; k<A.outerSize(); ++k)
for (SparseMatrix<double>::InnerIterator it(A,k); it; ++it)
it.valueRef() = new_val(it.row(), it.col());

3 - if you don't need the row/col indices:

for(...) A.coeffs()[k] = new_val;

In terms of speed, the options 2 and 3 are equivalent. Then the most appropriate solution depends on how and in which order you can provide the new values.

gael

On Sun, Dec 2, 2018 at 11:06 PM Matthieu Brucher <matthieu.brucher@xxxxxxxxx> wrote:
Hi all,

I want to reuse a sparse matrix structure and a solver for faster linear solve:

Eigen::SparseMatrix<double, Eigen::ColMajor> A;
Eigen::SparseLU<Eigen::SparseMatrix<double, Eigen::ColMajor>, Eigen::COLAMDOrdering<Eigen::Index> > solver;

I set A at the beginning with 0 entries and ask the solver to analyze the pattern.

Now,, I'd like to reuse the matrix instead of creating a new one, as I'm doing real-time computations and don't want any allocation there.

Can I use the comma operator? Or is there another operator that I can use to update the matrix existing entries? I tried to find information online but couldn't find the best practice for this.

Cheers,

Matthieu
--

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