org.apache.commons.math3.util.Precision Java Examples

The following examples show how to use org.apache.commons.math3.util.Precision. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath781() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 6, 7 }, 0);

    ArrayList<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 1, 2, 1 }, Relationship.LEQ, 2));
    constraints.add(new LinearConstraint(new double[] { -1, 1, 1 }, Relationship.LEQ, -1));
    constraints.add(new LinearConstraint(new double[] { 2, -3, 1 }, Relationship.LEQ, -1));

    double epsilon = 1e-6;
    SimplexSolver solver = new SimplexSolver();
    PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                              GoalType.MAXIMIZE, new NonNegativeConstraint(false));

    Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], 0.0d, epsilon) > 0);
    Assert.assertTrue(Precision.compareTo(solution.getPoint()[1], 0.0d, epsilon) > 0);
    Assert.assertTrue(Precision.compareTo(solution.getPoint()[2], 0.0d, epsilon) < 0);
    Assert.assertEquals(2.0d, solution.getValue(), epsilon);
}
 
Example #2
Source File: EigenDecomposition.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the block diagonal matrix D of the decomposition.
 * D is a block diagonal matrix.
 * Real eigenvalues are on the diagonal while complex values are on
 * 2x2 blocks { {real +imaginary}, {-imaginary, real} }.
 *
 * @return the D matrix.
 *
 * @see #getRealEigenvalues()
 * @see #getImagEigenvalues()
 */
public RealMatrix getD() {

    if (cachedD == null) {
        // cache the matrix for subsequent calls
        cachedD = MatrixUtils.createRealDiagonalMatrix(realEigenvalues);

        for (int i = 0; i < imagEigenvalues.length; i++) {
            if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) > 0) {
                cachedD.setEntry(i, i+1, imagEigenvalues[i]);
            } else if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0) {
                cachedD.setEntry(i, i-1, imagEigenvalues[i]);
            }
        }
    }
    return cachedD;
}
 
Example #3
Source File: MatrixUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** 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
 * @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 solveUpperTriangularSystem( 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 = 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)  );
        }
    }
}
 
Example #4
Source File: NPEfix_00188_s.java    From coming with MIT License 6 votes vote down vote up
/** Compute the point of the instance closest to another line.
 * @param line line to check against the instance
 * @return point of the instance closest to another line
 */
public Vector3D closestPoint(final Line line) {

    final double cos = direction.dotProduct(line.direction);
    final double n = 1 - cos * cos;
    if (n < Precision.EPSILON) {
        // the lines are parallel
        return zero;
    }

    final Vector3D delta0 = line.zero.subtract(zero);
    final double a        = delta0.dotProduct(direction);
    final double b        = delta0.dotProduct(line.direction);

    return new Vector3D(1, zero, (a - b * cos) / n, direction);

}
 
Example #5
Source File: StatisticalSummaryValues.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true iff <code>object</code> is a
 * <code>StatisticalSummaryValues</code> instance and all statistics have
 *  the same values as this.
 *
 * @param object the object to test equality against.
 * @return true if object equals this
 */
@Override
public boolean equals(Object object) {
    if (object == this ) {
        return true;
    }
    if (object instanceof StatisticalSummaryValues == false) {
        return false;
    }
    StatisticalSummaryValues stat = (StatisticalSummaryValues) object;
    return Precision.equalsIncludingNaN(stat.getMax(),      getMax())  &&
           Precision.equalsIncludingNaN(stat.getMean(),     getMean()) &&
           Precision.equalsIncludingNaN(stat.getMin(),      getMin())  &&
           Precision.equalsIncludingNaN(stat.getN(),        getN())    &&
           Precision.equalsIncludingNaN(stat.getSum(),      getSum())  &&
           Precision.equalsIncludingNaN(stat.getVariance(), getVariance());
}
 
Example #6
Source File: StatisticalSummaryValues.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true iff <code>object</code> is a
 * <code>StatisticalSummaryValues</code> instance and all statistics have
 *  the same values as this.
 *
 * @param object the object to test equality against.
 * @return true if object equals this
 */
@Override
public boolean equals(Object object) {
    if (object == this ) {
        return true;
    }
    if (object instanceof StatisticalSummaryValues == false) {
        return false;
    }
    StatisticalSummaryValues stat = (StatisticalSummaryValues) object;
    return Precision.equalsIncludingNaN(stat.getMax(),      getMax())  &&
           Precision.equalsIncludingNaN(stat.getMean(),     getMean()) &&
           Precision.equalsIncludingNaN(stat.getMin(),      getMin())  &&
           Precision.equalsIncludingNaN(stat.getN(),        getN())    &&
           Precision.equalsIncludingNaN(stat.getSum(),      getSum())  &&
           Precision.equalsIncludingNaN(stat.getVariance(), getVariance());
}
 
Example #7
Source File: SimplexSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Solves Phase 1 of the Simplex method.
 * @param tableau simple tableau for the problem
 * @throws MaxCountExceededException if the maximal iteration count has been exceeded
 * @throws UnboundedSolutionException if the model is found not to have a bounded solution
 * @throws NoFeasibleSolutionException if there is no feasible solution
 */
protected void solvePhase1(final SimplexTableau tableau)
    throws MaxCountExceededException, UnboundedSolutionException, NoFeasibleSolutionException {

    // make sure we're in Phase 1
    if (tableau.getNumArtificialVariables() == 0) {
        return;
    }

    while (!tableau.isOptimal()) {
        doIteration(tableau);
    }

    // if W is not zero then we have no feasible solution
    if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
        throw new NoFeasibleSolutionException();
    }
}
 
Example #8
Source File: NPEfix_00192_t.java    From coming with MIT License 6 votes vote down vote up
/** Compute the shortest distance between the instance and another line.
 * @param line line to check against the instance
 * @return shortest distance between the instance and the line
 */
public double distance(final Line line) {

    final Vector3D normal = Vector3D.crossProduct(direction, line.direction);
    final double n = normal.getNorm();
    if (n < Precision.SAFE_MIN) {
        // lines are parallel
        return distance(line.zero);
    }

    // signed separation of the two parallel planes that contains the lines
    final double offset = line.zero.subtract(zero).dotProduct(normal) / n;

    return FastMath.abs(offset);

}
 
Example #9
Source File: SimplexSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Solves Phase 1 of the Simplex method.
 *
 * @param tableau Simple tableau for the problem.
 * @throws TooManyIterationsException if the allowed number of iterations has been exhausted.
 * @throws UnboundedSolutionException if the model is found not to have a bounded solution.
 * @throws NoFeasibleSolutionException if there is no feasible solution?
 */
protected void solvePhase1(final SimplexTableau tableau)
    throws TooManyIterationsException,
           UnboundedSolutionException,
           NoFeasibleSolutionException {

    // make sure we're in Phase 1
    if (tableau.getNumArtificialVariables() == 0) {
        return;
    }

    while (!tableau.isOptimal()) {
        doIteration(tableau);
    }

    // if W is not zero then we have no feasible solution
    if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
        throw new NoFeasibleSolutionException();
    }
}
 
Example #10
Source File: cardumen_two_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Solves Phase 1 of the Simplex method.
 * @param tableau simple tableau for the problem
 * @throws MaxCountExceededException if the maximal iteration count has been exceeded
 * @throws UnboundedSolutionException if the model is found not to have a bounded solution
 * @throws NoFeasibleSolutionException if there is no feasible solution
 */
protected void solvePhase1(final SimplexTableau tableau)
    throws MaxCountExceededException, UnboundedSolutionException, NoFeasibleSolutionException {

    // make sure we're in Phase 1
    if (tableau.getNumArtificialVariables() == 0) {
        return;
    }

    while (!tableau.isOptimal()) {
        doIteration(tableau);
    }

    // if W is not zero then we have no feasible solution
    if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
        throw new NoFeasibleSolutionException();
    }
}
 
Example #11
Source File: NPEfix_00188_s.java    From coming with MIT License 6 votes vote down vote up
/** Compute the shortest distance between the instance and another line.
 * @param line line to check against the instance
 * @return shortest distance between the instance and the line
 */
public double distance(final Line line) {

    final Vector3D normal = Vector3D.crossProduct(direction, line.direction);
    final double n = normal.getNorm();
    if (n < Precision.SAFE_MIN) {
        // lines are parallel
        return distance(line.zero);
    }

    // signed separation of the two parallel planes that contains the lines
    final double offset = line.zero.subtract(zero).dotProduct(normal) / n;

    return FastMath.abs(offset);

}
 
Example #12
Source File: 1_SimplexSolver.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Solves Phase 1 of the Simplex method.
 * @param tableau simple tableau for the problem
 * @throws MaxCountExceededException if the maximal iteration count has been exceeded
 * @throws UnboundedSolutionException if the model is found not to have a bounded solution
 * @throws NoFeasibleSolutionException if there is no feasible solution
 */
protected void solvePhase1(final SimplexTableau tableau)
    throws MaxCountExceededException, UnboundedSolutionException, NoFeasibleSolutionException {

    // make sure we're in Phase 1
    if (tableau.getNumArtificialVariables() == 0) {
        return;
    }

    while (!tableau.isOptimal()) {
        doIteration(tableau);
    }

    // if W is not zero then we have no feasible solution
    if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
        throw new NoFeasibleSolutionException();
    }
}
 
Example #13
Source File: Cardumen_00258_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Solves Phase 1 of the Simplex method.
 * @param tableau simple tableau for the problem
 * @throws MaxCountExceededException if the maximal iteration count has been exceeded
 * @throws UnboundedSolutionException if the model is found not to have a bounded solution
 * @throws NoFeasibleSolutionException if there is no feasible solution
 */
protected void solvePhase1(final SimplexTableau tableau)
    throws MaxCountExceededException, UnboundedSolutionException, NoFeasibleSolutionException {

    // make sure we're in Phase 1
    if (tableau.getNumArtificialVariables() == 0) {
        return;
    }

    while (!tableau.isOptimal()) {
        doIteration(tableau);
    }

    // if W is not zero then we have no feasible solution
    if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
        throw new NoFeasibleSolutionException();
    }
}
 
Example #14
Source File: NPEfix_00178_t.java    From coming with MIT License 6 votes vote down vote up
/** Compute the shortest distance between the instance and another line.
 * @param line line to check against the instance
 * @return shortest distance between the instance and the line
 */
public double distance(final Line line) {

    final Vector3D normal = Vector3D.crossProduct(direction, line.direction);
    final double n = normal.getNorm();
    if (n < Precision.SAFE_MIN) {
        // lines are parallel
        return distance(line.zero);
    }

    // signed separation of the two parallel planes that contains the lines
    final double offset = line.zero.subtract(zero).dotProduct(normal) / n;

    return FastMath.abs(offset);

}
 
Example #15
Source File: Arja_00167_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Solves Phase 1 of the Simplex method.
 * @param tableau simple tableau for the problem
 * @throws MaxCountExceededException if the maximal iteration count has been exceeded
 * @throws UnboundedSolutionException if the model is found not to have a bounded solution
 * @throws NoFeasibleSolutionException if there is no feasible solution
 */
protected void solvePhase1(final SimplexTableau tableau)
    throws MaxCountExceededException, UnboundedSolutionException, NoFeasibleSolutionException {

    // make sure we're in Phase 1
    if (tableau.getNumArtificialVariables() == 0) {
        return;
    }

    while (!tableau.isOptimal()) {
        doIteration(tableau);
    }

    // if W is not zero then we have no feasible solution
    if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
        throw new NoFeasibleSolutionException();
    }
}
 
Example #16
Source File: Math_9_Line_t.java    From coming with MIT License 6 votes vote down vote up
/** Compute the point of the instance closest to another line.
 * @param line line to check against the instance
 * @return point of the instance closest to another line
 */
public Vector3D closestPoint(final Line line) {

    final double cos = direction.dotProduct(line.direction);
    final double n = 1 - cos * cos;
    if (n < Precision.EPSILON) {
        // the lines are parallel
        return zero;
    }

    final Vector3D delta0 = line.zero.subtract(zero);
    final double a        = delta0.dotProduct(direction);
    final double b        = delta0.dotProduct(line.direction);

    return new Vector3D(1, zero, (a - b * cos) / n, direction);

}
 
Example #17
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath781() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 6, 7 }, 0);

    ArrayList<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 1, 2, 1 }, Relationship.LEQ, 2));
    constraints.add(new LinearConstraint(new double[] { -1, 1, 1 }, Relationship.LEQ, -1));
    constraints.add(new LinearConstraint(new double[] { 2, -3, 1 }, Relationship.LEQ, -1));

    double epsilon = 1e-6;
    SimplexSolver solver = new SimplexSolver();
    PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                              GoalType.MAXIMIZE, new NonNegativeConstraint(false));

    Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], 0.0d, epsilon) > 0);
    Assert.assertTrue(Precision.compareTo(solution.getPoint()[1], 0.0d, epsilon) > 0);
    Assert.assertTrue(Precision.compareTo(solution.getPoint()[2], 0.0d, epsilon) < 0);
    Assert.assertEquals(2.0d, solution.getValue(), epsilon);
}
 
Example #18
Source File: NPEfix_00185_s.java    From coming with MIT License 6 votes vote down vote up
/** Compute the shortest distance between the instance and another line.
 * @param line line to check against the instance
 * @return shortest distance between the instance and the line
 */
public double distance(final Line line) {

    final Vector3D normal = Vector3D.crossProduct(direction, line.direction);
    final double n = normal.getNorm();
    if (n < Precision.SAFE_MIN) {
        // lines are parallel
        return distance(line.zero);
    }

    // signed separation of the two parallel planes that contains the lines
    final double offset = line.zero.subtract(zero).dotProduct(normal) / n;

    return FastMath.abs(offset);

}
 
Example #19
Source File: HypergeometricDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath644() {
    int N = 14761461;  // population
    int m = 1035;      // successes in population
    int n = 1841;      // number of trials

    int k = 0;
    final HypergeometricDistribution dist = new HypergeometricDistribution(N, m, n);
    
    Assert.assertTrue(Precision.compareTo(1.0, dist.upperCumulativeProbability(k), 1) == 0);
    Assert.assertTrue(Precision.compareTo(dist.cumulativeProbability(k), 0.0, 1) > 0);
    
    // another way to calculate the upper cumulative probability
    double upper = 1.0 - dist.cumulativeProbability(k) + dist.probability(k);
    Assert.assertTrue(Precision.compareTo(1.0, upper, 1) == 0);
}
 
Example #20
Source File: Arja_0053_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Solves Phase 1 of the Simplex method.
 * @param tableau simple tableau for the problem
 * @throws MaxCountExceededException if the maximal iteration count has been exceeded
 * @throws UnboundedSolutionException if the model is found not to have a bounded solution
 * @throws NoFeasibleSolutionException if there is no feasible solution
 */
protected void solvePhase1(final SimplexTableau tableau)
    throws MaxCountExceededException, UnboundedSolutionException, NoFeasibleSolutionException {

    // make sure we're in Phase 1
    if (tableau.getNumArtificialVariables() == 0) {
        return;
    }

    while (!tableau.isOptimal()) {
        doIteration(tableau);
    }

    // if W is not zero then we have no feasible solution
    if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
        throw new NoFeasibleSolutionException();
    }
}
 
Example #21
Source File: AnalyticsUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Rounds a value. If the given parameters has skip rounding, the value is
 * rounded to {@link AnalyticsUtils#DECIMALS_NO_ROUNDING}. decimals. If the
 * given number of decimals is specified, the value is rounded to the given
 * decimals. Otherwise, default rounding is used. If 0 decimals is explicitly
 * specified, this method returns a long value. Otherwise, a double value is
 * returned.
 *
 * @param params the query parameters.
 * @param decimals the number of decimals.
 * @param value the value.
 * @return a double.
 */
public static Number getRoundedValue( DataQueryParams params, Integer decimals, Double value )
{
    if ( value == null )
    {
        return value;
    }
    else if ( params.isSkipRounding() )
    {
        return Precision.round( value, DECIMALS_NO_ROUNDING );
    }
    else if ( decimals != null && decimals > 0 )
    {
        return Precision.round( value, decimals );
    }
    else if ( decimals != null && decimals == 0 )
    {
        return Math.round( value );
    }
    else
    {
        return MathUtils.getRounded( value );
    }
}
 
Example #22
Source File: EigenDecompositionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** test a matrix already in tridiagonal form. */
@Test
public void testTridiagonal() {
    Random r = new Random(4366663527842l);
    double[] ref = new double[30];
    for (int i = 0; i < ref.length; ++i) {
        if (i < 5) {
            ref[i] = 2 * r.nextDouble() - 1;
        } else {
            ref[i] = 0.0001 * r.nextDouble() + 6;
        }
    }
    Arrays.sort(ref);
    TriDiagonalTransformer t =
        new TriDiagonalTransformer(createTestMatrix(r, ref));
    EigenDecomposition ed;
    ed = new EigenDecomposition(t.getMainDiagonalRef(),
                                    t.getSecondaryDiagonalRef(),
                                    Precision.SAFE_MIN);
    double[] eigenValues = ed.getRealEigenvalues();
    Assert.assertEquals(ref.length, eigenValues.length);
    for (int i = 0; i < ref.length; ++i) {
        Assert.assertEquals(ref[ref.length - i - 1], eigenValues[i], 2.0e-14);
    }

}
 
Example #23
Source File: Arja_0029_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Solves Phase 1 of the Simplex method.
 * @param tableau simple tableau for the problem
 * @throws MaxCountExceededException if the maximal iteration count has been exceeded
 * @throws UnboundedSolutionException if the model is found not to have a bounded solution
 * @throws NoFeasibleSolutionException if there is no feasible solution
 */
protected void solvePhase1(final SimplexTableau tableau)
    throws MaxCountExceededException, UnboundedSolutionException, NoFeasibleSolutionException {

    // make sure we're in Phase 1
    if (tableau.getNumArtificialVariables() == 0) {
        return;
    }

    while (!tableau.isOptimal()) {
        doIteration(tableau);
    }

    // if W is not zero then we have no feasible solution
    if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
        throw new NoFeasibleSolutionException();
    }
}
 
Example #24
Source File: Arja_0080_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Solves Phase 1 of the Simplex method.
 * @param tableau simple tableau for the problem
 * @throws MaxCountExceededException if the maximal iteration count has been exceeded
 * @throws UnboundedSolutionException if the model is found not to have a bounded solution
 * @throws NoFeasibleSolutionException if there is no feasible solution
 */
protected void solvePhase1(final SimplexTableau tableau)
    throws MaxCountExceededException, UnboundedSolutionException, NoFeasibleSolutionException {

    // make sure we're in Phase 1
    if (tableau.getNumArtificialVariables() == 0) {
        return;
    }

    while (!tableau.isOptimal()) {
        doIteration(tableau);
    }

    // if W is not zero then we have no feasible solution
    if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
        throw new NoFeasibleSolutionException();
    }
}
 
Example #25
Source File: EigenDecomposition.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the block diagonal matrix D of the decomposition.
 * D is a block diagonal matrix.
 * Real eigenvalues are on the diagonal while complex values are on
 * 2x2 blocks { {real +imaginary}, {-imaginary, real} }.
 *
 * @return the D matrix.
 *
 * @see #getRealEigenvalues()
 * @see #getImagEigenvalues()
 */
public RealMatrix getD() {
    if (cachedD == null) {
        // cache the matrix for subsequent calls
        cachedD = MatrixUtils.createRealDiagonalMatrix(realEigenvalues);

        for (int i = 0; i < imagEigenvalues.length; i++) {
            if (Precision.compareTo(imagEigenvalues[i], 0.0, epsilon) > 0) {
                cachedD.setEntry(i, i+1, imagEigenvalues[i]);
            } else if (Precision.compareTo(imagEigenvalues[i], 0.0, epsilon) < 0) {
                cachedD.setEntry(i, i-1, imagEigenvalues[i]);
            }
        }
    }
    return cachedD;
}
 
Example #26
Source File: EigenDecomposition.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Transforms the matrix to Schur form and calculates the eigenvalues.
 *
 * @param matrix Matrix to transform.
 * @return the {@link SchurTransformer Shur transform} for this matrix
 */
private SchurTransformer transformToSchur(final RealMatrix matrix) {
    final SchurTransformer schurTransform = new SchurTransformer(matrix);
    final double[][] matT = schurTransform.getT().getData();

    realEigenvalues = new double[matT.length];
    imagEigenvalues = new double[matT.length];

    for (int i = 0; i < realEigenvalues.length; i++) {
        if (i == (realEigenvalues.length - 1) ||
            Precision.equals(matT[i + 1][i], 0.0, EPSILON)) {
            realEigenvalues[i] = matT[i][i];
        } else {
            final double x = matT[i + 1][i + 1];
            final double p = 0.5 * (matT[i][i] - x);
            final double z = FastMath.sqrt(FastMath.abs(p * p + matT[i + 1][i] * matT[i][i + 1]));
            realEigenvalues[i] = x + p;
            imagEigenvalues[i] = z;
            realEigenvalues[i + 1] = x + p;
            imagEigenvalues[i + 1] = -z;
            i++;
        }
    }
    return schurTransform;
}
 
Example #27
Source File: Math_9_Line_t.java    From coming with MIT License 6 votes vote down vote up
/** Compute the shortest distance between the instance and another line.
 * @param line line to check against the instance
 * @return shortest distance between the instance and the line
 */
public double distance(final Line line) {

    final Vector3D normal = Vector3D.crossProduct(direction, line.direction);
    final double n = normal.getNorm();
    if (n < Precision.SAFE_MIN) {
        // lines are parallel
        return distance(line.zero);
    }

    // signed separation of the two parallel planes that contains the lines
    final double offset = line.zero.subtract(zero).dotProduct(normal) / n;

    return FastMath.abs(offset);

}
 
Example #28
Source File: Line.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Compute the shortest distance between the instance and another line.
 * @param line line to check against the instance
 * @return shortest distance between the instance and the line
 */
public double distance(final Line line) {

    final Vector3D normal = Vector3D.crossProduct(direction, line.direction);
    final double n = normal.getNorm();
    if (n < Precision.SAFE_MIN) {
        // lines are parallel
        return distance(line.zero);
    }

    // signed separation of the two parallel planes that contains the lines
    final double offset = line.zero.subtract(zero).dotProduct(normal) / n;

    return FastMath.abs(offset);

}
 
Example #29
Source File: NPEfix_00192_t.java    From coming with MIT License 6 votes vote down vote up
/** Compute the point of the instance closest to another line.
 * @param line line to check against the instance
 * @return point of the instance closest to another line
 */
public Vector3D closestPoint(final Line line) {

    final double cos = direction.dotProduct(line.direction);
    final double n = 1 - cos * cos;
    if (n < Precision.EPSILON) {
        // the lines are parallel
        return zero;
    }

    final Vector3D delta0 = line.zero.subtract(zero);
    final double a        = delta0.dotProduct(direction);
    final double b        = delta0.dotProduct(line.direction);

    return new Vector3D(1, zero, (a - b * cos) / n, direction);

}
 
Example #30
Source File: Math_9_Line_s.java    From coming with MIT License 6 votes vote down vote up
/** Compute the shortest distance between the instance and another line.
 * @param line line to check against the instance
 * @return shortest distance between the instance and the line
 */
public double distance(final Line line) {

    final Vector3D normal = Vector3D.crossProduct(direction, line.direction);
    final double n = normal.getNorm();
    if (n < Precision.SAFE_MIN) {
        // lines are parallel
        return distance(line.zero);
    }

    // signed separation of the two parallel planes that contains the lines
    final double offset = line.zero.subtract(zero).dotProduct(normal) / n;

    return FastMath.abs(offset);

}