/// <summary> /// Creates an upper triangular array of delegates to calculate portions of each element of the hessian matrix /// </summary> protected override void CreateHessianDelegates() { HessianFunctions = new Delegates.DerivativePortionCalculator[pParameters.Length, pParameters.Length]; HessianFunctions[0, 0] = new Delegates.DerivativePortionCalculator(CalculateAADerivateComponent); HessianFunctions[0, 1] = new Delegates.DerivativePortionCalculator(CalculateABDerivativeComponent); HessianFunctions[1, 1] = new Delegates.DerivativePortionCalculator(CalculateBBDerivativeComponent); }
/// <summary> /// Since for sum of squares the method is simply a sum over each of the components, this general method performs the loop /// </summary> /// <param name="Params"></param> /// <param name="calc"></param> /// <returns></returns> private double EvaluateDerivative(double[] Params, Delegates.DerivativePortionCalculator calc) { double sum = 0; for (int i = 0; i < Params.Length; i++) { sum += calc(Params, x[i], y[i]); } return(sum); }
/// <summary> /// Fill in the Hessian matrix for the sum of squares function /// </summary> /// <param name="Params"></param> /// <param name="Hessian"></param> private void EvaluateHessian(double[] Params, ref double func, double[] grad, double[,] Hessian, object obj) { EvaluateSumOfSquaresGradient(Params, ref func, grad, obj); int n = Params.Length; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { Delegates.DerivativePortionCalculator calcDeriv = HessianFunctions[i, j]; Hessian[i, j] = EvaluateDerivative(Params, calcDeriv); if (i != j) { Hessian[j, i] = Hessian[i, j]; } } } }