开源Math.NET基础数学类库使用(06)直接求解线性方程组

该文章为转载文章,原文文章地址,请点击此处。

前言

在前几篇关于Math.NET的博客中(见上面链接),主要是介绍了Math.NET中主要的数值功能,并进行了简单的矩阵向量计算例子,接着使用Math.NET的矩阵等对象,对3种常用的矩阵数据交换格式的读写。一方面可以了解Math.NET的使用,另一方面以后也可以直接读取和保存数据为这两种格式,给大家的科研或者工作带来便利。接下来的文章将继续对Math.NET的功能进行讲解和演示,并附带一些数学方面的基础知识。毕竟很多人没有精力去研究Math.NET,那我就把我的研究心得一一写出来,方便后来人。

1.数值分析与线性方程

数值分析的基本含义与特点:

  数值分析(numerical analysis)是研究分析用计算机求解数学计算问题的数值计算方法及其理论的学科,是数学的一个分支,它以数字计算机求解数学问题的理论和方法为研究对象。为计算数学的主体部分。数值分析有如下特点:
1·面向计算机
2·有可靠的理论分析
3·要有好的计算复杂性
4·要有数值实验
5.要对算法进行误差分析  

  数值分析的主要内容:插值法,函数逼近,曲线拟和,数值积分,数值微分,解线性方程组的直接方法,解线性方程组的迭代法,非线性方程求根,常微分方程的数值解法。
  
所以我们今天要解决的就是数值分析的一个很小但又最常接触到的部分,方程(组)的求解。方程(组)的求解有2种方法,一种是直接求解,一种是迭代求解。直接方法比较好理解,相当与使用公式进行直接计算,结果也比较精确。而另外一种是迭代方法。从最初的猜测,迭代方法逐次近似形式收敛只在极限的精确解。

  正如上面所说,方程(组)的形式很多,不同的形式以及实际要求的精度都可以使用不同的方法,而本文主要介绍最简单,也是最常见的线性方程的直接求解方法。
  

2.Math.NET解线性方程源码分析

本文首先对Math.NET中求解线性方程的相关源码进行分析,这样大家碰到了问题,也可以更好的查找源码解决,或者进行扩展,实现自己的一些特殊需求。Math.NET中MathNet.Numerics.LinearAlgebra.Factorization命名空间下有一个泛型接口:ISolver,就是解AX = B类型的线性方程的接口类型。该接口功能很多,看看下面的接口源代码和注释(本人进行了简单的翻译),就很清楚了。

using System;

namespace MathNet.Numerics.LinearAlgebra.Factorization
{
    /// 形如AX = B的线性方程组求解的接口类型
    /// 泛型参数,支持类型有:double, single, , 
    /// 以及 .
    public interface ISolver where T : struct, IEquatable, IFormattable
    {
        /// 求解AX=B的线性方程组
        /// 右矩阵B.
        /// 返回求解结果矩阵 X.
        Matrix Solve(Matrix input);

        /// 求解AX=B的线性方程组
        /// 右矩阵 B.
        /// 求解结果矩阵, X.
        void Solve(Matrix input, Matrix result);

        /// 求解AX=b的线性方程组
        /// 等式的右边向量 b.
        /// 返回求解结果的左边向量 , x.
        Vector Solve(Vector input);

        /// 求解AX=b的线性方程组
        /// 等式的右边向量 b.
        /// 求解结果矩阵, x.
        void Solve(Vector input, Vector result);
    }
}

由于求解线性方程组主要用到了矩阵的分解,Math.NET实现了5种矩阵分解的算法:LU,QR,Svd,Evd,Cholesky。而GramSchmidt是继承QR的,每一个都是实现了ISolver接口,因此就可以直接使用矩阵的分解功能,直接进行线性方程组的求解。为了方便,我们举一个LU的源码例子,简单的看看源码的基本情况:

public abstract class LU : ISolver where T : struct, IEquatable, IFormattable
{
    static readonly T One = BuilderInstance.Matrix.One;

    readonly Lazy> _lazyL;
    readonly Lazy> _lazyU;
    readonly Lazy _lazyP;

    protected readonly Matrix Factors;
    protected readonly int[] Pivots;

    protected LU(Matrix factors, int[] pivots)
    {
        Factors = factors;
        Pivots = pivots;

        _lazyL = new Lazy>(ComputeL);
        _lazyU = new Lazy>(Factors.UpperTriangle);
        _lazyP = new Lazy(() => Permutation.FromInversions(Pivots));
    }

    Matrix ComputeL()
    {
        var result = Factors.LowerTriangle();
        for (var i = 0; i < result.RowCount; i++)
        {
            result.At(i, i, One);
        }
        return result;
    }

    /// Gets the lower triangular factor.
    public Matrix L
    {
        get { return _lazyL.Value; }
    }
    /// Gets the upper triangular factor.
    public Matrix U
    {
        get { return _lazyU.Value; }
    }
    /// Gets the permutation applied to LU factorization.
    public Permutation P
    {
        get { return _lazyP.Value; }
    }
    /// Gets the determinant of the matrix for which the LU factorization was computed.
    public abstract T Determinant { get; }
    public virtual Matrix Solve(Matrix input)
    {
        var x = Matrix.Build.SameAs(input, input.RowCount, input.ColumnCount);
        Solve(input, x);
        return x;
    }    
    public abstract void Solve(Matrix input, Matrix result);

    public virtual Vector Solve(Vector input)
    {
        var x = Vector.Build.SameAs(input, input.Count);
        Solve(input, x);
        return x;
    }    
    public abstract void Solve(Vector input, Vector result);    
    public abstract Matrix Inverse();
}

大家可能会注意到,上面是抽象类,这和Math.NET的实现是有关的。最终都是实现相应版本的Matrix矩阵,然后实现对应版本的类型的分解方法。下面例子会介绍具体使用,大家有疑问,可以拿着源码和例子,调试一番,知道上面的2个实现过程,就比较简单了

3.Math.NET求解线性方程的实例

假设下面是要求解的线性方程组(Ax=b):

5x+2y4z=73x7y+6z=384x+1y+5z=43

测试代码,由于求解的方法很多,只列举了几种,其他的以此类推:

var formatProvider = (CultureInfo) CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";

//先创建系数矩阵A 
var matrixA = DenseMatrix.OfArray(new[,] {{5.00, 2.00, -4.00}, {3.00, -7.00, 6.00}, {4.00, 1.00, 5.00}});

//创建向量b
var vectorB = new DenseVector(new[] {-7.0, 38.0, 43.0});

// 1.使用LU分解方法求解
var resultX = matrixA.LU().Solve(vectorB);
Console.WriteLine(@"1. Solution using LU decomposition");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));

// 2.使用QR分解方法求解
resultX = matrixA.QR().Solve(vectorB);
Console.WriteLine(@"2. Solution using QR decomposition");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));

// 3. 使用SVD分解方法求解
matrixA.Svd().Solve(vectorB, resultX);
Console.WriteLine(@"3. Solution using SVD decomposition");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));

// 4.使用Gram-Shmidt分解方法求解
matrixA.GramSchmidt().Solve(vectorB, resultX);
Console.WriteLine(@"4. Solution using Gram-Shmidt decomposition");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));

// 5.验证结果,就是把结果和A相乘,看看和b是否相等
var reconstructVecorB = matrixA*resultX;
Console.WriteLine(@"5. Multiply coefficient matrix 'A' by result vector 'x'");
Console.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider));

结果如下:

1. Solution using LU decomposition
DenseVector 3-Double
3.00
1.00
6.00

2. Solution using QR decomposition
DenseVector 3-Double
3.00
1.00
6.00

3. Solution using SVD decomposition
DenseVector 3-Double
3.00
1.00
6.00

4. Solution using Gram-Shmidt decomposition
DenseVector 3-Double
3.00
1.00
6.00

5. Multiply coefficient matrix 'A' by result vector 'x'
DenseVector 3-Double
-7.00
38.00
43.00

4.资源

如果本文资源或者显示有问题,请参考 本文原文地址:http://www.cnblogs.com/asxinyu/p/4285245.html

你可能感兴趣的:(C#)