Quantcast
Channel: Math.NET Numerics
Viewing all articles
Browse latest Browse all 971

New Post: How to load data into matrix

$
0
0

When solving a linear system Ax = b you can either choose a direct solver (like LU factorization) which will compute the exact solution, or use an iterative solver, which will compute an approximate solution. Read more about the possible choices athttp://scicomp.stackexchange.com/a/378

Here's some code which should explain how to use iterative solvers with Math.NET:

using System.Collections.Generic;
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.Iterative;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Generic.Solvers.StopCriterium;

///<summary>/// Example code for using iterative solvers.///</summary>publicclass Example
{
    publicstatic Vector Solve()
    {
        // Parallel is actually slower, so disable!
        MathNet.Numerics.Control.DisableParallelization = true;

        // Choose your solver: BiCgStab, TFQMR, GpBiCg or MlkBiCgStab.// BiCgStab is usually a good choice
        BiCgStab solver = new BiCgStab();

        // Choose stop criteriasvar stopCriterias = new List<IIterationStopCriterium>()
        {
            new ResidualStopCriterium(1e-5),
            new IterationCountStopCriterium(1000),
            new DivergenceStopCriterium(),
            new FailureStopCriterium()
        };

        // Set iterator
        solver.SetIterator(new Iterator(stopCriterias));

        // If necessary, choose a preconditioner (IncompleteLU// and Ilutp are slow, so try Diagonal first).// solver.SetPreconditioner(new Diagonal());var A = LoadMatrix();
        var b = LoadRhs();

        var solution = solver.Solve(A, b);

        bool success = false;

        foreach (var item in stopCriterias)
        {
            if (item.StopLevel == StopLevel.Convergence)
            {
                success = true;
                break;
            }
        }

        if (!success)
        {
            // Try another solver, a different preconditioner or// less restrictive stop criterias.
        }

        var residual = A * solution - b;

        // Check residual// double error = residual.Norm(2);return (Vector)solution;
    }

    privatestatic DenseVector LoadRhs()
    {
        var b = new DenseVector(4);

        b[0] = 1;
        b[1] = 1;
        b[2] = 1;
        b[3] = 1;

        return b;
    }

    privatestatic SparseMatrix LoadMatrix()
    {
        var matrix = new SparseMatrix(4, 4);

        matrix.At(0, 0, 4.5);
        matrix.At(1, 1, 2.9);
        matrix.At(2, 2, 3.0);
        matrix.At(3, 3, 1.0);
        matrix.At(0, 2, 3.2);
        matrix.At(1, 0, 3.1);
        matrix.At(1, 3, 0.9);
        matrix.At(2, 1, 1.7);
        matrix.At(3, 0, 3.5);
        matrix.At(3, 1, 0.4);

        return matrix;
    }
}

Viewing all articles
Browse latest Browse all 971

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>