?? mlj.java
字號:
package shared;
import java.lang.*;
import java.util.*;
/** This class contains methods and data members that were external to classes.
*/
public class MLJ {
/** The clamping epsilon used to determine if a real value is significantly
* different from another value.
*/
static public final double realEpsilon = 2.22204e-16;
/** The clamping epsilon used to determine if a real value is significantly
* different from another value.
*/
static public final double storedRealEpsilon= 1.1920928955078e-7;
/** The clamping epsilon used to determine if a real value is significantly
* different from another value during clamping.
*/
static public final double clampingEpsilon = realEpsilon * 10;
/** If the given source value is lower than the lower bound, the source value is
* changed to the lower bound value.
* @param source The value to be clamped.
* @param lowBound The lower bound.
* @param additionalErrMsg Message to be displayed if clamping occurs.
*/
static public void clamp_above(DoubleRef source, double lowBound,
String additionalErrMsg) {
clamp_above(source, lowBound, additionalErrMsg, 1);
}
/** If the given source value is lower than the lower bound, the source value is
* changed to the lower bound value.
* @param source The value to be clamped.
* @param lowBound The lower bound.
* @param additionalErrMsg Message to be displayed if clamping occurs.
* @param precMultiplier The precision multiplier for real values that are close to the lower bound.
*/
static public void clamp_above(DoubleRef source, double lowBound, String additionalErrMsg, int precMultiplier) {
if (precMultiplier < 0) {
Error.err(additionalErrMsg + '\n'+ "clamp_above(Real): precision multiplier ("+
precMultiplier + ") must be non-negative-->fatal_error");
}
if (source.value >= lowBound) return;
if (source.value < lowBound - clampingEpsilon * precMultiplier) {
Error.err(additionalErrMsg + '\n'+ "clamp_above(Real): minimum value allowed ("+
lowBound + ") exceeds variable to be clamped ("+ source + ") by more than is allowed ("+
clampingEpsilon + ")-->fatal_error");
}
source.value = lowBound;
}
/** Compares two double values for equality.
* @param lhs The left value to be compared.
* @param rhs The right value to be compared.
* @return TRUE if approximately equal, FALSE if significantly different.
*/
static public boolean approx_equal(double lhs, double rhs) {
return approx_equal(lhs,rhs,1);
}
/** Compares two double values for equality, using the given
* precisionMultiplier to drive approximate comparisons.
* @param lhs The left value to be compared.
* @param rhs The right value to be compared.
* @param precMultiplier The precision multiplier for determining if a value is signifacntly different.
* @return TRUE if approximately equal, FALSE if significantly different.
*/
static public boolean approx_equal(double lhs, double rhs, int precMultiplier) {
if (Globals.DBG) MLJ.ASSERT(precMultiplier >= 0,"MLJ::approx_equal: precMultiplier < 0");
return (Math.abs(lhs - rhs) <= clampingEpsilon * precMultiplier * Math.max(1, Math.min(Math.abs(lhs), Math.abs(rhs))));
}
/** Compares two arrays of double values for equality.
* @param lhs The left array to be compared.
* @param rhs The right array to be compared.
* @return TRUE if approximately equal, FALSE if significantly different.
*/
public static boolean approx_equal(double[] lhs, double[] rhs) {
return approx_equal(lhs,rhs,1);
}
/** Compares two arrays of doubles for equality, using the given
* precisionMultiplier to drive approximate comparisons at each element.
* @param lhs The left array to be compared.
* @param rhs The right array to be compared.
* @param precisionMultiplier The precision multiplier for determining if a value is signifacntly different.
* @return TRUE if approximately equal, FALSE if significantly different.
*/
public static boolean approx_equal(double[] lhs, double[] rhs,
int precisionMultiplier) {
// FALSE if bounds differ
if(lhs.length != rhs.length)
return false;
for (int x = 0; x < lhs.length; x++)
if (!approx_equal(lhs[x], rhs[x], precisionMultiplier))
return false;
return true;
}
/** Compares two matrices of double values for equality.
* @param lhs The left matrix to be compared.
* @param rhs The right matrix to be compared.
* @return TRUE if approximately equal, FALSE if significantly different.
*/
static public boolean approx_equal(double[][] lhs, double[][] rhs) {
return approx_equal(lhs,rhs,1);
}
/** Compares two matrices of double values for equality, using the given
* precisionMultiplier to drive approximate comparisons at each element.
* @param lhs The left matrix to be compared.
* @param rhs The right matrix to be compared.
* @param precisionMultiplier The precision multiplier for determining if a value is signifacntly different.
* @return TRUE if approximately equal, FALSE if significantly different.
*/
static public boolean approx_equal(double[][] lhs, double[][] rhs,
int precisionMultiplier) {
// FALSE if bounds differ
if((lhs.length != rhs.length) || (lhs[0].length != rhs[0].length))
return false;
for (int x = 0; x < lhs.length; x++)
for (int y = 0; y < lhs[0].length; y++)
if (!approx_equal(lhs[x][y], rhs[x][y], precisionMultiplier))
return false;
return true;
}
/** Checks if the double values are approximately equal and displays an error
* message if they are not.
* @param lhs The left value to be compared.
* @param rhs The right value to be compared.
* @param errMsg The error message to be displayed.
*/
static public void verify_approx_equal(double lhs, double rhs, String errMsg ) {
if (!approx_equal(lhs, rhs ))
Error.err(errMsg + '\n'+ lhs + " versus "+ rhs + "-->fatal_error");
}
/** If the given source value is lower than the lower bound, the source value is
* changed to the lower bound value. If the source value is above the higher bound,
* the higher bound is substituted.
* @param source The value to be clamped.
* @param lowBound The lower bound.
* @param highBound The higher bound.
* @param additionalErrMsg An additional error message to be displayed if clamping occurs.
*/
static public void clamp_to_range(DoubleRef source, double lowBound, double highBound, String additionalErrMsg) {
clamp_to_range(source, lowBound, highBound, additionalErrMsg, 1);
}
/** If the given source value is lower than the lower bound, the source value is
* changed to the lower bound value. If the source value is above the higher bound,
* the higher bound is substituted. The precision multiplier is to determine to
* what precision checks for significant difference are conducted.
* @param source The value to be clamped.
* @param lowBound The lower bound.
* @param highBound The higher bound.
* @param additionalErrMsg An additional error message to be displayed if clamping occurs.
* @param precMultiplier The precision multiplier for real values that are close to the a bound.
*/
static public void clamp_to_range(DoubleRef source, double lowBound, double highBound, String additionalErrMsg, int precMultiplier) {
if (lowBound > highBound) {
Error.err(additionalErrMsg + '\n'+ "clamp_to_range(Real): Lower bound of allowed range ("+
lowBound + ") can not be greater than the upper bound of the range ("+
highBound + ")-->fatal_error");
}
if (precMultiplier < 0) {
Error.err(additionalErrMsg + '\n'+ "clamp_to_range(Real): precision multiplier ("+
precMultiplier + ") must be non-negative-->fatal_error");
}
clamp_above(source, lowBound, additionalErrMsg, precMultiplier);
clamp_below(source, highBound, additionalErrMsg, precMultiplier);
}
/** If the given source value is higher than the higher bound, the source value is
* changed to the higher bound value.
* @param source The value to be clamped.
* @param highBound The higher bound.
* @param additionalErrMsg An additional error message to be displayed if clamping occurs.
*/
static public void clamp_below(DoubleRef source, double highBound, String additionalErrMsg) {
clamp_below(source, highBound, additionalErrMsg, 1);
}
/** If the given source value is higher than the higher bound, the source value is
* changed to the higher bound value.
* @param source The value to be clamped.
* @param highBound The higher bound.
* @param additionalErrMsg An additional error message to be displayed if clamping occurs.
* @param precMultiplier The precision multiplier for real values that are close to the higher bound.
*/
static public void clamp_below(DoubleRef source, double highBound, String additionalErrMsg, int precMultiplier) {
if (precMultiplier < 0) {
Error.err(additionalErrMsg + '\n'+ "clamp_below(double): precision multiplier ("+
precMultiplier + ") must be non-negative-->fatal_error");
}
if (source.value <= highBound) return;
if (source.value > highBound + clampingEpsilon * precMultiplier) {
Error.err(additionalErrMsg + '\n'+ "clamp_below(double): variable to be clamped ("+
source + ") exceeds maximum value allowed ("+ highBound + ") by more than allowed ("+
clampingEpsilon + ")-->fatal_error");
}
source.value = highBound;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -