Example usage for java.lang Math sqrt

List of usage examples for java.lang Math sqrt

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double sqrt(double a) 

Source Link

Document

Returns the correctly rounded positive square root of a double value.

Usage

From source file:Main.java

/**
 * Calculates the <em>brightness</em> of a color, based on its RGB values.
 * @param color the color whose brightness is in question
 * @return a brightness value in the range [0-255].
 * @see <a href="http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx">Calculating the Perceived Brightness of a Color</a>
 *//*w w w.j a  va 2s.  co  m*/
public static int colorBrightness(int color) {
    int r2 = Color.red(color) * Color.red(color);
    int g2 = Color.green(color) * Color.green(color);
    int b2 = Color.blue(color) * Color.blue(color);

    return (int) Math.sqrt(r2 * 0.241f + g2 * 0.691f + b2 * 0.068f);
}

From source file:EndmemberExtraction.java

public static ArrayList<Integer> ORASIS(double[][] data, int nData, int nDim, double threshold,
        int[] exemplarLabel) {
    ArrayRealVector vec;/*from   w w w .  j a v a  2 s  .c om*/
    ArrayList<ArrayRealVector> X = new ArrayList<>();
    ArrayList<ArrayRealVector> E = new ArrayList<>();
    ArrayList<Integer> exemplarIndex = new ArrayList<>();

    for (int i = 0; i < nData; i++) {
        vec = new ArrayRealVector(data[i]);
        vec.unitize();
        X.add(vec);
    }

    E.add(X.get(0));
    exemplarIndex.add(0);
    double t = Math.sqrt(2 * (1 - threshold));

    //Add first element of test spectra to set of exemplar spectra
    exemplarLabel[0] = 0;

    boolean flag;
    double maxCos, sigmaMin, sigmaMax, dotXR, dotER, cosTheta;

    double[] vecR = new double[nDim];
    for (int i = 0; i < nDim; i++) {
        vecR[i] = 1 / Math.sqrt(nDim);
    }
    ArrayRealVector R = new ArrayRealVector(vecR);

    ArrayRealVector exemplarSpec, testSpec;

    for (int i = 0; i < X.size(); i++) {
        if (i == 0 || exemplarLabel[i] == -1) {
            continue;
        }

        flag = false;
        maxCos = 0;
        testSpec = X.get(i);
        dotXR = testSpec.dotProduct(R);
        sigmaMin = dotXR - t;
        sigmaMax = dotXR + t;

        for (int j = 0; j < E.size(); j++) {
            exemplarSpec = E.get(j);
            dotER = exemplarSpec.dotProduct(R);

            if (dotER < sigmaMax && dotER > sigmaMin) {
                cosTheta = testSpec.dotProduct(exemplarSpec);

                if (cosTheta > threshold) {
                    //Test spectra is similar to one of the exemplar spectra
                    if (cosTheta > maxCos) {
                        maxCos = cosTheta;
                        exemplarLabel[i] = j;
                        //System.out.println("Count: "+i+"\texemplarLabel: "+exemplarLabel[i]);
                        flag = true;
                    }
                }
            }
        }

        if (!flag) {
            //Test spectra is unique, add it to set of exemplars
            E.add(testSpec);
            exemplarIndex.add(i);
            exemplarLabel[i] = E.size() - 1;
            //System.out.println("Count: "+i+"\texemplarLabel: "+exemplarLabel[i]);
        }

    }
    return exemplarIndex;
}

From source file:Main.java

/**
 * Determine the size of screen for this device.
 *
 * @param context//from w ww  .  j  av  a  2s  .c  o m
 * @return double
 * @deprecated
 */
@Deprecated
public static double tabletSize(Context context) {

    double size = 0;
    try {
        // Compute screen size
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        float screenWidth = dm.widthPixels / dm.xdpi;
        float screenHeight = dm.heightPixels / dm.ydpi;
        //noinspection SuspiciousNameCombination
        size = Math.sqrt(Math.pow(screenWidth, 2) + Math.pow(screenHeight, 2));
    } catch (Throwable ignored) {

    }

    return size;

}

From source file:Main.java

/**
 * This picks a dominant color, looking for high-saturation, high-value, repeated hues.
 * @param bitmap The bitmap to scan/* w  w  w  .  j a  v a2 s  .c  o  m*/
 * @param samples The approximate max number of samples to use.
 */
static int findDominantColorByHue(Bitmap bitmap, int samples) {
    final int height = bitmap.getHeight();
    final int width = bitmap.getWidth();
    int sampleStride = (int) Math.sqrt((height * width) / samples);
    if (sampleStride < 1) {
        sampleStride = 1;
    }

    // This is an out-param, for getting the hsv values for an rgb
    float[] hsv = new float[3];

    // First get the best hue, by creating a histogram over 360 hue buckets,
    // where each pixel contributes a score weighted by saturation, value, and alpha.
    float[] hueScoreHistogram = new float[360];
    float highScore = -1;
    int bestHue = -1;

    for (int y = 0; y < height; y += sampleStride) {
        for (int x = 0; x < width; x += sampleStride) {
            int argb = bitmap.getPixel(x, y);
            int alpha = 0xFF & (argb >> 24);
            if (alpha < 0x80) {
                // Drop mostly-transparent pixels.
                continue;
            }
            // Remove the alpha channel.
            int rgb = argb | 0xFF000000;
            Color.colorToHSV(rgb, hsv);
            // Bucket colors by the 360 integer hues.
            int hue = (int) hsv[0];
            if (hue < 0 || hue >= hueScoreHistogram.length) {
                // Defensively avoid array bounds violations.
                continue;
            }
            float score = hsv[1] * hsv[2];
            hueScoreHistogram[hue] += score;
            if (hueScoreHistogram[hue] > highScore) {
                highScore = hueScoreHistogram[hue];
                bestHue = hue;
            }
        }
    }

    SparseArray<Float> rgbScores = new SparseArray<Float>();
    int bestColor = 0xff000000;
    highScore = -1;
    // Go back over the RGB colors that match the winning hue,
    // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets.
    // The highest-scoring RGB color wins.
    for (int y = 0; y < height; y += sampleStride) {
        for (int x = 0; x < width; x += sampleStride) {
            int rgb = bitmap.getPixel(x, y) | 0xff000000;
            Color.colorToHSV(rgb, hsv);
            int hue = (int) hsv[0];
            if (hue == bestHue) {
                float s = hsv[1];
                float v = hsv[2];
                int bucket = (int) (s * 100) + (int) (v * 10000);
                // Score by cumulative saturation * value.
                float score = s * v;
                Float oldTotal = rgbScores.get(bucket);
                float newTotal = oldTotal == null ? score : oldTotal + score;
                rgbScores.put(bucket, newTotal);
                if (newTotal > highScore) {
                    highScore = newTotal;
                    // All the colors in the winning bucket are very similar. Last in wins.
                    bestColor = rgb;
                }
            }
        }
    }
    return bestColor;
}

From source file:Main.java

public static double distance(float x1, float y1, float x2, float y2) {
    float x = x1 - x2;
    float y = y1 - y2;
    return Math.sqrt(x * x + y * y);
}

From source file:Main.java

public static float distance(float[] p1, float[] p2) {
    return (float) Math.sqrt(squareDistance(p1, p2));
}

From source file:com.example.PJS.java

public static double N(double z) {
    /**/*from   w w  w. j a  va2  s . c o m*/
    * Normal Distribution Function, PDF probability density function
    *Odegaard Dic 2003, page 129
    */
    double n = 1 / Math.sqrt(2 * Math.PI) * Math.exp(-0.5 * z * z);
    return n;
}

From source file:Main.java

static double arcInRadians(Location A, Location B) {
    double latitudeArc = (A.getLatitude() - B.getLatitude()) * DEG_TO_RAD;
    double longitudeArc = (A.getLongitude() - B.getLongitude()) * DEG_TO_RAD;
    double latitudeH = Math.sin(latitudeArc * 0.5);
    latitudeH *= latitudeH;//www .  j a  va 2  s.c  o  m
    double lontitudeH = Math.sin(longitudeArc * 0.5);
    lontitudeH *= lontitudeH;
    double tmp = Math.cos(A.getLatitude() * DEG_TO_RAD) * Math.cos(B.getLatitude() * DEG_TO_RAD);
    return 2.0 * Math.asin(Math.sqrt(latitudeH + tmp * lontitudeH));
}

From source file:Main.java

public static double[] calculateKcal(double[][] data, int samplesPerWindow) {

    double res[] = new double[2]; // holds V and H result
    double history[] = new double[3];
    double d[] = new double[3];
    double p[] = new double[3];

    // this is historical average of the past samples
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < samplesPerWindow; j++) {
            history[i] += data[i][j];// w ww  .  j a  v a 2  s  .  c o m
        }
        history[i] /= samplesPerWindow;
    }

    for (int j = 0; j < samplesPerWindow; j++) {

        for (int i = 0; i < 3; i++) {
            d[i] = history[i] - data[i][j];
        }

        double num = 0;
        double den = 0;
        double value = 0;
        for (int i = 0; i < 3; i++) {
            num = (d[0] * history[0] + d[1] * history[1] + d[2] * history[2]);
            den = (history[0] * history[0] + history[1] * history[1] + history[2] * history[2]);

            if (den == 0)
                den = 0.01;
            value = (num / den) * history[i];
            p[i] = value;
        }

        double pMagn = p[0] * p[0] + p[1] * p[1] + p[2] * p[2];

        res[0] += Math.sqrt(pMagn);

        res[1] += Math.sqrt(
                (d[0] - p[0]) * (d[0] - p[0]) + (d[1] - p[1]) * (d[1] - p[1]) + (d[2] - p[2]) * (d[2] - p[2]));
    }
    return res;
}

From source file:Main.java

public static void startActivity(final Activity thisActivity, final Intent intent, final View triggerView,
        int colorOrImageRes, final long durationMills) {
    int[] location = new int[2];
    triggerView.getLocationInWindow(location);
    final int cx = location[0] + triggerView.getWidth();
    final int cy = location[1] + triggerView.getHeight() + (int) TypedValue
            .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 160, thisActivity.getResources().getDisplayMetrics());
    final ImageView view = new ImageView(thisActivity);
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setImageResource(colorOrImageRes);
    final ViewGroup decorView = (ViewGroup) thisActivity.getWindow().getDecorView();
    int w = decorView.getWidth();
    int h = decorView.getHeight();
    decorView.addView(view, w, h);//from www  .j av  a 2 s.c om
    final int finalRadius = (int) Math.sqrt(w * w + h * h) + 1;
    Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
    anim.setDuration(durationMills);
    anim.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            thisActivity.startActivity(intent);
            thisActivity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    });
    anim.start();
}