List of usage examples for java.lang Math signum
public static float signum(float f)
From source file:net.pms.util.Rational.java
/** * Returns a {@link Rational} whose value is {@code (this + value)}. * * @param value the value to be added to this {@link Rational}. * @return The addition result.// w ww . j a v a 2 s. c o m */ @Nonnull public Rational add(double value) { if (isNaN() || Double.isNaN(value)) { return NaN; } if (value == 0d) { return this; } if (isInfinite()) { if (Double.isInfinite(value) && signum() != Math.signum(value)) { return NaN; // Infinity minus infinity } return this; } return add(valueOf(value)); }
From source file:com.scurab.android.anuitorsample.widget.SlidingPaneLayout.java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (!mSlideEnabled) { return false; }//from w ww . j a v a2 s . co m final int action = MotionEventCompat.getActionMasked(ev); // Preserve the open state based on the last view that was touched. if (!mCanSlide && action == MotionEvent.ACTION_DOWN && getChildCount() > 1) { // After the first things will be slideable. final View secondChild = getChildAt(1); if (secondChild != null) { mPreservedOpenState = !mDragHelper.isViewUnder(secondChild, (int) ev.getX(), (int) ev.getY()); } } if (!mCanSlide || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) { mDragHelper.cancel(); return super.onInterceptTouchEvent(ev); } if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { mDragHelper.cancel(); return false; } boolean interceptTap = false; switch (action) { case MotionEvent.ACTION_DOWN: { mIsUnableToDrag = false; final float x = ev.getX(); final float y = ev.getY(); mInitialMotionX = x; mInitialMotionY = y; if (mDragHelper.isViewUnder(mSlideableView, (int) x, (int) y) && isDimmed(mSlideableView)) { interceptTap = true; } break; } case MotionEvent.ACTION_MOVE: { final float x = ev.getX(); final float y = ev.getY(); final float adx = Math.abs(x - mInitialMotionX); final float ady = Math.abs(y - mInitialMotionY); final int slop = mDragHelper.getTouchSlop(); if (HACK_MOD) { if (adx > slop && ady > adx || canScroll(this, false, (int) Math.signum(x - mInitialMotionX), Math.round(x), Math.round(y))) { mDragHelper.cancel(); mIsUnableToDrag = true; return false; } } else { if (adx > slop && ady > adx) { mDragHelper.cancel(); mIsUnableToDrag = true; return false; } } } } final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev); return interceptForDrag || interceptTap; }
From source file:curveavg.MedialAxisTransform.java
/** * Uses the Laguerre algorithm iteratively to span the range [0,1] and find * all the real roots./*w w w. j a va2s.c o m*/ */ public static SolverResult solveQuinticLaguerre(float a, float b, float c, float d, float e, float f) { // Initialize the result // System.out.println("quintic params: " + a + ", "+ b + ", "+ c + ", "+ d + ", "+ e + ", "+ f); SolverResult res = new SolverResult(); res.re = new float[6]; res.im = new float[6]; for (int i = 0; i < 6; i++) { res.im[i] = 1.0f; } // Create the quintic function and the solver double coefficients[] = { f, e, d, c, b, a }; PolynomialFunction qf = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); // Iteratively call the NewtonRaphson solution until no solution // exists double minBound = 0.0; double sol; int root_idx = 0; final double dt = 0.01; for (double t = -dt; t < 1.0; t += dt) { // Check if there is a sign-crossing between the two times double t2 = t + dt; double f1 = qf.value(t); double f2 = qf.value(t2); if (Math.signum(f1) == Math.signum(f2)) { continue; } // Attempt to get the solution try { sol = solver.solve(100, qf, t, t2); } catch (TooManyEvaluationsException | NumberIsTooLargeException exc) { break; } // Save the root and update the minimum bound res.re[root_idx] = (float) sol; res.im[root_idx] = 0.0f; minBound = sol + 1e-3; root_idx++; } // System.out.println("#quintic roots: " + root_idx); return res; }
From source file:com.owen.view.views.PieChart.java
/** * Helper method for translating (x,y) scroll vectors into scalar rotation of the pie. * * @param dx The x component of the current scroll vector. * @param dy The y component of the current scroll vector. * @param x The x position of the current touch, relative to the pie center. * @param y The y position of the current touch, relative to the pie center. * @return The scalar representing the change in angular position for this scroll. *//*from w w w . j a v a2s. c o m*/ private static float vectorToScalarScroll(float dx, float dy, float x, float y) { // get the length of the vector float l = (float) Math.sqrt(dx * dx + dy * dy); // decide if the scalar should be negative or positive by finding // the dot product of the vector perpendicular to (x,y). float crossX = -y; float crossY = x; float dot = (crossX * dx + crossY * dy); float sign = Math.signum(dot); return l * sign; }
From source file:com.opengamma.analytics.financial.model.volatility.BlackScholesFormulaRepository.java
/** * Charm, minus of second order derivative of option value, once spot and once time to maturity * @param spot The spot value of the underlying * @param strike The strike /* w w w. ja v a2 s. c o m*/ * @param timeToExpiry The time to expiry * @param lognormalVol The log-normal volatility * @param interestRate The interest rate * @param costOfCarry The cost of carry * @param isCall True for call * @return The charm */ public static double charm(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry, final boolean isCall) { ArgumentChecker.isTrue(spot >= 0.0, "negative/NaN spot; have {}", spot); ArgumentChecker.isTrue(strike >= 0.0, "negative/NaN strike; have {}", strike); ArgumentChecker.isTrue(timeToExpiry >= 0.0, "negative/NaN timeToExpiry; have {}", timeToExpiry); ArgumentChecker.isTrue(lognormalVol >= 0.0, "negative/NaN lognormalVol; have {}", lognormalVol); ArgumentChecker.isFalse(Double.isNaN(interestRate), "interestRate is NaN"); ArgumentChecker.isFalse(Double.isNaN(costOfCarry), "costOfCarry is NaN"); final double rootT = Math.sqrt(timeToExpiry); double sigmaRootT = lognormalVol * rootT; if (Double.isNaN(sigmaRootT)) { sigmaRootT = 1.; //ref value is returned } double coeff = Math.exp((costOfCarry - interestRate) * timeToExpiry); if (coeff < SMALL) { return 0.; } if (Double.isNaN(coeff)) { coeff = 1.; //ref value is returned } final int sign = isCall ? 1 : -1; double d1 = 0.; double d2 = 0.; if (Math.abs(spot - strike) < SMALL || (spot > LARGE && strike > LARGE) || sigmaRootT > LARGE) { final double coefD1 = Double.isNaN(Math.abs(costOfCarry) / lognormalVol) ? Math.signum(costOfCarry) + 0.5 * lognormalVol : (costOfCarry / lognormalVol + 0.5 * lognormalVol); final double tmpD1 = Math.abs(coefD1) < SMALL ? 0. : coefD1 * rootT; d1 = Double.isNaN(tmpD1) ? Math.signum(coefD1) : tmpD1; final double coefD2 = Double.isNaN(Math.abs(costOfCarry) / lognormalVol) ? Math.signum(costOfCarry) - 0.5 * lognormalVol : (costOfCarry / lognormalVol - 0.5 * lognormalVol); final double tmpD2 = Math.abs(coefD2) < SMALL ? 0. : coefD2 * rootT; d2 = Double.isNaN(tmpD2) ? Math.signum(coefD2) : tmpD2; } else { if (sigmaRootT < SMALL) { final double scnd = (Math.abs(costOfCarry) > LARGE && rootT < SMALL) ? Math.signum(costOfCarry) : costOfCarry * rootT; final double tmp = (Math.log(spot / strike) / rootT + scnd) / lognormalVol; d1 = Double.isNaN(tmp) ? 0. : tmp; d2 = d1; } else { final double tmp = costOfCarry * rootT / lognormalVol; final double sig = (costOfCarry >= 0.) ? 1. : -1.; final double scnd = Double.isNaN(tmp) ? ((lognormalVol < LARGE && lognormalVol > SMALL) ? sig / lognormalVol : sig * rootT) : tmp; final double d1Tmp = Math.log(spot / strike) / sigmaRootT + scnd + 0.5 * sigmaRootT; final double d2Tmp = Math.log(spot / strike) / sigmaRootT + scnd - 0.5 * sigmaRootT; d1 = Double.isNaN(d1Tmp) ? 0. : d1Tmp; d2 = Double.isNaN(d2Tmp) ? 0. : d2Tmp; } } // if (Double.isNaN(d1) || Double.isNaN(d2)) { // throw new IllegalArgumentException("NaN found"); // } double cocMod = costOfCarry / sigmaRootT; if (Double.isNaN(cocMod)) { cocMod = 1.; //ref value is returned } double tmp = d2 / timeToExpiry; tmp = Double.isNaN(tmp) ? (d2 >= 0. ? 1. : -1.) : tmp; double coefPdf = cocMod - 0.5 * tmp; final double normPdf = NORMAL.getPDF(d1); final double normCdf = NORMAL.getCDF(sign * d1); final double first = normPdf < SMALL ? 0. : (Double.isNaN(coefPdf) ? 0. : normPdf * coefPdf); final double second = normCdf < SMALL ? 0. : (costOfCarry - interestRate) * normCdf; final double res = -coeff * (first + sign * second); return Double.isNaN(res) ? 0. : res; }
From source file:org.kalypso.grid.GeoGridUtilities.java
private static double interpolateBilinear(final IGeoGrid grid, final Coordinate crd) throws GeoGridException { /* Find four adjacent cells */ final GeoGridCell c11 = cellFromPosition(grid, crd); // check on which side of the cell we are final Coordinate crd11 = toCoordinate(grid, c11); final double centerX = crd11.x; final double centerY = crd11.y; final Coordinate offsetX = grid.getOffsetX(); final Coordinate offsetY = grid.getOffsetX(); final int cellShiftX = (int) (Math.signum(offsetX.x) * (crd.x < centerX ? -1 : 1)); final int cellShiftY = (int) (Math.signum(offsetY.x) * (crd.y < centerY ? 1 : -1)); final GeoGridCell c12 = new GeoGridCell(c11.x + cellShiftX, c11.y); final GeoGridCell c21 = new GeoGridCell(c11.x, c11.y + cellShiftY); final GeoGridCell c22 = new GeoGridCell(c11.x + cellShiftX, c11.y + cellShiftY); final double v11 = grid.getValueChecked(c11.x, c11.y); final double v12 = grid.getValueChecked(c12.x, c12.y); final double v21 = grid.getValueChecked(c21.x, c21.y); final double v22 = grid.getValueChecked(c22.x, c22.y); final Coordinate crd12 = toCoordinate(grid, c12); final Coordinate crd21 = toCoordinate(grid, c21); final Coordinate crd22 = toCoordinate(grid, c22); try {/*ww w. ja va2s . c o m*/ // interpolate in x direction final LinearEquation lex1 = new LinearEquation(crd11.x, v11, crd12.x, v12); final LinearEquation lex2 = new LinearEquation(crd21.x, v21, crd22.x, v22); final double vx1 = lex1.computeY(crd.x); final double vx2 = lex2.computeY(crd.x); // interpolate in y direction final LinearEquation ley = new LinearEquation(crd11.y, vx1, crd22.y, vx2); return ley.computeY(crd.y); } catch (final SameXValuesException e) { // should never happen... e.printStackTrace(); return Double.NaN; } }
From source file:com.facebook.presto.operator.scalar.MathFunctions.java
@ScalarFunction @SqlType(StandardTypes.BIGINT)//w ww. j a v a2 s .c o m public static long sign(@SqlType(StandardTypes.BIGINT) long num) { return (long) Math.signum(num); }
From source file:com.h6ah4i.android.widget.advrecyclerview.draggable.RecyclerViewDragDropManager.java
private void handleScrollOnDraggingInternal(RecyclerView rv, boolean horizontal) { final int edge = (horizontal) ? rv.getWidth() : rv.getHeight(); if (edge == 0) { return;/* w w w. j a v a2s.c o m*/ } final float invEdge = (1.0f / edge); final float normalizedTouchPos = (horizontal ? mLastTouchX : mLastTouchY) * invEdge; final float threshold = SCROLL_THRESHOLD; final float invThreshold = (1.0f / threshold); final float centerOffset = normalizedTouchPos - 0.5f; final float absCenterOffset = Math.abs(centerOffset); final float acceleration = Math.max(0.0f, threshold - (0.5f - absCenterOffset)) * invThreshold; final int mask = mScrollDirMask; final DraggingItemDecorator decorator = mDraggingItemDecorator; int scrollAmount = (int) Math.signum(centerOffset) * (int) (SCROLL_AMOUNT_COEFF * mDragEdgeScrollSpeed * mDisplayDensity * acceleration + 0.5f); int actualScrolledAmount = 0; final ItemDraggableRange range = mDraggableRange; final int firstVisibleChild = CustomRecyclerViewUtils.findFirstCompletelyVisibleItemPosition(mRecyclerView); final int lastVisibleChild = CustomRecyclerViewUtils.findLastCompletelyVisibleItemPosition(mRecyclerView); boolean reachedToFirstHardLimit = false; boolean reachedToFirstSoftLimit = false; boolean reachedToLastHardLimit = false; boolean reachedToLastSoftLimit = false; if (firstVisibleChild != RecyclerView.NO_POSITION) { if (firstVisibleChild <= range.getStart()) { reachedToFirstSoftLimit = true; } if (firstVisibleChild <= (range.getStart() - 1)) { reachedToFirstHardLimit = true; } } if (lastVisibleChild != RecyclerView.NO_POSITION) { if (lastVisibleChild >= range.getEnd()) { reachedToLastSoftLimit = true; } if (lastVisibleChild >= (range.getEnd() + 1)) { reachedToLastHardLimit = true; } } // apply mask if (scrollAmount > 0) { if ((mask & (horizontal ? SCROLL_DIR_RIGHT : SCROLL_DIR_DOWN)) == 0) { scrollAmount = 0; } } else if (scrollAmount < 0) { if ((mask & (horizontal ? SCROLL_DIR_LEFT : SCROLL_DIR_UP)) == 0) { scrollAmount = 0; } } // scroll if ((!reachedToFirstHardLimit && (scrollAmount < 0)) || (!reachedToLastHardLimit && (scrollAmount > 0))) { safeEndAnimations(rv); actualScrolledAmount = (horizontal) ? scrollByXAndGetScrolledAmount(scrollAmount) : scrollByYAndGetScrolledAmount(scrollAmount); if (scrollAmount < 0) { decorator.setIsScrolling(!reachedToFirstSoftLimit); } else { decorator.setIsScrolling(!reachedToLastSoftLimit); } decorator.refresh(); if (mSwapTargetItemOperator != null) { mSwapTargetItemOperator.update(decorator.getDraggingItemTranslationX(), decorator.getDraggingItemTranslationY()); } } else { decorator.setIsScrolling(false); } final boolean actualIsScrolling = (actualScrolledAmount != 0); if (mEdgeEffectDecorator != null) { final float edgeEffectStrength = 0.005f; final int draggingItemTopLeft = (horizontal) ? decorator.getTranslatedItemPositionLeft() : decorator.getTranslatedItemPositionTop(); final int draggingItemBottomRight = (horizontal) ? decorator.getTranslatedItemPositionRight() : decorator.getTranslatedItemPositionBottom(); final int draggingItemCenter = (draggingItemTopLeft + draggingItemBottomRight) / 2; final int nearEdgePosition; if (firstVisibleChild == 0 && lastVisibleChild == 0) { // has only 1 item nearEdgePosition = (scrollAmount < 0) ? draggingItemTopLeft : draggingItemBottomRight; } else { nearEdgePosition = (draggingItemCenter < (edge / 2)) ? draggingItemTopLeft : draggingItemBottomRight; } final float nearEdgeOffset = (nearEdgePosition * invEdge) - 0.5f; final float absNearEdgeOffset = Math.abs(nearEdgeOffset); float edgeEffectPullDistance = 0; if ((absNearEdgeOffset > 0.4f) && (scrollAmount != 0) && !actualIsScrolling) { if (nearEdgeOffset < 0) { if (horizontal ? decorator.isReachedToLeftLimit() : decorator.isReachedToTopLimit()) { edgeEffectPullDistance = -mDisplayDensity * edgeEffectStrength; } } else { if (horizontal ? decorator.isReachedToRightLimit() : decorator.isReachedToBottomLimit()) { edgeEffectPullDistance = mDisplayDensity * edgeEffectStrength; } } } updateEdgeEffect(edgeEffectPullDistance); } ViewCompat.postOnAnimation(mRecyclerView, mCheckItemSwappingRunnable); }
From source file:com.facebook.presto.operator.scalar.MathFunctions.java
@Description("signum") @ScalarFunction("sign") @SqlType(StandardTypes.INTEGER)// www . ja v a2s . co m public static long signInteger(@SqlType(StandardTypes.INTEGER) long num) { return (long) Math.signum(num); }
From source file:com.facebook.presto.operator.scalar.MathFunctions.java
@Description("signum") @ScalarFunction("sign") @SqlType(StandardTypes.SMALLINT)/*from w ww . j a v a 2s .c o m*/ public static long signSmallint(@SqlType(StandardTypes.SMALLINT) long num) { return (long) Math.signum(num); }