List of usage examples for org.apache.commons.math.analysis.solvers NewtonSolver NewtonSolver
@Deprecated
public NewtonSolver()
From source file:cz.cuni.mff.d3s.spl.example.newton.app.Main.java
public static void main(String[] args) { try {/*from ww w. j a v a 2 s .co m*/ System.out.printf("Letting things settle down before actual computation.\n"); Thread.sleep(1000 * 1); } catch (InterruptedException ignored) { } /* This seed gives reasonable data, keep it at that ;-). */ Random random = new Random(2); double[] coefficients = generateCoefficients(random); PolynomialFunction function = new PolynomialFunction(coefficients); NewtonSolver solver = new NewtonSolver(); System.out.printf("Will solve polynomial function of degree %d.\n", function.degree()); inspectClass(solver); try { for (int i = 0; i < WARM_UP_LOOPS; i++) { double result = solver.solve(10000, function, -1000, 1000); } long startTimeNanos = System.nanoTime(); for (int i = 0; i < MEASURED_LOOPS; i++) { double result = solver.solve(10000, function, -1000, 1000); } long endTimeNanos = System.nanoTime(); long runTimeNanos = endTimeNanos - startTimeNanos; long runTimeMillis = runTimeNanos / (1000 * 1000); System.out.printf("%d loops of solving took %dns (%dms).\n", MEASURED_LOOPS, runTimeNanos, runTimeMillis); } catch (MaxIterationsExceededException e) { e.printStackTrace(); } catch (FunctionEvaluationException e) { e.printStackTrace(); } inspectClass(solver); }
From source file:geogebra.kernel.AlgoRootNewton.java
final double calcRoot(Function fun, double start) { double root = Double.NaN; if (rootFinderBrent == null) rootFinderBrent = new BrentSolver(Kernel.STANDARD_PRECISION); // try Brent method with borders close to start value try {//from w w w.j a v a 2 s . c om double step = (kernel.getXmax() - kernel.getXmin()) / 10; root = rootFinderBrent.solve(MAX_ITERATIONS, new RealRootAdapter(fun), start - step, start + step, start); if (checkRoot(fun, root)) { //System.out.println("1. Brent worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } // try Brent method on valid interval around start double[] borders = getDomain(fun, start); try { root = rootFinderBrent.solve(MAX_ITERATIONS, new RealRootAdapter(fun), borders[0], borders[1], start); if (checkRoot(fun, root)) { //System.out.println("2. Brent worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } // try Newton's method RealRootDerivFunction derivFun = fun.getRealRootDerivFunction(); if (derivFun != null) { // check if fun(start) is defined double eval = fun.evaluate(start); if (Double.isNaN(eval) || Double.isInfinite(eval)) { // shift left border slightly right borders[0] = 0.9 * borders[0] + 0.1 * borders[1]; start = (borders[0] + borders[1]) / 2; } if (rootFinderNewton == null) { rootFinderNewton = new NewtonSolver(); } try { root = rootFinderNewton.solve(MAX_ITERATIONS, new RealRootDerivAdapter(derivFun), borders[0], borders[1], start); if (checkRoot(fun, root)) { //System.out.println("Newton worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } } // neither Brent nor Newton worked return Double.NaN; }
From source file:geogebra.common.kernel.algos.AlgoRootNewton.java
public final double calcRoot(Function fun, double start) { double root = Double.NaN; if (rootFinderBrent == null) rootFinderBrent = new BrentSolver(Kernel.STANDARD_PRECISION); // try Brent method with borders close to start value try {/*ww w . j ava 2s.com*/ // arbitrary (used to depend on screen width) double step = 1; root = rootFinderBrent.solve(MAX_ITERATIONS, new RealRootAdapter(fun), start - step, start + step, start); if (checkRoot(fun, root)) { // System.out.println("1. Brent worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } // try Brent method on valid interval around start double[] borders = getDomain(fun, start); try { root = rootFinderBrent.solve(MAX_ITERATIONS, new RealRootAdapter(fun), borders[0], borders[1], start); if (checkRoot(fun, root)) { // System.out.println("2. Brent worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } // try Newton's method RealRootDerivFunction derivFun = fun.getRealRootDerivFunction(); if (derivFun != null) { // check if fun(start) is defined double eval = fun.evaluate(start); if (Double.isNaN(eval) || Double.isInfinite(eval)) { // shift left border slightly right borders[0] = 0.9 * borders[0] + 0.1 * borders[1]; start = (borders[0] + borders[1]) / 2; } if (rootFinderNewton == null) { rootFinderNewton = new NewtonSolver(); } try { root = rootFinderNewton.solve(MAX_ITERATIONS, new RealRootDerivAdapter(derivFun), borders[0], borders[1], start); if (checkRoot(fun, root)) { // System.out.println("Newton worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } } // neither Brent nor Newton worked return Double.NaN; }