本文整理汇总了Java中org.apache.commons.math.MathRuntimeException.createArithmeticException方法的典型用法代码示例。如果您正苦于以下问题:Java MathRuntimeException.createArithmeticException方法的具体用法?Java MathRuntimeException.createArithmeticException怎么用?Java MathRuntimeException.createArithmeticException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math.MathRuntimeException
的用法示例。
在下文中一共展示了MathRuntimeException.createArithmeticException方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: angle
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/** 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 MathRuntimeException.createArithmeticException("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 Math.asin(v3.getNorm() / normProduct);
}
return Math.PI - Math.asin(v3.getNorm() / normProduct);
}
// the vectors are sufficiently separated to use the cosine
return Math.acos(dot / normProduct);
}
示例2: Rotation
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/** Build a rotation from an axis and an angle.
* <p>We use the convention that angles are oriented according to
* the effect of the rotation on vectors around the axis. That means
* that if (i, j, k) is a direct frame and if we first provide +k as
* the axis and PI/2 as the angle to this constructor, and then
* {@link #applyTo(Vector3D) apply} the instance to +i, we will get
* +j.</p>
* @param axis axis around which to rotate
* @param angle rotation angle.
* @exception ArithmeticException if the axis norm is zero
*/
public Rotation(Vector3D axis, double angle) {
double norm = axis.getNorm();
if (norm == 0) {
throw MathRuntimeException.createArithmeticException("zero norm for rotation axis");
}
double halfAngle = -0.5 * angle;
double coeff = Math.sin(halfAngle) / norm;
q0 = Math.cos (halfAngle);
q1 = coeff * axis.getX();
q2 = coeff * axis.getY();
q3 = coeff * axis.getZ();
}
示例3: negate
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/**
* Return the additive inverse of this fraction.
* @return the negation of this fraction.
*/
public Fraction negate() {
if (numerator==Integer.MIN_VALUE) {
throw MathRuntimeException.createArithmeticException(
OVERFLOW_MESSAGE, numerator, denominator);
}
return new Fraction(-numerator, denominator);
}
示例4: BigFraction
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/**
* <p>
* Create a {@link BigFraction} given the numerator and denominator as
* <code>BigInteger</code>. The {@link BigFraction} is reduced to lowest terms.
* </p>
*
* @param num
* the numerator, must not be <code>null</code>.
* @param den
* the denominator, must not be <code>null</code>.
* @throws ArithmeticException
* if the denominator is <code>zero</code>.
* @throws NullPointerException
* if the numerator or the denominator is <code>zero</code>.
*/
public BigFraction(BigInteger num, BigInteger den) {
if (num == null) {
throw MathRuntimeException.createNullPointerException("numerator is null");
}
if (den == null) {
throw MathRuntimeException.createNullPointerException("denominator is null");
}
if (BigInteger.ZERO.equals(den)) {
throw MathRuntimeException.createArithmeticException(FORBIDDEN_ZERO_DENOMINATOR);
}
if (BigInteger.ZERO.equals(num)) {
numerator = BigInteger.ZERO;
denominator = BigInteger.ONE;
} else {
// reduce numerator and denominator by greatest common denominator
final BigInteger gcd = num.gcd(den);
if (BigInteger.ONE.compareTo(gcd) < 0) {
num = num.divide(gcd);
den = den.divide(gcd);
}
// move sign to numerator
if (BigInteger.ZERO.compareTo(den) > 0) {
num = num.negate();
den = den.negate();
}
// store the values in the final fields
numerator = num;
denominator = den;
}
}
示例5: getReducedFraction
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/**
* <p>Creates a <code>Fraction</code> instance with the 2 parts
* of a fraction Y/Z.</p>
*
* <p>Any negative signs are resolved to be on the numerator.</p>
*
* @param numerator the numerator, for example the three in 'three sevenths'
* @param denominator the denominator, for example the seven in 'three sevenths'
* @return a new fraction instance, with the numerator and denominator reduced
* @throws ArithmeticException if the denominator is <code>zero</code>
*/
public static Fraction getReducedFraction(int numerator, int denominator) {
if (denominator == 0) {
throw MathRuntimeException.createArithmeticException(
"zero denominator in fraction {0}/{1}",
numerator, denominator);
}
if (numerator==0) {
return ZERO; // normalize zero.
}
// allow 2^k/-2^31 as a valid fraction (where k>0)
if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {
numerator/=2; denominator/=2;
}
if (denominator < 0) {
if (numerator==Integer.MIN_VALUE ||
denominator==Integer.MIN_VALUE) {
throw MathRuntimeException.createArithmeticException(
"overflow in fraction {0}/{1}, cannot negate",
numerator, denominator);
}
numerator = -numerator;
denominator = -denominator;
}
// simplify fraction.
int gcd = MathUtils.gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
return new Fraction(numerator, denominator);
}
示例6: BigFraction
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/**
* <p>
* Create a {@link BigFraction} given the numerator and denominator as
* <code>BigInteger</code>. The {@link BigFraction} is reduced to lowest terms.
* </p>
*
* @param num
* the numerator, must not be <code>null</code>.
* @param den
* the denominator, must not be <code>null</code>.
* @throws ArithmeticException
* if the denominator is <code>zero</code>.
* @throws NullPointerException
* if the numerator or the denominator is <code>zero</code>.
*/
public BigFraction(BigInteger num, BigInteger den) {
if (num == null) {
throw MathRuntimeException.createNullPointerException("numerator is null");
}
if (den == null) {
throw MathRuntimeException.createNullPointerException("denominator is null");
}
if (BigInteger.ZERO.equals(den)) {
throw MathRuntimeException.createArithmeticException("denominator must be different from 0");
}
if (BigInteger.ZERO.equals(num)) {
numerator = BigInteger.ZERO;
denominator = BigInteger.ONE;
} else {
// reduce numerator and denominator by greatest common denominator
final BigInteger gcd = num.gcd(den);
if (BigInteger.ONE.compareTo(gcd) < 0) {
num = num.divide(gcd);
den = den.divide(gcd);
}
// move sign to numerator
if (BigInteger.ZERO.compareTo(den) > 0) {
num = num.negate();
den = den.negate();
}
// store the values in the final fields
numerator = num;
denominator = den;
}
}
示例7: unitize
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/** {@inheritDoc} */
public void unitize() throws ArithmeticException {
final double norm = getNorm();
if (norm == 0) {
throw MathRuntimeException.createArithmeticException("cannot normalize a zero norm vector");
}
for (int i = 0; i < data.length; i++) {
data[i] /= norm;
}
}
示例8: getReducedFraction
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/**
* <p>Creates a <code>Fraction</code> instance with the 2 parts
* of a fraction Y/Z.</p>
*
* <p>Any negative signs are resolved to be on the numerator.</p>
*
* @param numerator the numerator, for example the three in 'three sevenths'
* @param denominator the denominator, for example the seven in 'three sevenths'
* @return a new fraction instance, with the numerator and denominator reduced
* @throws ArithmeticException if the denominator is <code>zero</code>
*/
public static Fraction getReducedFraction(int numerator, int denominator) {
if (denominator == 0) {
throw MathRuntimeException.createArithmeticException(
ZERO_DENOMINATOR_MESSAGE, numerator, denominator);
}
if (numerator==0) {
return ZERO; // normalize zero.
}
// allow 2^k/-2^31 as a valid fraction (where k>0)
if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {
numerator/=2; denominator/=2;
}
if (denominator < 0) {
if (numerator==Integer.MIN_VALUE ||
denominator==Integer.MIN_VALUE) {
throw MathRuntimeException.createArithmeticException(
OVERFLOW_MESSAGE, numerator, denominator);
}
numerator = -numerator;
denominator = -denominator;
}
// simplify fraction.
int gcd = MathUtils.gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
return new Fraction(numerator, denominator);
}
示例9: divide
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/**
* <p>Divide the value of this fraction by another.</p>
*
* @param fraction the fraction to divide by, must not be <code>null</code>
* @return a <code>Fraction</code> instance with the resulting values
* @throws IllegalArgumentException if the fraction is <code>null</code>
* @throws ArithmeticException if the fraction to divide by is zero
* @throws ArithmeticException if the resulting numerator or denominator exceeds
* <code>Integer.MAX_VALUE</code>
*/
public Fraction divide(Fraction fraction) {
if (fraction == null) {
throw MathRuntimeException.createIllegalArgumentException(NULL_FRACTION);
}
if (fraction.numerator == 0) {
throw MathRuntimeException.createArithmeticException(
"the fraction to divide by must not be zero: {0}/{1}",
fraction.numerator, fraction.denominator);
}
return multiply(fraction.reciprocal());
}
示例10: unitVector
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
public RealVector unitVector() throws ArithmeticException {
final double norm = getNorm();
if (norm == 0) {
throw MathRuntimeException.createArithmeticException("zero norm");
}
return mapDivide(norm);
}
示例11: unitize
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
public void unitize() throws ArithmeticException {
final double norm = getNorm();
if (norm == 0) {
throw MathRuntimeException.createArithmeticException("cannot normalize a zero norm vector");
}
mapDivideToSelf(norm);
}
示例12: unitize
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
public void unitize() {
double norm = getNorm();
if (isDefaultValue(norm)) {
throw MathRuntimeException.createArithmeticException("cannot normalize a zero norm vector");
}
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), iter.value() / norm);
}
}
示例13: Fraction
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/**
* Create a fraction given the numerator and denominator. The fraction is
* reduced to lowest terms.
* @param num the numerator.
* @param den the denominator.
* @throws ArithmeticException if the denominator is <code>zero</code>
*/
public Fraction(int num, int den) {
if (den == 0) {
throw MathRuntimeException.createArithmeticException("zero denominator in fraction {0}/{1}",
num, den);
}
if (den < 0) {
if (num == Integer.MIN_VALUE || den == Integer.MIN_VALUE) {
throw MathRuntimeException.createArithmeticException("overflow in fraction {0}/{1}, cannot negate",
num, den);
}
num = -num;
den = -den;
}
// reduce numerator and denominator by greatest common denominator.
final int d = MathUtils.gcd(num, den);
if (d > 1) {
num /= d;
den /= d;
}
// move sign to numerator.
if (den < 0) {
num = -num;
den = -den;
}
this.numerator = num;
this.denominator = den;
}
示例14: computeCoefficients
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/**
* Calculate the coefficients of Lagrange polynomial from the
* interpolation data. It takes O(N^2) time.
* <p>
* Note this computation can be ill-conditioned. Use with caution
* and only when it is necessary.</p>
*
* @throws ArithmeticException if any abscissas coincide
*/
protected void computeCoefficients() throws ArithmeticException {
final int n = degree() + 1;
coefficients = new double[n];
for (int i = 0; i < n; i++) {
coefficients[i] = 0.0;
}
// c[] are the coefficients of P(x) = (x-x[0])(x-x[1])...(x-x[n-1])
final double[] c = new double[n+1];
c[0] = 1.0;
for (int i = 0; i < n; i++) {
for (int j = i; j > 0; j--) {
c[j] = c[j-1] - c[j] * x[i];
}
c[0] *= -x[i];
c[i+1] = 1;
}
final double[] tc = new double[n];
for (int i = 0; i < n; i++) {
// d = (x[i]-x[0])...(x[i]-x[i-1])(x[i]-x[i+1])...(x[i]-x[n-1])
double d = 1;
for (int j = 0; j < n; j++) {
if (i != j) {
d *= x[i] - x[j];
}
}
if (d == 0.0) {
// This happens only when two abscissas are identical.
for (int k = 0; k < n; ++k) {
if ((i != k) && (x[i] == x[k])) {
throw MathRuntimeException.createArithmeticException("identical abscissas x[{0}] == x[{1}] == {2} cause division by zero",
i, k, x[i]);
}
}
}
final double t = y[i] / d;
// Lagrange polynomial is the sum of n terms, each of which is a
// polynomial of degree n-1. tc[] are the coefficients of the i-th
// numerator Pi(x) = (x-x[0])...(x-x[i-1])(x-x[i+1])...(x-x[n-1]).
tc[n-1] = c[n]; // actually c[n] = 1
coefficients[n-1] += t * tc[n-1];
for (int j = n-2; j >= 0; j--) {
tc[j] = c[j+1] + tc[j+1] * x[i];
coefficients[j] += t * tc[j];
}
}
coefficientsComputed = true;
}
示例15: Rotation
import org.apache.commons.math.MathRuntimeException; //导入方法依赖的package包/类
/** Build a rotation from an axis and an angle.
* <p>We use the convention that angles are oriented according to
* the effect of the rotation on vectors around the axis. That means
* that if (i, j, k) is a direct frame and if we first provide +k as
* the axis and π/2 as the angle to this constructor, and then
* {@link #applyTo(Vector3D) apply} the instance to +i, we will get
* +j.</p>
* <p>Another way to represent our convention is to say that a rotation
* of angle θ about the unit vector (x, y, z) is the same as the
* rotation build from quaternion components { cos(-θ/2),
* x * sin(-θ/2), y * sin(-θ/2), z * sin(-θ/2) }.
* Note the minus sign on the angle!</p>
* <p>On the one hand this convention is consistent with a vectorial
* perspective (moving vectors in fixed frames), on the other hand it
* is different from conventions with a frame perspective (fixed vectors
* viewed from different frames) like the ones used for example in spacecraft
* attitude community or in the graphics community.</p>
* @param axis axis around which to rotate
* @param angle rotation angle.
* @exception ArithmeticException if the axis norm is zero
*/
public Rotation(Vector3D axis, double angle) {
double norm = axis.getNorm();
if (norm == 0) {
throw MathRuntimeException.createArithmeticException("zero norm for rotation axis");
}
double halfAngle = -0.5 * angle;
double coeff = Math.sin(halfAngle) / norm;
q0 = Math.cos (halfAngle);
q1 = coeff * axis.getX();
q2 = coeff * axis.getY();
q3 = coeff * axis.getZ();
}