AutoDiff.NET是一个纯.NET库,用于开发人员轻松地构成函数符号化,然后自动计算在任何给定的点函数值和梯度。它适用于结合一个基于梯度的优化库,已经在Mono 2.10 Linux和.NET 4 Windows上测试成功。
示例代码:
using AutoDiff; class Program { public static void Main(string[] args) { // define variables var x = new Variable(); var y = new Variable(); var z = new Variable(); // define our function var func = (x + y) * TermBuilder.Exp(z + x * y); // prepare arrays needed for evaluation/differentiation Variable[] vars = { x, y, z }; double[] values = {1, 2, -3 }; // evaluate func at (1, 2, -3) double value = Evaluator.Evaluate(func, vars, values); // calculate the gradient at (1, 2, -3) double[] gradient = Differentiator.Differentiate(func, vars, values); // print results Console.WriteLine("The value at (1, 2, -3) is " + value); Console.WriteLine("The gradient at (1, 2, -3) is ({0}, {1}, {2})", gradient[0], gradient[1], gradient[2]); } }