Java Code Examples for org.apache.commons.math3.util.ArithmeticUtils#gcd()

The following examples show how to use org.apache.commons.math3.util.ArithmeticUtils#gcd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetricsSystemImpl.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private synchronized void configureSinks() {
  sinkConfigs = config.getInstanceConfigs(SINK_KEY);
  int confPeriod = 0;
  for (Entry<String, MetricsConfig> entry : sinkConfigs.entrySet()) {
    MetricsConfig conf = entry.getValue();
    int sinkPeriod = conf.getInt(PERIOD_KEY, PERIOD_DEFAULT);
    confPeriod = confPeriod == 0 ? sinkPeriod
                                 : ArithmeticUtils.gcd(confPeriod, sinkPeriod);
    String clsName = conf.getClassName("");
    if (clsName == null) continue;  // sink can be registered later on
    String sinkName = entry.getKey();
    try {
      MetricsSinkAdapter sa = newSink(sinkName,
          conf.getString(DESC_KEY, sinkName), conf);
      sa.start();
      sinks.put(sinkName, sa);
    }
    catch (Exception e) {
      LOG.warn("Error creating sink '"+ sinkName +"'", e);
    }
  }
  period = confPeriod > 0 ? confPeriod
                          : config.getInt(PERIOD_KEY, PERIOD_DEFAULT);
}
 
Example 2
Source File: MetricsSystemImpl.java    From big-c with Apache License 2.0 6 votes vote down vote up
private synchronized void configureSinks() {
  sinkConfigs = config.getInstanceConfigs(SINK_KEY);
  int confPeriod = 0;
  for (Entry<String, MetricsConfig> entry : sinkConfigs.entrySet()) {
    MetricsConfig conf = entry.getValue();
    int sinkPeriod = conf.getInt(PERIOD_KEY, PERIOD_DEFAULT);
    confPeriod = confPeriod == 0 ? sinkPeriod
                                 : ArithmeticUtils.gcd(confPeriod, sinkPeriod);
    String clsName = conf.getClassName("");
    if (clsName == null) continue;  // sink can be registered later on
    String sinkName = entry.getKey();
    try {
      MetricsSinkAdapter sa = newSink(sinkName,
          conf.getString(DESC_KEY, sinkName), conf);
      sa.start();
      sinks.put(sinkName, sa);
    }
    catch (Exception e) {
      LOG.warn("Error creating sink '"+ sinkName +"'", e);
    }
  }
  period = confPeriod > 0 ? confPeriod
                          : config.getInt(PERIOD_KEY, PERIOD_DEFAULT);
}
 
Example 3
Source File: Math_1_Fraction_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Multiplies the value of this fraction by another, returning the
 * result in reduced form.</p>
 *
 * @param fraction  the fraction to multiply by, must not be {@code null}
 * @return a {@code Fraction} instance with the resulting values
 * @throws NullArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction multiply(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (numerator == 0 || fraction.numerator == 0) {
        return ZERO;
    }
    // knuth 4.5.1
    // make sure we don't overflow unless the result *must* overflow.
    int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator);
    int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
    return getReducedFraction
    (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2),
            ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1));
}
 
Example 4
Source File: Math_26_Fraction_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Multiplies the value of this fraction by another, returning the
 * result in reduced form.</p>
 *
 * @param fraction  the fraction to multiply by, must not be {@code null}
 * @return a {@code Fraction} instance with the resulting values
 * @throws NullArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction multiply(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (numerator == 0 || fraction.numerator == 0) {
        return ZERO;
    }
    // knuth 4.5.1
    // make sure we don't overflow unless the result *must* overflow.
    int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator);
    int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
    return getReducedFraction
    (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2),
            ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1));
}
 
Example 5
Source File: Fraction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Multiplies the value of this fraction by another, returning the
 * result in reduced form.</p>
 *
 * @param fraction  the fraction to multiply by, must not be {@code null}
 * @return a {@code Fraction} instance with the resulting values
 * @throws NullArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction multiply(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (numerator == 0 || fraction.numerator == 0) {
        return ZERO;
    }
    // knuth 4.5.1
    // make sure we don't overflow unless the result *must* overflow.
    int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator);
    int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
    return getReducedFraction
    (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2),
            ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1));
}
 
Example 6
Source File: Math_27_Fraction_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Multiplies the value of this fraction by another, returning the
 * result in reduced form.</p>
 *
 * @param fraction  the fraction to multiply by, must not be {@code null}
 * @return a {@code Fraction} instance with the resulting values
 * @throws NullArgumentException if the fraction is {@code null}
 * @throws MathArithmeticException if the resulting numerator or denominator exceeds
 *  {@code Integer.MAX_VALUE}
 */
public Fraction multiply(Fraction fraction) {
    if (fraction == null) {
        throw new NullArgumentException(LocalizedFormats.FRACTION);
    }
    if (numerator == 0 || fraction.numerator == 0) {
        return ZERO;
    }
    // knuth 4.5.1
    // make sure we don't overflow unless the result *must* overflow.
    int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator);
    int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
    return getReducedFraction
    (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2),
            ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1));
}
 
Example 7
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 8
Source File: Math_26_Fraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 9
Source File: Math_26_Fraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} instance with the 2 parts
 * of a fraction Y/Z.</p>
 *
 * <p>Any negative signs are resolved to be on the numerator.</p>
 *
 * @param numerator  the numerator, for example the three in 'three sevenths'
 * @param denominator  the denominator, for example the seven in 'three sevenths'
 * @return a new fraction instance, with the numerator and denominator reduced
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    if (numerator==0) {
        return ZERO; // normalize zero.
    }
    // allow 2^k/-2^31 as a valid fraction (where k>0)
    if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {
        numerator/=2; denominator/=2;
    }
    if (denominator < 0) {
        if (numerator==Integer.MIN_VALUE ||
                denominator==Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 10
Source File: Math_26_Fraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 11
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 12
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 13
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 14
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 15
Source File: SlidingWindowAssigner.java    From flink with Apache License 2.0 5 votes vote down vote up
protected SlidingWindowAssigner(long size, long slide, long offset, boolean isEventTime) {
	if (size <= 0 || slide <= 0) {
		throw new IllegalArgumentException(
			"SlidingWindowAssigner parameters must satisfy slide > 0 and size > 0");
	}

	this.size = size;
	this.slide = slide;
	this.offset = offset;
	this.isEventTime = isEventTime;
	this.paneSize = ArithmeticUtils.gcd(size, slide);
	this.numPanesPerWindow = MathUtils.checkedDownCast(size / paneSize);
}
 
Example 16
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} instance with the 2 parts
 * of a fraction Y/Z.</p>
 *
 * <p>Any negative signs are resolved to be on the numerator.</p>
 *
 * @param numerator  the numerator, for example the three in 'three sevenths'
 * @param denominator  the denominator, for example the seven in 'three sevenths'
 * @return a new fraction instance, with the numerator and denominator reduced
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    if (numerator==0) {
        return ZERO; // normalize zero.
    }
    // allow 2^k/-2^31 as a valid fraction (where k>0)
    if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {
        numerator/=2; denominator/=2;
    }
    if (denominator < 0) {
        if (numerator==Integer.MIN_VALUE ||
                denominator==Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 17
Source File: Fraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} instance with the 2 parts
 * of a fraction Y/Z.</p>
 *
 * <p>Any negative signs are resolved to be on the numerator.</p>
 *
 * @param numerator  the numerator, for example the three in 'three sevenths'
 * @param denominator  the denominator, for example the seven in 'three sevenths'
 * @return a new fraction instance, with the numerator and denominator reduced
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    if (numerator==0) {
        return ZERO; // normalize zero.
    }
    // allow 2^k/-2^31 as a valid fraction (where k>0)
    if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {
        numerator/=2; denominator/=2;
    }
    if (denominator < 0) {
        if (numerator==Integer.MIN_VALUE ||
                denominator==Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 18
Source File: Math_1_Fraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Creates a {@code Fraction} instance with the 2 parts
 * of a fraction Y/Z.</p>
 *
 * <p>Any negative signs are resolved to be on the numerator.</p>
 *
 * @param numerator  the numerator, for example the three in 'three sevenths'
 * @param denominator  the denominator, for example the seven in 'three sevenths'
 * @return a new fraction instance, with the numerator and denominator reduced
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    if (numerator==0) {
        return ZERO; // normalize zero.
    }
    // allow 2^k/-2^31 as a valid fraction (where k>0)
    if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {
        numerator/=2; denominator/=2;
    }
    if (denominator < 0) {
        if (numerator==Integer.MIN_VALUE ||
                denominator==Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
Example 19
Source File: Math_1_Fraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
Example 20
Source File: PolynomialExpansionMapper.java    From Alink with Apache License 2.0 5 votes vote down vote up
/**
 * calculate the length of the expended polynomial.
 *
 * @param num the item number of the input polynomial.
 * @param degree the degree of the polynomial.
 * @return the polynomial size.
 */
@VisibleForTesting
static int getPolySize(int num, int degree) {
	if (num == 0) {
		return 1;
	}
	if (num == 1 || degree == 1) {
		return num + degree;
	}
	if (degree > num) {
		return getPolySize(degree, num);
	}
	long res = 1;
	int i = num + 1;
	int j;
	if (num + degree < CONSTANT) {
		for (j = 1; j <= degree; ++j) {
			res = res * i / j;
			++i;
		}
	} else {
		int depth;
		for (j = 1; j <= degree; ++j) {
			depth = ArithmeticUtils.gcd(i, j);
			res = ArithmeticUtils.mulAndCheck(res / (j / depth), i / depth);
			++i;
		}
	}
	if (res > Integer.MAX_VALUE) {
		throw new IllegalArgumentException("The expended polynomial size is too large.");
	}
	return (int) res;

}