[
https://issues.apache.org/jira/browse/NUMBERS-188?focusedWorklogId=791548&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-791548
]
ASF GitHub Bot logged work on NUMBERS-188:
------------------------------------------
Author: ASF GitHub Bot
Created on: 15/Jul/22 18:35
Start Date: 15/Jul/22 18:35
Worklog Time Spent: 10m
Work Description: sumanth-rajkumar commented on code in PR #113:
URL: https://github.com/apache/commons-numbers/pull/113#discussion_r922429090
##########
commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/TestUtils.java:
##########
@@ -387,4 +424,314 @@ private static String preprocessTestData(String line,
TestDataFlagOption option,
}
return line;
}
+
+ /**
+ * Assert the complex with a scalar operation on the complex number is
equal to the expected value.
+ * No deltas for real or imaginary.
+ * If the imaginary part is not NaN the operation must also satisfy the
conjugate equality.
+ *
+ * <pre>
+ * op(conj(z)) = conj(op(z))
+ * </pre>
+ *
+ * <p>The results must be binary equal. This includes the sign of zero.
+ * @param c the complex
+ * @param operand the scalar
+ * @param operation the operation
+ * @param expected the expected
+ * @param actual the actual
+ * @param name the operation name
+ */
+ static void assertComplexScalar(Complex c, double operand,
ComplexScalarFunction<ComplexDouble> operation,
+ Complex expected, Complex actual, String
name) {
+ assertComplexScalar(c, operand, operation, expected, actual, name,
0.0D, 0.0D);
+ }
+
+ /**
+ * Assert the complex with a scalar operation on the complex number is
equal to the expected value.
+ * If the imaginary part is not NaN the operation must also satisfy the
conjugate equality.
+ *
+ * <pre>
+ * op(conj(z)) = conj(op(z))
+ * </pre>
+ *
+ * <p>The results must be binary equal. This includes the sign of zero.
+ * @param c the complex
+ * @param operand the scalar
+ * @param operation the operation
+ * @param expected the expected
+ * @param actual the actual
+ * @param name the operation name
+ * @param deltaReal real delta
+ * @param deltaImaginary imaginary delta
+ */
+ static void assertComplexScalar(Complex c, double operand,
ComplexScalarFunction<ComplexDouble> operation,
+ Complex expected, Complex actual, String
name, double deltaReal, double deltaImaginary) {
+
+ final ComplexDouble result = operation.apply(c, operand,
TestUtils.ComplexDoubleConstructor.of());
+
+ assertEquals(() -> c + "." + name + "(): real", expected.real(),
actual.real(), deltaReal);
+ assertEquals(() -> c + "." + name + "(): imaginary", expected.imag(),
actual.imag(), deltaImaginary);
+
+ assertEquals(() -> "ComplexFunctions." + name + "(" + c + "): real",
expected.real(), result.getReal(), deltaReal);
+ assertEquals(() -> "ComplexFunctions." + name + "(" + c + "):
imaginary", expected.imag(), result.getImaginary(), deltaImaginary);
+ }
+
+ /**
+ * Assert the double operation on the complex number is equal to the
expected value.
+ * No delta.
+ * If the imaginary part is not NaN the operation must also satisfy the
conjugate equality.
+ *
+ * <pre>
+ * op(conj(z)) = conj(op(z))
+ * </pre>
+ *
+ * <p>The results must be binary equal. This includes the sign of zero.
+ * @param c the complex
+ * @param operation the operation
+ * @param expected the expected
+ * @param name the operation name
+ */
+ static void assertDouble(Complex c, DoubleBinaryOperator operation,
+ double expected, String name) {
+ assertDouble(c.getReal(), c.getImaginary(), operation, expected, name,
0.0D);
+ }
+
+ /**
+ * Assert the double operation on the complex number (real and imag parts)
is equal to the expected value.
+ * If the imaginary part is not NaN the operation must also satisfy the
conjugate equality.
+ *
+ * <pre>
+ * op(conj(z)) = conj(op(z))
+ * </pre>
+ *
+ * <p>The results must be binary equal. This includes the sign of zero.
+ * @param r real
+ * @param i imaginary
+ * @param operation the operation
+ * @param expected the expected
+ * @param name the operation name
+ * @param delta delta
+ */
+ static void assertDouble(double r, double i, DoubleBinaryOperator
operation,
+ double expected, String name, double delta) {
+
+ final double result = operation.applyAsDouble(r, i);
+
+ assertEquals(() -> "ComplexFunctions." + name + "(" + expected + "):
result", expected, result, delta);
+ }
+
+ /**
+ * Assert the unary complex operation on the complex number is equal to
the expected value.
+ * No delta.
+ * If the imaginary part is not NaN the operation must also satisfy the
conjugate equality.
+ *
+ * <pre>
+ * op(conj(z)) = conj(op(z))
+ * </pre>
+ *
+ * <p>The results must be binary equal. This includes the sign of zero.
+ * @param c the complex
+ * @param operation1 the operation
+ * @param operation2 the second operation
+ * @param expected the expected
+ * @param name the operation name
+ */
+ static void assertComplexUnary(Complex c,
+ UnaryOperator<Complex> operation1,
ComplexUnaryOperator<ComplexDouble> operation2,
+ Complex expected, String name) {
+ assertComplexUnary(c, operation1, operation2, expected, name, 0.0D,
0.0D);
+ }
+
+ /**
+ * Assert the unary complex operation on the complex number is equal to
the expected value.
+ * If the imaginary part is not NaN the operation must also satisfy the
conjugate equality.
+ *
+ * <pre>
+ * op(conj(z)) = conj(op(z))
+ * </pre>
+ *
+ * <p>The results must be binary equal. This includes the sign of zero.
+ * @param c the complex
+ * @param operation1 the operation
+ * @param operation2 the second operation
+ * @param expected the expected
+ * @param name the operation name
+ * @param delta delta
+ */
+ static void assertComplexUnary(Complex c,
+ UnaryOperator<Complex> operation1,
ComplexUnaryOperator<ComplexDouble> operation2,
+ Complex expected, String name, double
delta) {
+ assertComplexUnary(c, operation1, operation2, expected, name, delta,
delta);
+ }
+
+ /**
+ * Assert the unary complex operation on the complex number is equal to
the expected value.
+ * If the imaginary part is not NaN the operation must also satisfy the
conjugate equality.
+ *
+ * <pre>
+ * op(conj(z)) = conj(op(z))
+ * </pre>
+ *
+ * <p>The results must be binary equal. This includes the sign of zero.
+ * @param c the complex
+ * @param operation1 the operation
+ * @param operation2 the second operation
+ * @param expected the expected
+ * @param name the operation name
+ * @param deltaReal real delta
+ * @param deltaImaginary imaginary delta
+ */
+ static void assertComplexUnary(Complex c,
+ UnaryOperator<Complex> operation1,
ComplexUnaryOperator<ComplexDouble> operation2,
+ Complex expected, String name, double
deltaReal, double deltaImaginary) {
+ final Complex result1 = operation1.apply(c);
+ final ComplexDouble result2 = operation2.apply(c,
TestUtils.ComplexDoubleConstructor.of());
+
+ assertEquals(() -> c + "." + name + "(): real", expected.real(),
result1.real(), deltaReal);
+ assertEquals(() -> c + "." + name + "(): imaginary", expected.imag(),
result1.imag(), deltaImaginary);
+
+ assertEquals(() -> "ComplexFunctions." + name + "(" + c + "): real",
expected.real(), result2.getReal(), deltaReal);
+ assertEquals(() -> "ComplexFunctions." + name + "(" + c + "):
imaginary", expected.imag(), result2.getImaginary(), deltaImaginary);
+ }
+
+ /**
+ * Assert the binary complex operation on the complex number is equal to
the expected value.
+ * If the imaginary part is not NaN the operation must also satisfy the
conjugate equality.
+ *
+ * <pre>
+ * op(conj(z)) = conj(op(z))
+ * </pre>
+ *
+ * <p>The results must be binary equal. This includes the sign of zero.
+ * @param c1 the first complex
+ * @param c2 the second complex
+ * @param operation1 the operation
+ * @param operation2 the second operation
+ * @param expected the expected
+ * @param name the operation name
+ */
+ static void assertComplexBinary(Complex c1, Complex c2,
+ BinaryOperator<Complex> operation1,
ComplexBinaryOperator<ComplexDouble> operation2,
+ Complex expected, String name) {
+ assertComplexBinary(c1, c2, operation1, operation2, expected, name,
0.0D, 0.0D);
+ }
+
+ /**
+ * Assert the binary complex operation on the complex number is equal to
the expected value.
+ * If the imaginary part is not NaN the operation must also satisfy the
conjugate equality.
+ *
+ * <pre>
+ * op(conj(z)) = conj(op(z))
+ * </pre>
+ *
+ * <p>The results must be binary equal. This includes the sign of zero.
+ * @param c1 the first complex
+ * @param c2 the second complex
+ * @param operation1 the complex operation
+ * @param operation2 the complexFunctions operation
+ * @param expected the expected result
+ * @param name the operation name
+ * @param deltaReal real delta
+ * @param deltaImaginary imaginary delta
+ */
+ static void assertComplexBinary(Complex c1, Complex c2,
+ BinaryOperator<Complex> operation1,
ComplexBinaryOperator<ComplexDouble> operation2,
+ Complex expected, String name, double
deltaReal, double deltaImaginary) {
+ final Complex result1 = operation1.apply(c1, c2);
+ final ComplexDouble result2 = operation2.apply(c1, c2,
TestUtils.ComplexDoubleConstructor.of());
+
+ assertEquals(() -> c1 + "." + name + "(" + c2 + "): real",
expected.real(), result1.real(), deltaReal);
+ assertEquals(() -> c1 + "." + name + "(" + c2 + "): imaginary",
expected.imag(), result1.imag(), deltaImaginary);
+
+ assertEquals(() -> "ComplexFunctions." + name + "(" + c1 + ", " + c2 +
"): real", expected.real(), result2.getReal(), deltaReal);
+ assertEquals(() -> "ComplexFunctions." + name + "(" + c1 + ", " + c2 +
"): imaginary", expected.imag(), result2.getImaginary(), deltaImaginary);
+ }
+
+ /**
+ * Assert the binary complex operation on the complex number is equal to
the expected value.
+ * If the imaginary part is not NaN the operation must also satisfy the
conjugate equality.
+ *
+ * <pre>
+ * op(conj(z)) = conj(op(z))
+ * </pre>
+ *
+ * <p>The results must be binary equal. This includes the sign of zero.
+ * @param c1 the first complex
+ * @param c2 the second complex
+ * @param operation1 the complex operation
+ * @param operation2 the complexFunctions operation
+ * @param resultChecker function to assert expected result
+ * @param name the operation name
+ */
+ static void assertComplexBinary(Complex c1, Complex c2,
+ BinaryOperator<Complex> operation1,
ComplexBinaryOperator<ComplexDouble> operation2,
+ ComplexConstructor<Boolean> resultChecker,
String name) {
+ final Complex result1 = operation1.apply(c1, c2);
+ final ComplexDouble result2 = operation2.apply(c1, c2,
TestUtils.ComplexDoubleConstructor.of());
+
+ Assertions.assertTrue(resultChecker.apply(result1.getReal(),
result1.getImaginary()), () -> c1 + "." + name + "(" + c2 + ")");
+ Assertions.assertTrue(resultChecker.apply(result2.getReal(),
result2.getImaginary()), () -> "ComplexFunctions." + c1 + "." + name + "(" +
c2 + ")");
+ }
+
+ /**
+ * Assert the two numbers are equal within the provided units of least
precision.
+ * The maximum count of numbers allowed between the two values is {@code
maxUlps - 1}.
+ *
+ * <p>Numbers must have the same sign. Thus -0.0 and 0.0 are never equal.
+ *
+ * @param msg the failure message
+ * @param expected the expected
+ * @param actual the actual
+ * @param delta delta
+ */
+ static void assertEquals(Supplier<String> msg, double expected, double
actual, double delta) {
+ Assertions.assertEquals(expected, actual, delta, msg);
+ }
+
+ static class ComplexDoubleConstructor implements
ComplexConstructor<ComplexDouble>, ComplexDouble {
Review Comment:
Is there a way I can call the ComplexNumber class in the assertComplex
operations in the other test classes? Before I said
TestUtils.ComplexDoubleConstructor.of() in the apply operation, is there a way
to do it like this with your suggested ComplexNumber class
Issue Time Tracking
-------------------
Worklog Id: (was: 791548)
Time Spent: 2h 40m (was: 2.5h)
> Static Method Refactor and Functional Interfaces
> ------------------------------------------------
>
> Key: NUMBERS-188
> URL: https://issues.apache.org/jira/browse/NUMBERS-188
> Project: Commons Numbers
> Issue Type: Sub-task
> Reporter: Sumanth Sulur Rajkumar
> Priority: Minor
> Labels: gsoc2022
> Time Spent: 2h 40m
> Remaining Estimate: 0h
>
> Refactored existing instance methods in Complex class as static functions in
> ComplexFunctions class using functional interface signatures
--
This message was sent by Atlassian Jira
(v8.20.10#820010)