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

The following examples show how to use org.apache.commons.math.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: Math_55_Vector3D_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 ArithmeticException 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 = dotProduct(v1, 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: 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 ArithmeticException 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 = dotProduct(v1, 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: JGenProg2017_0063_t.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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 4
Source File: OpenMapRealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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 5
Source File: jKali_0011_t.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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 6
Source File: ArrayRealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public RealVector unitVector() {
    final double norm = getNorm();
    if (norm == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }
    return mapDivide(norm);
}
 
Example 7
Source File: ArrayRealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    final double norm = getNorm();
    if (norm == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }
    mapDivideToSelf(norm);
}
 
Example 8
Source File: Nopol2017_0072_s.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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 9
Source File: JGenProg2015_004_t.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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 10
Source File: Math_49_OpenMapRealVector_s.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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 11
Source File: JGenProg2017_0063_s.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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 12
Source File: AbstractRealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
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);
}
 
Example 13
Source File: Nopol2015_006_t.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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 14
Source File: Nopol2015_006_s.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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 15
Source File: Cardumen_0045_t.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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 16
Source File: ArrayRealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public RealVector unitVector() {
    final double norm = getNorm();
    if (norm == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }
    return mapDivide(norm);
}
 
Example 17
Source File: Cardumen_00219_t.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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: ArrayRealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    final double norm = getNorm();
    if (norm == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
    }
    mapDivideToSelf(norm);
}
 
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() {
    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: Cardumen_00114_t.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void unitize() {
    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);
    }
}