List of usage examples for org.apache.commons.math.analysis.solvers UnivariateRealSolverFactory newInstance
public static UnivariateRealSolverFactory newInstance()
From source file:geogebra.kernel.AlgoRootInterval.java
final double calcRoot() { if (!(f.isDefined() && aGeo.isDefined() && bGeo.isDefined())) return Double.NaN; double root = Double.NaN; Function fun = f.getFunction(); if (rootFinder == null) { UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance(); rootFinder = fact.newBrentSolver(); }//w w w. j a v a 2 s .c o m try { // Brent's method root = rootFinder.solve(new RealRootAdapter(fun), a.getDouble(), b.getDouble()); } catch (Exception e) { try { // Let's try again by searchin for a valid domain first double[] borders = RealRootUtil.getDefinedInterval(fun, a.getDouble(), b.getDouble()); root = rootFinder.solve(new RealRootAdapter(fun), borders[0], borders[1]); } catch (Exception ex) { root = Double.NaN; } } // check result if (Math.abs(fun.evaluate(root)) < Kernel.MIN_PRECISION) return root; else return Double.NaN; }
From source file:geogebra.common.kernel.algos.AlgoRootInterval.java
@SuppressWarnings("deprecation") final double calcRoot() { if (!(f.isDefined() && aGeo.isDefined() && bGeo.isDefined())) { return Double.NaN; }/*from w w w. j a v a 2 s. co m*/ double root = Double.NaN; Function fun = f.getFunction(); if (rootFinder == null) { UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance(); rootFinder = fact.newBrentSolver(); rootPolisher = fact.newNewtonSolver(); } double min = a.getDouble(); double max = b.getDouble(); double newtonRoot = Double.NaN; try { // Brent's method (Apache 2.2) root = rootFinder.solve(new RealRootAdapter(fun), min, max); // Apache 3.3 - solver seems more accurate // #4691 // BrentSolver brent3 = new BrentSolver(); // root = brent3.solve(100, new RealRootAdapter3(fun), min, max); } catch (Exception e) { // e.printStackTrace(); Log.debug("problem finding root: " + e.getMessage()); try { // Let's try again by searching for a valid domain first double[] borders = RealRootUtil.getDefinedInterval(fun, min, max); root = rootFinder.solve(new RealRootAdapter(fun), borders[0], borders[1]); } catch (Exception ex) { // ex.printStackTrace(); Log.debug("problem finding root: " + ex.getMessage()); return Double.NaN; } } // Log.debug("result from Brent: " + root); // ******** Polish Root *************** // adpated from EquationSolver // #4691 try { newtonRoot = rootPolisher.solve(new RealRootDerivAdapter(fun), min, max, root); if (Math.abs(fun.evaluate(newtonRoot)) < Math.abs(fun.evaluate(root))) { root = newtonRoot; // Log.debug("polished result from Newton is better: " + // newtonRoot); } } catch (Exception e) { Log.debug("problem polishing root: " + e.getMessage()); } // check result if (Math.abs(fun.evaluate(root)) < Kernel.MIN_PRECISION) { return root; } Log.debug("problem with root accuracy"); return Double.NaN; }
From source file:ch.algotrader.option.OptionUtil.java
/** * Gets the implied volatility of a {@link Option} using a {@link UnivariateRealSolverFactory}. *//*from w ww . j a v a2s. c om*/ @SuppressWarnings("deprecation") public static double getImpliedVolatility(final double underlyingSpot, final double strike, final double currentValue, final double years, final double intrest, final double dividend, final OptionType type) throws MathException { if (years < 0) { throw new IllegalArgumentException("years cannot be negative"); } double intrinsicValue = getIntrinsicValue(underlyingSpot, strike, type); if (currentValue <= intrinsicValue) { throw new IllegalArgumentException( "cannot calculate volatility if optionValue is below intrinsic Value"); } UnivariateRealFunction function = volatility -> getOptionPrice(underlyingSpot, strike, volatility, years, intrest, dividend, type) - currentValue; UnivariateRealSolverFactory factory = UnivariateRealSolverFactory.newInstance(); UnivariateRealSolver solver = factory.newDefaultSolver(); solver.setAbsoluteAccuracy(0.0001); return solver.solve(function, 0.01, 2.0); }
From source file:geogebra.kernel.AlgoRoots.java
/** Brent's algo * Copied from AlgoRootInterval.java./*ww w . j a v a 2 s.c o m*/ */ public final static double calcSingleRoot(GeoFunction f, double left, double right) { UnivariateRealSolver rootFinder = UnivariateRealSolverFactory.newInstance().newBrentSolver(); //Apache lib if (!f.isDefined()) return Double.NaN; double root = Double.NaN; Function fun = f.getFunction(); try { // Brent's method root = rootFinder.solve(new RealRootAdapter(fun), left, right); } catch (Exception e) { try { // Let's try again by searching for a valid domain first double[] borders = RealRootUtil.getDefinedInterval(fun, left, right); root = rootFinder.solve(new RealRootAdapter(fun), borders[0], borders[1]); } catch (Exception ex) { root = Double.NaN; } //try-catch } //try-catch return root; }
From source file:geogebra.common.kernel.algos.AlgoRoots.java
/** * Brent's algo Copied from AlgoRootInterval.java. *///from www. ja va 2 s .c o m public final static double calcSingleRoot(GeoFunction f, double left, double right) { UnivariateRealSolver rootFinder = UnivariateRealSolverFactory.newInstance().newBrentSolver(); // Apache lib if (!f.isDefined()) return Double.NaN; double root = Double.NaN; Function fun = f.getFunction(); try { // Brent's method root = rootFinder.solve(new RealRootAdapter(fun), left, right); } catch (Exception e) { try { // Let's try again by searching for a valid domain first double[] borders = RealRootUtil.getDefinedInterval(fun, left, right); root = rootFinder.solve(new RealRootAdapter(fun), borders[0], borders[1]); } catch (Exception ex) { root = Double.NaN; } // try-catch } // try-catch return root; }
From source file:geogebra.common.kernel.EquationSolver.java
/** * Calculates all roots of a polynomial given by eqn using Laguerres method. * Polishes roots found. The roots are stored in eqn again. * //from ww w .ja va 2 s .c o m * @param eqn * coefficients of polynomial */ @SuppressWarnings("deprecation") private int laguerreAll(double[] eqn) { // for fast evaluation of polynomial (used for root polishing) PolyFunction polyFunc = new PolyFunction(eqn); PolyFunction derivFunc = polyFunc.getDerivative(); /* * double estx = 0; try { estx = rootPolisher.newtonRaphson(polyFunc, * LAGUERRE_START); Application.debug("newton estx: " + estx); if * (Double.isNaN(estx)) { estx = LAGUERRE_START; * Application.debug("corrected estx: " + estx); } } catch (Exception e) * {} */ // calc roots with Laguerre method /* * old code using Flanagan library //ComplexPoly poly = new * ComplexPoly(eqn); //Complex [] complexRoots = poly.roots(false, new * Complex(LAGUERRE_START, 0)); // don't polish here */ Complex[] complexRoots = null; try { if (laguerreSolver == null) { laguerreSolver = new LaguerreSolver(); } complexRoots = laguerreSolver.solveAll(eqn, LAGUERRE_START); } catch (Exception e) { System.err.println("EquationSolver.LaguerreSolver: " + e.getLocalizedMessage()); } if (complexRoots == null) complexRoots = new Complex[0]; // sort complexRoots by real part into laguerreRoots double[] laguerreRoots = new double[complexRoots.length]; for (int i = 0; i < laguerreRoots.length; i++) { laguerreRoots[i] = complexRoots[i].getReal(); } Arrays.sort(laguerreRoots); // get the roots from Laguerre method int realRoots = 0; double root; for (int i = 0; i < laguerreRoots.length; i++) { // System.out.println("laguerreRoots[" + i + "] = " + // laguerreRoots[i]); // let's polish all complex roots to get all real roots root = laguerreRoots[i]; // check if root is bounded in intervall [root-eps, root+eps] double left = i == 0 ? root - 1 : (root + laguerreRoots[i - 1]) / 2; double right = i == laguerreRoots.length - 1 ? root + 1 : (root + laguerreRoots[i + 1]) / 2; double f_left = polyFunc.evaluate(left); double f_right = polyFunc.evaluate(right); boolean bounded = f_left * f_right < 0.0; try { if (bounded) { if (rootFinderBrent == null) { UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance(); rootFinderBrent = fact.newBrentSolver(); } // small f'(root): don't go too far from our laguerre root ! double brentRoot = rootFinderBrent.solve(new RealRootAdapter(polyFunc), left, right, root); if (Math.abs(polyFunc.evaluate(brentRoot)) < Math.abs(polyFunc.evaluate(root))) { root = brentRoot; } // System.out.println("Polish bisectNewtonRaphson: " + // root); } else { if (rootFinderNewton == null) { UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance(); rootFinderNewton = fact.newNewtonSolver(); } // the root is not bounded: give Mr. Newton a chance double newtonRoot = rootFinderNewton.solve(new RealRootDerivAdapter(polyFunc), left, right, root); if (Math.abs(polyFunc.evaluate(newtonRoot)) < Math.abs(polyFunc.evaluate(root))) { root = newtonRoot; } // System.out.println("Polish newtonRaphson: " + root); } } catch (Exception e) { // Application.debug("Polish FAILED: "); // polishing failed: maybe we have an extremum here // try to find a local extremum try { if (rootFinderBrent == null) { UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance(); rootFinderBrent = fact.newBrentSolver(); } double brentRoot = rootFinderBrent.solve(new RealRootAdapter(derivFunc), left, right); if (Math.abs(polyFunc.evaluate(brentRoot)) < Math.abs(polyFunc.evaluate(root))) { root = brentRoot; } // System.out.println(" find extremum successfull: " + // root); } catch (Exception ex) { App.debug(ex.getMessage()); } } // check if the found root is really ok double[] val = polyFunc.evaluateDerivFunc(root); // get f(root) and // f'(root) double error = Math.abs(val[0]); // | f(root) | double slope = Math.abs(val[1]); boolean success; if (slope < 1) success = error < LAGUERRE_EPS; else success = error < LAGUERRE_EPS * slope; if (success) { // Application.debug("FOUND ROOT: " + root); eqn[realRoots] = root; realRoots++; } else { // Application.debug("no root: " + root + ", error " + error); } } return realRoots; }
From source file:geogebra.kernel.EquationSolver.java
/** * Calculates all roots of a polynomial given by eqn using Laguerres method. * Polishes roots found. The roots are stored in eqn again. * @param eqn: coefficients of polynomial *//*from w w w . ja v a 2 s . com*/ private int laguerreAll(double[] eqn) { // for fast evaluation of polynomial (used for root polishing) PolyFunction polyFunc = new PolyFunction(eqn); PolyFunction derivFunc = polyFunc.getDerivative(); /* double estx = 0; try { estx = rootPolisher.newtonRaphson(polyFunc, LAGUERRE_START); Application.debug("newton estx: " + estx); if (Double.isNaN(estx)) { estx = LAGUERRE_START; Application.debug("corrected estx: " + estx); } } catch (Exception e) {} */ // calc roots with Laguerre method /* old code using Flanagan library //ComplexPoly poly = new ComplexPoly(eqn); //Complex [] complexRoots = poly.roots(false, new Complex(LAGUERRE_START, 0)); // don't polish here */ Complex[] complexRoots = null; try { if (laguerreSolver == null) { laguerreSolver = new LaguerreSolver(); } complexRoots = laguerreSolver.solveAll(eqn, LAGUERRE_START); } catch (Exception e) { System.err.println("EquationSolver.LaguerreSolver: " + e.getLocalizedMessage()); } if (complexRoots == null) complexRoots = new Complex[0]; // sort complexRoots by real part into laguerreRoots double[] laguerreRoots = new double[complexRoots.length]; for (int i = 0; i < laguerreRoots.length; i++) { laguerreRoots[i] = complexRoots[i].getReal(); } Arrays.sort(laguerreRoots); // get the roots from Laguerre method int realRoots = 0; double root; for (int i = 0; i < laguerreRoots.length; i++) { //System.out.println("laguerreRoots[" + i + "] = " + laguerreRoots[i]); // let's polish all complex roots to get all real roots root = laguerreRoots[i]; // check if root is bounded in intervall [root-eps, root+eps] double left = i == 0 ? root - 1 : (root + laguerreRoots[i - 1]) / 2; double right = i == laguerreRoots.length - 1 ? root + 1 : (root + laguerreRoots[i + 1]) / 2; double f_left = polyFunc.evaluate(left); double f_right = polyFunc.evaluate(right); boolean bounded = f_left * f_right < 0.0; try { if (bounded) { if (rootFinderBrent == null) { UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance(); rootFinderBrent = fact.newBrentSolver(); } // small f'(root): don't go too far from our laguerre root ! double brentRoot = rootFinderBrent.solve(new RealRootAdapter(polyFunc), left, right, root); if (Math.abs(polyFunc.evaluate(brentRoot)) < Math.abs(polyFunc.evaluate(root))) { root = brentRoot; } //System.out.println("Polish bisectNewtonRaphson: " + root); } else { if (rootFinderNewton == null) { UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance(); rootFinderNewton = fact.newNewtonSolver(); } // the root is not bounded: give Mr. Newton a chance double newtonRoot = rootFinderNewton.solve(new RealRootDerivAdapter(polyFunc), left, right, root); if (Math.abs(polyFunc.evaluate(newtonRoot)) < Math.abs(polyFunc.evaluate(root))) { root = newtonRoot; } //System.out.println("Polish newtonRaphson: " + root); } } catch (Exception e) { // Application.debug("Polish FAILED: "); // polishing failed: maybe we have an extremum here // try to find a local extremum try { if (rootFinderBrent == null) { UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance(); rootFinderBrent = fact.newBrentSolver(); } double brentRoot = rootFinderBrent.solve(new RealRootAdapter(derivFunc), left, right); if (Math.abs(polyFunc.evaluate(brentRoot)) < Math.abs(polyFunc.evaluate(root))) { root = brentRoot; } //System.out.println(" find extremum successfull: " + root); } catch (Exception ex) { Application.debug(ex.getMessage()); } } // check if the found root is really ok double[] val = polyFunc.evaluateDerivFunc(root); // get f(root) and f'(root) double error = Math.abs(val[0]); // | f(root) | double slope = Math.abs(val[1]); boolean success; if (slope < 1) success = error < LAGUERRE_EPS; else success = error < LAGUERRE_EPS * slope; if (success) { // Application.debug("FOUND ROOT: " + root); eqn[realRoots] = root; realRoots++; } else { //Application.debug("no root: " + root + ", error " + error); } } return realRoots; }