Skip to content

Commit

Permalink
Moved "SAFE_MIN" and "EPSILON" from "MathUtils" to "Precision."
Browse files Browse the repository at this point in the history
JIRA: MATH-689
(Committing on behalf of erans)


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1189593 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
psteitz committed Oct 27, 2011
1 parent 2f10501 commit 3a7d733
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 140 deletions.
Expand Up @@ -81,7 +81,7 @@ public abstract class UnivariateRealIntegratorImpl implements UnivariateRealInte
* achieved due to large values or short mantissa length. If this
* should be the primary criterion for convergence rather then a
* safety measure, set the absolute accuracy to a ridiculously small value,
* like {@link org.apache.commons.math.util.MathUtils#SAFE_MIN MathUtils.SAFE_MIN}.</li>
* like {@link org.apache.commons.math.util.Precision#SAFE_MIN Precision.SAFE_MIN}.</li>
* <li>absolute accuracy:
* The default is usually chosen so that results in the interval
* -10..-0.1 and +0.1..+10 can be found with a reasonable accuracy. If the
Expand Down
Expand Up @@ -24,7 +24,7 @@
import org.apache.commons.math.geometry.euclidean.oned.Vector1D;
import org.apache.commons.math.geometry.partitioning.Embedding;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.Precision;

/** The class represent lines in a three dimensional space.
Expand Down Expand Up @@ -172,7 +172,7 @@ public double distance(final Line line) {

final Vector3D normal = Vector3D.crossProduct(direction, line.direction);
final double n = normal.getNorm();
if (n < MathUtils.SAFE_MIN) {
if (n < Precision.SAFE_MIN) {
// lines are parallel
return distance(line.zero);
}
Expand All @@ -192,7 +192,7 @@ public Vector3D closestPoint(final Line line) {

final double cos = direction.dotProduct(line.direction);
final double n = 1 - cos * cos;
if (n < MathUtils.EPSILON) {
if (n < Precision.EPSILON) {
// the lines are parallel
return zero;
}
Expand Down
Expand Up @@ -20,7 +20,7 @@
import org.apache.commons.math.exception.MaxCountExceededException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.Precision;
import org.apache.commons.math.util.FastMath;

/**
Expand All @@ -43,7 +43,7 @@
* </ul>
* <p>
* As of 2.0, this class supports only <strong>symmetric</strong> matrices, and
* hence computes only real real Eigenvalues. This implies the D matrix returned
* hence computes only real realEigenvalues. This implies the D matrix returned
* by {@link #getD()} is always diagonal and the imaginary values returned
* {@link #getImagEigenvalue(int)} and {@link #getImagEigenvalues()} are always
* null.
Expand Down Expand Up @@ -153,7 +153,7 @@ private boolean isSymmetric(final RealMatrix matrix,
boolean raiseException) {
final int rows = matrix.getRowDimension();
final int columns = matrix.getColumnDimension();
final double eps = 10 * rows * columns * MathUtils.EPSILON;
final double eps = 10 * rows * columns * Precision.EPSILON;
for (int i = 0; i < rows; ++i) {
for (int j = i + 1; j < columns; ++j) {
final double mij = matrix.getEntry(i, j);
Expand Down Expand Up @@ -497,10 +497,10 @@ private void findEigenVectors(double[][] householderMatrix) {
// Make null any main and secondary value too small to be significant
if (maxAbsoluteValue!=0.0) {
for (int i=0; i < n; i++) {
if (FastMath.abs(realEigenvalues[i])<=MathUtils.EPSILON*maxAbsoluteValue) {
if (FastMath.abs(realEigenvalues[i])<=Precision.EPSILON*maxAbsoluteValue) {
realEigenvalues[i]=0.0;
}
if (FastMath.abs(e[i])<=MathUtils.EPSILON*maxAbsoluteValue) {
if (FastMath.abs(e[i])<=Precision.EPSILON*maxAbsoluteValue) {
e[i]=0.0;
}
}
Expand Down Expand Up @@ -606,7 +606,7 @@ private void findEigenVectors(double[][] householderMatrix) {
// Make null any eigen value too small to be significant
if (maxAbsoluteValue!=0.0) {
for (int i=0; i < n; i++) {
if (FastMath.abs(realEigenvalues[i])<MathUtils.EPSILON*maxAbsoluteValue) {
if (FastMath.abs(realEigenvalues[i])<Precision.EPSILON*maxAbsoluteValue) {
realEigenvalues[i]=0.0;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/commons/math/linear/MatrixUtils.java
Expand Up @@ -37,7 +37,7 @@
import org.apache.commons.math.fraction.BigFraction;
import org.apache.commons.math.fraction.Fraction;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.Precision;

/**
* A collection of static methods that operate on or return matrices.
Expand Down Expand Up @@ -837,7 +837,7 @@ public static void solveLowerTriangularSystem( RealMatrix rm, RealVector b){
int rows = rm.getRowDimension();
for( int i = 0 ; i < rows ; i++ ){
double diag = rm.getEntry(i, i);
if( FastMath.abs(diag) < MathUtils.SAFE_MIN ){
if( FastMath.abs(diag) < Precision.SAFE_MIN ){
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
}
double bi = b.getEntry(i)/diag;
Expand Down Expand Up @@ -877,7 +877,7 @@ public static void solveUpperTriangularSystem( RealMatrix rm, RealVector b){
int rows = rm.getRowDimension();
for( int i = rows-1 ; i >-1 ; i-- ){
double diag = rm.getEntry(i, i);
if( FastMath.abs(diag) < MathUtils.SAFE_MIN ){
if( FastMath.abs(diag) < Precision.SAFE_MIN ){
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
}
double bi = b.getEntry(i)/diag;
Expand Down
Expand Up @@ -19,7 +19,7 @@
import org.apache.commons.math.exception.NumberIsTooLargeException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.Precision;

/**
* Calculates the compact Singular Value Decomposition of a matrix.
Expand Down Expand Up @@ -465,7 +465,7 @@ public SingularValueDecomposition(final RealMatrix matrix) {

// Set the small value tolerance used to calculate rank and pseudo-inverse
tol = FastMath.max(m * singularValues[0] * EPS,
FastMath.sqrt(MathUtils.SAFE_MIN));
FastMath.sqrt(Precision.SAFE_MIN));

if (!transposed) {
cachedU = MatrixUtils.createRealMatrix(U);
Expand Down Expand Up @@ -505,7 +505,7 @@ public RealMatrix getUT() {
/**
* Returns the diagonal matrix &Sigma; of the decomposition.
* <p>&Sigma; is a diagonal matrix. The singular values are provided in
* non-increasing order, for compatibility with JAMA.</p>
* non-increasing order, for compatibility with Jama.</p>
* @return the &Sigma; matrix
*/
public RealMatrix getS() {
Expand Down
Expand Up @@ -198,7 +198,7 @@ public void setParameterizedODE(final ParameterizedODE parameterizedOde) {
* </p>
* <p>
* Given a non zero parameter value pval for the parameter, a reasonable value
* for such a step is {@code pval * FastMath.sqrt(MathUtils.EPSILON)}.
* for such a step is {@code pval * FastMath.sqrt(Precision.EPSILON)}.
* </p>
* <p>
* A zero value for such a step doesn't enable to compute the parameter Jacobian matrix.
Expand Down
Expand Up @@ -17,7 +17,7 @@

package org.apache.commons.math.optimization;

import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.Precision;

/**
* Base class for all convergence checker implementations.
Expand All @@ -32,11 +32,11 @@ public abstract class AbstractConvergenceChecker<PAIR>
/**
* Default relative threshold.
*/
private static final double DEFAULT_RELATIVE_THRESHOLD = 100 * MathUtils.EPSILON;
private static final double DEFAULT_RELATIVE_THRESHOLD = 100 * Precision.EPSILON;
/**
* Default absolute threshold.
*/
private static final double DEFAULT_ABSOLUTE_THRESHOLD = 100 * MathUtils.SAFE_MIN;
private static final double DEFAULT_ABSOLUTE_THRESHOLD = 100 * Precision.SAFE_MIN;
/**
* Relative tolerance threshold.
*/
Expand Down
Expand Up @@ -22,7 +22,7 @@
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.optimization.VectorialPointValuePair;
import org.apache.commons.math.optimization.ConvergenceChecker;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.Precision;
import org.apache.commons.math.util.FastMath;


Expand Down Expand Up @@ -144,11 +144,11 @@ public class LevenbergMarquardtOptimizer extends AbstractLeastSquaresOptimizer {
* <li>Cost relative tolerance: 1e-10</li>
* <li>Parameters relative tolerance: 1e-10</li>
* <li>Orthogonality tolerance: 1e-10</li>
* <li>QR ranking threshold: {@link MathUtils#SAFE_MIN}</li>
* <li>QR ranking threshold: {@link Precision#SAFE_MIN}</li>
* </ul>
*/
public LevenbergMarquardtOptimizer() {
this(100, 1e-10, 1e-10, 1e-10, MathUtils.SAFE_MIN);
this(100, 1e-10, 1e-10, 1e-10, Precision.SAFE_MIN);
}

/**
Expand All @@ -161,13 +161,13 @@ public LevenbergMarquardtOptimizer() {
* <li>Cost relative tolerance: 1e-10</li>
* <li>Parameters relative tolerance: 1e-10</li>
* <li>Orthogonality tolerance: 1e-10</li>
* <li>QR ranking threshold: {@link MathUtils#SAFE_MIN}</li>
* <li>QR ranking threshold: {@link Precision#SAFE_MIN}</li>
* </ul>
*
* @param checker Convergence checker.
*/
public LevenbergMarquardtOptimizer(ConvergenceChecker<VectorialPointValuePair> checker) {
this(100, checker, 1e-10, 1e-10, 1e-10, MathUtils.SAFE_MIN);
this(100, checker, 1e-10, 1e-10, 1e-10, Precision.SAFE_MIN);
}

/**
Expand Down Expand Up @@ -214,7 +214,7 @@ public LevenbergMarquardtOptimizer(double initialStepBoundFactor,
* The default values for the algorithm settings are:
* <ul>
* <li>Initial step bound factor}: 100</li>
* <li>QR ranking threshold}: {@link MathUtils#SAFE_MIN}</li>
* <li>QR ranking threshold}: {@link Precision#SAFE_MIN}</li>
* </ul>
*
* @param costRelativeTolerance Desired relative error in the sum of
Expand All @@ -229,7 +229,7 @@ public LevenbergMarquardtOptimizer(double costRelativeTolerance,
double orthoTolerance) {
this(100,
costRelativeTolerance, parRelativeTolerance, orthoTolerance,
MathUtils.SAFE_MIN);
Precision.SAFE_MIN);
}

/**
Expand Down
Expand Up @@ -141,7 +141,7 @@ public class RandomDataImpl implements RandomData, Serializable {

/**
* MathUtils provides factorials up to 20, so let's use that limit together
* with MathUtils.EPSILON to generate the following code (a priori, we know that
* with Precision.EPSILON to generate the following code (a priori, we know that
* there will be 16 elements, but instead of hardcoding that, this is
* prettier):
*/
Expand Down
Expand Up @@ -19,7 +19,7 @@
import java.util.Arrays;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.Precision;
import org.apache.commons.math.util.MathArrays;

/**
Expand Down Expand Up @@ -153,7 +153,7 @@ public MillerUpdatingRegression(int numberOfVariables, boolean includeConstant,
* @param includeConstant include a constant automatically
*/
public MillerUpdatingRegression(int numberOfVariables, boolean includeConstant) {
this(numberOfVariables, includeConstant, MathUtils.EPSILON);
this(numberOfVariables, includeConstant, Precision.EPSILON);
}

/**
Expand Down Expand Up @@ -271,7 +271,7 @@ private void include(final double[] x, final double wi, final double yi) {
if (di != 0.0) {
dpi = smartAdd(di, wxi * xi);
double tmp = wxi * xi / di;
if (FastMath.abs(tmp) > MathUtils.EPSILON) {
if (FastMath.abs(tmp) > Precision.EPSILON) {
w = (di * w) / dpi;
}
} else {
Expand Down Expand Up @@ -312,13 +312,13 @@ private double smartAdd(double a, double b) {
double _a = FastMath.abs(a);
double _b = FastMath.abs(b);
if (_a > _b) {
double eps = _a * MathUtils.EPSILON;
double eps = _a * Precision.EPSILON;
if (_b > eps) {
return a + b;
}
return a;
} else {
double eps = _b * MathUtils.EPSILON;
double eps = _b * Precision.EPSILON;
if (_a > eps) {
return a + b;
}
Expand Down
Expand Up @@ -26,7 +26,7 @@
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.Precision;

/**
* Estimates an ordinary least squares regression model
Expand Down Expand Up @@ -706,7 +706,7 @@ public RegressionResults regress() throws ModelSpecificationException{
if( n < 3 ){
throw new NoDataException( LocalizedFormats.NOT_ENOUGH_DATA_REGRESSION );
}
if( FastMath.abs( sumXX ) > MathUtils.SAFE_MIN ){
if( FastMath.abs( sumXX ) > Precision.SAFE_MIN ){
final double[] params = new double[]{ getIntercept(), getSlope() };
final double mse = getMeanSquareError();
final double _syy = sumYY + sumY * sumY / ((double) n);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/commons/math/util/FastMath.java
Expand Up @@ -101,9 +101,9 @@ public class FastMath {
* already computed ones provided as literal arrays below.
* </p>
*/
private static final boolean RECOMPUTE_TABLES_AT_RUNTIME = false;
private static /* final */ boolean RECOMPUTE_TABLES_AT_RUNTIME = false;
/** Indicator for loading big tables from "resource" files. */
private static final boolean LOAD_RESOURCES = false;
private static /* final */ boolean LOAD_RESOURCES = false;

/** log(2) (high bits). */
private static final double LN_2_A = 0.693147063255310059;
Expand Down Expand Up @@ -358,7 +358,7 @@ private FastMath() {}
* @return the high order part of the mantissa
*/
private static double doubleHighPart(double d) {
if (d > -MathUtils.SAFE_MIN && d < MathUtils.SAFE_MIN){
if (d > -Precision.SAFE_MIN && d < Precision.SAFE_MIN){
return d; // These are un-normalised - don't try to convert
}
long xl = Double.doubleToLongBits(d);
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/org/apache/commons/math/util/MathUtils.java
Expand Up @@ -32,16 +32,6 @@
* @version $Id$
*/
public final class MathUtils {

/** Smallest positive number such that 1 - EPSILON is not numerically equal to 1. */
public static final double EPSILON = 0x1.0p-53;

/** Safe minimum, such that 1 / SAFE_MIN does not overflow.
* <p>In IEEE 754 arithmetic, this is also the smallest normalized
* number 2<sup>-1022</sup>.</p>
*/
public static final double SAFE_MIN = 0x1.0p-1022;

/**
* 2 &pi;.
* @since 2.1
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/apache/commons/math/util/Precision.java
Expand Up @@ -30,9 +30,19 @@
* @version $Id$
*/
public class Precision {
/**
* Smallest positive number such that {@code 1 - EPSILON} is not
* numerically equal to 1: {@value}.
*/
public static final double EPSILON = 0x1.0p-53;
/**
* Safe minimum, such that {@code 1 / SAFE_MIN} does not overflow.
* In IEEE 754 arithmetic, this is also the smallest normalized
* number 2<sup>-1022</sup>: {@value}.
*/
public static final double SAFE_MIN = 0x1.0p-1022;
/** Offset to order signed double numbers lexicographically. */
private static final long SGN_MASK = 0x8000000000000000L;

/** Offset to order signed double numbers lexicographically. */
private static final int SGN_MASK_FLOAT = 0x80000000;

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/apache/commons/math/dfp/DfpTest.java
Expand Up @@ -18,7 +18,7 @@
package org.apache.commons.math.dfp;

import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.Precision;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -1549,10 +1549,10 @@ public void testSqrt()
@Test
public void testIssue567() {
DfpField field = new DfpField(100);
Assert.assertEquals(0.0, field.getZero().toDouble(), MathUtils.SAFE_MIN);
Assert.assertEquals(0.0, field.newDfp(0.0).toDouble(), MathUtils.SAFE_MIN);
Assert.assertEquals(-1, FastMath.copySign(1, field.newDfp(-0.0).toDouble()), MathUtils.EPSILON);
Assert.assertEquals(+1, FastMath.copySign(1, field.newDfp(+0.0).toDouble()), MathUtils.EPSILON);
Assert.assertEquals(0.0, field.getZero().toDouble(), Precision.SAFE_MIN);
Assert.assertEquals(0.0, field.newDfp(0.0).toDouble(), Precision.SAFE_MIN);
Assert.assertEquals(-1, FastMath.copySign(1, field.newDfp(-0.0).toDouble()), Precision.EPSILON);
Assert.assertEquals(+1, FastMath.copySign(1, field.newDfp(+0.0).toDouble()), Precision.EPSILON);
}

@Test
Expand Down

0 comments on commit 3a7d733

Please sign in to comment.