本文整理汇总了Java中org.apache.commons.math3.exception.MathArithmeticException类的典型用法代码示例。如果您正苦于以下问题:Java MathArithmeticException类的具体用法?Java MathArithmeticException怎么用?Java MathArithmeticException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MathArithmeticException类属于org.apache.commons.math3.exception包,在下文中一共展示了MathArithmeticException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: angle
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的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
* @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 FieldVector3D<T> 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();
}
示例2: solveUpperTriangularSystem
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/** Solver a system composed of an Upper Triangular Matrix
* {@link RealMatrix}.
* <p>
* This method is called to solve systems of equations which are
* of the lower triangular form. The matrix {@link RealMatrix}
* is assumed, though not checked, to be in upper triangular form.
* The vector {@link RealVector} is overwritten with the solution.
* The matrix is checked that it is square and its dimensions match
* the length of the vector.
* </p>
* @param rm RealMatrix which is upper triangular
* @param b RealVector this is overwritten
* @throws DimensionMismatchException if the matrix and vector are not
* conformable
* @throws NonSquareMatrixException if the matrix {@code rm} is not
* square
* @throws MathArithmeticException if the absolute value of one of the diagonal
* coefficient of {@code rm} is lower than {@link Precision#SAFE_MIN}
*/
public static void solveUpperTriangularSystem(RealMatrix rm, RealVector b)
throws DimensionMismatchException, MathArithmeticException,
NonSquareMatrixException {
if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) {
throw new DimensionMismatchException(
(rm == null) ? 0 : rm.getRowDimension(),
(b == null) ? 0 : b.getDimension());
}
if( rm.getColumnDimension() != rm.getRowDimension() ){
throw new NonSquareMatrixException(rm.getRowDimension(),
rm.getColumnDimension());
}
int rows = rm.getRowDimension();
for( int i = rows-1 ; i >-1 ; i-- ){
double diag = rm.getEntry(i, i);
if( FastMath.abs(diag) < Precision.SAFE_MIN ){
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
}
double bi = b.getEntry(i)/diag;
b.setEntry(i, bi );
for( int j = i-1; j>-1; j-- ){
b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i) );
}
}
}
示例3: solveLowerTriangularSystem
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/**Solve a system of composed of a Lower Triangular Matrix
* {@link RealMatrix}.
* <p>
* This method is called to solve systems of equations which are
* of the lower triangular form. The matrix {@link RealMatrix}
* is assumed, though not checked, to be in lower triangular form.
* The vector {@link RealVector} is overwritten with the solution.
* The matrix is checked that it is square and its dimensions match
* the length of the vector.
* </p>
* @param rm RealMatrix which is lower triangular
* @param b RealVector this is overwritten
* @throws DimensionMismatchException if the matrix and vector are not
* conformable
* @throws NonSquareMatrixException if the matrix {@code rm} is not square
* @throws MathArithmeticException if the absolute value of one of the diagonal
* coefficient of {@code rm} is lower than {@link Precision#SAFE_MIN}
*/
public static void solveLowerTriangularSystem(RealMatrix rm, RealVector b)
throws DimensionMismatchException, MathArithmeticException,
NonSquareMatrixException {
if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) {
throw new DimensionMismatchException(
(rm == null) ? 0 : rm.getRowDimension(),
(b == null) ? 0 : b.getDimension());
}
if( rm.getColumnDimension() != rm.getRowDimension() ){
throw new NonSquareMatrixException(rm.getRowDimension(),
rm.getColumnDimension());
}
int rows = rm.getRowDimension();
for( int i = 0 ; i < rows ; i++ ){
double diag = rm.getEntry(i, i);
if( FastMath.abs(diag) < Precision.SAFE_MIN ){
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
}
double bi = b.getEntry(i)/diag;
b.setEntry(i, bi );
for( int j = i+1; j< rows; j++ ){
b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i) );
}
}
}
示例4: angle
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的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 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);
}
示例5: exactK
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/**
* Calculates the exact value of {@code P(D_n < d)} using the method described in [1] (reference
* in class javadoc above) and {@link org.apache.commons.math3.fraction.BigFraction} (see
* above).
*
* @param d statistic
* @param n sample size
* @return the two-sided probability of \(P(D_n < d)\)
* @throws MathArithmeticException if algorithm fails to convert {@code h} to a
* {@link org.apache.commons.math3.fraction.BigFraction} in expressing {@code d} as \((k
* - h) / m\) for integer {@code k, m} and \(0 \le h < 1\).
*/
private double exactK(double d, int n)
throws MathArithmeticException {
final int k = (int) Math.ceil(n * d);
final FieldMatrix<BigFraction> H = this.createExactH(d, n);
final FieldMatrix<BigFraction> Hpower = H.power(n);
BigFraction pFrac = Hpower.getEntry(k - 1, k - 1);
for (int i = 1; i <= n; ++i) {
pFrac = pFrac.multiply(i).divide(n);
}
/*
* BigFraction.doubleValue converts numerator to double and the denominator to double and
* divides afterwards. That gives NaN quite easy. This does not (scale is the number of
* digits):
*/
return pFrac.bigDecimalValue(20, BigDecimal.ROUND_HALF_UP).doubleValue();
}
示例6: taylor
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/** Evaluate Taylor expansion of a derivative structure.
* @param ds array holding the derivative structure
* @param dsOffset offset of the derivative structure in its array
* @param delta parameters offsets (Δx, Δy, ...)
* @return value of the Taylor expansion at x + Δx, y + Δy, ...
* @throws MathArithmeticException if factorials becomes too large
*/
public double taylor(final double[] ds, final int dsOffset, final double ... delta)
throws MathArithmeticException {
double value = 0;
for (int i = getSize() - 1; i >= 0; --i) {
final int[] orders = getPartialDerivativeOrders(i);
double term = ds[dsOffset + i];
for (int k = 0; k < orders.length; ++k) {
if (orders[k] > 0) {
try {
term *= FastMath.pow(delta[k], orders[k]) /
CombinatoricsUtils.factorial(orders[k]);
} catch (NotPositiveException e) {
// this cannot happen
throw new MathInternalError(e);
}
}
}
value += term;
}
return value;
}
示例7: ebeDivide
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/** {@inheritDoc} */
public FieldVector<T> ebeDivide(FieldVector<T> v)
throws DimensionMismatchException, MathArithmeticException {
try {
return ebeDivide((ArrayFieldVector<T>) v);
} catch (ClassCastException cce) {
checkVectorDimensions(v);
T[] out = MathArrays.buildArray(field, data.length);
for (int i = 0; i < data.length; i++) {
try {
out[i] = data[i].divide(v.getEntry(i));
} catch (final MathArithmeticException e) {
throw new MathArithmeticException(LocalizedFormats.INDEX, i);
}
}
return new ArrayFieldVector<T>(field, out, false);
}
}
示例8: floorDiv
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/** Finds q such that a = q b + r with 0 <= r < b if b > 0 and b < r <= 0 if b < 0.
* <p>
* This methods returns the same value as integer division when
* a and b are same signs, but returns a different value when
* they are opposite (i.e. q is negative).
* </p>
* @param a dividend
* @param b divisor
* @return q such that a = q b + r with 0 <= r < b if b > 0 and b < r <= 0 if b < 0
* @exception MathArithmeticException if b == 0
* @see #floorMod(int, int)
* @since 3.4
*/
public static int floorDiv(final int a, final int b) throws MathArithmeticException {
if (b == 0) {
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
}
final int m = a % b;
if ((a ^ b) >= 0 || m == 0) {
// a an b have same sign, or division is exact
return a / b;
} else {
// a and b have opposite signs and division is not exact
return (a / b) - 1;
}
}
示例9: floorMod
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/** Finds r such that a = q b + r with 0 <= r < b if b > 0 and b < r <= 0 if b < 0.
* <p>
* This methods returns the same value as integer modulo when
* a and b are same signs, but returns a different value when
* they are opposite (i.e. q is negative).
* </p>
* @param a dividend
* @param b divisor
* @return r such that a = q b + r with 0 <= r < b if b > 0 and b < r <= 0 if b < 0
* @exception MathArithmeticException if b == 0
* @see #floorDiv(int, int)
* @since 3.4
*/
public static int floorMod(final int a, final int b) throws MathArithmeticException {
if (b == 0) {
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
}
final int m = a % b;
if ((a ^ b) >= 0 || m == 0) {
// a an b have same sign, or division is exact
return m;
} else {
// a and b have opposite signs and division is not exact
return b + m;
}
}
示例10: solveLowerTriangularSystem
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/**Solve a system of composed of a Lower Triangular Matrix
* {@link RealMatrix}.
* <p>
* This method is called to solve systems of equations which are
* of the lower triangular form. The matrix {@link RealMatrix}
* is assumed, though not checked, to be in lower triangular form.
* The vector {@link RealVector} is overwritten with the solution.
* The matrix is checked that it is square and its dimensions match
* the length of the vector.
* </p>
* @param rm RealMatrix which is lower triangular
* @param b RealVector this is overwritten
* @exception IllegalArgumentException if the matrix and vector are not conformable
* @exception ArithmeticException there is a zero or near zero on the diagonal of rm
*/
public static void solveLowerTriangularSystem( RealMatrix rm, RealVector b){
if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) {
throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
(rm == null) ? 0 : rm.getRowDimension(),
(b == null) ? 0 : b.getDimension());
}
if( rm.getColumnDimension() != rm.getRowDimension() ){
throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_2x2,
rm.getRowDimension(),rm.getRowDimension(),
rm.getRowDimension(),rm.getColumnDimension());
}
int rows = rm.getRowDimension();
for( int i = 0 ; i < rows ; i++ ){
double diag = rm.getEntry(i, i);
if( FastMath.abs(diag) < Precision.SAFE_MIN ){
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
}
double bi = b.getEntry(i)/diag;
b.setEntry(i, bi );
for( int j = i+1; j< rows; j++ ){
b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i) );
}
}
}
示例11: EnumeratedDistribution
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/**
* Create an enumerated distribution using the given random number generator
* and probability mass function enumeration.
*
* @param rng random number generator.
* @param pmf probability mass function enumerated as a list of <T, probability>
* pairs.
* @throws NotPositiveException if any of the probabilities are negative.
* @throws NotFiniteNumberException if any of the probabilities are infinite.
* @throws NotANumberException if any of the probabilities are NaN.
* @throws MathArithmeticException all of the probabilities are 0.
*/
public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf)
throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException {
random = rng;
singletons = new ArrayList<T>(pmf.size());
final double[] probs = new double[pmf.size()];
for (int i = 0; i < pmf.size(); i++) {
final Pair<T, Double> sample = pmf.get(i);
singletons.add(sample.getKey());
final double p = sample.getValue();
if (p < 0) {
throw new NotPositiveException(sample.getValue());
}
if (Double.isInfinite(p)) {
throw new NotFiniteNumberException(p);
}
if (Double.isNaN(p)) {
throw new NotANumberException();
}
probs[i] = p;
}
probabilities = MathArrays.normalizeArray(probs, 1.0);
cumulativeProbabilities = new double[probabilities.length];
double sum = 0;
for (int i = 0; i < probabilities.length; i++) {
sum += probabilities[i];
cumulativeProbabilities[i] = sum;
}
}
示例12: unitize
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public void unitize() {
final double norm = getNorm();
if (norm == 0) {
throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
}
mapDivideToSelf(norm);
}
示例13: addAndCheck
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/**
* Add two long integers, checking for overflow.
*
* @param a Addend.
* @param b Addend.
* @param pattern Pattern to use for any thrown exception.
* @return the sum {@code a + b}.
* @throws MathArithmeticException if the result cannot be represented
* as a {@code long}.
* @since 1.2
*/
private static long addAndCheck(long a, long b, Localizable pattern) {
long ret;
if (a > b) {
// use symmetry to reduce boundary cases
ret = addAndCheck(b, a, pattern);
} else {
// assert a <= b
if (a < 0) {
if (b < 0) {
// check for negative overflow
if (Long.MIN_VALUE - b <= a) {
ret = a + b;
} else {
throw new MathArithmeticException(pattern, a, b);
}
} else {
// opposite sign addition is always safe
ret = a + b;
}
} else {
// assert a >= 0
// assert b >= 0
// check for positive overflow
if (a <= Long.MAX_VALUE - b) {
ret = a + b;
} else {
throw new MathArithmeticException(pattern, a, b);
}
}
}
return ret;
}
示例14: roundedK
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/**
* Calculates {@code P(D_n < d)} using method described in [1] and doubles
* (see above).
*
* @param d statistic
* @return the two-sided probability of {@code P(D_n < d)}
* @throws MathArithmeticException if algorithm fails to convert {@code h}
* to a {@link org.apache.commons.math3.fraction.BigFraction} in expressing
* {@code d} as {@code (k - h) / m} for integer {@code k, m} and
* {@code 0 <= h < 1}.
*/
private double roundedK(double d) throws MathArithmeticException {
final int k = (int) FastMath.ceil(n * d);
final FieldMatrix<BigFraction> HBigFraction = this.createH(d);
final int m = HBigFraction.getRowDimension();
/*
* Here the rounding part comes into play: use
* RealMatrix instead of FieldMatrix<BigFraction>
*/
final RealMatrix H = new Array2DRowRealMatrix(m, m);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
H.setEntry(i, j, HBigFraction.getEntry(i, j).doubleValue());
}
}
final RealMatrix Hpower = H.power(n);
double pFrac = Hpower.getEntry(k - 1, k - 1);
for (int i = 1; i <= n; ++i) {
pFrac *= (double) i / (double) n;
}
return pFrac;
}
示例15: normalize
import org.apache.commons.math3.exception.MathArithmeticException; //导入依赖的package包/类
/** {@inheritDoc} */
public Vector1D normalize() throws MathArithmeticException {
double s = getNorm();
if (s == 0) {
throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR);
}
return scalarMultiply(1 / s);
}