Example usage for java.lang Math min

List of usage examples for java.lang Math min

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double min(double a, double b) 

Source Link

Document

Returns the smaller of two double values.

Usage

From source file:Main.java

/**
 * Fills the array with random floats.  Values will be between min (inclusive) and
 * max (inclusive)./*  ww  w. j  av  a2s .  c  o  m*/
 */
public static void genRandomFloats(long seed, float min, float max, float array[], boolean includeExtremes) {
    Random r = new Random(seed);
    int minExponent = Math.min(Math.getExponent(min), 0);
    int maxExponent = Math.max(Math.getExponent(max), 0);
    if (minExponent < -6 || maxExponent > 6) {
        // Use an exponential distribution
        int exponentDiff = maxExponent - minExponent;
        for (int i = 0; i < array.length; i++) {
            float mantissa = r.nextFloat();
            int exponent = minExponent + r.nextInt(maxExponent - minExponent);
            int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
            float rand = sign * mantissa * (float) Math.pow(2.0, exponent);
            if (rand < min || rand > max) {
                continue;
            }
            array[i] = rand;
        }
    } else {
        // Use a linear distribution
        for (int i = 0; i < array.length; i++) {
            float rand = r.nextFloat();
            array[i] = min + rand * (max - min);
        }
    }
    // Seed a few special numbers we want to be sure to test.
    for (int i = 0; i < sInterestingDoubles.length; i++) {
        float f = (float) sInterestingDoubles[i];
        if (min <= f && f <= max) {
            array[r.nextInt(array.length)] = f;
        }
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
    if (includeExtremes) {
        array[r.nextInt(array.length)] = Float.NaN;
        array[r.nextInt(array.length)] = Float.POSITIVE_INFINITY;
        array[r.nextInt(array.length)] = Float.NEGATIVE_INFINITY;
        array[r.nextInt(array.length)] = Float.MIN_VALUE;
        array[r.nextInt(array.length)] = Float.MIN_NORMAL;
        array[r.nextInt(array.length)] = Float.MAX_VALUE;
        array[r.nextInt(array.length)] = -Float.MIN_VALUE;
        array[r.nextInt(array.length)] = -Float.MIN_NORMAL;
        array[r.nextInt(array.length)] = -Float.MAX_VALUE;
    }
}

From source file:Main.java

private static int lightenColor(int color, float fraction) {
    return (int) Math.min(color + (color * fraction), 255);
}

From source file:Main.java

/**
 * Returns Width or Height of the picture, depending on which size is smaller. Doesn't actually
 * decode the picture, so it is pretty efficient to run.
 *//*from   www.jav a2s  .com*/
public static int getSmallerExtentFromBytes(byte[] bytes) {
    final BitmapFactory.Options options = new BitmapFactory.Options();

    // don't actually decode the picture, just return its bounds
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);

    // test what the best sample size is
    return Math.min(options.outWidth, options.outHeight);
}

From source file:Main.java

public static void setupXAxis(NumberAxis numberAxis,
        ObservableList<XYChart.Series<Number, Number>> seriesList) {
    long min = Long.MAX_VALUE;
    long max = 0;

    for (XYChart.Series<Number, ?> series : seriesList) {
        for (Data<Number, ?> data : series.getData()) {
            min = Math.min(data.getXValue().longValue(), min);
            max = Math.max(data.getXValue().longValue(), max);
        }//w  ww. ja  v  a2 s  .com
    }
    setupXAxis(numberAxis, min, max);
}

From source file:Main.java

public static float blendDarken(float cS, float cB) {
    return Math.min(cS, cB);
}

From source file:Main.java

/**
 * Fills the array with random doubles.  Values will be between min (inclusive) and
 * max (inclusive)./*from w ww.j  a v a  2 s  . c  o  m*/
 */
public static void genRandomDoubles(long seed, double min, double max, double array[],
        boolean includeExtremes) {
    Random r = new Random(seed);
    int minExponent = Math.min(Math.getExponent(min), 0);
    int maxExponent = Math.max(Math.getExponent(max), 0);
    if (minExponent < -6 || maxExponent > 6) {
        // Use an exponential distribution
        int exponentDiff = maxExponent - minExponent;
        for (int i = 0; i < array.length; i++) {
            double mantissa = r.nextDouble();
            int exponent = minExponent + r.nextInt(maxExponent - minExponent);
            int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
            double rand = sign * mantissa * Math.pow(2.0, exponent);
            if (rand < min || rand > max) {
                continue;
            }
            array[i] = rand;
        }
    } else {
        // Use a linear distribution
        for (int i = 0; i < array.length; i++) {
            double rand = r.nextDouble();
            array[i] = min + rand * (max - min);
        }
    }
    // Seed a few special numbers we want to be sure to test.
    for (int i = 0; i < sInterestingDoubles.length; i++) {
        double d = sInterestingDoubles[i];
        if (min <= d && d <= max) {
            array[r.nextInt(array.length)] = d;
        }
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
    if (includeExtremes) {
        array[r.nextInt(array.length)] = Double.NaN;
        array[r.nextInt(array.length)] = Double.POSITIVE_INFINITY;
        array[r.nextInt(array.length)] = Double.NEGATIVE_INFINITY;
        array[r.nextInt(array.length)] = Double.MIN_VALUE;
        array[r.nextInt(array.length)] = Double.MIN_NORMAL;
        array[r.nextInt(array.length)] = Double.MAX_VALUE;
        array[r.nextInt(array.length)] = -Double.MIN_VALUE;
        array[r.nextInt(array.length)] = -Double.MIN_NORMAL;
        array[r.nextInt(array.length)] = -Double.MAX_VALUE;
    }
}

From source file:Main.java

public static Bitmap emboss(Bitmap bmp) {
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;

    int pixColor = 0;

    int newR = 0;
    int newG = 0;
    int newB = 0;

    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);
    int pos = 0;//  w  ww  .  j  av a 2  s .c  o  m
    for (int i = 1, length = height - 1; i < length; i++) {
        for (int k = 1, len = width - 1; k < len; k++) {
            pos = i * width + k;
            pixColor = pixels[pos];

            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);

            pixColor = pixels[pos + 1];
            newR = Color.red(pixColor) - pixR + 127;
            newG = Color.green(pixColor) - pixG + 127;
            newB = Color.blue(pixColor) - pixB + 127;

            newR = Math.min(255, Math.max(0, newR));
            newG = Math.min(255, Math.max(0, newG));
            newB = Math.min(255, Math.max(0, newB));

            pixels[pos] = Color.argb(255, newR, newG, newB);
        }
    }

    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:Main.java

/**
 * Centers the frame on the screen and sets its bounds. THis method should be
 * used after you call frame.pack() or frame.setSize().
 * @param frame//from   w  ww  .  j  av a2s . co m
 */
public static void centerFrame(JFrame frame) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Point center = ge.getCenterPoint();
    Rectangle bounds = ge.getMaximumWindowBounds();
    int w = Math.max(bounds.width / 2, Math.min(frame.getWidth(), bounds.width));
    int h = Math.max(bounds.height / 2, Math.min(frame.getHeight(), bounds.height));
    int x = center.x - w / 2, y = center.y - h / 2;

    frame.setBounds(x, y, w, h);

    if ((w == bounds.width) && (h == bounds.height)) {
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    }

    frame.validate();
}

From source file:ca.uwaterloo.iss4e.algorithm.PARX.java

public static Pair prepareVariable(final BigDecimal[] observations, int currentIndex, int trainingSize,
        int order, int seasons) throws SMASException {
    int numberOfElem = Math.min(trainingSize, currentIndex + 1);
    int size = numberOfElem / seasons;
    if (size < order + 1) {
        throw new SMASException("The number of backward elements should be at least " + (order + 1) * seasons);
    }/*from w w  w.j av  a2s.  c  o m*/
    double[] Y = new double[size];
    double[][] X = new double[size][order];
    for (int i = 0; i < size; ++i) {
        int idx = currentIndex - i * seasons;
        Y[i] = observations[idx].doubleValue();
        for (int j = 0; j < order; ++j) {
            X[i][order - 1 - j] = observations[idx - 1 - j].doubleValue();
        }
    }
    return new Pair(Y, X);
}

From source file:Main.java

public static int[] histogram(float[] dataPoints) {

    int[] buckets = { 0, 0, 0, 0, 0 }; // 5 buckets
    float minVal = 1000000.0F;
    float maxVal = -minVal;
    int numOfDataPoints = dataPoints.length;

    // Get the min and max values of the data points
    for (int i = 0; i < numOfDataPoints; i++) {

        minVal = Math.min(minVal, Math.abs(dataPoints[i]));
        maxVal = Math.max(maxVal, Math.abs(dataPoints[i]));
    }/*  www .  j a v a  2  s  . c  o m*/

    float bucketSize = (maxVal - minVal) / 5;
    // float bucketSize = 0.002F;

    // Count the number of points for each bucket
    for (int i = 0; i < numOfDataPoints; i++) {

        float val = Math.abs(dataPoints[i]);
        if (val <= minVal + bucketSize)
            buckets[0] = buckets[0] + 1; // between minVal and minVal + bucketSize
        else if (val <= minVal + 2 * bucketSize)
            buckets[1] = buckets[1] + 1; // between minVal and minVal + 2* bucketSize
        else if (val <= minVal + 3 * bucketSize)
            buckets[2] = buckets[2] + 1; // between minVal and minVal + 3* bucketSize
        else if (val <= minVal + 4 * bucketSize)
            buckets[3] = buckets[3] + 1; // between minVal and minVal + 4* bucketSize
        else
            buckets[4] = buckets[4] + 1; // greater than minVal + 4* bucketSize
    }
    return buckets;
}