Example usage for org.apache.commons.math3.linear Array2DRowFieldMatrix Array2DRowFieldMatrix

List of usage examples for org.apache.commons.math3.linear Array2DRowFieldMatrix Array2DRowFieldMatrix

Introduction

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

Prototype

public Array2DRowFieldMatrix(final T[] v) throws NoDataException 

Source Link

Document

Create a new (column) FieldMatrix using v as the data for the unique column of the created matrix.

Usage

From source file:model.LP.java

/**
 * Initializes a linear program./*ww  w .j av  a  2 s . c om*/
 * <p>
 * n being the number of variables and m being the number of constraints,
 * this {@code constructor} does the following:
 * <p><blockquote><pre>
 *     B is set to the identity matrix of dimension m.
 *
 *     The indices of the basic and non-basic variables are set to
 *     0..n-1 and n-1..n+m-1, respectively.
 *
 *     The slack variables are called w1..wm.
 * </pre></blockquote<p>
 *
 * @param N
 *        A {@code Matrix} with the coefficients
 *        of the non-basic variables.
 * @param b
 *        A {@code Matrix} with the upper bounds on
 *        the constraints in the original program.
 * @param c
 *        A {@code Matrix} with the coefficients of the
 *        decision variables in the original program.
 * @param x
 *        A {@code HashMap} mapping the indices of the
 *        basic and non-basic variables to their names.
 */
public LP(FieldMatrix<BigFraction> N, FieldVector<BigFraction> b, FieldVector<BigFraction> c,
        HashMap<Integer, String> x) {
    this(null, N, b, c, null, N.copy(), b.copy(), c.mapMultiply(BigFraction.MINUS_ONE).copy(), x,
            new int[N.getRowDimension()], new int[N.getColumnDimension()]);

    /* Create an identity matrix of BigFraction's */
    int m = N.getRowDimension();
    BigFraction[][] Bd = new BigFraction[m][m];
    for (int i = 0; i < m; i++) {
        Arrays.fill(Bd[i], BigFraction.ZERO);
        Bd[i][i] = BigFraction.ONE;
    }
    FieldMatrix<BigFraction> B = new Array2DRowFieldMatrix<BigFraction>(Bd);

    this.B = B;
    this.B_ = B.copy();

    for (int i = 0; i < Ni.length; i++)
        Ni[i] = i;
    for (int i = 0; i < Bi.length; i++) {
        Bi[i] = i + Ni.length;
        x.put(Bi[i], "w" + (i + 1));
    }
}

From source file:controller.Parser.java

static LP parse(File f) throws FileNotFoundException {
    Scanner s = new Scanner(f);
    Pattern p = Pattern.compile(dvarreg);

    HashMap<String, Integer> x = new HashMap<String, Integer>();
    HashMap<Integer, String> xReverse = new HashMap<Integer, String>();
    int xcol = 0;

    /* Get input size and names of the decision variables. */
    int constraints = -1; // Take the objective function into account.
    while (s.hasNextLine()) {
        String line = s.nextLine();

        if (line.trim().equals(""))
            continue;

        /* /*  w ww . ja v  a  2 s  .c om*/
         * TODO: Beware, will now accept invalid
         * files with multiple objective functions.
         */
        /*            if (!validConstraint(line) && !validObj(line)) {
        String e = "Unsupported format in file " + f;
        throw new IllegalArgumentException(e);
                    } */

        Matcher m = p.matcher(line);

        while (m.find()) {
            String var = m.group(3);
            if (validVarName(var) && !x.containsKey(var)) {
                x.put(var, xcol);
                xReverse.put(xcol++, var);
            }
        }
        constraints++;
    }

    BigFraction[][] Ndata = new BigFraction[constraints][x.size()];
    for (int i = 0; i < Ndata.length; i++) {
        Arrays.fill(Ndata[i], BigFraction.ZERO);
    }
    BigFraction[] bdata = new BigFraction[constraints];
    BigFraction[] cdata = new BigFraction[x.size()];
    Arrays.fill(cdata, BigFraction.ZERO);

    s = new Scanner(f);

    String obj = s.nextLine();
    Matcher m = p.matcher(obj);

    while (m.find()) {
        String var = m.group(3);
        if (!x.containsKey(var))
            continue;

        String sign = m.group(1);
        if (sign == null)
            sign = "+";

        String coeffStr = m.group(2);
        BigFraction coeff;
        if (coeffStr == null) {
            coeff = BigFraction.ONE;
        } else {
            coeff = new BigFraction(Double.parseDouble(coeffStr));
        }
        if (sign.equals("-"))
            coeff = coeff.negate();

        cdata[x.get(var)] = coeff;
    }

    int row = 0;
    while (s.hasNextLine()) {
        String line = s.nextLine();
        String[] split = line.split("<=");
        if (line.trim().equals(""))
            continue;
        if (split.length != 2) {
            String e = "Unsupported format in file " + f;
            throw new IllegalArgumentException(e);
        }
        m = p.matcher(line);
        bdata[row] = new BigFraction(Double.parseDouble(split[1]));

        while (m.find()) {
            String var = m.group(3);
            if (!x.containsKey(var))
                continue;

            String sign = m.group(1);
            if (sign == null)
                sign = "+";

            String coeffStr = m.group(2);
            BigFraction coeff;
            if (coeffStr == null) {
                coeff = BigFraction.ONE;
            } else {
                coeff = new BigFraction(Double.parseDouble(coeffStr));
            }
            if (sign.equals("-"))
                coeff = coeff.negate();

            Ndata[row][x.get(var)] = coeff;
        }
        row++;
    }

    return new LP(new Array2DRowFieldMatrix<BigFraction>(Ndata), new ArrayFieldVector<BigFraction>(bdata),
            new ArrayFieldVector<BigFraction>(cdata), xReverse);
}

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}./*from w w w  . j  a v a  2 s. c  o  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: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.
 * // w w  w .  ja v a  2s. 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:model.LP.java

public FieldMatrix<BigFraction> getConstraints() {
    return LP.addBlock(N, new Array2DRowFieldMatrix<BigFraction>(b.toArray()), RIGHT);
}

From source file:model.LP.java

/**
 * @return/*w w  w  .j  av a  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);
}