Remove use of modulus to detect odd/even

Use the lowest bit to detect the sign.
This commit is contained in:
aherbert 2022-10-13 16:49:11 +01:00
parent 9f01e23622
commit 6969438fda
18 changed files with 23 additions and 20 deletions

View File

@ -248,7 +248,7 @@ public abstract class ExtendedFieldElementAbstractTest<T extends RealFieldElemen
for (double x = -0.9; x < 0.9; x += 0.05) {
for (int n = 1; n < 5; ++n) {
if (x < 0) {
if (n % 2 == 1) {
if ((n & 1) == 1) {
checkRelative(-JdkMath.pow(-x, 1.0 / n), build(x).rootN(n));
}
} else {

View File

@ -156,7 +156,7 @@ public class HermiteRuleFactory extends BaseRuleFactory<Double> {
// Note: as written, the test for oddness will work for negative
// integers too (although it is not necessary here), preventing
// a FindBugs warning.
if (numberOfPoints % 2 != 0) {
if ((numberOfPoints & 1) != 0) {
double hm = H0;
for (int j = 1; j < numberOfPoints; j += 2) {
final double jp1 = j + 1.0;

View File

@ -185,7 +185,7 @@ public class LegendreHighPrecisionRuleFactory extends BaseRuleFactory<BigDecimal
// Note: as written, the test for oddness will work for negative
// integers too (although it is not necessary here), preventing
// a FindBugs warning.
if (numberOfPoints % 2 != 0) {
if ((numberOfPoints & 1) != 0) {
BigDecimal pmc = BigDecimal.ONE;
for (int j = 1; j < numberOfPoints; j += 2) {
final BigDecimal b_j = new BigDecimal(j, mContext);

View File

@ -120,7 +120,7 @@ public class LegendreRuleFactory extends BaseRuleFactory<Double> {
// Note: as written, the test for oddness will work for negative
// integers too (although it is not necessary here), preventing
// a FindBugs warning.
if (numberOfPoints % 2 != 0) {
if ((numberOfPoints & 1) != 0) {
double pmc = 1;
for (int j = 1; j < numberOfPoints; j += 2) {
pmc = -j * pmc / (j + 1);

View File

@ -85,7 +85,7 @@ public class SymmetricGaussIntegrator extends GaussIntegrator {
s = t;
}
if (ruleLength % 2 != 0) {
if ((ruleLength & 1) != 0) {
final double w = getWeight(iMax);
final double y = w * f.value(0d) - c;

View File

@ -155,8 +155,8 @@ public class CycleCrossover<T> implements CrossoverPolicy {
idx = parent1Rep.indexOf(item);
}
// for even cycles: swap the child elements on the indices found in this cycle
if (cycle++ % 2 != 0) {
// for odd cycles: swap the child elements on the indices found in this cycle
if ((cycle++ & 1) != 0) {
for (int i : indices) {
T tmp = child1Rep.get(i);
child1Rep.set(i, child2Rep.get(i));

View File

@ -220,7 +220,7 @@ public class AdamsBashforthFieldIntegrator<T extends RealFieldElement<T>> extend
// apply Taylor formula from high order to low order,
// for the sake of numerical accuracy
T variation = getField().getZero();
int sign = predictedNordsieck.getRowDimension() % 2 == 0 ? -1 : 1;
int sign = (predictedNordsieck.getRowDimension() & 1) == 0 ? -1 : 1;
for (int k = predictedNordsieck.getRowDimension() - 1; k >= 0; --k) {
variation = variation.add(predictedNordsieck.getEntry(k, i).multiply(sign));
sign = -sign;

View File

@ -215,7 +215,7 @@ public class AdamsBashforthIntegrator extends AdamsIntegrator {
// apply Taylor formula from high order to low order,
// for the sake of numerical accuracy
double variation = 0;
int sign = predictedNordsieck.getRowDimension() % 2 == 0 ? -1 : 1;
int sign = (predictedNordsieck.getRowDimension() & 1) == 0 ? -1 : 1;
for (int k = predictedNordsieck.getRowDimension() - 1; k >= 0; --k) {
variation += sign * predictedNordsieck.getEntry(k, i);
sign = -sign;

View File

@ -309,7 +309,7 @@ public class GraggBulirschStoerIntegrator extends AdaptiveStepsizeIntegrator {
public void setOrderControl(final int maximalOrder,
final double control1, final double control2) {
if (maximalOrder <= 6 || maximalOrder % 2 != 0) {
if (maximalOrder <= 6 || (maximalOrder & 1) != 0) {
this.maxOrder = 18;
}

View File

@ -547,7 +547,7 @@ public class DerivativeStructureTest extends ExtendedFieldElementAbstractTest<De
Assert.assertTrue(correctRoot.getPartialDerivative(1) > 0);
for (int order = 2; order <= maxOrder; ++order) {
Assert.assertTrue(Double.isInfinite(correctRoot.getPartialDerivative(order)));
if ((order % 2) == 0) {
if ((order & 1) == 0) {
Assert.assertTrue(correctRoot.getPartialDerivative(order) < 0);
} else {
Assert.assertTrue(correctRoot.getPartialDerivative(order) > 0);

View File

@ -200,14 +200,14 @@ public final class SimpsonIntegratorTest {
// sum ~ h/3 * [ f(x0) + 4f(x1) + 2f(x2) + 4f(x3) + 2f(x4) ... + 4f(xn-1) + f(xn) ]
// h = (b-a)/n
// f(xi) = f(a + i*h)
assert n > 0 && n % 2 == 0 : "n must be strictly positive and even";
assert n > 0 && (n & 1) == 0 : "n must be strictly positive and even";
final double h = (b - a) / n;
double sum4 = 0;
double sum2 = 0;
for (int i = 1; i < n; i++) {
// Alternate sums that are multiplied by 4 and 2
final double fxi = f.value(a + i * h);
if (i % 2 == 0) {
if ((i & 1) == 0) {
sum2 += fxi;
} else {
sum4 += fxi;

View File

@ -79,7 +79,8 @@ public class HermiteParametricTest extends GaussianQuadratureAbstractTest {
@Override
public double getExpectedValue(final int n) {
if (n % 2 == 1) {
if ((n & 1) == 1) {
// n is odd
return 0;
}

View File

@ -77,7 +77,8 @@ public class LegendreHighPrecisionParametricTest extends GaussianQuadratureAbstr
@Override
public double getExpectedValue(final int n) {
if (n % 2 == 1) {
if ((n & 1) == 1) {
// n is odd
return 0;
}
return 2d / (n + 1);

View File

@ -77,7 +77,8 @@ public class LegendreParametricTest extends GaussianQuadratureAbstractTest {
@Override
public double getExpectedValue(final int n) {
if (n % 2 == 1) {
if ((n & 1) == 1) {
// n is odd
return 0;
}
return 2d / (n + 1);

View File

@ -260,7 +260,7 @@ public class PolynomialsUtilsTest {
+26876802183334044115405d
};
for (int i = 0; i < l40.length; ++i) {
if (i % 2 == 0) {
if ((i & 1) == 0) {
double ci = numerators[i / 2] / denominator;
Assert.assertEquals(ci, l40[i], JdkMath.abs(ci) * 1e-15);
} else {

View File

@ -250,7 +250,7 @@ public abstract class ExtendedFieldElementAbstractTest<T extends RealFieldElemen
for (double x = -0.9; x < 0.9; x += 0.05) {
for (int n = 1; n < 5; ++n) {
if (x < 0) {
if (n % 2 == 1) {
if ((n & 1) == 1) {
checkRelative(-JdkMath.pow(-x, 1.0 / n), build(x).rootN(n));
}
} else {

View File

@ -116,7 +116,7 @@ public class EventFilterTest {
double t = t0 + (t1 - t0) * rng.nextDouble();
double g = eventFilter.g(t, new double[] { JdkMath.sin(t), JdkMath.cos(t) });
int turn = (int) JdkMath.floor((t - refSwitch) / (2 * JdkMath.PI));
if (turn % 2 == 0) {
if ((turn & 1) == 0) {
Assert.assertEquals( signEven * JdkMath.sin(t), g, 1.0e-10);
} else {
Assert.assertEquals(-signEven * JdkMath.sin(t), g, 1.0e-10);

View File

@ -156,7 +156,7 @@ public enum TestFunction {
}),
// https://scholarship.rice.edu/handle/1911/16304
ROSENBROCK(dim -> {
if (dim % 2 != 0) {
if ((dim & 1) != 0) {
throw new IllegalArgumentException("Must be a multiple of 2 (was: " + dim + ")");
}
final int last = dim / 2;