Java Code Examples for org.apache.commons.math3.util.FastMath#signum()
The following examples show how to use
org.apache.commons.math3.util.FastMath#signum() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RobustBrentSolver.java From gatk-protected with BSD 3-Clause "New" or "Revised" License | 6 votes |
@VisibleForTesting static List<Bracket> detectBrackets(final double[] x, final double[] f) { final List<Bracket> brackets = new ArrayList<>(); final double[] signs = new double[f.length]; for (int i = 0; i < f.length; i++) { signs[i] = FastMath.signum(f[i]); } double prevSignum = signs[0]; int prevIdx = 0; int idx = 1; while (idx < f.length) { if (signs[idx]*prevSignum <= 0) { brackets.add(new Bracket(x[prevIdx], x[idx])); prevIdx = idx; prevSignum = signs[idx]; } idx++; } return brackets; }
Example 2
Source File: RiddersSolver.java From astor with GNU General Public License v2.0 | 4 votes |
/** * {@inheritDoc} */ @Override protected double doSolve() { double min = getMin(); double max = getMax(); // [x1, x2] is the bracketing interval in each iteration // x3 is the midpoint of [x1, x2] // x is the new root approximation and an endpoint of the new interval double x1 = min; double y1 = computeObjectiveValue(x1); double x2 = max; double y2 = computeObjectiveValue(x2); // check for zeros before verifying bracketing if (y1 == 0) { return min; } if (y2 == 0) { return max; } verifyBracketing(min, max); final double absoluteAccuracy = getAbsoluteAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); final double relativeAccuracy = getRelativeAccuracy(); double oldx = Double.POSITIVE_INFINITY; while (true) { // calculate the new root approximation final double x3 = 0.5 * (x1 + x2); final double y3 = computeObjectiveValue(x3); if (FastMath.abs(y3) <= functionValueAccuracy) { return x3; } final double delta = 1 - (y1 * y2) / (y3 * y3); // delta > 1 due to bracketing final double correction = (FastMath.signum(y2) * FastMath.signum(y3)) * (x3 - x1) / FastMath.sqrt(delta); final double x = x3 - correction; // correction != 0 final double y = computeObjectiveValue(x); // check for convergence final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy); if (FastMath.abs(x - oldx) <= tolerance) { return x; } if (FastMath.abs(y) <= functionValueAccuracy) { return x; } // prepare the new interval for next iteration // Ridders' method guarantees x1 < x < x2 if (correction > 0.0) { // x1 < x < x3 if (FastMath.signum(y1) + FastMath.signum(y) == 0.0) { x2 = x; y2 = y; } else { x1 = x; x2 = x3; y1 = y; y2 = y3; } } else { // x3 < x < x2 if (FastMath.signum(y2) + FastMath.signum(y) == 0.0) { x1 = x; y1 = y; } else { x1 = x3; x2 = x; y1 = y3; y2 = y; } } oldx = x; } }
Example 3
Source File: MullerSolver.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Find a real root in the given interval. * * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param fMin function value at the lower bound. * @param fMax function value at the upper bound. * @return the point at which the function value is zero. * @throws TooManyEvaluationsException if the allowed number of calls to * the function to be solved has been exhausted. */ private double solve(double min, double max, double fMin, double fMax) throws TooManyEvaluationsException { final double relativeAccuracy = getRelativeAccuracy(); final double absoluteAccuracy = getAbsoluteAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); // [x0, x2] is the bracketing interval in each iteration // x1 is the last approximation and an interpolation point in (x0, x2) // x is the new root approximation and new x1 for next round // d01, d12, d012 are divided differences double x0 = min; double y0 = fMin; double x2 = max; double y2 = fMax; double x1 = 0.5 * (x0 + x2); double y1 = computeObjectiveValue(x1); double oldx = Double.POSITIVE_INFINITY; while (true) { // Muller's method employs quadratic interpolation through // x0, x1, x2 and x is the zero of the interpolating parabola. // Due to bracketing condition, this parabola must have two // real roots and we choose one in [x0, x2] to be x. final double d01 = (y1 - y0) / (x1 - x0); final double d12 = (y2 - y1) / (x2 - x1); final double d012 = (d12 - d01) / (x2 - x0); final double c1 = d01 + (x1 - x0) * d012; final double delta = c1 * c1 - 4 * y1 * d012; final double xplus = x1 + (-2.0 * y1) / (c1 + FastMath.sqrt(delta)); final double xminus = x1 + (-2.0 * y1) / (c1 - FastMath.sqrt(delta)); // xplus and xminus are two roots of parabola and at least // one of them should lie in (x0, x2) final double x = isSequence(x0, xplus, x2) ? xplus : xminus; final double y = computeObjectiveValue(x); // check for convergence final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy); if (FastMath.abs(x - oldx) <= tolerance || FastMath.abs(y) <= functionValueAccuracy) { return x; } // Bisect if convergence is too slow. Bisection would waste // our calculation of x, hopefully it won't happen often. // the real number equality test x == x1 is intentional and // completes the proximity tests above it boolean bisect = (x < x1 && (x1 - x0) > 0.95 * (x2 - x0)) || (x > x1 && (x2 - x1) > 0.95 * (x2 - x0)) || (x == x1); // prepare the new bracketing interval for next iteration if (!bisect) { x0 = x < x1 ? x0 : x1; y0 = x < x1 ? y0 : y1; x2 = x > x1 ? x2 : x1; y2 = x > x1 ? y2 : y1; x1 = x; y1 = y; oldx = x; } else { double xm = 0.5 * (x0 + x2); double ym = computeObjectiveValue(xm); if (FastMath.signum(y0) + FastMath.signum(ym) == 0.0) { x2 = xm; y2 = ym; } else { x0 = xm; y0 = ym; } x1 = 0.5 * (x0 + x2); y1 = computeObjectiveValue(x1); oldx = Double.POSITIVE_INFINITY; } } }
Example 4
Source File: RiddersSolver.java From astor with GNU General Public License v2.0 | 4 votes |
/** * {@inheritDoc} */ @Override protected double doSolve() throws TooManyEvaluationsException, NoBracketingException { double min = getMin(); double max = getMax(); // [x1, x2] is the bracketing interval in each iteration // x3 is the midpoint of [x1, x2] // x is the new root approximation and an endpoint of the new interval double x1 = min; double y1 = computeObjectiveValue(x1); double x2 = max; double y2 = computeObjectiveValue(x2); // check for zeros before verifying bracketing if (y1 == 0) { return min; } if (y2 == 0) { return max; } verifyBracketing(min, max); final double absoluteAccuracy = getAbsoluteAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); final double relativeAccuracy = getRelativeAccuracy(); double oldx = Double.POSITIVE_INFINITY; while (true) { // calculate the new root approximation final double x3 = 0.5 * (x1 + x2); final double y3 = computeObjectiveValue(x3); if (FastMath.abs(y3) <= functionValueAccuracy) { return x3; } final double delta = 1 - (y1 * y2) / (y3 * y3); // delta > 1 due to bracketing final double correction = (FastMath.signum(y2) * FastMath.signum(y3)) * (x3 - x1) / FastMath.sqrt(delta); final double x = x3 - correction; // correction != 0 final double y = computeObjectiveValue(x); // check for convergence final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy); if (FastMath.abs(x - oldx) <= tolerance) { return x; } if (FastMath.abs(y) <= functionValueAccuracy) { return x; } // prepare the new interval for next iteration // Ridders' method guarantees x1 < x < x2 if (correction > 0.0) { // x1 < x < x3 if (FastMath.signum(y1) + FastMath.signum(y) == 0.0) { x2 = x; y2 = y; } else { x1 = x; x2 = x3; y1 = y; y2 = y3; } } else { // x3 < x < x2 if (FastMath.signum(y2) + FastMath.signum(y) == 0.0) { x1 = x; y1 = y; } else { x1 = x3; x2 = x; y1 = y3; y2 = y; } } oldx = x; } }
Example 5
Source File: MullerSolver.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Find a real root in the given interval. * * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param fMin function value at the lower bound. * @param fMax function value at the upper bound. * @return the point at which the function value is zero. */ private double solve(double min, double max, double fMin, double fMax) throws TooManyEvaluationsException { final double relativeAccuracy = getRelativeAccuracy(); final double absoluteAccuracy = getAbsoluteAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); // [x0, x2] is the bracketing interval in each iteration // x1 is the last approximation and an interpolation point in (x0, x2) // x is the new root approximation and new x1 for next round // d01, d12, d012 are divided differences double x0 = min; double y0 = fMin; double x2 = max; double y2 = fMax; double x1 = 0.5 * (x0 + x2); double y1 = computeObjectiveValue(x1); double oldx = Double.POSITIVE_INFINITY; while (true) { // Muller's method employs quadratic interpolation through // x0, x1, x2 and x is the zero of the interpolating parabola. // Due to bracketing condition, this parabola must have two // real roots and we choose one in [x0, x2] to be x. final double d01 = (y1 - y0) / (x1 - x0); final double d12 = (y2 - y1) / (x2 - x1); final double d012 = (d12 - d01) / (x2 - x0); final double c1 = d01 + (x1 - x0) * d012; final double delta = c1 * c1 - 4 * y1 * d012; final double xplus = x1 + (-2.0 * y1) / (c1 + FastMath.sqrt(delta)); final double xminus = x1 + (-2.0 * y1) / (c1 - FastMath.sqrt(delta)); // xplus and xminus are two roots of parabola and at least // one of them should lie in (x0, x2) final double x = isSequence(x0, xplus, x2) ? xplus : xminus; final double y = computeObjectiveValue(x); // check for convergence final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy); if (FastMath.abs(x - oldx) <= tolerance || FastMath.abs(y) <= functionValueAccuracy) { return x; } // Bisect if convergence is too slow. Bisection would waste // our calculation of x, hopefully it won't happen often. // the real number equality test x == x1 is intentional and // completes the proximity tests above it boolean bisect = (x < x1 && (x1 - x0) > 0.95 * (x2 - x0)) || (x > x1 && (x2 - x1) > 0.95 * (x2 - x0)) || (x == x1); // prepare the new bracketing interval for next iteration if (!bisect) { x0 = x < x1 ? x0 : x1; y0 = x < x1 ? y0 : y1; x2 = x > x1 ? x2 : x1; y2 = x > x1 ? y2 : y1; x1 = x; y1 = y; oldx = x; } else { double xm = 0.5 * (x0 + x2); double ym = computeObjectiveValue(xm); if (FastMath.signum(y0) + FastMath.signum(ym) == 0.0) { x2 = xm; y2 = ym; } else { x0 = xm; y0 = ym; } x1 = 0.5 * (x0 + x2); y1 = computeObjectiveValue(x1); oldx = Double.POSITIVE_INFINITY; } } }
Example 6
Source File: Signum.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x) { return FastMath.signum(x); }
Example 7
Source File: Signum.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x) { return FastMath.signum(x); }
Example 8
Source File: RiddersSolver.java From astor with GNU General Public License v2.0 | 4 votes |
/** * {@inheritDoc} */ @Override protected double doSolve() { double min = getMin(); double max = getMax(); // [x1, x2] is the bracketing interval in each iteration // x3 is the midpoint of [x1, x2] // x is the new root approximation and an endpoint of the new interval double x1 = min; double y1 = computeObjectiveValue(x1); double x2 = max; double y2 = computeObjectiveValue(x2); // check for zeros before verifying bracketing if (y1 == 0) { return min; } if (y2 == 0) { return max; } verifyBracketing(min, max); final double absoluteAccuracy = getAbsoluteAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); final double relativeAccuracy = getRelativeAccuracy(); double oldx = Double.POSITIVE_INFINITY; while (true) { // calculate the new root approximation final double x3 = 0.5 * (x1 + x2); final double y3 = computeObjectiveValue(x3); if (FastMath.abs(y3) <= functionValueAccuracy) { return x3; } final double delta = 1 - (y1 * y2) / (y3 * y3); // delta > 1 due to bracketing final double correction = (FastMath.signum(y2) * FastMath.signum(y3)) * (x3 - x1) / FastMath.sqrt(delta); final double x = x3 - correction; // correction != 0 final double y = computeObjectiveValue(x); // check for convergence final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy); if (FastMath.abs(x - oldx) <= tolerance) { return x; } if (FastMath.abs(y) <= functionValueAccuracy) { return x; } // prepare the new interval for next iteration // Ridders' method guarantees x1 < x < x2 if (correction > 0.0) { // x1 < x < x3 if (FastMath.signum(y1) + FastMath.signum(y) == 0.0) { x2 = x; y2 = y; } else { x1 = x; x2 = x3; y1 = y; y2 = y3; } } else { // x3 < x < x2 if (FastMath.signum(y2) + FastMath.signum(y) == 0.0) { x1 = x; y1 = y; } else { x1 = x3; x2 = x; y1 = y3; y2 = y; } } oldx = x; } }
Example 9
Source File: RiddersSolver.java From astor with GNU General Public License v2.0 | 4 votes |
/** * {@inheritDoc} */ @Override protected double doSolve() throws TooManyEvaluationsException, NoBracketingException { double min = getMin(); double max = getMax(); // [x1, x2] is the bracketing interval in each iteration // x3 is the midpoint of [x1, x2] // x is the new root approximation and an endpoint of the new interval double x1 = min; double y1 = computeObjectiveValue(x1); double x2 = max; double y2 = computeObjectiveValue(x2); // check for zeros before verifying bracketing if (y1 == 0) { return min; } if (y2 == 0) { return max; } verifyBracketing(min, max); final double absoluteAccuracy = getAbsoluteAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); final double relativeAccuracy = getRelativeAccuracy(); double oldx = Double.POSITIVE_INFINITY; while (true) { // calculate the new root approximation final double x3 = 0.5 * (x1 + x2); final double y3 = computeObjectiveValue(x3); if (FastMath.abs(y3) <= functionValueAccuracy) { return x3; } final double delta = 1 - (y1 * y2) / (y3 * y3); // delta > 1 due to bracketing final double correction = (FastMath.signum(y2) * FastMath.signum(y3)) * (x3 - x1) / FastMath.sqrt(delta); final double x = x3 - correction; // correction != 0 final double y = computeObjectiveValue(x); // check for convergence final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy); if (FastMath.abs(x - oldx) <= tolerance) { return x; } if (FastMath.abs(y) <= functionValueAccuracy) { return x; } // prepare the new interval for next iteration // Ridders' method guarantees x1 < x < x2 if (correction > 0.0) { // x1 < x < x3 if (FastMath.signum(y1) + FastMath.signum(y) == 0.0) { x2 = x; y2 = y; } else { x1 = x; x2 = x3; y1 = y; y2 = y3; } } else { // x3 < x < x2 if (FastMath.signum(y2) + FastMath.signum(y) == 0.0) { x1 = x; y1 = y; } else { x1 = x3; x2 = x; y1 = y3; y2 = y; } } oldx = x; } }
Example 10
Source File: DerivativeStructure.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public DerivativeStructure signum() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), FastMath.signum(data[0])); }
Example 11
Source File: DerivativeStructure.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} * @since 3.2 */ public DerivativeStructure signum() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), FastMath.signum(data[0])); }
Example 12
Source File: Builtin.java From systemds with Apache License 2.0 | 4 votes |
@Override public double execute (double in) { switch(bFunc) { case SIN: return FASTMATH ? FastMath.sin(in) : Math.sin(in); case COS: return FASTMATH ? FastMath.cos(in) : Math.cos(in); case TAN: return FASTMATH ? FastMath.tan(in) : Math.tan(in); case ASIN: return FASTMATH ? FastMath.asin(in) : Math.asin(in); case ACOS: return FASTMATH ? FastMath.acos(in) : Math.acos(in); case ATAN: return Math.atan(in); //faster in Math // FastMath.*h is faster 98% of time than Math.*h in initial micro-benchmarks case SINH: return FASTMATH ? FastMath.sinh(in) : Math.sinh(in); case COSH: return FASTMATH ? FastMath.cosh(in) : Math.cosh(in); case TANH: return FASTMATH ? FastMath.tanh(in) : Math.tanh(in); case CEIL: return FASTMATH ? FastMath.ceil(in) : Math.ceil(in); case FLOOR: return FASTMATH ? FastMath.floor(in) : Math.floor(in); case LOG: return Math.log(in); //faster in Math case LOG_NZ: return (in==0) ? 0 : Math.log(in); //faster in Math case ABS: return Math.abs(in); //no need for FastMath case SIGN: return FASTMATH ? FastMath.signum(in) : Math.signum(in); case SQRT: return Math.sqrt(in); //faster in Math case EXP: return FASTMATH ? FastMath.exp(in) : Math.exp(in); case ROUND: return Math.round(in); //no need for FastMath case PLOGP: if (in == 0.0) return 0.0; else if (in < 0) return Double.NaN; else //faster in Math return in * Math.log(in); case SPROP: //sample proportion: P*(1-P) return in * (1 - in); case SIGMOID: //sigmoid: 1/(1+exp(-x)) return FASTMATH ? 1 / (1 + FastMath.exp(-in)) : 1 / (1 + Math.exp(-in)); case ISNA: return Double.isNaN(in) ? 1 : 0; case ISNAN: return Double.isNaN(in) ? 1 : 0; case ISINF: return Double.isInfinite(in) ? 1 : 0; default: throw new DMLRuntimeException("Builtin.execute(): Unknown operation: " + bFunc); } }
Example 13
Source File: LibSpoofPrimitives.java From systemds with Apache License 2.0 | 4 votes |
public static double[] vectSignWrite(double[] a, int ai, int len) { double[] c = allocVector(len, false); for( int j = 0; j < len; j++, ai++) c[j] = FastMath.signum(a[ai]); return c; }
Example 14
Source File: DerivativeStructure.java From astor with GNU General Public License v2.0 | 4 votes |
/** Compute the signum of the instance. * The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise * @param a number on which evaluation is done * @return -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a */ public DerivativeStructure signum() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), FastMath.signum(data[0])); }
Example 15
Source File: DerivativeStructure.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} * @since 3.2 */ public DerivativeStructure signum() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), FastMath.signum(data[0])); }
Example 16
Source File: MullerSolver.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Find a real root in the given interval. * * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param fMin function value at the lower bound. * @param fMax function value at the upper bound. * @return the point at which the function value is zero. * @throws TooManyEvaluationsException if the allowed number of calls to * the function to be solved has been exhausted. */ private double solve(double min, double max, double fMin, double fMax) throws TooManyEvaluationsException { final double relativeAccuracy = getRelativeAccuracy(); final double absoluteAccuracy = getAbsoluteAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); // [x0, x2] is the bracketing interval in each iteration // x1 is the last approximation and an interpolation point in (x0, x2) // x is the new root approximation and new x1 for next round // d01, d12, d012 are divided differences double x0 = min; double y0 = fMin; double x2 = max; double y2 = fMax; double x1 = 0.5 * (x0 + x2); double y1 = computeObjectiveValue(x1); double oldx = Double.POSITIVE_INFINITY; while (true) { // Muller's method employs quadratic interpolation through // x0, x1, x2 and x is the zero of the interpolating parabola. // Due to bracketing condition, this parabola must have two // real roots and we choose one in [x0, x2] to be x. final double d01 = (y1 - y0) / (x1 - x0); final double d12 = (y2 - y1) / (x2 - x1); final double d012 = (d12 - d01) / (x2 - x0); final double c1 = d01 + (x1 - x0) * d012; final double delta = c1 * c1 - 4 * y1 * d012; final double xplus = x1 + (-2.0 * y1) / (c1 + FastMath.sqrt(delta)); final double xminus = x1 + (-2.0 * y1) / (c1 - FastMath.sqrt(delta)); // xplus and xminus are two roots of parabola and at least // one of them should lie in (x0, x2) final double x = isSequence(x0, xplus, x2) ? xplus : xminus; final double y = computeObjectiveValue(x); // check for convergence final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy); if (FastMath.abs(x - oldx) <= tolerance || FastMath.abs(y) <= functionValueAccuracy) { return x; } // Bisect if convergence is too slow. Bisection would waste // our calculation of x, hopefully it won't happen often. // the real number equality test x == x1 is intentional and // completes the proximity tests above it boolean bisect = (x < x1 && (x1 - x0) > 0.95 * (x2 - x0)) || (x > x1 && (x2 - x1) > 0.95 * (x2 - x0)) || (x == x1); // prepare the new bracketing interval for next iteration if (!bisect) { x0 = x < x1 ? x0 : x1; y0 = x < x1 ? y0 : y1; x2 = x > x1 ? x2 : x1; y2 = x > x1 ? y2 : y1; x1 = x; y1 = y; oldx = x; } else { double xm = 0.5 * (x0 + x2); double ym = computeObjectiveValue(xm); if (FastMath.signum(y0) + FastMath.signum(ym) == 0.0) { x2 = xm; y2 = ym; } else { x0 = xm; y0 = ym; } x1 = 0.5 * (x0 + x2); y1 = computeObjectiveValue(x1); oldx = Double.POSITIVE_INFINITY; } } }
Example 17
Source File: LibSpoofPrimitives.java From systemds with Apache License 2.0 | 4 votes |
public static double[] vectSignWrite(double[] a, int[] aix, int ai, int alen, int len) { double[] c = allocVector(len, true); for( int j = ai; j < ai+alen; j++ ) c[aix[j]] = FastMath.signum(a[j]); return c; }
Example 18
Source File: LibSpoofPrimitives.java From systemds with Apache License 2.0 | 4 votes |
public static double[] vectSignWrite(double[] a, int ai, int len) { double[] c = allocVector(len, false); for( int j = 0; j < len; j++, ai++) c[j] = FastMath.signum(a[ai]); return c; }
Example 19
Source File: Signum.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x) { return FastMath.signum(x); }
Example 20
Source File: Signum.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x) { return FastMath.signum(x); }