Java Code Examples for org.apache.commons.math3.util.FastMath#acos()

The following examples show how to use org.apache.commons.math3.util.FastMath#acos() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Vector3D.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Compute the angular separation between two vectors.
 * <p>This method computes the angular separation between two
 * vectors using the dot product for well separated vectors and the
 * cross product for almost aligned vectors. This allows to have a
 * good accuracy in all cases, even for vectors very close to each
 * other.</p>
 * @param v1 first vector
 * @param v2 second vector
 * @return angular separation between v1 and v2
 * @exception MathArithmeticException if either vector has a null norm
 */
public static double angle(Vector3D v1, Vector3D v2) {

    double normProduct = v1.getNorm() * v2.getNorm();
    if (normProduct == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }

    double dot = v1.dotProduct(v2);
    double threshold = normProduct * 0.9999;
    if ((dot < -threshold) || (dot > threshold)) {
        // the vectors are almost aligned, compute using the sine
        Vector3D v3 = crossProduct(v1, v2);
        if (dot >= 0) {
            return FastMath.asin(v3.getNorm() / normProduct);
        }
        return FastMath.PI - FastMath.asin(v3.getNorm() / normProduct);
    }

    // the vectors are sufficiently separated to use the cosine
    return FastMath.acos(dot / normProduct);

}
 
Example 2
Source File: 1_Vector3D.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/** Compute the angular separation between two vectors.
 * <p>This method computes the angular separation between two
 * vectors using the dot product for well separated vectors and the
 * cross product for almost aligned vectors. This allows to have a
 * good accuracy in all cases, even for vectors very close to each
 * other.</p>
 * @param v1 first vector
 * @param v2 second vector
 * @return angular separation between v1 and v2
 * @exception MathArithmeticException if either vector has a null norm
 */
public static double angle(Vector3D v1, Vector3D v2) throws MathArithmeticException {

    double normProduct = v1.getNorm() * v2.getNorm();
    if (normProduct == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }

    double dot = v1.dotProduct(v2);
    double threshold = normProduct * 0.9999;
    if ((dot < -threshold) || (dot > threshold)) {
        // the vectors are almost aligned, compute using the sine
        Vector3D v3 = crossProduct(v1, v2);
        if (dot >= 0) {
            return FastMath.asin(v3.getNorm() / normProduct);
        }
        return FastMath.PI - FastMath.asin(v3.getNorm() / normProduct);
    }

    // the vectors are sufficiently separated to use the cosine
    return FastMath.acos(dot / normProduct);

}
 
Example 3
Source File: AngularVelocity.java    From riiablo with Apache License 2.0 6 votes vote down vote up
@Override
protected void process(int entityId) {
  Angle angleComponent = mAngle.get(entityId);
  Vector2 angle  = angleComponent.angle;
  Vector2 target = angleComponent.target;
  if (angle.epsilonEquals(target)) {
    return;
  }

  float acos = (float) FastMath.acos(angle.dot(target));
  float asin = (float) FastMath.asin(angle.crs(target));

  float theta = ANGULAR_VELOCITY * world.delta;
  if (acos < theta) {
    angle.set(target);
    return;
  }

  theta = asin < 0 ? -theta : theta;
  angle.rotateRad(theta);
}
 
Example 4
Source File: Vector3D.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Compute the angular separation between two vectors.
 * <p>This method computes the angular separation between two
 * vectors using the dot product for well separated vectors and the
 * cross product for almost aligned vectors. This allows to have a
 * good accuracy in all cases, even for vectors very close to each
 * other.</p>
 * @param v1 first vector
 * @param v2 second vector
 * @return angular separation between v1 and v2
 * @exception MathArithmeticException if either vector has a null norm
 */
public static double angle(Vector3D v1, Vector3D v2) throws MathArithmeticException {

    double normProduct = v1.getNorm() * v2.getNorm();
    if (normProduct == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }

    double dot = v1.dotProduct(v2);
    double threshold = normProduct * 0.9999;
    if ((dot < -threshold) || (dot > threshold)) {
        // the vectors are almost aligned, compute using the sine
        Vector3D v3 = crossProduct(v1, v2);
        if (dot >= 0) {
            return FastMath.asin(v3.getNorm() / normProduct);
        }
        return FastMath.PI - FastMath.asin(v3.getNorm() / normProduct);
    }

    // the vectors are sufficiently separated to use the cosine
    return FastMath.acos(dot / normProduct);

}
 
Example 5
Source File: JGenProg2017_0061_t.java    From coming with MIT License 6 votes vote down vote up
/** Compute the angular separation between two vectors.
 * <p>This method computes the angular separation between two
 * vectors using the dot product for well separated vectors and the
 * cross product for almost aligned vectors. This allows to have a
 * good accuracy in all cases, even for vectors very close to each
 * other.</p>
 * @param v1 first vector
 * @param v2 second vector
 * @return angular separation between v1 and v2
 * @exception MathArithmeticException if either vector has a null norm
 */
public static double angle(Vector3D v1, Vector3D v2) {

    double normProduct = v1.getNorm() * v2.getNorm();
    if (normProduct == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }

    double dot = v1.dotProduct(v2);
    double threshold = normProduct * 0.9999;
    if ((dot < -threshold) || (dot > threshold)) {
        // the vectors are almost aligned, compute using the sine
        Vector3D v3 = crossProduct(v1, v2);
        if (dot >= 0) {
            return FastMath.asin(v3.getNorm() / normProduct);
        }
        return FastMath.PI - FastMath.asin(v3.getNorm() / normProduct);
    }

    // the vectors are sufficiently separated to use the cosine
    return FastMath.acos(dot / normProduct);

}
 
Example 6
Source File: Vector3D.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Compute the angular separation between two vectors.
 * <p>This method computes the angular separation between two
 * vectors using the dot product for well separated vectors and the
 * cross product for almost aligned vectors. This allows to have a
 * good accuracy in all cases, even for vectors very close to each
 * other.</p>
 * @param v1 first vector
 * @param v2 second vector
 * @return angular separation between v1 and v2
 * @exception MathArithmeticException if either vector has a null norm
 */
public static double angle(Vector3D v1, Vector3D v2) throws MathArithmeticException {

    double normProduct = v1.getNorm() * v2.getNorm();
    if (normProduct == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }

    double dot = v1.dotProduct(v2);
    double threshold = normProduct * 0.9999;
    if ((dot < -threshold) || (dot > threshold)) {
        // the vectors are almost aligned, compute using the sine
        Vector3D v3 = crossProduct(v1, v2);
        if (dot >= 0) {
            return FastMath.asin(v3.getNorm() / normProduct);
        }
        return FastMath.PI - FastMath.asin(v3.getNorm() / normProduct);
    }

    // the vectors are sufficiently separated to use the cosine
    return FastMath.acos(dot / normProduct);

}
 
Example 7
Source File: SphericalCoordinates.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Build a spherical coordinates transformer from Cartesian coordinates.
 * @param v Cartesian coordinates
 */
public SphericalCoordinates(final Vector3D v) {

    // Cartesian coordinates
    this.v = v;

    // remaining spherical coordinates
    this.r     = v.getNorm();
    this.theta = v.getAlpha();
    this.phi   = FastMath.acos(v.getZ() / r);

}
 
Example 8
Source File: SphericalCoordinates.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Build a spherical coordinates transformer from Cartesian coordinates.
 * @param v Cartesian coordinates
 */
public SphericalCoordinates(final Vector3D v) {

    // Cartesian coordinates
    this.v = v;

    // remaining spherical coordinates
    this.r     = v.getNorm();
    this.theta = v.getAlpha();
    this.phi   = FastMath.acos(v.getZ() / r);

}
 
Example 9
Source File: CircularKernelTrick.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public float calculate(MathVector leftVector, MathVector rightVector) {
    float coefficient = distance.getCoefficient(leftVector, rightVector);
    if (coefficient > sigma) {
        return 0F;
    } else {
        coefficient = coefficient / sigma;
        return (float) ((pi * FastMath.acos(-coefficient)) - (pi * coefficient * FastMath.sqrt(1F - euclidean.getCoefficient(leftVector, rightVector) / sigma)));
    }
}
 
Example 10
Source File: HaversineDistance.java    From mapreduce-kmeans with Apache License 2.0 5 votes vote down vote up
@Override
public final double measureDistance(double[] a, double[] b) {
	// lat must be on index 0 and lng on index 1
	a = Arrays.copyOf(a, 2);
	b = Arrays.copyOf(b, 2);
	// first convert them to radians
	a[0] = a[0] / 180.0 * Math.PI;
	a[1] = a[1] / 180.0 * Math.PI;
	b[0] = b[0] / 180.0 * Math.PI;
	b[1] = b[1] / 180.0 * Math.PI;

	return FastMath.acos(FastMath.sin(a[0]) * FastMath.sin(b[0]) + FastMath.cos(a[0]) * FastMath.cos(b[0])
			* FastMath.cos(a[1] - b[1]))
			* EARTH_RADIUS_IN_METERS;
}
 
Example 11
Source File: SphericalCoordinates.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Build a spherical coordinates transformer from Cartesian coordinates.
 * @param v Cartesian coordinates
 */
public SphericalCoordinates(final Vector3D v) {

    // Cartesian coordinates
    this.v = v;

    // remaining spherical coordinates
    this.r     = v.getNorm();
    this.theta = v.getAlpha();
    this.phi   = FastMath.acos(v.getZ() / r);

}
 
Example 12
Source File: Rotation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Get the angle of the rotation.
 * @return angle of the rotation (between 0 and &pi;)
 * @see #Rotation(Vector3D, double)
 */
public double getAngle() {
  if ((q0 < -0.1) || (q0 > 0.1)) {
    return 2 * FastMath.asin(FastMath.sqrt(q1 * q1 + q2 * q2 + q3 * q3));
  } else if (q0 < 0) {
    return 2 * FastMath.acos(-q0);
  }
  return 2 * FastMath.acos(q0);
}
 
Example 13
Source File: Rotation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Get the angle of the rotation.
 * @return angle of the rotation (between 0 and &pi;)
 * @see #Rotation(Vector3D, double)
 */
public double getAngle() {
  if ((q0 < -0.1) || (q0 > 0.1)) {
    return 2 * FastMath.asin(FastMath.sqrt(q1 * q1 + q2 * q2 + q3 * q3));
  } else if (q0 < 0) {
    return 2 * FastMath.acos(-q0);
  }
  return 2 * FastMath.acos(q0);
}
 
Example 14
Source File: LibSpoofPrimitives.java    From systemds with Apache License 2.0 4 votes vote down vote up
public static void vectAcosAdd(double[] a, double[] c, int[] aix, int ai, int ci, int alen, int len) {
	for( int j = ai; j < ai+alen; j++ )
		c[ci + aix[j]] += FastMath.acos(a[j]);
}
 
Example 15
Source File: Builtin.java    From systemds with Apache License 2.0 4 votes vote down vote up
@Override
public double execute (double in) {
	switch(bFunc) {
		case SIN:    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
		// FastMath.*h is faster 98% of time than Math.*h in initial micro-benchmarks
		case SINH:   return FASTMATH ? FastMath.sinh(in) : Math.sinh(in);
		case COSH:   return FASTMATH ? FastMath.cosh(in) : Math.cosh(in);
		case TANH:   return FASTMATH ? FastMath.tanh(in) : Math.tanh(in);
		case CEIL:   return FASTMATH ? FastMath.ceil(in) : Math.ceil(in);
		case FLOOR:  return FASTMATH ? FastMath.floor(in) : Math.floor(in);
		case LOG:    return Math.log(in); //faster in Math
		case LOG_NZ: return (in==0) ? 0 : Math.log(in); //faster in Math
		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 //faster in Math
				return 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 ISNA: return Double.isNaN(in) ? 1 : 0;
		case ISNAN: return Double.isNaN(in) ? 1 : 0;
		case ISINF: return Double.isInfinite(in) ? 1 : 0;
		
		default:
			throw new DMLRuntimeException("Builtin.execute(): Unknown operation: " + bFunc);
	}
}
 
Example 16
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Compute arc cosine of a derivative structure.
 * @param operand array holding the operand
 * @param operandOffset offset of the operand in its array
 * @param result array where result must be stored (for
 * arc cosine the result array <em>cannot</em> be the input
 * array)
 * @param resultOffset offset of the result in its array
 */
public void acos(final double[] operand, final int operandOffset,
                final double[] result, final int resultOffset) {

    // create the function value and derivatives
    double[] function = new double[1 + order];
    final double x = operand[operandOffset];
    function[0] = FastMath.acos(x);
    if (order > 0) {
        // the nth order derivative of acos has the form:
        // dn(acos(x)/dxn = P_n(x) / [1 - x^2]^((2n-1)/2)
        // where P_n(x) is a degree n-1 polynomial with same parity as n-1
        // P_1(x) = -1, P_2(x) = -x, P_3(x) = -2x^2 - 1 ...
        // the general recurrence relation for P_n is:
        // P_n(x) = (1-x^2) P_(n-1)'(x) + (2n-3) x P_(n-1)(x)
        // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array
        final double[] p = new double[order];
        p[0] = -1;
        final double x2    = x * x;
        final double f     = 1.0 / (1 - x2);
        double coeff = FastMath.sqrt(f);
        function[1] = coeff * p[0];
        for (int n = 2; n <= order; ++n) {

            // update and evaluate polynomial P_n(x)
            double v = 0;
            p[n - 1] = (n - 1) * p[n - 2];
            for (int k = n - 1; k >= 0; k -= 2) {
                v = v * x2 + p[k];
                if (k > 2) {
                    p[k - 2] = (k - 1) * p[k - 1] + (2 * n - k) * p[k - 3];
                } else if (k == 2) {
                    p[0] = p[1];
                }
            }
            if ((n & 0x1) == 0) {
                v *= x;
            }

            coeff *= f;
            function[n] = coeff * v;

        }
    }

    // apply function composition
    compose(operand, operandOffset, function, result, resultOffset);

}
 
Example 17
Source File: Acos.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.acos(x);
}
 
Example 18
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Compute arc cosine of a derivative structure.
 * @param operand array holding the operand
 * @param operandOffset offset of the operand in its array
 * @param result array where result must be stored (for
 * arc cosine the result array <em>cannot</em> be the input
 * array)
 * @param resultOffset offset of the result in its array
 */
public void acos(final double[] operand, final int operandOffset,
                final double[] result, final int resultOffset) {

    // create the function value and derivatives
    double[] function = new double[1 + order];
    final double x = operand[operandOffset];
    function[0] = FastMath.acos(x);
    if (order > 0) {
        // the nth order derivative of acos has the form:
        // dn(acos(x)/dxn = P_n(x) / [1 - x^2]^((2n-1)/2)
        // where P_n(x) is a degree n-1 polynomial with same parity as n-1
        // P_1(x) = -1, P_2(x) = -x, P_3(x) = -2x^2 - 1 ...
        // the general recurrence relation for P_n is:
        // P_n(x) = (1-x^2) P_(n-1)'(x) + (2n-3) x P_(n-1)(x)
        // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array
        final double[] p = new double[order];
        p[0] = -1;
        final double x2    = x * x;
        final double f     = 1.0 / (1 - x2);
        double coeff = FastMath.sqrt(f);
        function[1] = coeff * p[0];
        for (int n = 2; n <= order; ++n) {

            // update and evaluate polynomial P_n(x)
            double v = 0;
            p[n - 1] = (n - 1) * p[n - 2];
            for (int k = n - 1; k >= 0; k -= 2) {
                v = v * x2 + p[k];
                if (k > 2) {
                    p[k - 2] = (k - 1) * p[k - 1] + (2 * n - k) * p[k - 3];
                } else if (k == 2) {
                    p[0] = p[1];
                }
            }
            if ((n & 0x1) == 0) {
                v *= x;
            }

            coeff *= f;
            function[n] = coeff * v;

        }
    }

    // apply function composition
    compose(operand, operandOffset, function, result, resultOffset);

}
 
Example 19
Source File: Acos.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.acos(x);
}
 
Example 20
Source File: LibSpoofPrimitives.java    From systemds with Apache License 2.0 4 votes vote down vote up
public static void vectAcosAdd(double[] a, double[] c, int ai, int ci, int len) {
	for( int j = ai; j < ai+len; j++, ci++)
		c[ci] +=  FastMath.acos(a[j]);
}