我有以下功能
public Func<double[], double> Function { get; set; }
this.Function = (x) =>
Math.Exp(-Math.Pow(x[0] - 1, 2)) + Math.Exp(-0.5 * Math.Pow(x[1] - 2, 2));
和梯度,它是上面函数的偏导数
public Func<double[], double[]> Gradient { get; set; }
this.Gradient = (x) => new double[]
{
// df/dx = -2 * e^(-(x - 1)²)(x - 1).
-2 * Math.Exp(-Math.Pow(x[0] - 1, 2)) * (x[0] - 1),
// df/dy = -e^(-1/2(y - 2)²) * (y - 2).
-Math.Exp(-0.5 * Math.Pow(x[1] - 2, 2)) * (x[1] - 2)
};
采取function
我可以做的否定
Func<double[], double> oldFunc = this.Function;
this.Function = (x) => -oldFunc(x);
我的问题是,如何才能以gradient
相同的方式取反每个偏导数?
using System.Linq;
Func<double[], double[]> oldFunc = this.Gradient;
this.Gradient = (x) => oldFunc(x).Select( y => -y).ToArray();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句