本文整理汇总了Java中org.apache.commons.math3.util.FastMath.abs方法的典型用法代码示例。如果您正苦于以下问题:Java FastMath.abs方法的具体用法?Java FastMath.abs怎么用?Java FastMath.abs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math3.util.FastMath
的用法示例。
在下文中一共展示了FastMath.abs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateP
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
private double updateP (double duration, double[] p, double[] pDot, double[] pDotDot, double[] pDotDotDot){
double max_dotdotdot = 0.0;
for (int i = 0; i < (p.length-1); i++){
if (FastMath.abs(pDotDotDot[i]) > max_dotdotdot)
max_dotdotdot = FastMath.abs(pDotDotDot[i]);
}
double timeStep = FastMath.min(FastMath.pow((epsilon*6/max_dotdotdot), 1.0/3), FastMath.min(duration, max_step));
double timeStepSquare = timeStep*timeStep*0.5;
for (int i = 0; i < (p.length-1); i++){
double new_val = p[i] + pDot[i]*timeStep + pDotDot[i]*timeStepSquare;
double diff = FastMath.abs(new_val - p[i]);
while (new_val > 1 || new_val < 0 || diff>0.2){
timeStep *= 0.9;
timeStepSquare = timeStep*timeStep*0.5;
new_val = p[i] + pDot[i]*timeStep + pDotDot[i]*timeStepSquare;
diff = FastMath.abs(new_val - p[i]);
}
}
doUpdating(timeStep, timeStepSquare, p, pDot, pDotDot);
duration -= timeStep;
return duration;
}
示例2: nthRoot
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
public ValueType nthRoot(CalculatedValue g, int n)
{
try
{
final List<Complex> roots = g.getComplex().nthRoot(n);
for (Complex root : roots)
{
if (FastMath.abs(root.getImaginary()) < 1E-15)
{
return setValue(root.getReal());
}
}
if (!roots.isEmpty())
{
return setComplexValue(roots.get(0));
}
}
catch (Exception ex)
{
// nothing to do
}
return invalidate(ErrorType.NOT_A_NUMBER);
}
示例3: solveLowerTriangularSystem
import org.apache.commons.math3.util.FastMath; //导入方法依赖的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: getCell
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
/** Get the cell to which a point belongs.
* <p>If the returned cell is a leaf node the points belongs to the
* interior of the node, if the cell is an internal node the points
* belongs to the node cut sub-hyperplane.</p>
* @param point point to check
* @param tolerance tolerance below which points close to a cut hyperplane
* are considered to belong to the hyperplane itself
* @return the tree cell to which the point belongs
*/
public BSPTree<S> getCell(final Point<S> point, final double tolerance) {
if (cut == null) {
return this;
}
// position of the point with respect to the cut hyperplane
final double offset = cut.getHyperplane().getOffset(point);
if (FastMath.abs(offset) < tolerance) {
return this;
} else if (offset <= 0) {
// point is on the minus side of the cut hyperplane
return minus.getCell(point, tolerance);
} else {
// point is on the plus side of the cut hyperplane
return plus.getCell(point, tolerance);
}
}
示例5: updateP
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
private double updateP (double duration, double[] p, double[] pDot, double[] pDotDot, double[] pDotDotDot) throws Exception{
double max_dotdotdot = 0.0;
for (int i = 0; i < p.length; i++){
if (FastMath.abs(pDotDotDot[i]) > max_dotdotdot)
max_dotdotdot = FastMath.abs(pDotDotDot[i]);
}
double timeStep = FastMath.min(FastMath.pow((epsilon*6/max_dotdotdot), 1.0/3), FastMath.min(duration, max_step));
double timeStepSquare = timeStep*timeStep*0.5;
for (int i = 0; i < p.length; i++){
double new_val = p[i] + pDot[i]*timeStep + pDotDot[i]*timeStepSquare;
double diff = FastMath.abs(new_val - p[i]);
while (new_val > 1 || new_val < 0 || diff>0.2){
timeStep *= 0.9;
if (timeStep<1e-32){
System.out.println("values are " + p[i] + " " + pDot[i] + " " + pDotDot[i] + " " + pDotDotDot[i]);
System.out.println("index is " + i + " of " + p.length + " " + lineages*states);
System.out.println("minimum step size of 1e-32 reached");
throw new Exception("error in the calculation of transition probabilities");
}
timeStepSquare = timeStep*timeStep*0.5;
new_val = p[i] + pDot[i]*timeStep + pDotDot[i]*timeStepSquare;
diff = FastMath.abs(new_val - p[i]);
}
}
doUpdating(timeStep, timeStepSquare, p, pDot, pDotDot);
duration -= timeStep;
return duration;
}
示例6: intersection
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
/** Get the intersection point of three planes.
* @param plane1 first plane1
* @param plane2 second plane2
* @param plane3 third plane2
* @return intersection point of three planes, null if some planes are parallel
*/
public static Vector3D intersection(final Plane plane1, final Plane plane2, final Plane plane3) {
// coefficients of the three planes linear equations
final double a1 = plane1.w.getX();
final double b1 = plane1.w.getY();
final double c1 = plane1.w.getZ();
final double d1 = plane1.originOffset;
final double a2 = plane2.w.getX();
final double b2 = plane2.w.getY();
final double c2 = plane2.w.getZ();
final double d2 = plane2.originOffset;
final double a3 = plane3.w.getX();
final double b3 = plane3.w.getY();
final double c3 = plane3.w.getZ();
final double d3 = plane3.originOffset;
// direct Cramer resolution of the linear system
// (this is still feasible for a 3x3 system)
final double a23 = b2 * c3 - b3 * c2;
final double b23 = c2 * a3 - c3 * a2;
final double c23 = a2 * b3 - a3 * b2;
final double determinant = a1 * a23 + b1 * b23 + c1 * c23;
if (FastMath.abs(determinant) < 1.0e-10) {
return null;
}
final double r = 1.0 / determinant;
return new Vector3D(
(-a23 * d1 - (c1 * b3 - c3 * b1) * d2 - (c2 * b1 - c1 * b2) * d3) * r,
(-b23 * d1 - (c3 * a1 - c1 * a3) * d2 - (c1 * a2 - c2 * a1) * d3) * r,
(-c23 * d1 - (b1 * a3 - b3 * a1) * d2 - (b2 * a1 - b1 * a2) * d3) * r);
}
示例7: isNonSingular
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
/** {@inheritDoc} */
public boolean isNonSingular() {
for (double diag : rDiag) {
if (FastMath.abs(diag) <= threshold) {
return false;
}
}
return true;
}
示例8: distance1
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
/** {@inheritDoc} */
public double distance1(Vector<Euclidean2D> p) {
Vector2D p3 = (Vector2D) p;
final double dx = FastMath.abs(p3.x - x);
final double dy = FastMath.abs(p3.y - y);
return dx + dy;
}
示例9: distance1
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
/** {@inheritDoc} */
public double distance1(Vector<Euclidean3D> v) {
final Vector3D v3 = (Vector3D) v;
final double dx = FastMath.abs(v3.x - x);
final double dy = FastMath.abs(v3.y - y);
final double dz = FastMath.abs(v3.z - z);
return dx + dy + dz;
}
示例10: getNorm
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
/**
* Computes the L1 norm of the (quasi-)triangular matrix T.
*
* @return the L1 norm of matrix T
*/
private double getNorm() {
double norm = 0.0;
for (int i = 0; i < matrixT.length; i++) {
// as matrix T is (quasi-)triangular, also take the sub-diagonal element into account
for (int j = FastMath.max(i - 1, 0); j < matrixT.length; j++) {
norm += FastMath.abs(matrixT[i][j]);
}
}
return norm;
}
示例11: getFormat
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
public String getFormat() {
if (FastMath.abs(sig) < 0.001) {
return "%.3g";
} else {
return "%.2g";
}
}
示例12: findSmallSubDiagonalElement
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
/**
* Find the first small sub-diagonal element and returns its index.
*
* @param startIdx the starting index for the search
* @param norm the L1 norm of the matrix
* @return the index of the first small sub-diagonal element
*/
private int findSmallSubDiagonalElement(final int startIdx, final double norm) {
int l = startIdx;
while (l > 0) {
double s = FastMath.abs(matrixT[l - 1][l - 1]) + FastMath.abs(matrixT[l][l]);
if (s == 0.0) {
s = norm;
}
if (FastMath.abs(matrixT[l][l - 1]) < epsilon * s) {
break;
}
l--;
}
return l;
}
示例13: gcdPositive
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
/**
* Gcd between two positive numbers.
* <p>
* Gets the greatest common divisor of two numbers, using the "binary gcd" method,
* which avoids division and modulo operations. See Knuth 4.5.2 algorithm B.
* This algorithm is due to Josef Stein (1961).
* </p>
* Special cases:
* <ul>
* <li>The result of {@code gcd(x, x)}, {@code gcd(0, x)} and {@code gcd(x, 0)} is the value of {@code x}.</li>
* <li>The invocation {@code gcd(0, 0)} is the only one which returns {@code 0}.</li>
* </ul>
*
* @param a first number, must be ≥ 0
* @param b second number, must be ≥ 0
* @return gcd(a,b)
*/
static int gcdPositive(int a, int b){
// both a and b must be positive, it is not checked here
// gdc(a,0) = a
if (a == 0) {
return b;
} else if (b == 0) {
return a;
}
// make a and b odd, keep in mind the common power of twos
final int aTwos = Integer.numberOfTrailingZeros(a);
a >>= aTwos;
final int bTwos = Integer.numberOfTrailingZeros(b);
b >>= bTwos;
final int shift = FastMath.min(aTwos, bTwos);
// a and b >0
// if a > b then gdc(a,b) = gcd(a-b,b)
// if a < b then gcd(a,b) = gcd(b-a,a)
// so next a is the absolute difference and next b is the minimum of current values
while (a != b) {
final int delta = a - b;
b = FastMath.min(a, b);
a = FastMath.abs(delta);
// for speed optimization:
// remove any power of two in a as b is guaranteed to be odd throughout all iterations
a >>= Integer.numberOfTrailingZeros(a);
}
// gcd(a,a) = a, just "add" the common power of twos
return a << shift;
}
示例14: g
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
/**
* Computes the <a href="http://en.wikipedia.org/wiki/G-test">G statistic
* for Goodness of Fit</a> comparing {@code observed} and {@code expected}
* frequency counts.
*
* <p>This statistic can be used to perform a G test (Log-Likelihood Ratio
* Test) evaluating the null hypothesis that the observed counts follow the
* expected distribution.</p>
*
* <p><strong>Preconditions</strong>: <ul>
* <li>Expected counts must all be positive. </li>
* <li>Observed counts must all be ≥ 0. </li>
* <li>The observed and expected arrays must have the same length and their
* common length must be at least 2. </li></ul></p>
*
* <p>If any of the preconditions are not met, a
* {@code MathIllegalArgumentException} is thrown.</p>
*
* <p><strong>Note:</strong>This implementation rescales the
* {@code expected} array if necessary to ensure that the sum of the
* expected and observed counts are equal.</p>
*
* @param observed array of observed frequency counts
* @param expected array of expected frequency counts
* @return G-Test statistic
* @throws NotPositiveException if {@code observed} has negative entries
* @throws NotStrictlyPositiveException if {@code expected} has entries that
* are not strictly positive
* @throws DimensionMismatchException if the array lengths do not match or
* are less than 2.
*/
public double g(final double[] expected, final long[] observed)
throws NotPositiveException, NotStrictlyPositiveException,
DimensionMismatchException {
if (expected.length < 2) {
throw new DimensionMismatchException(expected.length, 2);
}
if (expected.length != observed.length) {
throw new DimensionMismatchException(expected.length, observed.length);
}
MathArrays.checkPositive(expected);
MathArrays.checkNonNegative(observed);
double sumExpected = 0d;
double sumObserved = 0d;
for (int i = 0; i < observed.length; i++) {
sumExpected += expected[i];
sumObserved += observed[i];
}
double ratio = 1d;
boolean rescale = false;
if (FastMath.abs(sumExpected - sumObserved) > 10E-6) {
ratio = sumObserved / sumExpected;
rescale = true;
}
double sum = 0d;
for (int i = 0; i < observed.length; i++) {
final double dev = rescale ?
FastMath.log((double) observed[i] / (ratio * expected[i])) :
FastMath.log((double) observed[i] / expected[i]);
sum += ((double) observed[i]) * dev;
}
return 2d * sum;
}
示例15: Cholesky
import org.apache.commons.math3.util.FastMath; //导入方法依赖的package包/类
/**
* Calculates the Cholesky decomposition of the given matrix.
* @param matrix the matrix to decompose
* @param relativeSymmetryThreshold threshold above which off-diagonal
* elements are considered too different and matrix not symmetric
* @param absolutePositivityThreshold threshold below which diagonal
* elements are considered null and matrix not positive definite
* @throws NonSquareMatrixException if the matrix is not square.
* @throws NonSymmetricMatrixException if the matrix is not symmetric.
* @throws NonPositiveDefiniteMatrixException if the matrix is not
* strictly positive definite.
* @see #Cholesky(RealMatrix)
* @see #DEFAULT_RELATIVE_SYMMETRY_THRESHOLD
* @see #DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD
*/
public Cholesky(final RealMatrix matrix,
final double relativeSymmetryThreshold,
final double absolutePositivityThreshold) {
if (!matrix.isSquare()) {
throw new NonSquareMatrixException(matrix.getRowDimension(),
matrix.getColumnDimension());
}
final int order = matrix.getRowDimension();
lTData = matrix.getData();
cachedL = null;
cachedLT = null;
// check the matrix before transformation
for (int i = 0; i < order; ++i) {
final double[] lI = lTData[i];
// check off-diagonal elements (and reset them to 0)
for (int j = i + 1; j < order; ++j) {
final double[] lJ = lTData[j];
final double lIJ = lI[j];
final double lJI = lJ[i];
final double maxDelta =
relativeSymmetryThreshold * FastMath.max(FastMath.abs(lIJ), FastMath.abs(lJI));
if (FastMath.abs(lIJ - lJI) > maxDelta) {
//double mean = .5*(lIJ+lJI);
//lTData[j][i] = lTData[i][j] = mean;
System.out.print("entries: "+lIJ+" "+lJI);
throw new NonSymmetricMatrixException(i, j, relativeSymmetryThreshold);
}
lJ[i] = 0;
}
}
// transform the matrix
for (int i = 0; i < order; ++i) {
final double[] ltI = lTData[i];
// check diagonal element
if (ltI[i] <= absolutePositivityThreshold) {
throw new NonPositiveDefiniteMatrixException(ltI[i], i, absolutePositivityThreshold);
}
ltI[i] = FastMath.sqrt(ltI[i]);
final double inverse = 1.0 / ltI[i];
for (int q = order - 1; q > i; --q) {
ltI[q] *= inverse;
final double[] ltQ = lTData[q];
for (int p = q; p < order; ++p) {
ltQ[p] -= ltI[q] * ltI[p];
}
}
}
}