This project has moved and is read-only. For the latest updates, please go here.

Setting the Starting Vector for Iterative Solvers

Dec 18, 2013 at 12:08 AM
Hi guys,

I recently inherited some Code at work, I'm however quite new to the Math.Net Numerics library. I want to solve a System of linear equations Ax=b, and I know quite a lot about the form of the matrix, my vector b and have a relatively precise guess for x. Hence I would like to use an Iterative Solver and use my prediction for x as a starting value. Is this possible in Math.Net? I looked in the Documentation but could not find any Info on setting Initial Values. I really hope I just overlooked this, since my tests in R show that this initial guess speeds up convergence quite a bit.

I'd be really happy for some info,

Florian
Dec 18, 2013 at 12:15 PM
Edited Dec 18, 2013 at 12:17 PM
Hi Florian,

The iterative solvers accept a result vector, where the resulting solution is then put into. This vector is effectively also used as initial guess, although note that its values will be overwritten. Unfortunately this is not exposed in the Matrix.SolverIterative routines, but works when you use the solvers directly. Example in LINQPad (with its Dump methods):
var A = Matrix<double>.Build.SparseOfArray(new double[,] { { 1, 1, 1 }, { 1, 2, 3 }, { 1, 3, 6 } });
var b = Vector<double>.Build.Dense(new double[] {3, 1, 4});

//var x = Vector<double>.Build.Dense(A.RowCount);
var x = Vector<double>.Build.Dense(new double[] { 10.5, -12.5, 5.5 });

var preconditioner = new MILU0Preconditioner();
//var preconditioner = new UnitPreconditioner<double>();
var iterator = new Iterator<double>(new IIterationStopCriterium<double>[]
   {
       new IterationCountStopCriterium<double>(100),
       new ResidualStopCriterium<double>(1e-12),
       new DivergenceStopCriterium<double>(),
       new FailureStopCriterium<double>()
   });
   
var solver = new BiCgStab();
solver.Solve(A, b, x, iterator, preconditioner);

iterator.Dump();
x.Dump();

A.LU().Solve(b).Dump("Verify with LU decompositon");
A.QR().Solve(b).Dump("Verify with QR decompositon");
Thanks,
Christoph
Marked as answer by cdrnet on 12/30/2013 at 6:00 AM
Dec 18, 2013 at 12:23 PM
Edited Dec 18, 2013 at 12:25 PM
NB: we've had other requests for simpler manual iteration (e.g. to apply just a few iterations), so this is one of the areas we want to improve in v3.

PS: the code above is against current master (roughly v3.0.0-alpha6)
Dec 18, 2013 at 12:58 PM
Great! That's exactly what I was hoping for.

Thank you very much!