Example usage for java.lang Math abs

List of usage examples for java.lang Math abs

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double abs(double a) 

Source Link

Document

Returns the absolute value of a double value.

Usage

From source file:cn.edu.suda.core.stats.StatsUtils.java

public static DataMatrix addTTest(DataMatrix dm, int m, int n) {
    int col = dm.getDcol();
    dm.addCols(2);//from ww w  .  j av a2  s. co  m
    dm.setColname(col, "ABS_t_value");
    dm.setColname(col + 1, "P_value");
    for (int i = 0; i < dm.getDrow(); i++) {
        double[] array = dm.getRow(i);
        SummaryStatistics stats1 = new SummaryStatistics();
        SummaryStatistics stats2 = new SummaryStatistics();
        for (int j = 0; j < m; j++) {
            stats1.addValue(array[j]);
        }
        for (int j = m; j < m + n; j++) {
            stats2.addValue(array[j]);
        }
        double var1 = stats1.getVariance();
        double var2 = stats2.getVariance();
        if (var1 == 0 && var2 == 0) {
            dm.setValue(i, col, 0);
            dm.setValue(i, col + 1, 1);
        } else {
            double t = Math.abs(TestUtils.t(stats1, stats2));
            double p = TestUtils.tTest(stats1, stats2);
            t = Utils.formatNumber(t, 4);
            p = Utils.formatNumber(p, 4);
            dm.setValue(i, col, t);
            dm.setValue(i, col + 1, p);
        }
    }
    return dm;
}

From source file:Main.java

/**
 * Iterate over supported camera preview sizes to see which one best fits the
 * dimensions of the given view while maintaining the aspect ratio. If none can,
 * be lenient with the aspect ratio.//from   www. j  av a2 s  . com
 *
 * @param sizes Supported camera preview sizes.
 * @param w The width of the view.
 * @param h The height of the view.
 * @return Best match camera preview size to fit in the view.
 */
public static Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {

    Log.d(TAG, "getOptimalPreviewSize");

    // Use a very small tolerance because we want an exact match.
    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio = (double) w / h;
    if (sizes == null)
        return null;

    Camera.Size optimalSize = null;

    // Start with max value and refine as we iterate over available preview sizes. This is the
    // minimum difference between view and camera height.
    double minDiff = Double.MAX_VALUE;

    // Target view height
    int targetHeight = h;

    // Try to find a preview size that matches aspect ratio and the target view size.
    // Iterate over all available sizes and pick the largest size that can fit in the view and
    // still maintain the aspect ratio.
    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
            continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    // Cannot find preview size that matches the aspect ratio, ignore the requirement
    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}

From source file:ch.algotrader.option.SABR.java

/**
 * Perfors a SABR calibartion based on specified volatilities.
 *
 * @return SABRSmileVO The SABR smile/*from   w  w  w .ja  va  2  s.co m*/
 */
public static SABRSmileVO calibrate(final Double[] strikes, final Double[] volatilities, final double atmVol,
        final double forward, final double years) throws SABRException {

    MultivariateRealFunction estimateRhoAndVol = x -> {

        double r = x[0];
        double v = x[1];
        double alpha = findAlpha(forward, forward, atmVol, years, beta, x[0], x[1]);
        double sumErrors = 0;

        for (int i = 0; i < volatilities.length; i++) {

            double modelVol = vol(forward, strikes[i], years, alpha, beta, r, v);
            sumErrors += Math.pow(modelVol - volatilities[i], 2);
        }

        if (Math.abs(r) > 1) {
            sumErrors = 1e100;
        }

        return sumErrors;
    };

    NelderMead nelderMead = new NelderMead();
    RealPointValuePair result;
    try {
        result = nelderMead.optimize(estimateRhoAndVol, GoalType.MINIMIZE, new double[] { -0.5, 2.6 });
    } catch (MathException ex) {
        throw new SABRException(ex.getMessage(), ex);
    }

    double rho = result.getPoint()[0];
    double volVol = result.getPoint()[1];

    SABRSmileVO params = new SABRSmileVO();
    params.setYears(years);
    params.setRho(rho);
    params.setVolVol(volVol);
    params.setAlpha(findAlpha(forward, forward, atmVol, years, beta, rho, volVol));
    params.setAtmVol(atmVol);

    return params;
}

From source file:Main.java

static double betacf(final double a, final double b, final double x) {
    final int maxIterations = 50;
    int m = 1;/*from   w w  w. j  a v a2 s . c  o  m*/
    final double eps = 3.0E-5;
    double am = 1.0;
    double bm = 1.0;
    double az = 1.0;
    final double qab = a + b;
    final double qap = a + 1.0;
    final double qam = a - 1.0;
    double bz = 1.0 - qab * x / qap;
    double ap;
    double bp;
    double app;
    double bpp;
    for (double aold = 0.0; m < 50 && Math.abs(az - aold) >= 3.0E-5 * Math.abs(az); aold = az, am = ap
            / bpp, bm = bp / bpp, az = app / bpp, bz = 1.0, ++m) {
        final double em = m;
        final double tem = em + em;
        double d = em * (b - m) * x / ((qam + tem) * (a + tem));
        ap = az + d * am;
        bp = bz + d * bm;
        d = -(a + em) * (qab + em) * x / ((a + tem) * (qap + tem));
        app = ap + d * az;
        bpp = bp + d * bz;
    }
    return az;
}

From source file:com.metawiring.load.generators.RandomStringRangeGenerator.java

@Override
public String get() {
    long value = Math.abs(theTwister.nextLong());
    value %= length;
    value += min;
    return String.valueOf(value);
}

From source file:Main.java

/**
 * A fast blur algorithm. This is not being used. Instead
 * use render script {RSBlur} as it is fast and uses the GPU for it.
 * @param sentBitmap//from   w  w w  .  j  a va2 s. c om
 * @param radius
 * @return
 */
public static Bitmap FastBlur(Bitmap sentBitmap, int radius) {
    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

    if (radius < 1) {
        return (null);
    }

    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int[] pix = new int[w * h];
    Log.e(TAG, w + " " + h + " " + pix.length);
    bitmap.getPixels(pix, 0, w, 0, 0, w, h);

    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;

    int r[] = new int[wh];
    int g[] = new int[wh];
    int b[] = new int[wh];
    int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
    int vmin[] = new int[Math.max(w, h)];

    int divsum = (div + 1) >> 1;
    divsum *= divsum;
    int dv[] = new int[256 * divsum];
    for (i = 0; i < 256 * divsum; i++) {
        dv[i] = (i / divsum);
    }

    yw = yi = 0;

    int[][] stack = new int[div][3];
    int stackpointer;
    int stackstart;
    int[] sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum;
    int rinsum, ginsum, binsum;

    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        for (i = -radius; i <= radius; i++) {
            p = pix[yi + Math.min(wm, Math.max(i, 0))];
            sir = stack[i + radius];
            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            rbs = r1 - Math.abs(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }
        }
        stackpointer = radius;

        for (x = 0; x < w; x++) {

            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (y == 0) {
                vmin[x] = Math.min(x + radius + 1, wm);
            }
            p = pix[yw + vmin[x]];

            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[(stackpointer) % div];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        yp = -radius * w;
        for (i = -radius; i <= radius; i++) {
            yi = Math.max(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];
            sir[1] = g[yi];
            sir[2] = b[yi];

            rbs = r1 - Math.abs(i);

            rsum += r[yi] * rbs;
            gsum += g[yi] * rbs;
            bsum += b[yi] * rbs;

            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }

            if (i < hm) {
                yp += w;
            }
        }
        yi = x;
        stackpointer = radius;
        for (y = 0; y < h; y++) {
            // Preserve alpha channel: ( 0xff000000 & pix[yi] )
            pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (x == 0) {
                vmin[y] = Math.min(y + r1, hm) * w;
            }
            p = x + vmin[y];

            sir[0] = r[p];
            sir[1] = g[p];
            sir[2] = b[p];

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[stackpointer];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi += w;
        }
    }

    Log.e(TAG, w + " " + h + " " + pix.length);
    bitmap.setPixels(pix, 0, w, 0, 0, w, h);

    return (bitmap);
}

From source file:MainClass.java

public BufferedImage emboss(BufferedImage src) {
    int width = src.getWidth();
    int height = src.getHeight();

    BufferedImage dst;//w  w  w. j  a v  a2 s.  com
    dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++) {
            int upperLeft = 0;
            int lowerRight = 0;

            if (i > 0 && j > 0)
                upperLeft = src.getRGB(j - 1, i - 1);

            if (i < height - 1 && j < width - 1)
                lowerRight = src.getRGB(j + 1, i + 1);

            int redDiff = ((lowerRight >> 16) & 255) - ((upperLeft >> 16) & 255);

            int greenDiff = ((lowerRight >> 8) & 255) - ((upperLeft >> 8) & 255);

            int blueDiff = (lowerRight & 255) - (upperLeft & 255);

            int diff = redDiff;
            if (Math.abs(greenDiff) > Math.abs(diff))
                diff = greenDiff;
            if (Math.abs(blueDiff) > Math.abs(diff))
                diff = blueDiff;

            int grayColor = 128 + diff;

            if (grayColor > 255)
                grayColor = 255;
            else if (grayColor < 0)
                grayColor = 0;

            int newColor = (grayColor << 16) + (grayColor << 8) + grayColor;

            dst.setRGB(j, i, newColor);
        }

    return dst;
}

From source file:com.opengamma.analytics.financial.model.option.pricing.fourier.MeanCorrection.java

/**
 * Returns the mean (drift) corrected characteristic exponent
 * @param t The time//from  w  w w.j a  v  a2 s. c o m
 * @return A function to calculate the characteristic exponent
 */
@Override
public Function1D<ComplexNumber, ComplexNumber> getFunction(final double t) {

    final Function1D<ComplexNumber, ComplexNumber> func = _base.getFunction(t);
    final ComplexNumber temp = func.evaluate(MINUS_I);
    Validate.isTrue(Math.abs(temp.getImaginary()) < 1e-12, "problem with CharacteristicExponent");
    final ComplexNumber w = new ComplexNumber(0, -temp.getReal());

    return new Function1D<ComplexNumber, ComplexNumber>() {
        @Override
        public ComplexNumber evaluate(final ComplexNumber u) {
            return add(func.evaluate(u), multiply(w, u));
        }
    };
}

From source file:com.opengamma.analytics.financial.interestrate.InterestRateCurveSensitivityUtils.java

/**
 * Takes a list of curve sensitivities (i.e. an unordered list of pairs of times and sensitivities) and returns a list order by ascending
 * time, and with sensitivities that occur at the same time netted (zero net sensitivities are removed)
 * @param old An unordered list of pairs of times and sensitivities
 * @param relTol Relative tolerance - if the net divided by gross sensitivity is less than this it is ignored/removed
 * @param absTol Absolute tolerance  - is the net sensitivity is less than this it is ignored/removed
 * @return A time ordered netted list//from w ww  .j  a va 2s. c o  m
 */
static final List<DoublesPair> clean(final List<DoublesPair> old, final double relTol, final double absTol) {

    Validate.notNull(old, "null list");
    Validate.isTrue(relTol >= 0.0 && absTol >= 0.0);
    if (old.size() == 0) {
        return new ArrayList<DoublesPair>();
    }
    final List<DoublesPair> res = new ArrayList<DoublesPair>();
    final DoublesPair[] sort = old.toArray(new DoublesPair[] {});
    Arrays.sort(sort, FirstThenSecondDoublesPairComparator.INSTANCE);
    final DoublesPair pairOld = sort[0];
    double tOld = pairOld.first;
    double sum = pairOld.getSecond();
    double scale = Math.abs(sum);
    double t = tOld;
    for (int i = 1; i < sort.length; i++) {
        final DoublesPair pair = sort[i];
        t = pair.first;
        if (t > tOld) {
            if (Math.abs(sum) > absTol && Math.abs(sum) / scale > relTol) {
                res.add(new DoublesPair(tOld, sum));
            }
            tOld = t;
            sum = pair.getSecondDouble();
            scale = Math.abs(sum);
        } else {
            sum += pair.getSecondDouble();
            scale += Math.abs(pair.getSecondDouble());
        }
    }

    if (Math.abs(sum) > absTol && Math.abs(sum) / scale > relTol) {
        res.add(new DoublesPair(t, sum));
    }

    return res;
}

From source file:com.machinepublishers.jbrowserdriver.Util.java

private static String randomAlphanumeric() {
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < 4; i++) {
        builder.append(Long.toString(Math.abs(secureRand.nextInt()), Math.min(36, Character.MAX_RADIX)));
    }/*  www. ja  va2 s .c  om*/
    return builder.toString();
}