本文整理汇总了Java中org.apache.commons.math3.util.ArithmeticUtils类的典型用法代码示例。如果您正苦于以下问题:Java ArithmeticUtils类的具体用法?Java ArithmeticUtils怎么用?Java ArithmeticUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ArithmeticUtils类属于org.apache.commons.math3.util包,在下文中一共展示了ArithmeticUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: configureSinks
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
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);
}
示例2: pow
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
/**
* <p>
* Returns a <code>BigFraction</code> whose value is
* <tt>(this<sup>exponent</sup>)</tt>, returning the result in reduced form.
* </p>
*
* @param exponent
* exponent to which this <code>BigFraction</code> is to be raised.
* @return <tt>this<sup>exponent</sup></tt> as a <code>BigFraction</code>.
*/
public BigFraction pow(final long exponent) {
if (exponent == 0) {
return ONE;
}
if (numerator.signum() == 0) {
return this;
}
if (exponent < 0) {
return new BigFraction(ArithmeticUtils.pow(denominator, -exponent),
ArithmeticUtils.pow(numerator, -exponent));
}
return new BigFraction(ArithmeticUtils.pow(numerator, exponent),
ArithmeticUtils.pow(denominator, exponent));
}
示例3: multiply
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
/**
* <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));
}
示例4: toBaseK
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
/**
* Converts an integer into its base-k representation.
*
* @param number the integer to convert
* @param k the base
* @param length the length of the resulting base-k representation
* @return the base-k representation of the given number
* @throws Exception
*/
private static int[] toBaseK(int number, int k, int length) throws Exception {
int value = length-1;
int[] kary = new int[length];
int i = 0;
if (number >= ArithmeticUtils.pow(k, length)) {
throw new Exception("number can not be represented in " +
"base-k with specified number of digits");
}
while (number != 0) {
if (number >= ArithmeticUtils.pow(k, value)) {
kary[i]++;
number -= ArithmeticUtils.pow(k, value);
} else {
value--;
i++;
}
}
return kary;
}
示例5: findIndex
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
/**
* Returns the index of the specified solution in this adaptive grid
* archive, or {@code -1} if the solution is not within the current lower
* and upper bounds.
*
* @param solution the specified solution
* @return the index of the specified solution in this adaptive grid
* archive, or {@code -1} if the solution is not within the current
* lower and upper bounds
*/
public int findIndex(MOSolutionBase<Type> solution) {
int index = 0;
for (int i = 0; i < num_obj; i++) {
double value = solution.getObjective(i);
if ((value < minimum[i]) || (value > maximum[i])) {
return -1;
} else {
int tempIndex = (int)(numberOfDivisions *
((value - minimum[i]) / (maximum[i] - minimum[i])));
// handle special case where value = maximum[i]
if (tempIndex == numberOfDivisions) {
tempIndex--;
}
index += tempIndex * ArithmeticUtils.pow(numberOfDivisions, i);
}
}
return index;
}
示例6: howManyPossibleConstraints
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
@Override
public long howManyPossibleConstraints() {
int realBranchingLimit =
(this.branchingLimit < this.taskCharArchive.size()
? this.branchingLimit
: this.taskCharArchive.size() - 1);
long numberOfPossibleConstraintsPerActivity = 0;
for (int i = 1; i <= realBranchingLimit; i++) {
numberOfPossibleConstraintsPerActivity +=
ArithmeticUtils
.binomialCoefficient(
this.taskCharArchive.size(), // n
i); // k
}
return
( MetaConstraintUtils.getAllDiscoverableForwardRelationConstraintTemplates().size() -1 + // out-branching
MetaConstraintUtils.getAllDiscoverableBackwardRelationConstraintTemplates().size() -1 // in branching
)
* tasksToQueryFor.size()
* numberOfPossibleConstraintsPerActivity;
}
示例7: testMany
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
@Test
public void testMany()
{
int choose = 5;
List<String> items = new ArrayList<String>();
for (int i = 0; i < 30; i++)
{
items.add(String.format("%s", i));
}
long count = 0;
for (List<String> strList : new CombinationIterator<String>(items, choose))
{
count++;
}
assertEquals(ArithmeticUtils.binomialCoefficient(items.size(), choose), count);
}
示例8: testOne
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
@Test
public void testOne()
{
int choose = 1;
List<String> items = new ArrayList<String>();
for (int i = 0; i < 30; i++)
{
items.add(String.format("%s", i));
}
CombinationIterator<String> iter = new CombinationIterator<String>(items, choose);
long count = 0;
for (List<String> strList : iter)
{
count++;
}
assertEquals(ArithmeticUtils.binomialCoefficient(items.size(), choose), count);
}
示例9: testChooseAll
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
@Test
public void testChooseAll()
{
int choose = 30;
List<String> items = new ArrayList<String>();
for (int i = 0; i < 30; i++)
{
items.add(String.format("%s", i));
}
CombinationIterator<String> iter = new CombinationIterator<String>(items, choose);
long count = 0;
for (List<String> strList : iter)
{
count++;
}
assertEquals(ArithmeticUtils.binomialCoefficient(items.size(), choose), count);
}
示例10: testMany
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
@Test
public void testMany()
{
for (int n = 1; n < 8; n++)
{
List<String> items = Collections.nCopies(n, "");
int count = 0;
PermutationIterator<String> iter = new PermutationIterator<String>(items);
while(iter.hasNext())
{
iter.next();
count++;
}
assertEquals(ArithmeticUtils.factorial(items.size()), count);
}
}
示例11: taylor
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
/** Evaluate Taylor expansion of a derivative structure.
* @param ds array holding the derivative structure
* @param dsOffset offset of the derivative structure in its array
* @param delta parameters offsets (Δx, Δy, ...)
* @return value of the Taylor expansion at x + Δx, y + Δy, ...
* @throws MathArithmeticException if factorials becomes too large
*/
public double taylor(final double[] ds, final int dsOffset, final double ... delta)
throws MathArithmeticException {
double value = 0;
for (int i = getSize() - 1; i >= 0; --i) {
final int[] orders = getPartialDerivativeOrders(i);
double term = ds[dsOffset + i];
for (int k = 0; k < orders.length; ++k) {
if (orders[k] > 0) {
try {
term *= FastMath.pow(delta[k], orders[k]) /
ArithmeticUtils.factorial(orders[k]);
} catch (NotPositiveException e) {
// this cannot happen
throw new MathInternalError(e);
}
}
}
value += term;
}
return value;
}
示例12: configureSinks
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
private synchronized void configureSinks() {
sinkConfigs = config.getInstanceConfigs(SINK_KEY);
long confPeriodMillis = 0;
for (Entry<String, MetricsConfig> entry : sinkConfigs.entrySet()) {
MetricsConfig conf = entry.getValue();
int sinkPeriod = conf.getInt(PERIOD_KEY, PERIOD_DEFAULT);
// Support configuring periodMillis for testing.
long sinkPeriodMillis =
conf.getLong(PERIOD_MILLIS_KEY, sinkPeriod * 1000);
confPeriodMillis = confPeriodMillis == 0 ? sinkPeriodMillis
: ArithmeticUtils.gcd(confPeriodMillis, sinkPeriodMillis);
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);
}
}
long periodSec = config.getInt(PERIOD_KEY, PERIOD_DEFAULT);
period = confPeriodMillis > 0 ? confPeriodMillis
: config.getLong(PERIOD_MILLIS_KEY, periodSec * 1000);
}
示例13: Fraction
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
/**
* 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;
}
示例14: getReducedFraction
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
/**
* <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);
}
示例15: AbstractAlignedProcessingTimeWindowOperator
import org.apache.commons.math3.util.ArithmeticUtils; //导入依赖的package包/类
protected AbstractAlignedProcessingTimeWindowOperator(
F function,
KeySelector<IN, KEY> keySelector,
TypeSerializer<KEY> keySerializer,
TypeSerializer<STATE> stateTypeSerializer,
long windowLength,
long windowSlide) {
super(function);
if (windowLength < MIN_SLIDE_TIME) {
throw new IllegalArgumentException("Window length must be at least " + MIN_SLIDE_TIME + " msecs");
}
if (windowSlide < MIN_SLIDE_TIME) {
throw new IllegalArgumentException("Window slide must be at least " + MIN_SLIDE_TIME + " msecs");
}
if (windowLength < windowSlide) {
throw new IllegalArgumentException("The window size must be larger than the window slide");
}
final long paneSlide = ArithmeticUtils.gcd(windowLength, windowSlide);
if (paneSlide < MIN_SLIDE_TIME) {
throw new IllegalArgumentException(String.format(
"Cannot compute window of size %d msecs sliding by %d msecs. " +
"The unit of grouping is too small: %d msecs", windowLength, windowSlide, paneSlide));
}
this.function = requireNonNull(function);
this.keySelector = requireNonNull(keySelector);
this.keySerializer = requireNonNull(keySerializer);
this.stateTypeSerializer = requireNonNull(stateTypeSerializer);
this.windowSize = windowLength;
this.windowSlide = windowSlide;
this.paneSize = paneSlide;
this.numPanesPerWindow = MathUtils.checkedDownCast(windowLength / paneSlide);
}