1. Math.NET Numerics(最全面的科学计算库)
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.Statistics;
// 数值积分
double integral = Integrate.OnClosedInterval(x => x * x, 0, 1); // 输出:0.333...
// 线性代数(矩阵运算)
var matrix = Matrix<double>.Build.DenseOfArray(new double[,] {
{1, 2},
{3, 4}
});
var vector = Vector<double>.Build.Dense(new double[] {5, 6});
var result = matrix.Solve(vector); // 解线性方程组 [5,6]
// 统计计算
double[] data = { 1.2, 2.3, 3.4, 4.5 };
double mean = data.Mean(); // 平均值
double stdDev = data.StandardDeviation(); // 标准差
2. ALGLIB(数值分析与优化)
using alglib;
// 求解微分方程
alglib.odesolverstate state;
alglib.odesolverreport rep;
double[] y0 = {1}; // 初始值
alglib.odesolverrkck(y0, 0, (double[] y, double x, double[] dy) => {
dy[0] = -y[0]; // dy/dx = -y
}, out state, out rep);
double[] y1 = new double[1];
alglib.odesolverresults(state, out y1, out rep);
// 快速傅里叶变换(FFT)
double[] signal = {1, 2, 1, 2};
alglib.complex[] spectrum;
alglib.fftr1d(signal, out spectrum); // 输出频域结果
3. Accord.NET(统计与机器学习)
using Accord.Math;
using Accord.Statistics;
// 概率分布
var normal = new Accord.Statistics.Distributions.Univariate.NormalDistribution(mean: 0, stdDev: 1);
double pdf = normal.ProbabilityDensityFunction(0); // 标准正态分布在0处的密度
// 假设检验(T检验)
double[] sample1 = { 5, 7, 9, 6, 8 };
double[] sample2 = { 4, 6, 7, 5, 5 };
var tTest = new TwoSampleTTest(sample1, sample2);
Console.WriteLine($"P值: {tTest.PValue}"); // 判断差异显著性
4. ILNumerics(高性能数值计算)
using ILNumerics;
// 创建三维曲面图
var scene = new ILScene {
new ILPlotCube(twoDMode: false) {
new ILSurface((x, y) =>
Math.Sin(x) * Math.Cos(y),
xmin: -5, xmax: 5,
ymin: -5, ymax: 5
) { Colormap = Colormaps.Jet }
}
};
ILPanel panel = new ILPanel();
panel.Scene = scene; // 显示3D图形
5. NumSharp(类似Python NumPy的API)
using NumSharp;
// 创建NDArray并操作
var array = np.array(new double[] {1, 2, 3, 4});
var matrix = np.arange(9).reshape(3, 3); // 3x3矩阵
// 广播运算
var result = array + 10; // [11,12,13,14]
// 线性代数
var eigenvalues = np.linalg.eigvals(matrix); // 计算特征值
6. 数据可视化(OxyPlot + LiveCharts)
// OxyPlot绘制科学图表
var model = new PlotModel { Title = "函数图像" };
model.Series.Add(new FunctionSeries(Math.Sin, -10, 10, 0.1, "sin(x)"));
PlotView myPlot = new PlotView();
myPlot.Model = model;
// LiveCharts实时数据流
var chart = new CartesianChart {
Series = new SeriesCollection {
new LineSeries {
Values = new ChartValues<double> { 2, 4, 6, 8 }
}
}
};
典型应用场景
-
信号处理:FFT滤波/频谱分析
-
控制系统:微分方程求解/传递函数模拟
-
统计分析:假设检验/回归分析
-
物理仿真:有限元分析(FEM)/计算流体动力学(CFD)
-
金融工程:蒙特卡洛模拟/期权定价
提示:工业场景中常结合OPC UA采集实时数据,用这些库进行在线质量分析(如SPC统计过程控制)或设备健康监测(振动频谱分析)。
各库可通过NuGet安装,推荐组合方案:
-
基础计算:Math.NET + NumSharp
-
专业分析:ALGLIB + Accord.NET
-
可视化:OxyPlot + ScottPlot