1.使用LU分解求解线性方程组Ax=b的解(2019.3.17)
首先创建一个矩阵A,矩阵数据可以使用二维数组作为源数据,使用DenseMatrix.OfArray(double[,] array)
进行创建
double[,] A =
{
{3, 1.4,0,0 },
{1.4, 2, 2.1, 0 },
{0 ,2.1,4,3 },
{0,0,3,3.4 }
};
Matrix<double> matrix = DenseMatrix.OfArray(A);
然后创建列向量b,为DenseVector
对象,可以直接使用new DenseVector(double[] array)
构造
DenseVector b = new DenseVector(new double[] {1, 2, 3, 4});
使用LU()
对matrix进行分解,LU()
返回的对象实现了ISolver<T>
接口,可以直接调用Solve(b)
解出方程
var X = matrix.LU().Solve(b)
这样X
便是方程组AX=b
的解了
2.将Matrix导出成Mat文件,使用Matlab打开(2019/5/9)
要将Matrix导出为Mat文件,首先需要从Nuget安装MathNet.Numerics.Data.Matlab
添加引用MathNet.Numerics.Data.Matlab
在该名称空间下,主要使用
MatlabWriter.Pack(Matrix<T> matrix, string matrixName)
MatlabWriter.Store(string filePath, IEnumerable<MatlabMatrix> matrices)
Ex:
List<MatlabMatrix> ms = new List<MatlabMatrix>
{
MatlabWriter.Pack(A, "A"),
MatlabWriter.Pack(DenseMatrix.OfColumnVectors(b), "b"),
MatlabWriter.Pack(DenseMatrix.OfColumnVectors(x), "x")
};
MatlabWriter.Store(FilePath, ms);