/** * Subtract an integer. * * @param val the number to be subtracted from this * @return this - val. */ public Rational subtract(int val) { Rational val2 = new Rational(val, 1); return (subtract(val2)); } /* Rational.subtract */
/** * Subtract an integer. * * @param val the number to be subtracted from this * @return this - val. */ public Rational subtract(BigInteger val) { Rational val2 = new Rational(val, BigInteger.ONE); return (subtract(val2)); } /* Rational.subtract */
/** * binomial (n choose m). * * @param n the numerator. Equals the size of the set to choose from. * @param m the denominator. Equals the number of elements to select. * @return the binomial coefficient. */ public static Rational binomial(Rational n, BigInteger m) { if (m.compareTo(BigInteger.ZERO) == 0) { return Rational.ONE; } Rational bin = n; for (BigInteger i = BigInteger.valueOf(2); i.compareTo(m) != 1; i = i.add(BigInteger.ONE)) { bin = bin.multiply(n.subtract(i.subtract(BigInteger.ONE))).divide(i); } return bin; } /* Rational.binomial */
private Rational doubleSum(int n) { Rational resul = Rational.ZERO; for (int k = 0; k <= n; k++) { Rational jsum = Rational.ZERO; BigInteger bin = BigInteger.ONE; for (int j = 0; j <= k; j++) { BigInteger jpown = BigInteger.valueOf(j).pow(n); if (j % 2 == 0) { jsum = jsum.add(bin.multiply(jpown)); } else { jsum = jsum.subtract(bin.multiply(jpown)); } /* update binomial(k,j) recursively */ bin = bin.multiply(BigInteger.valueOf(k - j)).divide(BigInteger.valueOf(j + 1)); } resul = resul.add(jsum.divide(BigInteger.valueOf(k + 1))); } return resul; } } /* Bernoulli */
/** * binomial (n choose m). * * @param n the numerator. Equals the size of the set to choose from. * @param m the denominator. Equals the number of elements to select. * @return the binomial coefficient. */ public static Rational binomial(Rational n, int m) { if (m == 0) { return Rational.ONE; } Rational bin = n; for (int i = 2; i <= m; i++) { bin = bin.multiply(n.subtract(i - 1)).divide(i); } return bin; } /* Rational.binomial */
betsum = betsum.add(b); } else { betsum = betsum.subtract(b);
/** * Subtract an integer. * * @param val the number to be subtracted from this * @return this - val. */ public Rational subtract(int val) { Rational val2 = new Rational(val, 1); return (subtract(val2)); } /* Rational.subtract */
/** * Subtract an integer. * * @param val the number to be subtracted from this * @return this - val. */ public Rational subtract(BigInteger val) { Rational val2 = new Rational(val, BigInteger.ONE); return (subtract(val2)); } /* Rational.subtract */
/** * binomial (n choose m). * * @param n the numerator. Equals the size of the set to choose from. * @param m the denominator. Equals the number of elements to select. * @return the binomial coefficient. */ public static Rational binomial(Rational n, BigInteger m) { if (m.compareTo(BigInteger.ZERO) == 0) { return Rational.ONE; } Rational bin = n; for (BigInteger i = BigInteger.valueOf(2); i.compareTo(m) != 1; i = i.add(BigInteger.ONE)) { bin = bin.multiply(n.subtract(i.subtract(BigInteger.ONE))).divide(i); } return bin; } /* Rational.binomial */
private Rational doubleSum(int n) { Rational resul = Rational.ZERO; for (int k = 0; k <= n; k++) { Rational jsum = Rational.ZERO; BigInteger bin = BigInteger.ONE; for (int j = 0; j <= k; j++) { BigInteger jpown = BigInteger.valueOf(j).pow(n); if (j % 2 == 0) { jsum = jsum.add(bin.multiply(jpown)); } else { jsum = jsum.subtract(bin.multiply(jpown)); } /* update binomial(k,j) recursively */ bin = bin.multiply(BigInteger.valueOf(k - j)).divide(BigInteger.valueOf(j + 1)); } resul = resul.add(jsum.divide(BigInteger.valueOf(k + 1))); } return resul; } } /* Bernoulli */
/** * binomial (n choose m). * * @param n the numerator. Equals the size of the set to choose from. * @param m the denominator. Equals the number of elements to select. * @return the binomial coefficient. */ public static Rational binomial(Rational n, int m) { if (m == 0) { return Rational.ONE; } Rational bin = n; for (int i = 2; i <= m; i++) { bin = bin.multiply(n.subtract(i - 1)).divide(i); } return bin; } /* Rational.binomial */
betsum = betsum.add(b); } else { betsum = betsum.subtract(b);