List of usage examples for org.apache.commons.math3.linear FieldVector getDimension
int getDimension();
From source file:model.LP.java
public static BigFraction getMinValue(FieldVector<BigFraction> bf) { BigFraction min = bf.getEntry(0);/*from w ww .j a va 2 s .c o 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:model.LP.java
/** * Find an entering variable index according * to the largest coefficients rule./*from ww w . j a v a 2 s . co 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//from ww w . j a v a 2s . co 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:org.rhwlab.BHCnotused.GaussianGIWPrior.java
private void vectorAsString(FieldVector v, StringBuilder builder) { boolean first = true; builder.append("("); for (int i = 0; i < v.getDimension(); ++i) { if (!first) { builder.append(String.format(",%d", ((Dfp) v.getEntry(i)).intValue())); } else {//from w ww. j a v a2s. c o m builder.append(String.format("%d", ((Dfp) v.getEntry(i)).intValue())); } first = false; } builder.append(")"); }