Example usage for org.apache.commons.math3.util FastMath hypot

List of usage examples for org.apache.commons.math3.util FastMath hypot

Introduction

In this page you can find the example usage for org.apache.commons.math3.util FastMath hypot.

Prototype

public static double hypot(final double x, final double y) 

Source Link

Document

Returns the hypotenuse of a triangle with sides x and y - sqrt(x2 +y2)
avoiding intermediate overflow or underflow.

Usage

From source file:msi.gaml.operators.Maths.java

public static void main(final String[] args) throws ParseException {
    java.lang.System.out.println("Various format tests");
    java.lang.System.out.println("NumberFormat.parse1e1 = " + NumberFormat.getInstance(Locale.US).parse("1e1"));
    java.lang.System.out.println("Double.parse 1e1 = " + Double.parseDouble("1e1"));
    java.lang.System.out/*ww  w.j  a v a 2s  . c  o  m*/
            .println("NumberFormat.parse 1E1 = " + NumberFormat.getInstance(Locale.US).parse("1E1"));
    java.lang.System.out.println("Double.parse 1E1 = " + Double.parseDouble("1E1"));
    java.lang.System.out
            .println("NumberFormat.parse 1.0e1 = " + NumberFormat.getInstance(Locale.US).parse("1.0e1"));
    java.lang.System.out.println("Double.parse 1.0e1 = " + Double.parseDouble("1.0e1"));
    java.lang.System.out.println(
            "NumberFormat.parse 0.001E+10 = " + NumberFormat.getInstance(Locale.US).parse("0.001E+10"));
    java.lang.System.out.println("Double.parse 0.001E+10 = " + Double.parseDouble("0.001E+10"));
    java.lang.System.out.println(
            "NumberFormat.parse 0.001E-10 = " + NumberFormat.getInstance(Locale.US).parse("0.001E-10"));
    java.lang.System.out.println("Double.parse 0.001E-10 = " + Double.parseDouble("0.001E-10"));
    java.lang.System.out
            .println("NumberFormat.parse 0.001e-10 =" + NumberFormat.getInstance(Locale.US).parse("0.001e-10"));
    java.lang.System.out.println("Double.parse 0.001e-10 = " + Double.parseDouble("0.001e-10"));
    java.lang.System.out.println("Various arithmetic tests");
    java.lang.System.out.println("cos(PI) = " + Maths.cos_rad(PI));
    java.lang.System.out.println("sin_rad(0.0) = " + sin_rad(0.0));
    java.lang.System.out.println("tan_rad(0.0) = " + tan_rad(0.0));
    java.lang.System.out.println("sin(360) = " + sin(360));
    java.lang.System.out.println("sin(-720) = " + sin(-720));
    java.lang.System.out.println("sin(360.0) = " + sin(360.0));
    java.lang.System.out.println("sin(-720.0) = " + sin(-720.0));
    java.lang.System.out.println("sin(90) = " + sin(90));
    java.lang.System.out.println("sin(45) = " + sin(45));
    java.lang.System.out.println("sin(0) = " + sin(0));
    java.lang.System.out.println("sin(135) = " + sin(135));

    java.lang.System.out.println("Math.sin(360.0) = " + Math.sin(2 * Math.PI));
    // java.lang.System.out.println("3.0 = 3" + (3d == 3));
    // java.lang.System.out.println("3.0 != 3" + (3d != 3));
    java.lang.System.out.println("floor and ceil 2.7 " + floor(2.7) + " and " + ceil(2.7));
    java.lang.System.out.println("floor and ceil -2.7 " + floor(-2.7) + " and " + ceil(-2.7));
    java.lang.System.out.println("floor and ceil -2 " + floor(-2) + " and " + ceil(-2));
    java.lang.System.out.println("floor and ceil 3 " + floor(3) + " and " + ceil(3));
    double atan2diff = 0;
    double atan2diff2 = 0;
    Random rand = new Random();
    long s1 = 0;
    long t1 = 0;
    long t2 = 0;
    long t3 = 0;
    // for ( int i = 0; i < 10000000; i++ ) {
    // double x = rand.nextDouble();
    // double y = rand.nextDouble();
    // s1 = java.lang.System.currentTimeMillis();
    // double a1 = Math.atan2(x, y);
    // t1 += java.lang.System.currentTimeMillis() - s1;
    // s1 = java.lang.System.currentTimeMillis();
    // double a2 = FastMath.atan2(x, y);
    // t2 += java.lang.System.currentTimeMillis() - s1;
    // s1 = java.lang.System.currentTimeMillis();
    // double a3 = Maths.atan2Opt2(x, y);
    // t3 += java.lang.System.currentTimeMillis() - s1;
    //
    // atan2diff += Math.abs(a1 - a2);
    // atan2diff2 += Math.abs(a1 - a3);
    // }
    // java.lang.System.out.println("atan2diff : " + atan2diff + "  atan2diff2 : " + atan2diff2 + " t1 : " + t1 +
    // " t2 : " + t2 + " t3 : " + t3);

    long t4 = 0;
    long t5 = 0;
    long t6 = 0;
    double distDiff1 = 0;
    double distDiff2 = 0;
    for (int i = 0; i < 1000000; i++) {
        double x1 = rand.nextDouble();
        double y1 = rand.nextDouble();
        double x2 = rand.nextDouble();
        double y2 = rand.nextDouble();
        Coordinate c1 = new Coordinate(x1, y1);
        Coordinate c2 = new Coordinate(x2, y2);

        s1 = java.lang.System.currentTimeMillis();
        double a1 = Math.hypot(x2 - x1, y2 - y1);
        t4 += java.lang.System.currentTimeMillis() - s1;
        s1 = java.lang.System.currentTimeMillis();
        double a2 = FastMath.hypot(x2 - x1, y2 - y1);
        t5 += java.lang.System.currentTimeMillis() - s1;
        s1 = java.lang.System.currentTimeMillis();
        double a3 = c1.distance(c2);
        t6 += java.lang.System.currentTimeMillis() - s1;
        distDiff1 += Math.abs(a1 - a2);
        distDiff2 += Math.abs(a1 - a3);
    }
    java.lang.System.out.println("distDiff1 : " + distDiff1 + "  distDiff2 : " + distDiff2 + " t4 : " + t4
            + " t5 : " + t5 + " t6 : " + t6);

    long t7 = 0;
    long t8 = 0;
    distDiff1 = 0;

    for (int i = 0; i < 1000000; i++) {
        double a1, a2;
        double x1 = rand.nextDouble();
        double x2 = rand.nextDouble();
        double y1 = rand.nextDouble();
        double y2 = rand.nextDouble();
        double z1 = 0.0;
        double z2 = 0.0;
        GamaPoint c2 = new GamaPoint(x2, y2, z2);

        s1 = java.lang.System.currentTimeMillis();
        if (z1 == 0d && c2.getZ() == 0d) {
            a1 = hypot(x1, x2, y1, y2);
        } else {
            a1 = hypot(x1, x2, y1, y2, z1, z2);
        }
        t7 += java.lang.System.currentTimeMillis() - s1;
        s1 = java.lang.System.currentTimeMillis();
        a2 = hypot(x1, x2, y1, y2, z1, c2.getZ());
        t8 += java.lang.System.currentTimeMillis() - s1;
        distDiff1 += Math.abs(a1 - a2);
    }
    java.lang.System.out.println(
            "with 0.0 check : " + t7 + "  with direct 3 parameters call : " + t8 + " distance : " + distDiff1);
    // java.lang.System.out.println("Infinity to int:" + (int) Double.POSITIVE_INFINITY);
    // java.lang.System.out.println("NaN to int:" + (int) Double.NaN);
    // GuiUtils.debug("(int) (1.0/0.0):" + (int) (1.0 / 0.0));
    // GuiUtils.debug("(int) (1.0/0):" + (int) (1.0 / 0));
    // GuiUtils.debug("(int) (1.0/0d):" + (int) (1 / 0d));
    // GuiUtils.debug("(int) (1/0):" + 1 / 0);
}

From source file:com.nextbreakpoint.nextfractal.mandelbrot.core.Expression.java

public static Number opPow(MutableNumber out, Number a, double b) {
    double m = Math.pow(FastMath.hypot(a.r(), a.i()), b);
    double f = Math.atan2(a.i(), a.r()) * b;
    return out.set(m * Math.cos(f), m * Math.sin(f));
}

From source file:com.tussle.collision.CollisionEdge.java

public void draw(ShapeRenderer drawer) {

    double len = FastMath.hypot(endx - startx, endy - starty);
    if (len > 0) {
        double dx = (starty - endy) * 10 / len;
        double dy = (endx - startx) * 10 / len;
        drawer.line((float) startx, (float) starty, (float) endx, (float) endy);
        drawer.line((float) startx, (float) starty, (float) (startx - dx), (float) (starty - dy));
        drawer.line((float) endx, (float) endy, (float) (endx - dx), (float) (endy - dy));
    } else//w ww . jav  a 2 s  . c om
        drawer.point((float) startx, (float) starty, 0);
}

From source file:com.tussle.main.Intersector.java

public static ProjectionVector dispSegmentPoint(double startX, double startY, double endX, double endY,
        double pointX, double pointY) {
    double x;/* w w w.  ja v  a2  s.c  o m*/
    double y;
    if (endX - startX == 0 && endY - startY == 0) {
        x = startX;
        y = startY;
    } else {
        double length2 = (endX - startX) * (endX - startX) + (endY - startY) * (endY - startY);
        double tprime = (pointX - startX) * (endX - startX) + (pointY - startY) * (endY - startY);
        if (tprime < 0) {
            x = startX;
            y = startY;
        } else if (tprime > length2) {
            x = endX;
            y = endY;
        } else {
            x = startX + tprime * (endX - startX) / length2;
            y = startY + tprime * (endY - startY) / length2;
        }
    }
    if (x - pointX == 0 && y - pointY == 0) {
        double len = FastMath.hypot(endY - startY, startX - endX);
        return new ProjectionVector((endY - startY) / len, (startX - endX) / len, 0);
    }
    double dist = FastMath.hypot(x - pointX, y - pointY);
    return new ProjectionVector((x - pointX) / dist, (y - pointY) / dist, dist);
}

From source file:com.tussle.collision.CollisionStadium.java

public void draw(ShapeRenderer drawer) {
    drawer.circle((float) startx, (float) starty, (float) radius);
    drawer.circle((float) endx, (float) endy, (float) radius);
    double len = FastMath.hypot(endx - startx, endy - starty);
    if (len > 0) {
        double dx = (starty - endy) * radius / len;
        double dy = (endx - startx) * radius / len;
        drawer.line((float) (startx + dx), (float) (starty + dy), (float) (endx + dx), (float) (endy + dy));
        drawer.line((float) (startx - dx), (float) (starty - dy), (float) (endx - dx), (float) (endy - dy));
    }/*  w ww . ja  v  a  2s  .c  om*/
}

From source file:com.tussle.collision.CollisionCorner.java

public CollisionCorner interpolate(CollisionShape other) {
    if (!(other instanceof CollisionCorner))
        throw new IllegalArgumentException();
    CollisionCorner o = (CollisionCorner) other;
    double minXSum = minVec.xComp() + o.minVec.xComp();
    double minYSum = minVec.yComp() + o.minVec.yComp();
    double maxXSum = maxVec.xComp() + o.maxVec.xComp();
    double maxYSum = maxVec.yComp() + o.maxVec.yComp();
    ProjectionVector minProj;//from  w  w w  . ja  va  2s  .  co m
    ProjectionVector maxProj;
    if (minXSum == 0 && minYSum == 0) {
        if (minVec.xComp() * o.minVec.xComp() + minVec.yComp() * o.minVec.yComp() > 0) {
            minProj = new ProjectionVector(minVec.xComp(), minVec.yComp(), 0);
        } else {
            minProj = new ProjectionVector(minVec.yComp(), -minVec.xComp(), 0);
        }
    } else {
        double magnitude = FastMath.hypot(minXSum, minYSum);
        minProj = new ProjectionVector(minXSum / magnitude, minYSum / magnitude, magnitude / 2);
    }
    if (maxXSum == 0 && maxYSum == 0) {
        if (maxVec.xComp() * o.maxVec.xComp() + maxVec.yComp() * o.maxVec.yComp() > 0) {
            maxProj = new ProjectionVector(maxVec.xComp(), maxVec.yComp(), 0);
        } else {
            maxProj = new ProjectionVector(maxVec.yComp(), -maxVec.xComp(), 0);
        }
    } else {
        double magnitude = FastMath.hypot(maxXSum, maxYSum);
        maxProj = new ProjectionVector(maxXSum / magnitude, maxYSum / magnitude, magnitude / 2);
    }
    return new CollisionCorner((x + o.x) / 2, (y + o.y) / 2, minProj, maxProj);
}

From source file:edu.stanford.cfuller.imageanalysistools.filter.GradientFilter.java

/**
 * Applies the GradientFilter to the specified Image.
 * @param im    The Image that will be replaced by its gradient.
 *///from www  .  j av a2 s .c o m
@Override
public void apply(WritableImage im) {

    final int kernelSize = 3;
    int halfKernelSize = (kernelSize - 1) / 2;

    RealMatrix kernel1 = new Array2DRowRealMatrix(kernelSize, kernelSize);

    kernel1.setEntry(0, 0, 1);
    kernel1.setEntry(1, 0, 0);
    kernel1.setEntry(2, 0, -1);
    kernel1.setEntry(0, 1, 1);
    kernel1.setEntry(1, 1, 0);
    kernel1.setEntry(2, 1, -1);
    kernel1.setEntry(0, 2, 1);
    kernel1.setEntry(1, 2, 0);
    kernel1.setEntry(2, 2, -1);

    RealMatrix kernel2 = new Array2DRowRealMatrix(kernelSize, kernelSize);

    kernel2.setEntry(0, 0, -1);
    kernel2.setEntry(1, 0, -1);
    kernel2.setEntry(2, 0, -1);
    kernel2.setEntry(0, 1, 0);
    kernel2.setEntry(1, 1, 0);
    kernel2.setEntry(2, 1, 0);
    kernel2.setEntry(0, 2, 1);
    kernel2.setEntry(1, 2, 1);
    kernel2.setEntry(2, 2, 1);

    Image copy = ImageFactory.create(im);

    ImageCoordinate ic = ImageCoordinate.createCoordXYZCT(0, 0, 0, 0, 0);

    for (ImageCoordinate i : im) {

        double outputVal = 0;
        double output1 = 0;
        double output2 = 0;

        if (i.get(ImageCoordinate.X) == 0 || i.get(ImageCoordinate.Y) == 0
                || i.get(ImageCoordinate.X) == copy.getDimensionSizes().get(ImageCoordinate.X) - 1
                || i.get(ImageCoordinate.Y) == copy.getDimensionSizes().get(ImageCoordinate.Y) - 1) {
            outputVal = 0;
        } else {
            for (int p = -1 * halfKernelSize; p < halfKernelSize + 1; p++) {
                for (int q = -1 * halfKernelSize; q < halfKernelSize + 1; q++) {
                    ic.set(ImageCoordinate.X, i.get(ImageCoordinate.X) + p);
                    ic.set(ImageCoordinate.Y, i.get(ImageCoordinate.Y) + q);
                    output1 += kernel1.getEntry(p + halfKernelSize, q + halfKernelSize) * copy.getValue(ic);
                    output2 += kernel2.getEntry(p + halfKernelSize, q + halfKernelSize) * copy.getValue(ic);
                }
            }

            outputVal = FastMath.hypot(output1, output2);
        }

        im.setValue(i, (float) Math.floor(outputVal));

    }

    ic.recycle();

}

From source file:com.nextbreakpoint.nextfractal.mandelbrot.core.Expression.java

public static double funcHypot(double x, double y) {
    return FastMath.hypot(x, y);
}

From source file:com.nextbreakpoint.nextfractal.mandelbrot.core.Expression.java

public static double funcMod(Number x) {
    return FastMath.hypot(x.r(), x.i());
}

From source file:com.tussle.collision.CollisionCorner.java

public CollisionCorner transformBy(double dx, double dy, double rot, double scale, boolean flip) {
    double cos = FastMath.cos(FastMath.toRadians(rot));
    double sin = FastMath.sin(FastMath.toRadians(rot));
    double locx = x * (flip ? -scale : scale);
    double locy = y * scale;
    double lowXNorm = (minVec.xNorm() * cos * (flip ? -scale : scale) - minVec.yNorm() * sin * scale);
    double lowYNorm = (minVec.yNorm() * sin * scale + minVec.xNorm() * cos * (flip ? -scale : scale));
    double lowMag = FastMath.hypot(lowXNorm, lowYNorm);
    ProjectionVector lowVec = new ProjectionVector(lowXNorm / lowMag, lowYNorm / lowMag, 1);
    double highXNorm = (maxVec.xNorm() * cos * (flip ? -scale : scale) - maxVec.yNorm() * sin * scale);
    double highYNorm = (maxVec.yNorm() * sin * scale + maxVec.xNorm() * cos * (flip ? -scale : scale));
    double highMag = FastMath.hypot(highXNorm, highYNorm);
    ProjectionVector highVec = new ProjectionVector(highXNorm / highMag, highYNorm / highMag, 1);
    return new CollisionCorner(locx * cos - locy * sin + dx, locx * sin + locy * cos + dy, lowVec, highVec);
}