Example usage for org.apache.commons.math3.linear FieldMatrix getEntry

List of usage examples for org.apache.commons.math3.linear FieldMatrix getEntry

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear FieldMatrix getEntry.

Prototype

T getEntry(int row, int column) throws OutOfRangeException;

Source Link

Document

Returns the entry in the specified row and column.

Usage

From source file:controller.VisLP.java

private static boolean feasible(Point2D p2d, FieldMatrix<BigFraction> N, FieldVector<BigFraction> b) {
    double x = p2d.getX();
    double y = p2d.getY();

    for (int j = 0; j < N.getRowDimension(); j++) {
        float nx = N.getEntry(j, 0).floatValue();
        float ny = N.getEntry(j, 1).floatValue();
        float val = (float) (nx * x + ny * y);
        if (val > b.getEntry(j).floatValue())
            return false;
    }//from w ww.  j a va  2 s  .com

    return true;
}

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 w w .  j  a v a2  s  .  co 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:controller.VisLP.java

/**
 * Draw the linear constraints of an {@code LP} and color
 * it's feasible region in a given {@code CCSystem}.
 * /*from ww  w  .  ja va  2  s  .  c  om*/
 * @param cs
 *        a {@code CCSystem}.
 * @param lp
 *        a {@code LP}.
 */
static void drawLP(CCSystem cs, LP lp) {
    cs.clear();

    /* Don't draw the LP if it is not in two variables */
    if (lp == null || lp.getNoBasic() != 2) {
        cs.setVisibleAxes(false);
        return;
    }
    cs.setVisibleAxes(true);

    CCSLine line;
    FieldMatrix<BigFraction> cons = lp.getConstraints();
    cons = checkForBounds(cons);

    /* Draw all constraints as lines, except hidden bounded constraint */
    for (int i = 0; i < cons.getRowDimension() - 1; i++) {
        line = new CCSLine(cons.getEntry(i, 0).doubleValue(), cons.getEntry(i, 1).doubleValue(),
                cons.getEntry(i, 2).doubleValue(), Color.gray);
        cs.addLine(line);
    }

    Point2D[] fpoints = getFeasibleIntersections(cons);

    /* 
     * Move the center of the coordinate system
     * to the center of the feasible region.
     */
    if (readScope) {
        scopeArea(cs, fpoints, true);
        readScope = false;
    }
    if (feasScope && lp.feasible(false)) {
        scopeArea(cs, fpoints, false);
        feasScope = false;
    }

    /* If there is no feasible region there is no need to try to color it */
    if (fpoints.length == 0)
        return;

    /* Draw all feasible solutions as points */
    Point2D[] pconv = convex(fpoints);
    for (Point2D p2d : pconv) {
        CCSPoint ccsp = new CCSPoint(p2d.getX(), p2d.getY());
        if (!unb.contains(p2d))
            cs.addPoint(ccsp);
    }

    /* Color the region depending on whether it is unbounded or not. */
    if (unb.size() == 0) {
        cs.addPolygon(new CCSPolygon(pconv, Color.pink, true));
    } else if (unb.size() == 1) {
        GradientPaint gp = new GradientPaint(pconv[0], Color.pink, unb.get(0), cs.getBackground());
        cs.addPolygon(new CCSPolygon(pconv, gp, true));
    } else {
        Point2D p1 = unb.get(0);
        Point2D p2 = unb.get(1);
        double xavg = (p1.getX() + p2.getX()) / 2.0;
        double yavg = (p1.getY() + p2.getY()) / 2.0;
        /* 
         * Move the end point of the gradient further away from the
         * polygon edge to make the end of the gradient look less sudden.
         */
        xavg *= 0.9;
        yavg *= 0.9;

        Point2D pavg = new Point2D.Double(xavg, yavg);

        /* Fade into the background color */
        GradientPaint gp = new GradientPaint(pconv[0], Color.pink, pavg, cs.getBackground());

        cs.addPolygon(new CCSPolygon(pconv, gp, true));
    }

    /* Draw the current objective function */
    FieldVector<BigFraction> obj = lp.getObjFunction();
    line = new CCSLine(obj.getEntry(0).doubleValue(), obj.getEntry(1).doubleValue(), lp.objVal().doubleValue(),
            Color.red);
    cs.addLine(line);

    /* Draw the current basic solution as a point. */
    BigFraction[] point = lp.point();
    cs.addPoint(new CCSPoint(point[0].doubleValue(), point[1].doubleValue(), Color.red, true));
}

From source file:lirmm.inria.fr.math.TestUtils.java

/** verifies that two matrices are equal */
public static void assertEquals(FieldMatrix<? extends FieldElement<?>> expected,
        FieldMatrix<? extends FieldElement<?>> observed) {

    Assert.assertNotNull("Observed should not be null", observed);

    if (expected.getColumnDimension() != observed.getColumnDimension()
            || expected.getRowDimension() != observed.getRowDimension()) {
        StringBuilder messageBuffer = new StringBuilder();
        messageBuffer.append("Observed has incorrect dimensions.");
        messageBuffer/*ww  w .j  av  a 2  s . co m*/
                .append("\nobserved is " + observed.getRowDimension() + " x " + observed.getColumnDimension());
        messageBuffer
                .append("\nexpected " + expected.getRowDimension() + " x " + expected.getColumnDimension());
        Assert.fail(messageBuffer.toString());
    }

    for (int i = 0; i < expected.getRowDimension(); ++i) {
        for (int j = 0; j < expected.getColumnDimension(); ++j) {
            FieldElement<?> eij = expected.getEntry(i, j);
            FieldElement<?> oij = observed.getEntry(i, j);
            Assert.assertEquals(eij, oij);
        }
    }
}

From source file:model.LP.java

/**
 * Return a newly created {@code Matrix} with a new block
 * {@code Matrix} added either horizontally or vertically
 * next to the original {@code Matrix}.// w w w  .  j  av  a  2 s  .co m
 *
 * @param  B
 *         {@code Matrix}to append to the parent {@code Matrix}.
 * @param  modifier
 *         Matrix.HORIZONTAL or Matrix.VERTICAL.
 * @return
 *         The original {@code Matrix}with a new {@code Matrix} block.
 */
public static FieldMatrix<BigFraction> addBlock(FieldMatrix<BigFraction> A, FieldMatrix<BigFraction> B,
        int modifier) {
    int Am = A.getRowDimension();
    int An = A.getColumnDimension();
    int Bm = B.getRowDimension();
    int Bn = B.getColumnDimension();

    String e = String.format(
            "Illegal operation: Cannot add a matrix block" + " of size %d x %d to a matrix of size %d x %d.",
            Am, An, Bm, Bn);

    if ((modifier == RIGHT && Am != Bm || modifier == UNDER && An != Bn)) {
        throw new IllegalArgumentException(e);
    }

    int newm = Am;
    int newn = An;

    int ci = 0;
    int cj = 0;

    switch (modifier) {
    case RIGHT:
        newn += Bn;
        cj = An;
        break;
    case UNDER:
        /* Fall through */
    default:
        newm += Bm;
        ci = Am;
    }

    BigFraction cdata[][] = new BigFraction[newm][newn];

    /* Copy A's data into cdata */
    for (int i = 0; i < Am; i++) {
        for (int j = 0; j < An; j++) {
            cdata[i][j] = A.getEntry(i, j);
        }
    }

    /* Add the new block of data */
    for (int i = 0; i < Bm; i++) {
        for (int j = 0; j < Bn; j++) {
            cdata[i + ci][j + cj] = B.getEntry(i, j);
        }
    }

    return new Array2DRowFieldMatrix<BigFraction>(cdata);
}

From source file:model.LP.java

/**
 * @return//  w w  w  .j  a  va 2 s .c  o  m
 *         A {@code Matrix} of double precision numbers representing
 *         the dictionary of the current Linear Program.
 */
public FieldMatrix<BigFraction> dictionary() {
    BigFraction[][] data = new BigFraction[Bi.length + 1][Ni.length + 1];
    for (int i = 0; i < Ni.length; i++) {
        data[0][i + 1] = c_.getEntry(i).negate();
    }
    for (int i = 0; i < Bi.length; i++) {
        data[i + 1][0] = b_.getEntry(i);
    }

    data[0][0] = objVal();

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

    for (int i = 0; i < Bi.length; i++) {
        for (int j = 0; j < Ni.length; j++) {
            data[i + 1][j + 1] = values.getEntry(i, j).negate();
        }
    }

    return new Array2DRowFieldMatrix<BigFraction>(data);
}

From source file:model.LP.java

public LP reinstate() {
    FieldVector<BigFraction> nc_ = new ArrayFieldVector<BigFraction>(c_.getDimension(), BigFraction.ZERO);
    FieldMatrix<BigFraction> bin = new FieldLUDecomposition<BigFraction>(B_).getSolver().getInverse()
            .multiply(N_);/*from   www  . j a  v a2s. co  m*/

    for (int i = 0; i < Bi.length; i++) {
        int k = Bi[i];
        if (k < Ni.length) {
            for (int j = 0; j < Ni.length; j++) {
                BigFraction bf = c.getEntry(k).multiply(bin.getEntry(i, j));
                nc_.setEntry(j, nc_.getEntry(j).add(bf));
            }
        }
    }

    for (int i = 0; i < Ni.length; i++) {
        int k = Ni[i];
        if (k < Ni.length) {
            nc_.setEntry(i, nc_.getEntry(i).subtract(c.getEntry(i)));
        }
    }

    return new LP(B, N, b, c, B_, N_, b_, nc_, x, Bi, Ni);
}

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

/**
 * Calculates the exact value of {@code P(D_n < d)} using the method described in [1] (reference
 * in class javadoc above) and {@link org.apache.commons.math3.fraction.BigFraction} (see
 * above)./* w  ww.  j  a  v  a 2  s .co m*/
 *
 * @param d statistic
 * @param n sample size
 * @return the two-sided probability of \(P(D_n < d)\)
 * @throws MathArithmeticException 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 \le h < 1\).
 */
private double exactK(double d, int n) throws MathArithmeticException {

    final int k = (int) Math.ceil(n * d);

    final FieldMatrix<BigFraction> H = this.createExactH(d, n);
    final FieldMatrix<BigFraction> Hpower = H.power(n);

    BigFraction pFrac = Hpower.getEntry(k - 1, k - 1);

    for (int i = 1; i <= n; ++i) {
        pFrac = pFrac.multiply(i).divide(n);
    }

    /*
     * BigFraction.doubleValue converts numerator to double and the denominator to double and
     * divides afterwards. That gives NaN quite easy. This does not (scale is the number of
     * digits):
     */
    return pFrac.bigDecimalValue(20, BigDecimal.ROUND_HALF_UP).doubleValue();
}