Example usage for java.lang Math signum

List of usage examples for java.lang Math signum

Introduction

In this page you can find the example usage for java.lang Math signum.

Prototype

public static float signum(float f) 

Source Link

Document

Returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.

Usage

From source file:com.opengamma.analytics.financial.interestrate.payments.provider.CouponFixedAccruedCompoundingDiscountingMethod.java

/**
 * Computes the present value of the fixed coupon with positive notional (abs(notional) is used) by discounting.
 * @param coupon The coupon./*  ww w .jav a2 s.  com*/
 * @param multicurves The multi-curve provider.
 * @return The present value.
 */
public CurrencyAmount presentValuePositiveNotional(final CouponFixedAccruedCompounding coupon,
        final MulticurveProviderInterface multicurves) {
    Validate.notNull(coupon, "Coupon");
    Validate.notNull(multicurves, "multicurve");
    return CurrencyAmount.of(coupon.getCurrency(), Math.signum(coupon.getNotional())
            * presentValue(coupon, multicurves).getAmount(coupon.getCurrency()));
}

From source file:de.tuberlin.uebb.jdae.llmsl.events.EventEvaluator.java

private boolean isCandidate(final int i) {
    final int store = (current + 1) % 2;
    final ContinuousEvent cev = c_events[i];
    final double os = Math.signum(guards[current][i]);
    final double ns = Math.signum(guards[store][i]);

    if (os != ns) {
        switch (cev.direction) {
        case UP://from   www . j  a v a 2  s  .com
            return ns > 0;
        case DOWN:
            return ns < 0;
        case BOTH:
            return true;
        }
    }
    return false;
}

From source file:me.selinali.tribbble.ui.archive.ArchiveFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_archive, container, false);
    mUnbinder = ButterKnife.bind(this, view);
    setupPadding();/* www . j  a v  a  2 s . c  om*/

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        private int previousDy;

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (Math.signum(previousDy) != Math.signum(dy) && dy != 0) {
                ((MainActivity) getActivity()).showBottomBar(dy < 0);
            }
            previousDy = dy;
        }
    });
    bind(ArchiveManager.instance().getArchivedShots());
    return view;
}

From source file:com.caseystella.analytics.outlier.batch.rpca.RPCA.java

private double[] softThreshold(double[] x, double penalty) {
    for (int i = 0; i < x.length; i++) {
        x[i] = Math.signum(x[i]) * Math.max(Math.abs(x[i]) - penalty, 0);
    }/*from  w  w  w  .j ava  2  s .c o m*/
    return x;
}

From source file:curveavg.SolverTest.java

@Test
public void testQuinticZero_NewtonRaphsonFailureCase() {

    // Solve using NewtonRaphson and count the number of results
    float c[] = { -38.764412f, 76.10117f, -56.993206f, 28.603401f, -10.2824955f, 1.0f };
    MedialAxisTransform.SolverResult res = MedialAxisTransform.solveQuinticNewtonRaphson(c[0], c[1], c[2], c[3],
            c[4], c[5]);//from   ww w .ja  v a  2 s  .  c o m
    int rootsNR = 0;
    for (int i = 0; i < res.im.length; i++) {
        if (Math.abs(res.im[i]) < 1e-3)
            rootsNR++;
    }
    System.out.println("# NR roots: " + rootsNR);

    // Solve using NewtonRaphson and count the number of results
    MedialAxisTransform.SolverResult res2 = MedialAxisTransform.solveQuinticLaguerre(c[0], c[1], c[2], c[3],
            c[4], c[5]);
    int rootsL = 0;
    for (int i = 0; i < res.im.length; i++) {
        if (Math.abs(res2.im[i]) < 1e-3)
            rootsL++;
    }
    System.out.println("# L roots: " + rootsL);

    // Solve using Laguerre
    double dt = 0.01;
    double coefficients[] = new double[6];
    for (int i = 0; i < 6; i++)
        coefficients[i] = c[5 - i];
    PolynomialFunction f = new PolynomialFunction(coefficients);
    LaguerreSolver solver = new LaguerreSolver();
    for (double t = 0; t <= (1.0 - dt); t += dt) {

        // Check if there is a sign-crossing between the two times
        double t2 = t + dt;
        double f1 = f.value(t);
        double f2 = f.value(t2);
        if (Math.signum(f1) == Math.signum(f2))
            continue;

        // Compute using Laguerre
        double result = solver.solve(100, f, t, t2);
        System.out.println("Result: " + result);
    }

}

From source file:org.kuali.rice.krad.demo.uif.lookup.TravelLookupableImpl.java

/**
 * Validates that any numeric value is non-negative.
 *
 * @param form lookup form instance containing the lookup data
 * @param propertyName property name of the search criteria field to be validated
 * @param searchPropertyValue value given for field to search for
 *//*from   w  w  w .  j a v  a  2  s .co m*/
protected void validateSearchParameterPositiveValues(LookupForm form, String propertyName,
        String searchPropertyValue) {
    if (StringUtils.isBlank(searchPropertyValue)) {
        return;
    }

    NumberFormat format = NumberFormat.getInstance();
    Number number = null;
    try {
        number = format.parse(searchPropertyValue);
    } catch (ParseException e) {
        return;
    }

    if (Math.signum(number.doubleValue()) < 0) {
        GlobalVariables.getMessageMap().putError(propertyName,
                RiceKeyConstants.ERROR_NEGATIVES_NOT_ALLOWED_ON_FIELD, getCriteriaLabel(form, propertyName));
    }
}

From source file:com.opengamma.analytics.financial.model.volatility.NormalImpliedVolatilityFormula.java

/**
 * Computes the implied volatility from the price in a normally distributed asset price world.
 * @param data The model data. The data volatility, if not zero, is used as a starting point for the volatility search.
 * @param option The option.// www .  j a v  a2  s. co m
 * @param optionPrice The option price.
 * @return The implied volatility.
 */
public double getImpliedVolatility(final NormalFunctionData data, final EuropeanVanillaOption option,
        final double optionPrice) {
    final double numeraire = data.getNumeraire();
    final boolean isCall = option.isCall();
    final double f = data.getForward();
    final double k = option.getStrike();
    final double intrinsicPrice = numeraire * Math.max(0, (isCall ? 1 : -1) * (f - k));
    Validate.isTrue(optionPrice > intrinsicPrice || CompareUtils.closeEquals(optionPrice, intrinsicPrice, 1e-6),
            "option price (" + optionPrice + ") less than intrinsic value (" + intrinsicPrice + ")");
    if (Double.doubleToLongBits(optionPrice) == Double.doubleToLongBits(intrinsicPrice)) {
        return 0.0;
    }
    double sigma = (Math.abs(data.getNormalVolatility()) < 1E-10 ? 0.3 * f : data.getNormalVolatility());
    NormalFunctionData newData = new NormalFunctionData(f, numeraire, sigma);
    final double maxChange = 0.5 * f;
    double[] priceDerivative = new double[3];
    double price = NORMAL_PRICE_FUNCTION.getPriceAdjoint(option, newData, priceDerivative);
    double vega = priceDerivative[1];
    double change = (price - optionPrice) / vega;
    double sign = Math.signum(change);
    change = sign * Math.min(maxChange, Math.abs(change));
    if (change > 0 && change > sigma) {
        change = sigma;
    }
    int count = 0;
    while (Math.abs(change) > EPS) {
        sigma -= change;
        newData = new NormalFunctionData(f, numeraire, sigma);
        price = NORMAL_PRICE_FUNCTION.getPriceAdjoint(option, newData, priceDerivative);
        vega = priceDerivative[1];
        change = (price - optionPrice) / vega;
        sign = Math.signum(change);
        change = sign * Math.min(maxChange, Math.abs(change));
        if (change > 0 && change > sigma) {
            change = sigma;
        }
        if (count++ > MAX_ITERATIONS) {
            final BracketRoot bracketer = new BracketRoot();
            final BisectionSingleRootFinder rootFinder = new BisectionSingleRootFinder(EPS);
            final Function1D<Double, Double> func = new Function1D<Double, Double>() {
                @SuppressWarnings({ "synthetic-access" })
                @Override
                public Double evaluate(final Double volatility) {
                    final NormalFunctionData myData = new NormalFunctionData(data.getForward(),
                            data.getNumeraire(), volatility);
                    return NORMAL_PRICE_FUNCTION.getPriceFunction(option).evaluate(myData) - optionPrice;
                }
            };
            final double[] range = bracketer.getBracketedPoints(func, 0.0, 10.0);
            return rootFinder.getRoot(func, range[0], range[1]);
        }
    }
    return sigma;
}

From source file:us.fatehi.pointlocation6709.PointLocation.java

/**
 * {@inheritDoc}//from   w  w  w.j a  v a  2s  .c o  m
 *
 * @see java.lang.Comparable#compareTo(java.lang.Object)
 */
@Override
public int compareTo(final PointLocation pointLocation) {
    int comparison;
    comparison = (int) Math.signum(altitude - pointLocation.altitude);
    if (comparison == 0) {
        comparison = latitude.compareTo(pointLocation.latitude);
    }
    if (comparison == 0) {
        comparison = longitude.compareTo(pointLocation.longitude);
    }
    return comparison;
}

From source file:com.opengamma.analytics.math.statistics.distribution.LaplaceDistribution.java

/**
 * {@inheritDoc}/*from  ww  w.  j ava2 s.c  om*/
 */
@Override
public double nextRandom() {
    final double u = _engine.nextDouble() - 0.5;
    return _mu - _b * Math.signum(u) * Math.log(1 - 2 * Math.abs(u));
}

From source file:de.biomedical_imaging.ij.steger.Width.java

public void bresenham(double nx, double ny, double px, double py, double length, Offset[] line,
        MutableInt num_points) {//from ww  w  .ja v  a  2s. c o  m
    int i, n, x, y, s1, s2, xchg, maxit;
    double e, dx, dy, t;

    x = 0;
    y = 0;
    dx = Math.abs(nx);
    dy = Math.abs(ny);
    s1 = (int) Math.signum(nx);
    s2 = (int) Math.signum(ny);
    px *= s1;
    py *= s2;
    if (dy > dx) {
        t = dx;
        dx = dy;
        dy = t;
        t = px;
        px = py;
        py = t;
        xchg = 1;
    } else {
        xchg = 0;
    }
    maxit = (int) Math.ceil(length * dx);
    e = (0.5 - px) * dy / dx - (0.5 - py);
    n = 0;
    for (i = 0; i <= maxit; i++) {
        line[n].x = x;
        line[n].y = y;
        n++;
        while (e >= -1e-8) {
            if (!(xchg == 0))
                x += s1;
            else
                y += s2;
            e--;
            if (e > -1) {
                line[n].x = x;
                line[n].y = y;
                n++;
            }
        }
        if (!(xchg == 0))
            y += s2;
        else
            x += s1;
        e += dy / dx;
    }
    num_points.setValue(n);
}