private static void CalculateDescentDirection(Gradient gradient, Vector currentGuessedRoot, double sigma, Vector d) { Vector xkPlusSigmaKTimesDk = LinearAlgebraOperations.AddVectorToVector(currentGuessedRoot, LinearAlgebraOperations.MultiplyVectorWithScalar(d, sigma)); double normalFunctionResult = gradient.func.Solve(currentGuessedRoot[0], currentGuessedRoot[1]); double changeFunctionResult = gradient.func.Solve(xkPlusSigmaKTimesDk[0], xkPlusSigmaKTimesDk[1]); while (changeFunctionResult > normalFunctionResult && sigma > 0 && (d[0] < 0 && d[1] < 0)) { xkPlusSigmaKTimesDk = LinearAlgebraOperations.AddVectorToVector(currentGuessedRoot, LinearAlgebraOperations.MultiplyVectorWithScalar(d, sigma)); normalFunctionResult = gradient.func.Solve(currentGuessedRoot[0], currentGuessedRoot[1]); changeFunctionResult = gradient.func.Solve(xkPlusSigmaKTimesDk[0], xkPlusSigmaKTimesDk[1]); sigma -= 0.05; for (int i = 0; i < d.Size; i++) { d[i] += 0.05; } } }
public static Vector CalculateRootWithConjugateGradientMethod(Gradient gradient, Vector rootPoint) { Vector previousCalculatedRoot = new Vector(new double[rootPoint.Size]); Vector gradientResult = gradient.Solve(rootPoint[0], rootPoint[1]); d = new Vector(new double[] { -1, -1 }); sigma = 0.5; while (!gradientResult.Equals(new Vector(new double[] { 0, 0 }))) { CalculateDescentDirection(gradient, rootPoint, sigma, d); previousCalculatedRoot = rootPoint; rootPoint = LinearAlgebraOperations.AddVectorToVector(rootPoint, LinearAlgebraOperations.MultiplyVectorWithScalar(d, sigma)); gradientResult = gradient.Solve(rootPoint[0], rootPoint[1]); } return(rootPoint); }