public void CubicPolynomialFunctionValueTest() { Func<double, double> f = x => 3 * Math.Pow(x, 3) + 2 * x - 6; var current = f(2); var df = new NumericalDerivative(3, 0); Assert.AreEqual(38, df.EvaluateDerivative(f, 2, 1, current), 1e-8); }
public void CreateDerivativeFunctionHandleTest() { Func<double, double> f = x => 3 * Math.Pow(x, 3) + 2 * x - 6; var nd = new NumericalDerivative(5, 2); var df = nd.CreateDerivativeFunctionHandle(f, 3); Assert.AreEqual(18, df(0)); // Test new function with same nd class Func<double, double> f2 = x => 2 * Math.Pow(x, 3) + 2 * x - 6; var df2 = nd.CreateDerivativeFunctionHandle(f2, 3); Assert.AreEqual(12, df2(0)); // Original delegate not changed Assert.AreEqual(18, df(0)); }
public Surface(VectorFunction r) { this.r = r; List<CommonFunction> components = r.components; List<DefinedCommonFunction> derivativesU = new List<DefinedCommonFunction>(); List<DefinedCommonFunction> derivativesV = new List<DefinedCommonFunction>(); NumericalDerivative derivative = new NumericalDerivative(); derivativesU.Add(new DefinedCommonFunction(derivative.CreatePartialDerivativeFunctionHandle(components[0].getCommonFunction(), 0, 1))); derivativesV.Add(new DefinedCommonFunction(derivative.CreatePartialDerivativeFunctionHandle(components[0].getCommonFunction(), 1, 1))); derivativesU.Add(new DefinedCommonFunction(derivative.CreatePartialDerivativeFunctionHandle(components[1].getCommonFunction(), 0, 1))); derivativesV.Add(new DefinedCommonFunction(derivative.CreatePartialDerivativeFunctionHandle(components[1].getCommonFunction(), 1, 1))); derivativesU.Add(new DefinedCommonFunction(derivative.CreatePartialDerivativeFunctionHandle(components[2].getCommonFunction(), 0, 1))); derivativesV.Add(new DefinedCommonFunction(derivative.CreatePartialDerivativeFunctionHandle(components[2].getCommonFunction(), 1, 1))); VectorFunction v1 = new VectorFunction(derivativesU); VectorFunction v2 = new VectorFunction(derivativesV); VectorFunction normalLength = new Vector3Mul(v1, v2); CommonFunction length = new Length(normalLength); List<bool> normalPow = new List<bool>(); normalPow.Add(true); normalPow.Add(false); List<CommonFunction> normalx = new List<CommonFunction>(); normalx.Add(normalLength.components[0]); normalx.Add(length); List<CommonFunction> normaly = new List<CommonFunction>(); normaly.Add(normalLength.components[1]); normaly.Add(length); List<CommonFunction> normalz = new List<CommonFunction>(); normalz.Add(normalLength.components[2]); normalz.Add(length); List<CommonFunction> normalCoordinates = new List<CommonFunction>(); normalCoordinates.Add(new Mul(normalx, normalPow)); normalCoordinates.Add(new Mul(normaly, normalPow)); normalCoordinates.Add(new Mul(normalz, normalPow)); normal = new VectorFunction(normalCoordinates); }
public void CubicPolynomialThirdDerivativeAtAnyValTest() { Func<double, double> f = x => 3 * Math.Pow(x, 3) + 2 * x - 6; var df = new NumericalDerivative(5, 2); Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3)); Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3)); df.Center = 0; Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3)); Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3)); df.Center = 1; Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3)); Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3)); df.Center = 2; Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3)); Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3)); df.Center = 3; Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3)); Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3)); df.Center = 4; Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3)); Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3)); }
/// <summary> /// Creates a numerical Jacobian with a specified differentiation scheme. /// </summary> /// <param name="points">Number of points for Jacobian evaluation.</param> /// <param name="center">Center point for differentiation.</param> public NumericalJacobian(int points, int center) { _df = new NumericalDerivative(points, center); }
/// <summary> /// Creates a numerical Hessian with a specified differentiation scheme. /// </summary> /// <param name="points">Number of points for Hessian evaluation.</param> /// <param name="center">Center point for differentiation.</param> public NumericalHessian(int points, int center) { _df = new NumericalDerivative(points, center); }
public void VectorFunctionMixedPartialDerivativeTest() { Func<double[], double>[] f = { (x) => Math.Pow(x[0],2) - 3*x[1], (x) => x[1]*x[1] + 2*x[0]*x[1] }; var x0 = new double[] { 2, 2 }; var df = new NumericalDerivative(); Assert.AreEqual(new double[] { 0, 2 }, df.EvaluateMixedPartialDerivative(f, x0, new int[] { 0, 1 }, 2)); Assert.AreEqual(new double[] { 0, 2 }, df.EvaluateMixedPartialDerivative(f, x0, new int[] { 1, 0 }, 2)); }
public void SinFirstDerivativeAtZeroTest() { Func<double, double> f = Math.Sin; var df = new NumericalDerivative(); Assert.AreEqual(1, df.EvaluateDerivative(f, 0, 1), 1e-10); }
public void VectorFunction1PartialDerivativeTest() { Func<double[], double>[] f = { (x) => Math.Pow(x[0],2) - 3*x[1], (x) => x[1]*x[1] + 2*x[0]*x[1] }; var x0 = new double[] { 2, 2 }; var g = new double[] { 4, 4 }; var df = new NumericalDerivative(); Assert.AreEqual(g, df.EvaluatePartialDerivative(f, x0, 0, 1)); Assert.AreEqual(new double[] { 2, 0 }, df.EvaluatePartialDerivative(f, x0, 0, 2)); Assert.AreEqual(new double[] { -3, 8 }, df.EvaluatePartialDerivative(f, x0, 1, 1)); }
public void RosenbrockFunctionMixedDerivativeTwoVariableSecondOrderTest() { Func<double[], double> f = x => Math.Pow(1 - x[0], 2) + 100 * Math.Pow(x[1] - Math.Pow(x[0], 2), 2); var df = new NumericalDerivative(); var x0 = new double[] { 2, 2 }; var parameterIndex = new[] { 0, 1 }; Assert.AreEqual(-800, df.EvaluateMixedPartialDerivative(f, x0, parameterIndex, 2)); }
public void RosenbrockFunctionMixedDerivativeOneVariableSecondOrderTest() { Func<double[], double> f = x => Math.Pow(1 - x[0], 2) + 100 * Math.Pow(x[1] - Math.Pow(x[0], 2), 2); var df = new NumericalDerivative(); var x0 = new double[] { 2, 2 }; var parameterindex = new int[] { 0, 0 }; Assert.AreEqual(1602, df.EvaluatePartialDerivative(f, x0, 0, 1), 1e-6); Assert.AreEqual(4002, df.EvaluateMixedPartialDerivative(f, x0, parameterindex, 2)); }
public void ExponentialFunctionPartialDerivativeCurrentValueTest() { //Test Function Func<double[], double> f = (x) => Math.Sin(x[0] * x[1]) + Math.Exp(-x[0] / 2) + x[1] / x[0]; //Analytical partial dfdx Func<double[], double> dfdx = (x) => Math.Cos(x[0] * x[1]) * x[1] - Math.Exp(-x[0] / 2) / 2 - x[1] / Math.Pow(x[0], 2); //Analytical partial dfdy Func<double[], double> dfdy = (x) => Math.Cos(x[0] * x[1]) * x[0] + 1 / x[0]; // Current value var x1 = new double[] { 3, 3 }; var current = f(x1); var df = new NumericalDerivative(5, 2); Assert.AreEqual(dfdx(x1), df.EvaluatePartialDerivative(f, x1, 0, 1, current), 1e-8); Assert.AreEqual(dfdy(x1), df.EvaluatePartialDerivative(f, x1, 1, 1, current), 1e-8); }
public void ExponentialFunctionPartialDerivativeTest() { //Test Function Func<double[], double> f = (x) => Math.Sin(x[0] * x[1]) + Math.Exp(-x[0] / 2) + x[1] / x[0]; //Analytical partial dfdx Func<double[], double> dfdx = (x) => Math.Cos(x[0] * x[1]) * x[1] - Math.Exp(-x[0] / 2) / 2 - x[1] / Math.Pow(x[0], 2); //Analytical partial dfdy Func<double[], double> dfdy = (x) => Math.Cos(x[0] * x[1]) * x[0] + 1 / x[0]; var df = new NumericalDerivative(3, 1); var x1 = new double[] { 3, 3 }; Assert.AreEqual(dfdx(x1), df.EvaluatePartialDerivative(f, x1, 0, 1), 1e-8); Assert.AreEqual(dfdy(x1), df.EvaluatePartialDerivative(f, x1, 1, 1), 1e-8); var x2 = new double[] { 300, -50 }; df.StepType = StepType.Absolute; Assert.AreEqual(dfdx(x2), df.EvaluatePartialDerivative(f, x2, 0, 1), 1e-5); Assert.AreEqual(dfdy(x2), df.EvaluatePartialDerivative(f, x2, 1, 1), 1e-2); }