Example usage for org.apache.commons.math3.fraction BigFraction compareTo

List of usage examples for org.apache.commons.math3.fraction BigFraction compareTo

Introduction

In this page you can find the example usage for org.apache.commons.math3.fraction BigFraction compareTo.

Prototype

public int compareTo(final BigFraction object) 

Source Link

Document

Compares this object to another based on size.

Usage

From source file:controller.VisLP.java

/**
 * Dictionary form assumes all x-values >= 0. For a geometric
 * representation we need these constraints explicitly stated
 * in order to draw the feasible region.
 * //from   w  ww  .  j  a v a 2  s  .c o m
 * This method adds the constraints x >= 0 (-x <= 0) 
 * and y >= 0 (-y <= 0) unless more bounding constraints
 * on the x and y-values already exists.
 * 
 * It also always adds a line with a negative slope with
 * a <<high enough>> positive x- and y-intercept needed
 * to color unbounded feasible regions.
 * 
 * @param  cons
 *         A constraints-matrix
 * @return
 *         A constraints matrix guaranteed to have lower bounds.
 */
static FieldMatrix<BigFraction> checkForBounds(FieldMatrix<BigFraction> cons) {
    boolean lowerx = false;
    boolean lowery = false;

    BigFraction valsum = BigFraction.ZERO;

    /* Does lower bounds already exist? */
    for (int i = 0; i < cons.getRowDimension(); i++) {
        BigFraction x = cons.getEntry(i, 0);
        BigFraction y = cons.getEntry(i, 1);
        if (x.compareTo(BigFraction.ZERO) < 0 && y.equals(BigFraction.ZERO)) {
            lowerx = true;
        } else if (x.equals(BigFraction.ZERO) && y.compareTo(BigFraction.ZERO) < 0) {
            lowery = true;
        }

        valsum = valsum.add(cons.getEntry(i, 2).abs());
    }

    FieldMatrix<BigFraction> ncons = cons.copy();

    BigFraction[] cxdata = new BigFraction[] { BigFraction.MINUS_ONE, BigFraction.ZERO, BigFraction.ZERO };
    BigFraction[] cydata = new BigFraction[] { BigFraction.ZERO, BigFraction.MINUS_ONE, BigFraction.ZERO };
    /* Add lower bounds if they do not exist */
    if (!lowerx) {
        FieldMatrix<BigFraction> c = new Array2DRowFieldMatrix<BigFraction>(cxdata).transpose();
        ncons = LP.addBlock(ncons, c, LP.UNDER);
    }
    if (!lowery) {
        FieldMatrix<BigFraction> c = new Array2DRowFieldMatrix<BigFraction>(cydata).transpose();
        ncons = LP.addBlock(ncons, c, LP.UNDER);
    }

    valsum = valsum.add(BigFraction.TWO).multiply(valsum);
    BigFraction[] uc = new BigFraction[] { BigFraction.ONE, BigFraction.ONE, valsum };

    FieldMatrix<BigFraction> c = new Array2DRowFieldMatrix<BigFraction>(uc).transpose();
    ncons = LP.addBlock(ncons, c, LP.UNDER);

    return ncons;
}

From source file:model.LP.java

public static BigFraction getMinValue(FieldVector<BigFraction> bf) {
    BigFraction min = bf.getEntry(0);/*ww  w  .  j a v a2s . co m*/

    for (int i = 1; i < bf.getDimension(); i++) {
        BigFraction val = bf.getEntry(i);
        if (val.compareTo(min) < 0) {
            min = val;
        }
    }
    return min;
}

From source file:info.gehrels.voting.NotMoreThanTheAllowedNumberOfCandidatesCanReachItQuorum.java

private BigFraction min(BigFraction a, BigFraction b) {
    if (a.compareTo(b) < 0) {
        return a;
    } else {/*from   www .j a  va2  s  .c o m*/
        return b;
    }
}

From source file:model.LP.java

/**
 * Find an entering variable index according
 * to the largest coefficients rule.//from   ww w . j  a  va 2  s.c o m
 *
 * @param  dual
 *         If true, find an entering variable index for the dual dictionary.
 *         Otherwise, find one for the primal dictionary.
 * @return
 *         An entering variable index.
 */
private int entering(boolean dual) {
    String e = "Incumbent basic solution is optimal.";
    String e2 = String.format("Incumbent basic solution is %s infeasible", dual ? "dually" : "primal");

    if (optimal(dual))
        throw new RuntimeException(e);
    if (!feasible(dual))
        throw new RuntimeException(e2);

    FieldVector<BigFraction> check = dual ? b_ : c_;

    BigFraction min = BigFraction.ZERO;
    int index = -1;

    for (int i = 0; i < check.getDimension(); i++) {
        BigFraction val = check.getEntry(i);
        if (val.compareTo(min) < 0) {
            min = val;
            index = i;
        }
    }
    return index;
}

From source file:model.LP.java

/**
 * Find a leaving variable index that is the most
 * bounding on the given entering variable index.
 *
 * @param  entering/*  ww  w .  j  a va 2 s  .c o m*/
 *         an entering variable index.
 * @param  dual
 *         If true, find a leaving variable index for the dual dictionary.
 *         Otherwise, find one for the primal dictionary.
 * @return
 *         A leaving variable index.
 */
private int leaving(int entering, boolean dual) {
    FieldVector<BigFraction> check;
    FieldVector<BigFraction> sd;

    FieldMatrix<BigFraction> bin = new FieldLUDecomposition<BigFraction>(B_).getSolver().getInverse()
            .multiply(N_);

    if (dual) {
        check = c_;
        FieldVector<BigFraction> unit = new ArrayFieldVector<BigFraction>(bin.getRowDimension(),
                BigFraction.ZERO);
        unit.setEntry(entering, BigFraction.ONE);
        sd = bin.transpose().scalarMultiply(BigFraction.MINUS_ONE).operate(unit);
    } else {
        check = b_;
        FieldVector<BigFraction> unit = new ArrayFieldVector<BigFraction>(bin.getColumnDimension(),
                BigFraction.ZERO);
        unit.setEntry(entering, BigFraction.ONE);
        sd = bin.operate(unit);
    }

    boolean unbounded = true;
    int index = -1;

    /* Check for unboundedness and find first non-zero element in check */
    for (int i = 0; i < sd.getDimension(); i++) {
        if (!check.getEntry(i).equals(BigFraction.ZERO) && index == -1) {
            index = i;
        }
        if (sd.getEntry(i).compareTo(BigFraction.ZERO) > 0) {
            unbounded = false;
        }
    }
    String e = "Program is unbounded.";
    if (unbounded)
        throw new RuntimeException(e);

    /* Set temporarily max value as ratio of the first divisible pair. */
    BigFraction max = sd.getEntry(index).divide(check.getEntry(index));

    for (int i = 0; i < sd.getDimension(); i++) {
        BigFraction num = sd.getEntry(i);
        BigFraction denom = check.getEntry(i);

        if (!denom.equals(BigFraction.ZERO)) {
            BigFraction val = num.divide(denom);
            if (val.compareTo(max) > 0) {
                max = val;
                index = i;
            }
        } else {
            if (num.compareTo(BigFraction.ZERO) > 0)
                return i;
        }
    }

    return index;
}

From source file:cc.redberry.core.number.Complex.java

@Override
public Complex add(BigFraction fraction) {
    return fraction.compareTo(BigFraction.ZERO) == 0 ? this : new Complex(real.add(fraction), imaginary);
}

From source file:cc.redberry.core.number.Complex.java

@Override
public Complex subtract(BigFraction fraction) {
    return fraction.compareTo(BigFraction.ZERO) == 0 ? this : new Complex(real.subtract(fraction), imaginary);
}

From source file:cc.redberry.core.number.Complex.java

@Override
public Complex multiply(BigFraction fraction) {
    return fraction.compareTo(BigFraction.ONE) == 0 ? this
            : new Complex(real.multiply(fraction), imaginary.multiply(fraction));
}

From source file:cc.redberry.core.number.Complex.java

@Override
public Complex divide(BigFraction fraction) {
    return fraction.compareTo(BigFraction.ONE) == 0 ? this
            : new Complex(real.divide(fraction), imaginary.divide(fraction));
}

From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java

/***
 * Creates {@code H} of size {@code m x m} as described in [1] (see above).
 *
 * @param d statistic//from   w ww  .  j  av a2 s.c  o m
 * @param n sample size
 * @return H matrix
 * @throws NumberIsTooLargeException if fractional part is greater than 1
 * @throws FractionConversionException 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 <= h < 1\).
 */
private FieldMatrix<BigFraction> createExactH(double d, int n)
        throws NumberIsTooLargeException, FractionConversionException {

    final int k = (int) Math.ceil(n * d);
    final int m = 2 * k - 1;
    final double hDouble = k - n * d;
    if (hDouble >= 1) {
        throw new NumberIsTooLargeException(hDouble, 1.0, false);
    }
    BigFraction h = null;
    try {
        h = new BigFraction(hDouble, 1.0e-20, 10000);
    } catch (final FractionConversionException e1) {
        try {
            h = new BigFraction(hDouble, 1.0e-10, 10000);
        } catch (final FractionConversionException e2) {
            h = new BigFraction(hDouble, 1.0e-5, 10000);
        }
    }
    final BigFraction[][] Hdata = new BigFraction[m][m];

    /*
     * Start by filling everything with either 0 or 1.
     */
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < m; ++j) {
            if (i - j + 1 < 0) {
                Hdata[i][j] = BigFraction.ZERO;
            } else {
                Hdata[i][j] = BigFraction.ONE;
            }
        }
    }

    /*
     * Setting up power-array to avoid calculating the same value twice: hPowers[0] = h^1 ...
     * hPowers[m-1] = h^m
     */
    final BigFraction[] hPowers = new BigFraction[m];
    hPowers[0] = h;
    for (int i = 1; i < m; ++i) {
        hPowers[i] = h.multiply(hPowers[i - 1]);
    }

    /*
     * First column and last row has special values (each other reversed).
     */
    for (int i = 0; i < m; ++i) {
        Hdata[i][0] = Hdata[i][0].subtract(hPowers[i]);
        Hdata[m - 1][i] = Hdata[m - 1][i].subtract(hPowers[m - i - 1]);
    }

    /*
     * [1] states: "For 1/2 < h < 1 the bottom left element of the matrix should be (1 - 2*h^m +
     * (2h - 1)^m )/m!" Since 0 <= h < 1, then if h > 1/2 is sufficient to check:
     */
    if (h.compareTo(BigFraction.ONE_HALF) == 1) {
        Hdata[m - 1][0] = Hdata[m - 1][0].add(h.multiply(2).subtract(1).pow(m));
    }

    /*
     * Aside from the first column and last row, the (i, j)-th element is 1/(i - j + 1)! if i -
     * j + 1 >= 0, else 0. 1's and 0's are already put, so only division with (i - j + 1)! is
     * needed in the elements that have 1's. There is no need to calculate (i - j + 1)! and then
     * divide - small steps avoid overflows. Note that i - j + 1 > 0 <=> i + 1 > j instead of
     * j'ing all the way to m. Also note that it is started at g = 2 because dividing by 1 isn't
     * really necessary.
     */
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < i + 1; ++j) {
            if (i - j + 1 > 0) {
                for (int g = 2; g <= i - j + 1; ++g) {
                    Hdata[i][j] = Hdata[i][j].divide(g);
                }
            }
        }
    }
    return new Array2DRowFieldMatrix<BigFraction>(BigFractionField.getInstance(), Hdata);
}