Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#ZERO_NORM

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#ZERO_NORM . 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: Vector2D.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(Vector2D v1, Vector2D 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
        final double n = FastMath.abs(MathArrays.linearCombination(v1.x, v2.y, -v1.y, v2.x));
        if (dot >= 0) {
            return FastMath.asin(n / normProduct);
        }
        return FastMath.PI - FastMath.asin(n / normProduct);
    }

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

}
 
Example 2
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 3
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 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: Cardumen_0040_s.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: FieldVector3D.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
 * @param <T> the type of the field elements
 * @return angular separation between v1 and v2
 * @exception MathArithmeticException if either vector has a null norm
 */
public static <T extends RealFieldElement<T>> T angle(final FieldVector3D<T> v1, final Vector3D v2)
    throws MathArithmeticException {

    final T normProduct = v1.getNorm().multiply(v2.getNorm());
    if (normProduct.getReal() == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }

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

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

}
 
Example 7
Source File: Plane.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Set the normal vactor.
 * @param normal normal direction to the plane (will be copied)
 * @exception MathArithmeticException if the normal norm is too small
 */
private void setNormal(final Vector3D normal) throws MathArithmeticException {
    final double norm = normal.getNorm();
    if (norm < 1.0e-10) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }
    w = new Vector3D(1.0 / norm, normal);
}
 
Example 8
Source File: NPEfix_00192_s.java    From coming with MIT License 5 votes vote down vote up
/** Reset the instance as if built from two points.
 * @param p1 first point belonging to the line (this can be any point)
 * @param p2 second point belonging to the line (this can be any point, different from p1)
 * @exception MathIllegalArgumentException if the points are equal
 */
public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException {
    final Vector3D delta = p2.subtract(p1);
    final double norm2 = delta.getNormSq();
    if (norm2 == 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM);
    }
    this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta);
    zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta);
}
 
Example 9
Source File: Line.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Reset the instance as if built from two points.
 * @param p1 first point belonging to the line (this can be any point)
 * @param p2 second point belonging to the line (this can be any point, different from p1)
 * @exception MathIllegalArgumentException if the points are equal
 */
public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException {
    final Vector3D delta = p2.subtract(p1);
    final double norm2 = delta.getNormSq();
    if (norm2 == 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM);
    }
    this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta);
    zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta);
}
 
Example 10
Source File: NPEfix_00178_t.java    From coming with MIT License 5 votes vote down vote up
/** Reset the instance as if built from two points.
 * @param p1 first point belonging to the line (this can be any point)
 * @param p2 second point belonging to the line (this can be any point, different from p1)
 * @exception MathIllegalArgumentException if the points are equal
 */
public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException {
    final Vector3D delta = p2.subtract(p1);
    final double norm2 = delta.getNormSq();
    if (norm2 == 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM);
    }
    this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta);
    zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta);
}
 
Example 11
Source File: Line.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Reset the instance as if built from two points.
 * @param p1 first point belonging to the line (this can be any point)
 * @param p2 second point belonging to the line (this can be any point, different from p1)
 * @exception MathIllegalArgumentException if the points are equal
 */
public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException {
    final Vector3D delta = p2.subtract(p1);
    final double norm2 = delta.getNormSq();
    if (norm2 == 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM);
    }
    this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta);
    zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta);
}
 
Example 12
Source File: RealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the cosine of the angle between this vector and the
 * argument.
 *
 * @param v Vector.
 * @return the cosine of the angle between this vector and {@code v}.
 * @throws MathArithmeticException if {@code this} or {@code v} is the null
 * vector
 * @throws DimensionMismatchException if the dimensions of {@code this} and
 * {@code v} do not match
 */
public double cosine(RealVector v) throws DimensionMismatchException,
    MathArithmeticException {
    final double norm = getNorm();
    final double vNorm = v.getNorm();

    if (norm == 0 ||
        vNorm == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }
    return dotProduct(v) / (norm * vNorm);
}
 
Example 13
Source File: NPEfix_00192_t.java    From coming with MIT License 5 votes vote down vote up
/** Reset the instance as if built from two points.
 * @param p1 first point belonging to the line (this can be any point)
 * @param p2 second point belonging to the line (this can be any point, different from p1)
 * @exception MathIllegalArgumentException if the points are equal
 */
public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException {
    final Vector3D delta = p2.subtract(p1);
    final double norm2 = delta.getNormSq();
    if (norm2 == 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM);
    }
    this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta);
    zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta);
}
 
Example 14
Source File: NPEfix_00187_t.java    From coming with MIT License 5 votes vote down vote up
/** Reset the instance as if built from two points.
 * @param p1 first point belonging to the line (this can be any point)
 * @param p2 second point belonging to the line (this can be any point, different from p1)
 * @exception MathIllegalArgumentException if the points are equal
 */
public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException {
    final Vector3D delta = p2.subtract(p1);
    final double norm2 = delta.getNormSq();
    if (norm2 == 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM);
    }
    this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta);
    zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta);
}
 
Example 15
Source File: NPEfix_00182_t.java    From coming with MIT License 5 votes vote down vote up
/** Reset the instance as if built from two points.
 * @param p1 first point belonging to the line (this can be any point)
 * @param p2 second point belonging to the line (this can be any point, different from p1)
 * @exception MathIllegalArgumentException if the points are equal
 */
public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException {
    final Vector3D delta = p2.subtract(p1);
    final double norm2 = delta.getNormSq();
    if (norm2 == 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM);
    }
    this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta);
    zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta);
}
 
Example 16
Source File: NPEfix_00184_s.java    From coming with MIT License 5 votes vote down vote up
/** Reset the instance as if built from two points.
 * @param p1 first point belonging to the line (this can be any point)
 * @param p2 second point belonging to the line (this can be any point, different from p1)
 * @exception MathIllegalArgumentException if the points are equal
 */
public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException {
    final Vector3D delta = p2.subtract(p1);
    final double norm2 = delta.getNormSq();
    if (norm2 == 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM);
    }
    this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta);
    zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta);
}
 
Example 17
Source File: OpenMapRealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() throws MathArithmeticException {
    double norm = getNorm();
    if (isDefaultValue(norm)) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }
    Iterator iter = entries.iterator();
    while (iter.hasNext()) {
        iter.advance();
        entries.put(iter.key(), iter.value() / norm);
    }
}
 
Example 18
Source File: NPEfix_00179_s.java    From coming with MIT License 5 votes vote down vote up
/** Reset the instance as if built from two points.
 * @param p1 first point belonging to the line (this can be any point)
 * @param p2 second point belonging to the line (this can be any point, different from p1)
 * @exception MathIllegalArgumentException if the points are equal
 */
public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException {
    final Vector3D delta = p2.subtract(p1);
    final double norm2 = delta.getNormSq();
    if (norm2 == 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM);
    }
    this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta);
    zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta);
}
 
Example 19
Source File: OpenMapRealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() throws MathArithmeticException {
    double norm = getNorm();
    if (isDefaultValue(norm)) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }
    Iterator iter = entries.iterator();
    while (iter.hasNext()) {
        iter.advance();
        entries.put(iter.key(), iter.value() / norm);
    }
}
 
Example 20
Source File: RealVector.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Computes the cosine of the angle between this vector and the
 * argument.
 *
 * @param v Vector.
 * @return the cosine of the angle between this vector and {@code v}.
 * @throws MathArithmeticException if {@code this} or {@code v} is the null
 * vector
 * @throws DimensionMismatchException if the dimensions of {@code this} and
 * {@code v} do not match
 */
public double cosine(RealVector v) {
    final double norm = getNorm();
    final double vNorm = v.getNorm();

    if (norm == 0 ||
        vNorm == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }
    return dotProduct(v) / (norm * vNorm);
}