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:Main.java

public static final double[] realSymetricMatrix2x2(final double ixx, final double iyy, final double ixy) {
    // Matrix: [ Ixx Ixy ; Ixy Iyy ];

    final double term = Math.sqrt((ixx - iyy) * (ixx - iyy) + 4 * ixy * ixy);

    final double mu_1 = 0.5 * (ixx + iyy + term);
    final double mu_2 = 0.5 * (ixx + iyy - term);

    if (Math.abs(iyy) > Float.MIN_VALUE) {

        final double cos = 2 * ixy;
        final double sin = iyy - ixx + term;
        final double norm = Math.sqrt(cos * cos + sin * sin);
        if (norm > Float.MIN_VALUE) {
            return new double[] { mu_1, mu_2, cos / norm, sin / norm };
        }/*from  ww w . ja  v  a 2 s.  c  om*/

    }

    // Edge case logic

    // NB BDZ - cosAlpha and sinAlpha edge cases determined by comparing
    // Float.MIN_VALUE cases to values near it to see trend lines.

    double cosAlpha;
    double sinAlpha;

    // default cosAlpha and sinAlpha

    if (ixx < 0) {
        cosAlpha = 0;
        sinAlpha = 1;
    } else if (iyy >= 0) {
        if (ixy >= 0) {
            cosAlpha = 1;
            sinAlpha = 0;
        } else { // ixy < 0
            cosAlpha = -1;
            sinAlpha = 0;
        }
    } else { // iyy < 0
        if (ixy >= 0) {
            cosAlpha = 1;
            sinAlpha = 0;
        } else { // ixy < 0
            cosAlpha = -1;
            sinAlpha = 0;
        }
    }

    return new double[] { mu_1, mu_2, cosAlpha, sinAlpha };

}

From source file:SiteSelector.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    int siteIndex = Math.abs(random.nextInt()) % sites.size();
    String site = sites.elementAt(siteIndex);

    res.setStatus(res.SC_MOVED_TEMPORARILY);
    res.setHeader("Location", site);
}

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.// w  w w . j  a va  2  s. c  o m
 *
 * @param sizes Camera 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) {
    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio = w > h ? (double) w / h : (double) h / w;

    if (sizes == null)
        return null;

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    // Try to find an size match aspect ratio and size
    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 - h) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - h);
        }
    }

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

    return optimalSize;
}

From source file:PowerMethod.power_method.java

public static power_object power_method(RealMatrix A, RealMatrix v, double epsilon, int N) {
    RealMatrix uInit = v;//from ww  w . j  a  va 2 s .  c o m
    double k = 0;
    double prevK = 0;
    int num = 0;
    double accuracy;
    for (int i = N; i > 0; i--) {
        RealMatrix uN = A.multiply(uInit);
        k = uN.getEntry(0, 0);
        for (int j = 0; j < uN.getRowDimension(); j++) {
            uN.setEntry(j, 0, uN.getEntry(j, 0) / k);
        }
        uInit = uN;

        accuracy = Math.abs(k - prevK);
        if (accuracy <= epsilon) {
            break;
        }

        if (accuracy > epsilon && i == 1) {
            return null;
        }

        prevK = k;
        num++;
    }
    double eVal = k;
    RealMatrix eVec = new Array2DRowRealMatrix(uInit.getRowDimension(), uInit.getColumnDimension());

    eVec.setColumnMatrix(0, uInit.getColumnMatrix(0));
    power_object retVal = new power_object(eVal, eVec, num);
    return retVal;
}

From source file:Main.java

public static <T> T generateRandom(Class<T> objectClass) {
    Random r = new Random();
    if (objectClass.equals(String.class)) {
        String s = "";
        for (int i = 0; i < 10; i++) {
            char c = (char) (Math.abs(r.nextInt()) % ('Z' - 'A') + 'A');
            s = s + c;//from  w w w . j  ava  2  s  . c  om
        }
        return objectClass.cast(s);
    } else if (objectClass.equals(Integer.class)) {
        Integer s = r.nextInt();
        return objectClass.cast(s);
    } else if (objectClass.equals(Long.class)) {
        Long s = r.nextLong();
        return objectClass.cast(s);
    } else if (objectClass.equals(java.util.Date.class)) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.set(java.util.Calendar.MONTH, Math.abs(r.nextInt()) % 12);
        c.set(java.util.Calendar.DAY_OF_MONTH, Math.abs(r.nextInt()) % 30);
        return objectClass.cast(c.getTime());
    }
    return null;
}

From source file:Main.java

private static Point findBestPreviewSizeValue(CharSequence previewSizeString, Point screenResolution) {
    int bestX = 0;
    int bestY = 0;
    int diff = Integer.MAX_VALUE;
    for (String previewSize : COMMA_PATTERN.split(previewSizeString)) {

        previewSize = previewSize.trim();
        int dimPosition = previewSize.indexOf('x');
        if (dimPosition < 0) {
            continue;
        }// w  w  w  .j  av  a2 s.c om

        int newX;
        int newY;
        try {
            newX = Integer.parseInt(previewSize.substring(0, dimPosition));
            newY = Integer.parseInt(previewSize.substring(dimPosition + 1));
        } catch (NumberFormatException nfe) {
            continue;
        }

        int newDiff = Math.abs(newX - screenResolution.x) + Math.abs(newY - screenResolution.y);
        if (newDiff == 0) {
            bestX = newX;
            bestY = newY;
            break;
        } else if (newDiff < diff) {
            bestX = newX;
            bestY = newY;
            diff = newDiff;
        }

    }
    if (bestX > 0 && bestY > 0) {
        return new Point(bestX, bestY);
    }
    return null;
}

From source file:Main.java

/**
 * Gets straighten matrix for the given bounds and degrees.
 */// w w  w .  j a  v a 2  s  .  c o m
public static void getStraightenMatrix(RectF bounds, float degrees, Matrix matrix) {
    matrix.reset();
    if ((degrees != 0) && !bounds.isEmpty()) {
        float w = bounds.width() / 2;
        float h = bounds.height() / 2;
        float adjustAngle;
        if ((degrees < 0 && w > h) || (degrees > 0 && w <= h)) {
            // The top left point is the boundary.
            adjustAngle = (float) Math.atan(h / -w) + MATH_PI + degrees * DEGREES_TO_RADIAN;
        } else {
            // The top right point is the boundary.
            adjustAngle = (float) Math.atan(h / w) - MATH_PI + degrees * DEGREES_TO_RADIAN;
        }
        float radius = (float) Math.hypot(w, h);
        float scaleX = (float) Math.abs(radius * Math.cos(adjustAngle)) / w;
        float scaleY = (float) Math.abs(radius * Math.sin(adjustAngle)) / h;
        float scale = Math.max(scaleX, scaleY);

        postRotateMatrix(degrees, new RectF(bounds), matrix);
        matrix.postScale(scale, scale);
    }
}

From source file:Main.java

public static Camera.Size getRotatedOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
    // 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;/*from  w  w  w .jav  a2s .  co  m*/

    // 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 targetWidth = w;

    // 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.width - targetWidth) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.width - targetWidth);
        }
    }

    // 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.width - targetWidth) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.width - targetWidth);
            }
        }
    }
    return optimalSize;
}

From source file:Main.java

public static String generateRandomDate() {
    Random r = new Random();
    java.util.Calendar c = java.util.Calendar.getInstance();
    c.set(java.util.Calendar.MONTH, Math.abs(r.nextInt()) % 12);
    c.set(java.util.Calendar.DAY_OF_MONTH, Math.abs(r.nextInt()) % 30);
    c.setLenient(true);/*from  w ww . j a  v  a 2 s .  c  om*/
    return DATE_FORMAT.format(c.getTime());
}

From source file:Main.java

/**
 * Returns <code>true</code> if the argument is a finite
 * floating-point value; returns <code>false</code> otherwise (for
 * NaN and infinity arguments).//ww  w.j a va 2s .  c  o  m
 *
 * @param d the <code>double</code> value to be tested
 * @return <code>true</code> if the argument is a finite
 * floating-point value, <code>false</code> otherwise.
 */
public static boolean isFinite(double d) {
    return Math.abs(d) <= DoubleConsts.MAX_VALUE;
}