Example usage for java.lang Math acos

List of usage examples for java.lang Math acos

Introduction

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

Prototype

public static double acos(double a) 

Source Link

Document

Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.

Usage

From source file:org.esa.beam.util.math.FastMathPerformance.java

public void testAcos() {
    System.gc();//from   w  w  w .  jav  a  2s  .  c om
    double x = 0;
    long time = System.nanoTime();
    for (int i = 0; i < RUNS; i++)
        x += StrictMath.acos(i / 10000000.0);
    long strictTime = System.nanoTime() - time;

    System.gc();
    double y = 0;
    time = System.nanoTime();
    for (int i = 0; i < RUNS; i++)
        y += FastMath.acos(i / 10000000.0);
    long fastTime = System.nanoTime() - time;

    System.gc();
    double z = 0;
    time = System.nanoTime();
    for (int i = 0; i < RUNS; i++)
        z += Math.acos(i / 10000000.0);
    long mathTime = System.nanoTime() - time;
    report("acos", x + y + z, strictTime, fastTime, mathTime);
}

From source file:org.apache.sysml.runtime.functionobjects.Builtin.java

public double execute(double in) throws DMLRuntimeException {
    switch (bFunc) {
    case SIN:/*from w w w.jav  a  2s .  c o m*/
        return FASTMATH ? FastMath.sin(in) : Math.sin(in);
    case COS:
        return FASTMATH ? FastMath.cos(in) : Math.cos(in);
    case TAN:
        return FASTMATH ? FastMath.tan(in) : Math.tan(in);
    case ASIN:
        return FASTMATH ? FastMath.asin(in) : Math.asin(in);
    case ACOS:
        return FASTMATH ? FastMath.acos(in) : Math.acos(in);
    case ATAN:
        return Math.atan(in); //faster in Math
    case CEIL:
        return FASTMATH ? FastMath.ceil(in) : Math.ceil(in);
    case FLOOR:
        return FASTMATH ? FastMath.floor(in) : Math.floor(in);
    case LOG:
        return FASTMATH ? FastMath.log(in) : Math.log(in);
    case LOG_NZ:
        return (in == 0) ? 0 : FASTMATH ? FastMath.log(in) : Math.log(in);
    case ABS:
        return Math.abs(in); //no need for FastMath         
    case SIGN:
        return FASTMATH ? FastMath.signum(in) : Math.signum(in);
    case SQRT:
        return Math.sqrt(in); //faster in Math      
    case EXP:
        return FASTMATH ? FastMath.exp(in) : Math.exp(in);
    case ROUND:
        return Math.round(in); //no need for FastMath

    case PLOGP:
        if (in == 0.0)
            return 0.0;
        else if (in < 0)
            return Double.NaN;
        else
            return (in * (FASTMATH ? FastMath.log(in) : Math.log(in)));

    case SPROP:
        //sample proportion: P*(1-P)
        return in * (1 - in);

    case SIGMOID:
        //sigmoid: 1/(1+exp(-x))
        return FASTMATH ? 1 / (1 + FastMath.exp(-in)) : 1 / (1 + Math.exp(-in));

    case SELP:
        //select positive: x*(x>0)
        return (in > 0) ? in : 0;

    default:
        throw new DMLRuntimeException("Builtin.execute(): Unknown operation: " + bFunc);
    }
}

From source file:geogebra.kernel.EquationSolver.java

final public int solveCubic(double eqn[], double res[]) {

    int roots = 0;
    double d = eqn[3];
    if (Math.abs(d) < kernel.EPSILON) {
        // The cubic has degenerated to quadratic (or line or ...).
        return solveQuadratic(eqn, res);
    }//from   www.j a  v a2s.c o  m
    double a = eqn[2] / d;
    double b = eqn[1] / d;
    double c = eqn[0] / d;
    double q = (a * a - 3 * b); //D(q) = 2aD(a) + 3D(b)
    double r = (2 * a * a * a - 9 * a * b + 27 * c); // D(r) = (3aa-9b)D(a) - 9aD(b) + 27D(c)

    double Q = q / 9; //D(Q) = D(q)/9
    double R = r / 54;

    double Q3 = Q * Q * Q;
    double R2 = R * R;

    double CR2 = 729 * r * r; // D(CR2) = 729*2rD(r)    ( |1458r(3aa-9b) - 8748q*2a| + |-9a*1458r -8748q*3| + |27*1458r| )*kernel.EPSILON 
    double CQ3 = 2916 * q * q * q; //D(CQ3) = 2916*3qD(q)  (D(root) ~ D(2sqrt(Q))= -1/sqrt(Q)  D(Q), |D(root)| |2a+3|/sqrt(9q) *kernel.EPSILON

    //Application.debug("Q = "+Q+" R = "+R+" Q3 = "+Q3+" R2 = "+R2+" CR2 = "+CR2+" CQ3 = "+CQ3);

    //Application.debug(Math.abs(CR2 - CQ3)+"");

    if (Math.abs(R) < kernel.EPSILON && Math.abs(Q) < kernel.EPSILON) // if (R == 0 && Q == 0)
    {
        res[roots++] = -a / 3;
        res[roots++] = -a / 3;
        res[roots++] = -a / 3;
        return 3;
    }
    // Michael Borcherds changed to check CR2 equal to CQ3 to first 8 significant digits
    // important for eg y=(ax-b)^2(cx-d)
    // |D(CR2-CQ3)|< (|r(aa-3b) - 4qa| + |-3ar -6q| + |9r|)*13122*sqrt(q) / |2a+3| *kernel.EPSILON
    // for simplicity, it (may be)  about 10* max(CR2,CR3)/|2a+3| * kernel.EPSILON
    //else if (Math.abs(CR2 - CQ3) < Math.max(CR2, CQ3) * kernel.EPSILON) // else if (CR2 == CQ3)
    else if (Math.abs(CR2 - CQ3) < Math.max(CR2, CQ3) * 10 / Math.max(1, Math.abs(2 * a + 3)) * kernel.EPSILON) // else if (CR2 == CQ3) 
    {
        // this test is actually R2 == Q3, written in a form suitable
        //     for exact computation with integers 

        // Due to finite precision some double roots may be missed, and
        //     considered to be a pair of complex roots z = x +/- kernel.EPSILON i
        //     close to the real axis. 

        double sqrtQ = Math.sqrt(Q);

        if (R > 0) {
            res[roots++] = -2 * sqrtQ - a / 3;
            res[roots++] = sqrtQ - a / 3;
            res[roots++] = sqrtQ - a / 3;
        } else {
            res[roots++] = -sqrtQ - a / 3;
            res[roots++] = -sqrtQ - a / 3;
            res[roots++] = 2 * sqrtQ - a / 3;
        }
        return 3;
    } else if (CR2 < CQ3) // equivalent to R2 < Q3 
    {
        double sqrtQ = Math.sqrt(Q);
        double sqrtQ3 = sqrtQ * sqrtQ * sqrtQ;
        double theta = Math.acos(R / sqrtQ3);
        double norm = -2 * sqrtQ;
        res[roots++] = norm * Math.cos(theta / 3) - a / 3;
        res[roots++] = norm * Math.cos((theta + 2.0 * Math.PI) / 3) - a / 3;
        res[roots++] = norm * Math.cos((theta - 2.0 * Math.PI) / 3) - a / 3;

        // GeoGebra addition
        //TODO: find a better way to deal with this
        if (res != eqn) //if res has the same reference as eqn, it's not possible to fix roots
            fixRoots(res, eqn);

        return 3;
    } else {
        double sgnR = (R >= 0 ? 1 : -1);
        double A = -sgnR * Math.pow(Math.abs(R) + Math.sqrt(R2 - Q3), 1.0 / 3.0);
        double B = Q / A;
        res[roots++] = A + B - a / 3;
        return 1;
    }
}

From source file:Geometry.java

/**
 * Find the angle between twree points. P0 is center point
 * /*from  w ww . j ava 2  s  .  co  m*/
 * @param p0, p1, p2  Three points finding angle between [x,y,z].
 * @return            Angle (in radians) between given points.
 */
public static double computeAngle(double[] p0, double[] p1, double[] p2) {
    double[] v0 = Geometry.createVector(p0, p1);
    double[] v1 = Geometry.createVector(p0, p2);

    double dotProduct = Geometry.computeDotProduct(v0, v1);

    double length1 = Geometry.length(v0);
    double length2 = Geometry.length(v1);

    double denominator = length1 * length2;

    double product = denominator != 0.0 ? dotProduct / denominator : 0.0;

    double angle = Math.acos(product);

    return angle;
}

From source file:geogebra.common.kernel.EquationSolver.java

final public int solveCubic(double eqn[], double res[], double eps) {

    int roots = 0;
    double d = eqn[3];
    if (Math.abs(d) < eps) {
        // The cubic has degenerated to quadratic (or line or ...).
        return solveQuadratic(eqn, res, eps);
    }/*from w  ww .  j  av  a  2s  .  c om*/
    double a = eqn[2] / d;
    double b = eqn[1] / d;
    double c = eqn[0] / d;
    double q = (a * a - 3 * b); // D(q) = 2aD(a) + 3D(b)
    double r = (2 * a * a * a - 9 * a * b + 27 * c); // D(r) = (3aa-9b)D(a)
    // - 9aD(b) + 27D(c)

    double Q = q / 9; // D(Q) = D(q)/9
    double R = r / 54;

    double Q3 = Q * Q * Q;
    double R2 = R * R;

    double CR2 = 729 * r * r; // D(CR2) = 729*2rD(r) ( |1458r(3aa-9b) -
    // 8748q*2a| + |-9a*1458r -8748q*3| +
    // |27*1458r| )*kernel.STANDARD_PRECISION
    double CQ3 = 2916 * q * q * q; // D(CQ3) = 2916*3qD(q) (D(root) ~
    // D(2sqrt(Q))= -1/sqrt(Q) D(Q),
    // |D(root)| |2a+3|/sqrt(9q)
    // *kernel.STANDARD_PRECISION

    // Application.debug("Q = "+Q+" R = "+R+" Q3 = "+Q3+" R2 = "+R2+" CR2 = "+CR2+" CQ3 = "+CQ3);

    // Application.debug(Math.abs(CR2 - CQ3)+"");

    if (Math.abs(R) < Kernel.STANDARD_PRECISION && Math.abs(Q) < Kernel.STANDARD_PRECISION) // if
    // (R
    // ==
    // 0
    // &&
    // Q
    // ==
    // 0)
    {
        res[roots++] = -a / 3;
        res[roots++] = -a / 3;
        res[roots++] = -a / 3;
        return 3;
    }
    // Michael Borcherds changed to check CR2 equal to CQ3 to first 8
    // significant digits
    // important for eg y=(ax-b)^2(cx-d)
    // |D(CR2-CQ3)|< (|r(aa-3b) - 4qa| + |-3ar -6q| + |9r|)*13122*sqrt(q) /
    // |2a+3| *kernel.STANDARD_PRECISION
    // for simplicity, it (may be) about 10* max(CR2,CR3)/|2a+3| *
    // kernel.STANDARD_PRECISION
    // else if (Math.abs(CR2 - CQ3) < Math.max(CR2, CQ3) *
    // kernel.STANDARD_PRECISION)
    // // else if (CR2 == CQ3)
    else if (Math.abs(CR2 - CQ3) < Math.max(CR2, CQ3) * 10 / Math.max(1, Math.abs(2 * a + 3))
            * Kernel.STANDARD_PRECISION) // else
    // if
    // (CR2
    // ==
    // CQ3)
    {
        // this test is actually R2 == Q3, written in a form suitable
        // for exact computation with integers

        // Due to finite precision some double roots may be missed, and
        // considered to be a pair of complex roots z = x +/-
        // kernel.STANDARD_PRECISION
        // i
        // close to the real axis.

        double sqrtQ = Math.sqrt(Q);

        if (R > 0) {
            res[roots++] = -2 * sqrtQ - a / 3;
            res[roots++] = sqrtQ - a / 3;
            res[roots++] = sqrtQ - a / 3;
        } else {
            res[roots++] = -sqrtQ - a / 3;
            res[roots++] = -sqrtQ - a / 3;
            res[roots++] = 2 * sqrtQ - a / 3;
        }
        return 3;
    } else if (CR2 < CQ3) // equivalent to R2 < Q3
    {
        double sqrtQ = Math.sqrt(Q);
        double sqrtQ3 = sqrtQ * sqrtQ * sqrtQ;
        double theta = Math.acos(R / sqrtQ3);
        double norm = -2 * sqrtQ;
        res[roots++] = norm * Math.cos(theta / 3) - a / 3;
        res[roots++] = norm * Math.cos((theta + 2.0 * Math.PI) / 3) - a / 3;
        res[roots++] = norm * Math.cos((theta - 2.0 * Math.PI) / 3) - a / 3;

        // GeoGebra addition
        // TODO: find a better way to deal with this
        if (res != eqn) // if res has the same reference as eqn, it's not
            // possible to fix roots
            fixRoots(res, eqn);

        return 3;
    } else {
        double sgnR = (R >= 0 ? 1 : -1);
        double A = -sgnR * Math.pow(Math.abs(R) + Math.sqrt(R2 - Q3), 1.0 / 3.0);
        double B = Q / A;
        res[roots++] = A + B - a / 3;
        return 1;
    }
}

From source file:se.llbit.chunky.renderer.scene.Sky.java

/**
 * Bilinear interpolated panoramic skymap color
 * @param ray//from   ww  w .ja v a 2s.c o  m
 */
public void getSkyColorInterpolated(Ray ray) {
    switch (mode) {
    case SKYMAP_PANORAMIC: {
        if (mirrored) {
            double theta = FastMath.atan2(ray.d.z, ray.d.x);
            theta += rotation;
            theta /= Constants.TAU;
            theta = (theta % 1 + 1) % 1;
            double phi = Math.abs(Math.asin(ray.d.y)) / Constants.HALF_PI;
            skymap.getColorInterpolated(theta, phi, ray.color);
        } else {
            double theta = FastMath.atan2(ray.d.z, ray.d.x);
            theta += rotation;
            theta /= Constants.TAU;
            if (theta > 1 || theta < 0) {
                theta = (theta % 1 + 1) % 1;
            }
            double phi = (Math.asin(ray.d.y) + Constants.HALF_PI) / Math.PI;
            skymap.getColorInterpolated(theta, phi, ray.color);
        }
        break;
    }
    case SKYMAP_SPHERICAL: {
        double cos = FastMath.cos(-rotation);
        double sin = FastMath.sin(-rotation);
        double x = cos * ray.d.x + sin * ray.d.z;
        double y = ray.d.y;
        double z = -sin * ray.d.x + cos * ray.d.z;
        double len = Math.sqrt(x * x + y * y);
        double theta = (len < Ray.EPSILON) ? 0 : Math.acos(-z) / (Constants.TAU * len);
        double u = theta * x + .5;
        double v = .5 + theta * y;
        skymap.getColorInterpolated(u, v, ray.color);
        break;
    }
    case SKYBOX: {
        double cos = FastMath.cos(-rotation);
        double sin = FastMath.sin(-rotation);
        double x = cos * ray.d.x + sin * ray.d.z;
        double y = ray.d.y;
        double z = -sin * ray.d.x + cos * ray.d.z;
        double xabs = QuickMath.abs(x);
        double yabs = QuickMath.abs(y);
        double zabs = QuickMath.abs(z);
        if (y > xabs && y > zabs) {
            double alpha = 1 / yabs;
            skybox[SKYBOX_UP].getColorInterpolated((1 + x * alpha) / 2.0, (1 + z * alpha) / 2.0, ray.color);
        } else if (-z > xabs && -z > yabs) {
            double alpha = 1 / zabs;
            skybox[SKYBOX_FRONT].getColorInterpolated((1 + x * alpha) / 2.0, (1 + y * alpha) / 2.0, ray.color);
        } else if (z > xabs && z > yabs) {
            double alpha = 1 / zabs;
            skybox[SKYBOX_BACK].getColorInterpolated((1 - x * alpha) / 2.0, (1 + y * alpha) / 2.0, ray.color);
        } else if (-x > zabs && -x > yabs) {
            double alpha = 1 / xabs;
            skybox[SKYBOX_LEFT].getColorInterpolated((1 - z * alpha) / 2.0, (1 + y * alpha) / 2.0, ray.color);
        } else if (x > zabs && x > yabs) {
            double alpha = 1 / xabs;
            skybox[SKYBOX_RIGHT].getColorInterpolated((1 + z * alpha) / 2.0, (1 + y * alpha) / 2.0, ray.color);
        } else if (-y > xabs && -y > zabs) {
            double alpha = 1 / yabs;
            skybox[SKYBOX_DOWN].getColorInterpolated((1 + x * alpha) / 2.0, (1 - z * alpha) / 2.0, ray.color);
        }
        break;
    }
    default:
        getSkyDiffuseColorInner(ray);
    }
    if (scene.sunEnabled) {
        addSunColor(ray);
    }
    //ray.color.scale(skyLightModifier);
    ray.color.w = 1;
}

From source file:ExSpotLight.java

private Group buildArrows() {
    // Create a switch group to hold the different arrow fan
    // spread angle choices. Enable child choice writing.
    arrowSpreadAngleSwitch = new Switch();
    arrowSpreadAngleSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Create a set of arrow fans, one per spread angle
    // shown on the menu.
    AnnotationArrowFan af = null;/* ww  w.  ja v  a2s. c o m*/
    float spread = 0.0f;
    for (int i = 0; i < spreads.length; i++) {
        spread = ((Double) spreads[i].value).floatValue();
        af = new AnnotationArrowFan(0.0f, 0.0f, 0.0f, // center position
                2.5f, // arrow length
                -spread, // start angle
                spread, // end angle
                5); // number of arrows
        arrowSpreadAngleSwitch.addChild(af);
    }

    // Select the current fan.
    arrowSpreadAngleSwitch.setWhichChild(currentSpread);

    // Create an outer transform group used to change the fan
    // position. Enable writing of its transform.
    arrowPositionTransformGroup = new TransformGroup();
    arrowPositionTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // Create a set of Transform3Ds for the different arrow positions.
    arrowPositionTransforms = new Transform3D[positions.length];
    Point3f pos;
    Vector3f v = new Vector3f();
    for (int i = 0; i < positions.length; i++) {
        // Create a Transform3D, setting its translation.
        arrowPositionTransforms[i] = new Transform3D();
        pos = (Point3f) positions[i].value;
        v.set(pos);
        arrowPositionTransforms[i].setTranslation(v);
    }

    // Set the initial transform to be the current position
    arrowPositionTransformGroup.setTransform(arrowPositionTransforms[currentPosition]);

    // Create an inner transform group surrounding the arrows,
    // used to set the aim direction. Enable writing of its transform.
    arrowDirectionTransformGroup = new TransformGroup();
    arrowDirectionTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // Add the switch group to the direction-change transform group,
    // and add the direction-change transform group to the
    // position-change transform gorup.
    arrowDirectionTransformGroup.addChild(arrowSpreadAngleSwitch);
    arrowPositionTransformGroup.addChild(arrowDirectionTransformGroup);

    // Create a set of Transform3Ds for the different
    // arrow directions.
    arrowDirectionTransforms = new Transform3D[directions.length];
    Vector3f dir = new Vector3f();
    Vector3f positiveX = new Vector3f(1.0f, 0.0f, 0.0f);
    Vector3f axis = new Vector3f();
    float angle;
    float dot;

    for (int i = 0; i < directions.length; i++) {
        // Normalize the direction vector
        dir.normalize((Vector3f) directions[i].value);

        // Cross the direction vector with the arrow's
        // +X aim direction to get a vector orthogonal
        // to both. This is the rotation axis.
        axis.cross(positiveX, dir);
        if (axis.x == 0.0f && axis.y == 0.0f && axis.z == 0.0f) {
            // New direction is parallel to current
            // arrow direction. Default to a Y axis.
            axis.y = 1.0f;
        }

        // Compute the angle between the direction and +X
        // vectors, where:
        //
        //   cos(angle) = (dir dot positiveX)
        //                -------------------------------
        //                (positiveX.length * dir.length)
        //
        // but since positiveX is normalized (as created
        // above) and dir has been normalized, both have
        // a length of 1. So, the angle between the
        // vectors is:
        //
        //   angle = arccos(dir dot positiveX)
        dot = dir.dot(positiveX);
        angle = (float) Math.acos(dot);

        // Create a Transform3D, setting its rotation using
        // an AxisAngle4f, which takes an XYZ rotation vector
        // and an angle to rotate by around that vector.
        arrowDirectionTransforms[i] = new Transform3D();
        arrowDirectionTransforms[i].setRotation(new AxisAngle4f(axis.x, axis.y, axis.z, angle));
    }

    // Set the initial transform to be the current aim direction.
    arrowDirectionTransformGroup.setTransform(arrowDirectionTransforms[currentDirection]);

    return arrowPositionTransformGroup;
}

From source file:im.ene.lab.design.widget.coverflow.FeatureCoverFlow.java

private float getAngleOnCircle(int childCenter) {
    float x = getRelativePosition(childCenter) / mRadius;
    if (x < -1.0f)
        x = -1.0f;// w  w w  .java2  s.  c  om
    if (x > 1.0f)
        x = 1.0f;

    return (float) (Math.acos(x) / Math.PI * 180.0f - 90.0f);
}

From source file:com.ibm.bi.dml.runtime.functionobjects.Builtin.java

public double execute(double in) throws DMLRuntimeException {
    switch (bFunc) {
    case SIN://from  w  w  w . j av a  2s  .co  m
        return FASTMATH ? FastMath.sin(in) : Math.sin(in);
    case COS:
        return FASTMATH ? FastMath.cos(in) : Math.cos(in);
    case TAN:
        return FASTMATH ? FastMath.tan(in) : Math.tan(in);
    case ASIN:
        return FASTMATH ? FastMath.asin(in) : Math.asin(in);
    case ACOS:
        return FASTMATH ? FastMath.acos(in) : Math.acos(in);
    case ATAN:
        return Math.atan(in); //faster in Math
    case CEIL:
        return FASTMATH ? FastMath.ceil(in) : Math.ceil(in);
    case FLOOR:
        return FASTMATH ? FastMath.floor(in) : Math.floor(in);
    case LOG:
        //if ( in <= 0 )
        //   throw new DMLRuntimeException("Builtin.execute(): logarithm can only be computed for non-negative numbers (input = " + in + ").");
        // for negative numbers, Math.log will return NaN
        return FASTMATH ? FastMath.log(in) : Math.log(in);
    case LOG_NZ:
        return (in == 0) ? 0 : FASTMATH ? FastMath.log(in) : Math.log(in);

    case ABS:
        return Math.abs(in); //no need for FastMath

    case SQRT:
        //if ( in < 0 )
        //   throw new DMLRuntimeException("Builtin.execute(): squareroot can only be computed for non-negative numbers (input = " + in + ").");
        return Math.sqrt(in); //faster in Math

    case PLOGP:
        if (in == 0.0)
            return 0.0;
        else if (in < 0)
            return Double.NaN;
        else
            return (in * (FASTMATH ? FastMath.log(in) : Math.log(in)));

    case EXP:
        return FASTMATH ? FastMath.exp(in) : Math.exp(in);

    case ROUND:
        return Math.round(in); //no need for FastMath

    case SPROP:
        //sample proportion: P*(1-P)
        return in * (1 - in);

    case SIGMOID:
        //sigmoid: 1/(1+exp(-x))
        return FASTMATH ? 1 / (1 + FastMath.exp(-in)) : 1 / (1 + Math.exp(-in));

    case SELP:
        //select positive: x*(x>0)
        return (in > 0) ? in : 0;

    default:
        throw new DMLRuntimeException("Builtin.execute(): Unknown operation: " + bFunc);
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the arc cosine of the number.
 * //  www  .j  a  va  2s. c  om
 * @param a the number
 * @return the arc cosine of the number
 * @see Math#acos(double)
 */
public static Number acos(Number a) {
    return Math.acos(a.doubleValue());
}