MathNet矩阵分解_c# math.net解线性方程_islinyoubiao的博客-程序员宅基地

技术标签: C#  SVD  QR  CSharp自动化实践  矩阵分解  LU  

矩阵分解是解线性方程组的一个方法,比较容易。

有LU,QR,SVD等等方法。在c#中用MathNet操作起来也是比较简单。

https://blog.csdn.net/c914620529/article/details/50393223/

https://www.cnblogs.com/asxinyu/p/4285245.html

https://www.cnblogs.com/asxinyu/p/Dotnet_Opensource_MathNet_Basic_Statistics_10.html

程序如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathNet.Numerics.LinearAlgebra.Double;

namespace SimpleCalcModule
{
    static public class MatrixCalc
    {
        //LU
        public static bool calc1(int n, double[,] A, double[] b, ref double[] X)
        {
            if (n < 3)
                return false;
            if (b.Length != n)
                return false;
            if (A.GetLength(0) != n && A.GetLength(1) != n)
                return false;
            try
            {
                var matrixA = new DenseMatrix(n, n);
                var vectorB = new DenseVector(n);
                for (int i = 0; i < n; i++)
                {
                    vectorB[i] = b[i];
                    X[i] = .0;
                    for (int j = 0; j < n; j++)
                    {
                        matrixA[i, j] = A[i, j];
                    }
                }
                //1.LU
                var resultX = (DenseVector)matrixA.LU().Solve(vectorB);
                X = resultX.ToArray();
                if (X.Length == n)
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
            
        }
        //QR
        public static bool calc2(int n, double[,] A, double[] b, ref double[] X)
        {
            if (n < 3)
                return false;
            if (b.Length != n)
                return false;
            if (A.GetLength(0) != n && A.GetLength(1) != n)
                return false;
            try
            {
                var matrixA = new DenseMatrix(n, n);
                var vectorB = new DenseVector(n);
                for (int i = 0; i < n; i++)
                {
                    vectorB[i] = b[i];
                    X[i] = .0;
                    for (int j = 0; j < n; j++)
                    {
                        matrixA[i, j] = A[i, j];
                    }
                }
                //1.LU
                var resultX = (DenseVector)matrixA.QR().Solve(vectorB);
                X = resultX.ToArray();
                if (X.Length == n)
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }
        //SVD
        public static bool calc3(int n, double[,] A, double[] b, ref double[] X)
        {
            if (n < 3)
                return false;
            if (b.Length != n)
                return false;
            if (A.GetLength(0) != n && A.GetLength(1) != n)
                return false;
            try
            {
                var matrixA = new DenseMatrix(n, n);
                var vectorB = new DenseVector(n);
                for (int i = 0; i < n; i++)
                {
                    vectorB[i] = b[i];
                    X[i] = .0;
                    for (int j = 0; j < n; j++)
                    {
                        matrixA[i, j] = A[i, j];
                    }
                }
                //1.LU
                var resultX = (DenseVector)matrixA.Svd().Solve(vectorB);
                X = resultX.ToArray();
                if (X.Length == n)
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }
        //Gram-Shmidt
        public static bool calc4(int n, double[,] A, double[] b, ref double[] X)
        {
            if (n < 3)
                return false;
            if (b.Length != n)
                return false;
            if (A.GetLength(0) != n && A.GetLength(1) != n)
                return false;
            try
            {
                var matrixA = new DenseMatrix(n, n);
                var vectorB = new DenseVector(n);
                for (int i = 0; i < n; i++)
                {
                    vectorB[i] = b[i];
                    X[i] = .0;
                    for (int j = 0; j < n; j++)
                    {
                        matrixA[i, j] = A[i, j];
                    }
                }
                //1.LU
                var resultX = (DenseVector)matrixA.GramSchmidt().Solve(vectorB);
                X = resultX.ToArray();
                if (X.Length == n)
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }
    }
}

测试函数
 

if (commOperate == 99)
            {
                //double[,] a = { { 8.1, 2.3, -1.5, 6.1 }, { 0.5, -6.23, 0.87, 2.3 }, { 2.5, 1.5, 10.2, 1.8 } };
                //double[,] a = { { 2, -1, 3, 1 }, { 4, 2, 5, 4 }, { 1, 2, 0, 7 } };

                double[,] A = { { .0, .0, 1.0 }, { 50.0, .0, 1.0 }, { .0, 50.0, 1.0 } };
                int n = A.GetLength(0);//数组a的第一维长度,即行数,3
                double[] b1 = new double[n];//存放解的数组,初始值为0
                double[] b2 = new double[n];//存放解的数组,初始值为0
                b1[0] = 37.5;
                b1[1] = 62.5;
                b1[2] = 37.5;
                double[] X1 = new double[n];
                double[] X2 = new double[n];
                double[] X3 = new double[n];
                double[] X4 = new double[n];
                b2[0] = 38.75;
                b2[1] = 38.75;
                b2[2] = 63.75;
                double[] X5 = new double[n];
                double[] X6 = new double[n];
                double[] X7 = new double[n];
                double[] X8 = new double[n];
                bool rst1 = SimpleCalcModule.MatrixCalc.calc1(n, A, b1, ref X1);
                bool rst2 = SimpleCalcModule.MatrixCalc.calc2(n, A, b1, ref X2);
                bool rst3 = SimpleCalcModule.MatrixCalc.calc3(n, A, b1, ref X3);
                bool rst4 = SimpleCalcModule.MatrixCalc.calc4(n, A, b1, ref X4);
                bool rst5 = SimpleCalcModule.MatrixCalc.calc1(n, A, b2, ref X5);
                bool rst6 = SimpleCalcModule.MatrixCalc.calc2(n, A, b2, ref X6);
                bool rst7 = SimpleCalcModule.MatrixCalc.calc3(n, A, b2, ref X7);
                bool rst8 = SimpleCalcModule.MatrixCalc.calc4(n, A, b2, ref X8);
                double x4 = 50;
                double y4 = 50;
                double Tx1 = X1[0] * x4 + X1[1] * y4 + X1[2];
                double Tx2 = X2[0] * x4 + X2[1] * y4 + X2[2];
                double Tx3 = X3[0] * x4 + X3[1] * y4 + X3[2];
                double Tx4 = X4[0] * x4 + X4[1] * y4 + X4[2];

                double Ty1 = X5[0] * x4 + X5[1] * y4 + X5[2];
                double Ty2 = X6[0] * x4 + X6[1] * y4 + X6[2];
                double Ty3 = X7[0] * x4 + X7[1] * y4 + X7[2];
                double Ty4 = X8[0] * x4 + X8[1] * y4 + X8[2];

            }

多谢,亲爱的美美。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/islinyoubiao/article/details/114555908

智能推荐

HTML5日期输入类型(date)_html5 date-程序员宅基地

HTML5让Web程序员的工作变得异常简单,我们得到的不仅仅只有一个“日期”类型的input,还有一系列相关的日期、时间参数让我们自定义。我们虽然称之为“日期”类型,但这里的type实际上是可以为“date”、“week”、“month”、“time”、“datetime”和“datetime-local”。下面我将用实例加图文的方式向大家演示各种type的外观表现。1. 日期()_html5 date

执行jmeter_server.bat时出现‘findstr‘ 不是内部或外部命令,也不是可运行的程序或批处理文件_findstr' 不是内部或外部命令,也不是可运行的程序 winserver2008-程序员宅基地

打开cmd,执行命令findstr /?说明C:\Windows\System32\findstr.exe未生效,因安装极少数软件,导致path值被篡改检查环境变量里的path加上%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\如果还是不行,环境变量path中再加上C:\windows\system32亲测有效..._findstr' 不是内部或外部命令,也不是可运行的程序 winserver2008

linux c++操作mysql数据库_Linux下C++连MySQL数据库-程序员宅基地

1.查看本地有没有安装mysql,命令就是mysql,如果有这个命令就表示安装了mysql数据库软件。如果没有就自行安装。MySQL-server-4.0.16-0.i386.rpmMySQL-client-4.0.16-0.i386.rpm2.查看本地有没有安装mysql的开发包。命令是 `mysql_config--cflags --libs` 如果有安装就会有一串编译选项和连接库。如果没有就..._mysql lib linux c++

DispSync 分析-程序员宅基地

在SurfaceFlinger的构造方法中调用了DispSync的init方法对DispSync进行初始化,mPrimaryDispSync.init(hasSyncFramework, dispSyncPresentTimeOffset);调用流程图如下,DispSync的构造方法如下,DispSync::DispSync(const char* name) : mName(name),..._dispsync

ExtJS用Grid显示数据后如何自动选取第一条记录-程序员宅基地

用Grid显示数据后,如何让系统自动选取第一条记录呢?在显示Grid时由于其Store正在loading,没法在Grid选取第一条记录,因为还没有记录,所以应在其Store进行操作。查看Ext.data.Store的load()方法如下:load( [options] )Loads data into the Store via the configured proxy

随便推点

废弃原因-程序员宅基地

主要原因 as follows : 1、注册版wordPress连rar都不让上传,十分尴尬,劳动成果没有地方放。 2、WordPress不是专业程序员论坛,高手并不多,看的人就不用提了,少得可怜。 3、也应该换一个简单、朴素、功能强大(并不是WordPress功能不强)、较专业、环保的blog用下了。 4、特别喜欢之处在于,..._marquee被遗弃原因

arrayfire的工程创建例子_arrayfire如何编译-程序员宅基地

首先介绍如何操作以下内容:(推荐) 如果需要移动项目执行需要设定:1.设定AF_PATH 环境变量. 通常指定要连接的 ArrayFire headers和 libraries (libaf.lib, libafGFX.lib).2.添加 ArrayFire DLLs 到PATH 环境变量a.Win64: %AF_PATH%/examples/bin_x64b.Win32:_arrayfire如何编译

ListView动态更新item的例子-程序员宅基地

1.ListViewUpdate.java文件:[code="java"]package com.example;import java.util.Random;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.On...

Oracle字符集的设置_oracle数据库字符集怎么设置-程序员宅基地

文章目录一、字符集的概念1、字符编码(character encoding):2、字符集(Character set)3、国际编码(Unicode)4、汉字的编码5、汉字的编码选择6、编码的转换二、Oracle的字符集1、字符集和国家字符集2、支持中文的字符集3、NLS_LANG参数三、服务端的字符集1、查看服务端字符集2、修改服务端字符集四、客户端的字符集1、Linux环境2、Windows环境..._oracle数据库字符集怎么设置

python2和python3中TestSuite().addTest的区别-程序员宅基地

Python2中unittest.TestSuite().addTest()的参数是这样的:unittest.TestSuite().addTest(TestFun("test_nam"));其中TestFun是继承于TestCase的类,test_name是里面的测试函数。然后运行传list当参数: name_list=[TestFun("test_nam01"...

python实现概率统计_机器学习中的概率统计:Python语言描述-程序员宅基地

机器学习中的概率统计:Python语言描述作者:张雨萌 著出版日期:2020年12月文件大小:19.93M支持设备:¥50.00在线试读适用客户端:言商书局iPad/iPhone客户端:下载 Android客户端:下载PC客户端:下载更多详情:查看?对图书下载、阅读卡购买有疑问:立即进入帮助中心>>图书简介目录本书围绕机器学习算法中涉及的概率统计知识展开介绍,沿着概率思想、变量分布、参..._概率统计融合python