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

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

Introduction

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

Prototype

BigFraction TWO

To view the source code for org.apache.commons.math3.fraction BigFraction TWO.

Click Source Link

Document

A fraction representing "2 / 1".

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.
 * //  ww w.  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;
}