Exemple #1
0
 public Rational add(Rational b) {
   if (zero(num)) return b;
   else if (zero(b.num)) return this;
   Rational rv = new Rational(this);
   rv.addEq(b);
   return rv;
 }
Exemple #2
0
 public static Rational sum(Iterable<Rational> list) {
   Rational sum = ZERO;
   for (Rational rat : list) {
     sum.addEq(rat);
   }
   return sum;
 }
Exemple #3
0
 public Rational reciprocate() {
   if (zero(num)) throw new ArithmeticException("Divide by zero");
   Rational rv = new Rational(this);
   rv.flip();
   if (negative(den)) {
     rv.num = rv.num.negate();
     rv.den = rv.den.negate();
   }
   return rv;
 }
Exemple #4
0
  // TODO: put this somewhere else...
  public static void printRow(
      String name, Rational value, ColumnTextWriter colpp, boolean excludeZero) {
    if (!value.isZero() || !excludeZero) {
      colpp.writeCol(name);
      colpp.writeCol(value.toString());

      if (!BigIntegerUtils.one(value.den)) {
        colpp.writeCol(String.format("%.3f", value.doubleValue()));
      }
      colpp.endRow();
    }
  }
Exemple #5
0
  public int compareTo(Rational other) {
    if (num.equals(other.num) && den.equals(other.den)) return 0;

    // see if it is a num only compare...
    if (den.equals(other.den)) return (greater(other.num, this.num)) ? -1 : 1;

    // check signs...
    if ((zero(num) || negative(num)) && positive(other.num)) return -1;
    else if (positive(num) && (zero(other.num) || negative(other.num))) return 1;

    BigInteger c1 = new BigInteger(other.num.toString());
    BigInteger c2 = new BigInteger(other.den.toString());
    Rational c = new Rational(c1, c2);
    c = c.negate();
    c.addEq(this);
    return (c.isZero() ? 0 : (negative(c.num) ? -1 : 1));
  }
Exemple #6
0
  public static Rational valueOf(BigDecimal x) {
    BigInteger num = x.unscaledValue();
    BigInteger den = BigInteger.ONE;

    int scale = x.scale();
    while (scale > 0) {
      den = den.multiply(BigInteger.TEN);
      --scale;
    }
    while (scale < 0) {
      num = num.multiply(BigInteger.TEN);
      ++scale;
    }

    Rational rv = new Rational(num, den);
    rv.reduce();
    return rv;
  }
Exemple #7
0
  public static Rational[] probVector(int length, Random prng) {
    if (length == 0) return new Rational[] {};
    else if (length == 1) return new Rational[] {Rational.ONE};

    double dProb = prng.nextDouble();
    Rational probA = Rational.valueOf(dProb);
    Rational probB = Rational.valueOf(1 - dProb);
    if (length == 2) {
      return new Rational[] {probA, probB};
    } else {
      Rational[] a = probVector(length / 2, prng);
      Rational[] b = probVector((length + 1) / 2, prng);
      Rational[] c = new Rational[a.length + b.length];
      for (int i = 0; i < a.length; ++i) {
        c[i] = a[i].multiply(probA);
      }
      for (int i = 0; i < b.length; ++i) {
        c[a.length + i] = b[i].multiply(probB);
      }
      return c;
    }
  }
Exemple #8
0
 public Rational divide(Rational b) {
   Rational rv = new Rational(b);
   rv.flip();
   rv.mulEq(this);
   return rv;
 }
Exemple #9
0
 public Rational multiply(Rational b) {
   if (zero(num) || zero(b.num)) return ZERO;
   Rational rv = new Rational(this);
   rv.mulEq(b);
   return rv;
 }
Exemple #10
0
 public Rational subtract(long b) {
   if (b == 0) return this;
   Rational c = new Rational(-b);
   c.addEq(this);
   return c;
 }
Exemple #11
0
 public Rational subtract(Rational b) {
   if (zero(b.num)) return this;
   Rational c = b.negate();
   if (!zero(num)) c.addEq(this);
   return c;
 }
Exemple #12
0
 public Rational add(long b) {
   if (b == 0) return this;
   Rational rv = new Rational(b);
   rv.addEq(this);
   return rv;
 }