C#矩阵运算类库

时间:2023-11-10 09:32:32

这个类库是本人参考许多相关资料之后做出的C#矩阵运算类库,因为C#的数值计算库相对比较少,所以希望这个类库能够给大家带来一些帮助。

源码github网址:https://github.com/JoshuaHe2015/MatrixLibrary

功能介绍:(持续更新中)

1、矩阵的基本运算:

  矩阵的加、减、乘、除、求逆、求幂、求秩、求行列式、转置。运算包括矩阵与矩阵的运算,矩阵与向量的运算和矩阵与标量的运算。

 using System;
using LinearAlgebra;
namespace MatrixLibraryTest
{
class Program
{
static void Main(string[] args)
{
Matrix A = Matrix.Create(, , new double[] { , , , });
Matrix B = new double[,] {
{ , },
{ , } };
Matrix C = A + B;
Matrix D = A * ;
Matrix E = A * B;
Matrix F = E.Inverse();
Console.WriteLine(C);
Console.WriteLine(D);
Console.WriteLine(E);
Console.WriteLine(F);
Console.ReadKey();
}
}
}

2、矩阵分解:

  LU分解、QR分解

 using System;
using LinearAlgebra;
namespace MatrixLibraryTest
{
class Program
{
static void Main(string[] args)
{
Matrix A = new double[,]
{
{,, },
{,, },
{,, }
};
var lu = A.LU();
Console.WriteLine(lu.L);
Console.WriteLine(lu.U);
Matrix B = new double[,]
{
{,,- },
{,, },
{,-, },
{-,, }
};
var qr = B.QR();
Console.WriteLine(qr.Q);
Console.WriteLine(qr.R);
Console.ReadKey();
}
}
}

3、IO操作:

  支持从文本文件中读取矩阵、将矩阵写入文本文件

 using System;
using LinearAlgebra;
namespace MatrixLibraryTest
{
class Program
{
static void Main(string[] args)
{
Matrix A = Matrix.Load("D:\\mat_A.txt");
Console.WriteLine(A);
Matrix B = Matrix.Random(, );
B.Save("D:\\mat_B");
Console.ReadKey();
}
}
}

4、特殊运算:

  求Hessen Berg矩阵,求解矩阵特征值

5、线性方程组的求解:

  高斯消元法求解线性方程组、QR分解求最小二乘解、共轭梯度法求对称正定方程组6、特殊矩阵:

  生成零矩阵、一矩阵、单位矩阵

7、提取矩阵子集:

  可以提取矩阵的行、列或对角

8、其他:

  支持复数运算与向量运算

参考文献:

  1、数值分析(第5版)/李庆扬 著/清华大学出版社

  2、C#数值计算算法编程/周长发 著/电子工业出版社