C#矩陣運算類庫


這個類庫是本人參考許多相關資料之后做出的C#矩陣運算類庫,因為C#的數值計算庫相對比較少,所以希望這個類庫能夠給大家帶來一些幫助。

源碼github網址:https://github.com/JoshuaHe2015/MatrixLibrary

功能介紹:(持續更新中)

1、矩陣的基本運算:

  矩陣的加、減、乘、除、求逆、求冪、求秩、求行列式、轉置。運算包括矩陣與矩陣的運算,矩陣與向量的運算和矩陣與標量的運算。

 1 using System;
 2 using LinearAlgebra;
 3 namespace MatrixLibraryTest
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Matrix A = Matrix.Create(2, 2, new double[] { 1, 2, 3, 4 });
10             Matrix B = new double[,] {
11                 { 5, 6 },
12                 { 7, 8 } };
13             Matrix C = A + B;
14             Matrix D = A * 2;
15             Matrix E = A * B;
16             Matrix F = E.Inverse();
17             Console.WriteLine(C);
18             Console.WriteLine(D);
19             Console.WriteLine(E);
20             Console.WriteLine(F);
21             Console.ReadKey();
22         }
23     }
24 }

 

2、矩陣分解:

  LU分解、QR分解

 1 using System;
 2 using LinearAlgebra;
 3 namespace MatrixLibraryTest
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Matrix A = new double[,]
10             {
11               {1,2,3 },
12               {2,5,2 },
13               {3,1,5 }
14             };
15             var lu = A.LU();
16             Console.WriteLine(lu.L);
17             Console.WriteLine(lu.U);
18             Matrix B = new double[,]
19             {
20                 {1,1,-1 },
21                 {2,1,0 },
22                 {1,-1,0 },
23                 {-1,2,1 }
24             };
25             var qr = B.QR();
26             Console.WriteLine(qr.Q);
27             Console.WriteLine(qr.R);
28             Console.ReadKey();
29         }
30     }
31 }

 

3、IO操作:

  支持從文本文件中讀取矩陣、將矩陣寫入文本文件

 1 using System;
 2 using LinearAlgebra;
 3 namespace MatrixLibraryTest
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Matrix A = Matrix.Load("D:\\mat_A.txt");
10             Console.WriteLine(A);
11             Matrix B = Matrix.Random(3, 3);
12             B.Save("D:\\mat_B");
13             Console.ReadKey();
14         }
15     }
16 }

 

4、特殊運算:

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

5、線性方程組的求解:

  高斯消元法求解線性方程組、QR分解求最小二乘解、共軛梯度法求對稱正定方程組6、特殊矩陣:

  生成零矩陣、一矩陣、單位矩陣

7、提取矩陣子集:

  可以提取矩陣的行、列或對角

8、其他:

  支持復數運算與向量運算

 

參考文獻:

  1、數值分析(第5版)/李慶揚 著/清華大學出版社

  2、C#數值計算算法編程/周長發 著/電子工業出版社

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM