From fee5591fdb4ff88717881d8a2709e8d925b841da Mon Sep 17 00:00:00 2001 From: Shreyas Balaji Nagaraja Date: Thu, 6 Apr 2023 22:03:04 -0300 Subject: [PATCH] Rename Variable, Decompose Conditional, Explaining variable, Pull up Variable, Push down variable: Done --- .../apache/commons/numbers/angle/Angle.java | 13 ++++-- .../commons/numbers/angle/AngleTest.java | 6 +-- .../examples/jmh/gamma/ErfPerformance.java | 46 ++++++++----------- .../apache/commons/numbers/primes/Primes.java | 28 +++++++---- 4 files changed, 48 insertions(+), 45 deletions(-) diff --git a/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/Angle.java b/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/Angle.java index a61e43a3d..926fbfb6a 100644 --- a/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/Angle.java +++ b/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/Angle.java @@ -23,8 +23,6 @@ * Represents the angle concept. */ public abstract class Angle implements DoubleSupplier { - /** 2π. */ - public static final double TWO_PI = 2 * Math.PI; /** π/2. */ public static final double PI_OVER_TWO = 0.5 * Math.PI; /** Turns to degrees conversion factor. */ @@ -92,6 +90,8 @@ public boolean equals(final Object other) { * Unit: turns. */ public static final class Turn extends Angle { + /** 2π. */ + public static final double TWO_PI = 2 * Math.PI; /** Zero. */ public static final Turn ZERO = Turn.of(0d); /** Normalizing operator (result will be within the {@code [0, 1[} interval). */ @@ -146,12 +146,15 @@ public static DoubleUnaryOperator normalizer(final double lo) { * Unit: radians. */ public static final class Rad extends Angle { + /** 2π. */ + public static final double ANGLE_TWO_PI = 2 * Math.PI; + /** Zero. */ public static final Rad ZERO = Rad.of(0d); /** π. */ public static final Rad PI = Rad.of(Math.PI); /** 2π. */ - public static final Rad TWO_PI = Rad.of(Angle.TWO_PI); + public static final Rad TWO_PI = Rad.of(ANGLE_TWO_PI); /** Normalizing operator (result will be within the [0, 2π[ interval). */ public static final DoubleUnaryOperator WITHIN_0_AND_2PI = normalizer(0d); /** Normalizing operator (result will be within the [-π, π[ interval). */ @@ -175,7 +178,7 @@ public static Rad of(final double angle) { /** {@inheritDoc} */ @Override public Turn toTurn() { - return Turn.of(getAsDouble() / Angle.TWO_PI); + return Turn.of(getAsDouble() / ANGLE_TWO_PI); } /** {@inheritDoc} */ @@ -198,7 +201,7 @@ public Deg toDeg() { * @return the normalization operator. */ public static DoubleUnaryOperator normalizer(final double lo) { - return new Normalizer(lo, Angle.TWO_PI); + return new Normalizer(lo, ANGLE_TWO_PI); } } diff --git a/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/AngleTest.java b/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/AngleTest.java index 2f5367f1d..f38ef3387 100644 --- a/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/AngleTest.java +++ b/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/AngleTest.java @@ -32,7 +32,7 @@ void testConstants() { Assertions.assertEquals(0d, Angle.Deg.ZERO.getAsDouble()); Assertions.assertEquals(Math.PI, Angle.Rad.PI.getAsDouble()); Assertions.assertEquals(2 * Math.PI, Angle.Rad.TWO_PI.getAsDouble()); - Assertions.assertEquals(2 * Math.PI, Angle.TWO_PI); + Assertions.assertEquals(2 * Math.PI, Angle.Turn.TWO_PI); Assertions.assertEquals(Math.PI / 2, Angle.PI_OVER_TWO); } @@ -133,11 +133,11 @@ void testNormalizeVeryCloseToBounds() { // arrange final double pi = Math.PI; - double small = Math.ulp(Angle.TWO_PI); + double small = Math.ulp(Angle.Turn.TWO_PI); double tiny = 5e-17; // pi + tiny = pi (the value is too small to add to pi) // act/assert - Assertions.assertEquals(Angle.TWO_PI - small, nPi.applyAsDouble(-small)); + Assertions.assertEquals(Angle.Turn.TWO_PI - small, nPi.applyAsDouble(-small)); Assertions.assertEquals(small, nPi.applyAsDouble(small)); Assertions.assertEquals(pi - small, nZero.applyAsDouble(-pi - small)); diff --git a/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/gamma/ErfPerformance.java b/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/gamma/ErfPerformance.java index 3df24b782..dac4f4526 100644 --- a/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/gamma/ErfPerformance.java +++ b/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/gamma/ErfPerformance.java @@ -146,6 +146,16 @@ protected double[] createNumbers(SplittableRandom rng) { * Contains an array of numbers and the method to compute the error function. */ public abstract static class FunctionData extends NumberData { + /** The type of the data. + Should be num uniform + Declared in ErfPerformance class. **/ + @Param({NUM_UNIFORM}) + private String NUM_TYPE; + /** The type of the data. + Should be num uniform + declared in ErfPerformance class. **/ + @Param({NUM_UNIFORM, NUM_INVERSE_UNIFORM}) + private String NUM_AND_INVERSE_TYPE; /** The function. */ private DoubleUnaryOperator function; @@ -208,15 +218,11 @@ public void setup() { */ @State(Scope.Benchmark) public static class ErfData extends FunctionData { - /** The type of the data. */ - @Param({NUM_UNIFORM, NUM_INVERSE_UNIFORM}) - private String type; - /** {@inheritDoc} */ @Override protected double[] createNumbers(SplittableRandom rng) { DoubleSupplier generator; - if (NUM_INVERSE_UNIFORM.equals(type)) { + if (NUM_INVERSE_UNIFORM.equals(FunctionData.NUM_AND_INVERSE_TYPE)) { // p range: [-1, 1) // The final value is generated using the inverse erf function. generator = () -> InverseErf.value(makeSignedDouble(rng)); @@ -225,7 +231,7 @@ protected double[] createNumbers(SplittableRandom rng) { // Note: Values are not distinguishable from +/-1 when |x| > 6 generator = () -> makeSignedDouble(rng) * 6; } else { - throw new IllegalStateException(UNKNOWN + type); + throw new IllegalStateException(UNKNOWN + FunctionData.NUM_AND_INVERSE_TYPE); } return DoubleStream.generate(generator).limit(getSize()).toArray(); } @@ -270,15 +276,11 @@ protected void verify() { */ @State(Scope.Benchmark) public static class ErfcData extends FunctionData { - /** The type of the data. */ - @Param({NUM_UNIFORM, NUM_INVERSE_UNIFORM}) - private String type; - /** {@inheritDoc} */ @Override protected double[] createNumbers(SplittableRandom rng) { DoubleSupplier generator; - if (NUM_INVERSE_UNIFORM.equals(type)) { + if (NUM_INVERSE_UNIFORM.equals(FunctionData.NUM_AND_INVERSE_TYPE)) { // q range: [0, 2) // The final value is generated using the inverse erfc function. generator = () -> InverseErfc.value(rng.nextDouble() * 2); @@ -288,7 +290,7 @@ protected double[] createNumbers(SplittableRandom rng) { // Shift the range [-17, 17) to [-6, 28) generator = () -> makeSignedDouble(rng) * 17 + 11; } else { - throw new IllegalStateException(UNKNOWN + type); + throw new IllegalStateException(UNKNOWN + FunctionData.NUM_AND_INVERSE_TYPE); } return DoubleStream.generate(generator).limit(getSize()).toArray(); } @@ -334,21 +336,15 @@ protected void verify() { */ @State(Scope.Benchmark) public static class InverseErfData extends FunctionData { - /** - * The type of the data. - */ - @Param({NUM_UNIFORM}) - private String type; - /** {@inheritDoc} */ @Override protected double[] createNumbers(SplittableRandom rng) { DoubleSupplier generator; - if (NUM_UNIFORM.equals(type)) { + if (NUM_UNIFORM.equals(FunctionData.NUM_TYPE)) { // range [-1, 1) generator = () -> makeSignedDouble(rng); } else { - throw new IllegalStateException(UNKNOWN + type); + throw new IllegalStateException(UNKNOWN + FunctionData.NUM_TYPE); } return DoubleStream.generate(generator).limit(getSize()).toArray(); } @@ -393,21 +389,15 @@ protected void verify() { */ @State(Scope.Benchmark) public static class InverseErfcData extends FunctionData { - /** - * The type of the data. - */ - @Param({NUM_UNIFORM}) - private String type; - /** {@inheritDoc} */ @Override protected double[] createNumbers(SplittableRandom rng) { DoubleSupplier generator; - if (NUM_UNIFORM.equals(type)) { + if (NUM_UNIFORM.equals(FunctionData.NUM_TYPE)) { // range [0, 2) generator = () -> rng.nextDouble() * 2; } else { - throw new IllegalStateException(UNKNOWN + type); + throw new IllegalStateException(UNKNOWN + FunctionData.NUM_TYPE); } return DoubleStream.generate(generator).limit(getSize()).toArray(); } diff --git a/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java b/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java index 860678d15..39a7e1841 100644 --- a/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java +++ b/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java @@ -58,20 +58,35 @@ public static boolean isPrime(int n) { } return SmallPrimes.millerRabinPrimeTest(n); } - + /** + * Return the number which is not a multiple of 3. + * + * @param n Positive number. + * @return number which is not a multiple of 3. + * @throws IllegalArgumentException if n < 0. + */ + public static int nonMultipleOf3(int n) { + final int remainder = n % 3; + if (0 == remainder) { // if n % 3 == 0 + n += 2; // n % 3 == 2 + } else if (1 == remainder) { // if n % 3 == 1 + n += 4; // n % 3 == 2 + } + return n; + } /** * Return the smallest prime greater than or equal to n. * * @param n Positive number. * @return the smallest prime greater than or equal to {@code n}. - * @throws IllegalArgumentException if n < 0. */ public static int nextPrime(int n) { if (n < 0) { throw new IllegalArgumentException(MessageFormat.format(NUMBER_TOO_SMALL, n, 0)); } if (n <= 2) { - return 2; + int firstPrime = 2; + return firstPrime; // 2 is first possible smallest prime greater than or equal to n, n here can be 0, 1 or 2 } n |= 1; // make sure n is odd @@ -81,12 +96,7 @@ public static int nextPrime(int n) { // prepare entry in the +2, +4 loop: // n should not be a multiple of 3 - final int rem = n % 3; - if (0 == rem) { // if n % 3 == 0 - n += 2; // n % 3 == 2 - } else if (1 == rem) { // if n % 3 == 1 - n += 4; // n % 3 == 2 - } + n = nonMultipleOf3(n); while (true) { // this loop skips all multiple of 3 if (isPrime(n)) { return n;