List of usage examples for org.apache.commons.math3.fraction BigFraction ONE
BigFraction ONE
To view the source code for org.apache.commons.math3.fraction BigFraction ONE.
Click Source Link
From source file:cc.redberry.core.number.NumberUtils.java
public static Rational createRational(BigFraction fraction) { //FUTURE investigate performance if (fraction.getNumerator().equals(BigInteger.ZERO)) return Rational.ZERO; if (BigFraction.ONE.equals(fraction)) return Rational.ONE; return new Rational(fraction); }
From source file:info.gehrels.voting.singleTransferableVote.VoteState.java
private VoteState(long ballotId, Vote<CANDIDATE_TYPE> vote) { this(ballotId, vote, BigFraction.ONE, 0); }
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 w w.ja va2 s . com * 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
/** * Initializes a linear program.//w w w. jav a 2 s . co m * <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:info.gehrels.voting.singleTransferableVote.WeightedInclusiveGregoryMethodTest.java
@Test public void doesNotReduceVoteWeightOfThoseVotesThatDidNotHaveTheElectedCandidateAsCurrentPreferredCandidate() { VoteWeightRecalculator<Candidate> voteWeightRecalculator = wigm.recalculatorFor(); ImmutableCollection<VoteState<Candidate>> voteStates = voteWeightRecalculator.recalculateExceededVoteWeight( CANDIDATE_A, BigFraction.ONE, VOTE_STATES_FIXTURE, ALL_HOPEFUL_CANDIDATE_STATE); assertThat(voteStates,/*from www . j a v a 2 s.c o m*/ hasItems( aVoteState(Matchers.<VoteState<Candidate>>allOf(withBallotId(BALLOT_BCDA.id), withVoteWeight(BigFraction.ONE))), aVoteState(Matchers.<VoteState<Candidate>>allOf(withBallotId(BALLOT_NO_VOTES.id), withVoteWeight(BigFraction.ONE))))); }
From source file:gedi.util.math.stat.distributions.OccupancyNumberDistribution.java
private void computeRational() { int maxA = b == 0 ? k : Math.min(k, n / b); aToProb = new double[maxA + 1]; BigFraction[] aToProb = new BigFraction[maxA + 1]; BigInteger bfac = factorial(b); long start = System.currentTimeMillis(); double maxDiff = 0; aToProb[maxA] = BigFraction.ONE; for (int a = maxA - 1; a >= 0; a--) { int m = Math.min(k - a + 1, aToProb.length - a); aToProb[a] = BigFraction.ZERO;/*from w w w.ja v a2s . co m*/ for (int i = 1; i < m; i++) { BigInteger rat = binomialCoefficientLargeInteger(k - a, i).multiply(factorial(n - a * b, i * b)); if (n - a * b - i * b > 0) rat = rat.multiply(BigInteger.valueOf(k - a - i).pow(n - a * b - i * b)); if (m - i > 0) rat = rat.multiply(bfac.pow(m - i)); aToProb[a] = aToProb[a].add(new BigFraction(rat, BigInteger.ONE).multiply(aToProb[a + i])); } BigInteger rat = bfac.pow(m).multiply(BigInteger.valueOf(k - a).pow(n - a * b)); aToProb[a] = BigFraction.ONE.subtract(aToProb[a].multiply(new BigFraction(BigInteger.ONE, rat))); this.aToProb[a] = new BigFraction(binomialCoefficientLargeInteger(k, a), BigInteger.ONE) .multiply(aToProb[a].multiply(rationalv(a, b, k, n))).doubleValue(); maxDiff = max(maxDiff, abs(this.aToProb[a] - approximateProbability(a))); if (System.currentTimeMillis() - start > 500) { aToProxProb = this.aToProb = computeApproximateNormal(); return; } } // System.out.printf(Locale.US,"%d\t%d\t%d\t%d\t%.4g\t%.4f\n",b,k,n,maxDigit,maxDiff,(System.currentTimeMillis()-start)/1000.0); }
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. * /*www . j a v a2s .c om*/ * 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:gedi.util.math.stat.distributions.OccupancyNumberDistribution.java
private static final BigFraction rationalv(int a, int b, int k, int n) { BigFraction re = BigFraction.ONE; for (int i = n; i > n - a * b; i--) re = re.multiply(new BigFraction(i, 1)); BigFraction ba = new BigFraction(1, 1); for (int i = 1; i <= b; i++) ba = ba.multiply(new BigFraction(i, 1)); re = re.divide(a == 0 ? BigFraction.ONE : ba.pow(a)); BigFraction f1 = a * b == 0 ? BigFraction.ONE : new BigFraction(1, k).pow(a * b); BigFraction f2 = n - a * b == 0 ? BigFraction.ONE : new BigFraction(k - a, k).pow(n - a * b); return re.multiply(f1).multiply(f2); }
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 w ww . j a v a2 s. 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:model.LP.java
/** * Return a new linear program with a new objective function * making the program dually feasible./* www . j a v a 2 s . c o m*/ * * @return A linear program. */ public LP phaseOneObj() { FieldVector<BigFraction> nc_ = new ArrayFieldVector<BigFraction>(c_.getDimension(), BigFraction.ONE); return new LP(B, N, b, c, B_, N_, b_, nc_, x, Bi, Ni); }