package etomica.graph.model.impl;

import etomica.graph.model.Coefficient;

public class CoefficientImpl implements Coefficient {

  private int numerator;
  private int denominator;
  private int sign;

  public CoefficientImpl(int value) {

    this(value, 1, 1);
  }

  public CoefficientImpl(int numerator, int denominator) {

    this(numerator, denominator, 1);
  }

  public CoefficientImpl(int numerator, int denominator, int sgn) {

    this.numerator = numerator;
    this.denominator = denominator;
    sign = sgn;
  }

  public void add(Coefficient value) {

    if (getDenominator() == value.getDenominator()) {
      numerator = sign * numerator + value.getSign() * value.getNumerator();
    }
    else {
      numerator = sign * numerator * value.getDenominator() + value.getSign() * value.getNumerator() * denominator;
      denominator = getDenominator() * value.getDenominator();
    }
    sign = 1;
    if (numerator < 0) {
      numerator = -numerator;
      sign = -1;
    }
  }

  public Coefficient copy() {

    return new CoefficientImpl(numerator, denominator, sign);
  }

  public int getDenominator() {

    return denominator;
  }

  public int getNumerator() {

    return numerator;
  }

  public int getSign() {

    return sign;
  }

  public void inc() {

    numerator += denominator;
  }

  public void multiply(Coefficient value) {

    numerator = numerator * value.getNumerator();
    denominator = denominator * value.getDenominator();
    sign = sign * value.getSign();
  }

  public void setDenominator(int value) {

    denominator = value;
  }

  public void setNumerator(int value) {

    numerator = value;
  }

  public void switchSign() {

    sign = -sign;
  }

  @Override
  public String toString() {

    return (sign < 0 ? "-" : "") + numerator + (denominator == 1 ? "" : "/" + denominator);
  }
}

@@ -515,28 +515,42 @@
 sign = sgn;%0A
+    reduce();%0A
   %7D%0A
-
 %0A  public vo
@@ -795,32 +795,42 @@
  value.getSign()
+%0A         
  * value.getNume
@@ -1013,16 +1013,30 @@
 ;%0A    %7D%0A
+    reduce();%0A
   %7D%0A%0A  p
@@ -1538,24 +1538,1172 @@
 .getSign();%0A
+    reduce();%0A  %7D%0A%0A  protected void reduce() %7B%0Aouter: do %7B%0A      int min = numerator;%0A      int max = denominator;%0A      if (min %3E max) %7B%0A        int t = min;%0A        min = max;%0A        max = t;%0A      %7D%0A      if (min %3C 2) %7B%0A        return;%0A      %7D%0A      // find factors for min, see if each factor is a factor of max%0A      int sqrt = (int)Math.sqrt(min);%0A      for (int i = 1; i%3Csqrt+1; i++) %7B%0A        int fac = min/i;%0A        if (fac*i == min) %7B%0A          // i and fac are factors of min%0A          int fac2 = max/fac;%0A          if (fac*fac2 == max) %7B%0A            // fac is a common factor%0A            numerator /= fac;%0A            denominator /= fac;%0A            // now start over%0A            reduce();%0A            continue outer;%0A          %7D%0A          if (i%3E1) %7B%0A            fac2 = max/i;%0A            if (fac2*i == max) %7B%0A              // i is a common factor%0A              numerator /= i;%0A              denominator /= i;%0A              // now start over%0A              reduce();%0A              continue outer;%0A            %7D%0A          %7D%0A        %7D%0A      %7D%0A      // we want one pass, with the option starting over from the middle%0A    %7D while (false);%0A
   %7D%0A%0A  publi
@@ -2928,24 +2928,24 @@
 String() %7B%0A%0A
-
     return (
@@ -2976,16 +2976,24 @@
 umerator
+%0A       
  + (deno

add bits to simplify fractions like 3/6 => 1/2