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

/**
 * Returns a {@link Collection} containing the intersection
 * of the given {@link Collection}s./*from   w  w  w  . j a v  a2  s  . co  m*/
 * <p>
 * The cardinality of each element in the returned {@link Collection}
 * will be equal to the minimum of the cardinality of that element
 * in the two given {@link Collection}s.
 *
 * @param a  the first collection, must not be null
 * @param b  the second collection, must not be null
 * @return the intersection of the two collections
 */
public static Collection intersection(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new HashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        for (int i = 0, m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

/**
 * Computes the lightness value in HSL standard for the given color.
 *//*from www.  j a  va2s.c o m*/
private static float getLightnessForColor(int color) {
    int red = Color.red(color);
    int green = Color.green(color);
    int blue = Color.blue(color);
    int largest = Math.max(red, Math.max(green, blue));
    int smallest = Math.min(red, Math.min(green, blue));
    int average = (largest + smallest) / 2;
    return average / 255.0f;
}

From source file:Main.java

private static Bitmap fastBlur(Context context, Bitmap sentBitmap, int radius) {

    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

    if (radius < 1) {
        return (null);
    }/*from  ww w. j  av  a 2 s.  co  m*/

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

    int[] pix = new int[w * h];
    //        Log.e("pix", 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 temp = 256 * divsum;
    int dv[] = new int[temp];
    for (i = 0; i < temp; 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("pix", w + " " + h + " " + pix.length);
    bitmap.setPixels(pix, 0, w, 0, 0, w, h);
    return (bitmap);
}

From source file:drpc.KMeansDrpcQuery.java

private static double computeRootMeanSquareDeviation(double[] v, double[] w) {
    double distance = 0;
    for (int i = 0; i < v.length && i < w.length; i++) {
        distance += Math.pow((v[i] - w[i]), 2);
    }/*from  w w w. ja  v  a 2 s .  c  o m*/
    return Math.sqrt(distance / (Math.min(v.length, w.length)));
}

From source file:Main.java

/**
 * Compares the initial elements of two arrays.
 *
 * @param a1  array 1./*from  w  ww.  j  a v a  2s. com*/
 * @param a2  array 2.
 *
 * @return An integer showing the relative ordering.
 */
public static int compareVersionArrays(Comparable[] a1, Comparable[] a2) {
    int length = Math.min(a1.length, a2.length);
    for (int i = 0; i < length; i++) {
        Comparable o1 = a1[i];
        Comparable o2 = a2[i];
        if (o1 == null && o2 == null) {
            // cannot decide ..
            continue;
        }
        if (o1 == null) {
            return 1;
        }
        if (o2 == null) {
            return -1;
        }
        int retval = o1.compareTo(o2);
        if (retval != 0) {
            return retval;
        }
    }
    return 0;
}

From source file:Main.java

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details.//  ww w  . jav a2s . c om
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = Math.max(0, Math.min(1, (float) Math.pow(x, 3)));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.START:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.END:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width * x0, height * y0, width * x1, height * y1, stopColors, null,
                    Shader.TileMode.CLAMP);
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}

From source file:cz.hobrasoft.pdfmu.PdfmuUtils.java

/**
 * Converts an array of keys and an array of values to a {@link Map}.
 *
 * @param <K> the type of keys.//from w  ww  .j av a 2  s  .com
 * @param <V> the type of values.
 * @param keys the array of keys. Only the keys that have a matching value
 * are used.
 * @param values the array of values. Only the values that have a matching
 * key are used.
 * @return a {@link Map} that contains a key-value pair for each index in
 * keys and values. The order of the entries is preserved by the underlying
 * implementation ({@link LinkedHashMap}).
 */
// TODO: Rename
public static <K, V> Map<K, V> sortedMap(K[] keys, V[] values) {
    Map<K, V> result = new LinkedHashMap<>();
    int n = Math.min(keys.length, values.length);
    for (int i = 0; i < n; ++i) {
        result.put(keys[i], values[i]);
    }
    return result;
}

From source file:ColorUtil.java

public static final Color add(Color c1, Color c2) {
    return c1 == null ? c2
            : c2 == null ? c1//from   ww w .j av a  2 s .c o m
                    : new Color(Math.min(255, c1.getRed() + c2.getRed()),
                            Math.min(255, c1.getGreen() + c2.getGreen()),
                            Math.min(255, c1.getBlue() + c2.getBlue()), c1.getAlpha());
}

From source file:de.dhke.projects.cutil.collections.BagUtil.java

public static <T> Bag<T> intersectBags(final Bag<T> b0, final Bag<T> b1, final Bag<T> targetBag) {
    targetBag.clear();//w  w w.j av  a 2 s .  c om
    for (T item : b0) {
        final int count0 = b0.getCount(item);
        final int count1 = b1.getCount(item);
        if ((count0 > 0) && (count1 > 0)) {
            targetBag.add(item, Math.min(count0, count1));
        }
    }
    return targetBag;
}

From source file:Main.java

/**
 * Resamples the captured photo to fit the screen for better memory usage.
 *
 * @param context   The application context.
 * @param imagePath The path of the photo to be resampled.
 * @return The resampled bitmap/*from w  w  w  .java 2s .  c  om*/
 */
static Bitmap resamplePic(Context context, String imagePath) {

    // Get device screen size information
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    manager.getDefaultDisplay().getMetrics(metrics);

    int targetH = metrics.heightPixels;
    int targetW = metrics.widthPixels;

    // Get the dimensions of the original bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;

    return BitmapFactory.decodeFile(imagePath);
}