You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/12/12 12:26:26 UTC
[commons-numbers] 05/05: Added Complex.ofImaginary(double)
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 276028dcea4c334f2088f0a22fbe444f83dfe206
Author: aherbert <ah...@apache.org>
AuthorDate: Thu Dec 12 12:25:16 2019 +0000
Added Complex.ofImaginary(double)
---
.../apache/commons/numbers/complex/Complex.java | 18 +++++++---
.../commons/numbers/complex/ComplexTest.java | 41 ++++++++++------------
2 files changed, 33 insertions(+), 26 deletions(-)
diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
index 1973990..e1afc49 100644
--- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
+++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
@@ -149,6 +149,16 @@ public final class Complex implements Serializable {
}
/**
+ * Create a complex number given the imaginary part.
+ *
+ * @param imaginary Imaginary part.
+ * @return {@code Complex} object
+ */
+ public static Complex ofImaginary(double imaginary) {
+ return new Complex(0, imaginary);
+ }
+
+ /**
* Creates a Complex from its polar representation.
*
* <p>If {@code r} is infinite and {@code theta} is finite, infinite or NaN
@@ -391,13 +401,13 @@ public final class Complex implements Serializable {
*
* <p>Note: This method preserves the sign of the real component {@code a} if it is {@code -0.0}.
* The sign would be lost if adding {@code (0 + i d)} using
- * {@link #add(Complex) add(Complex.ofCartesian(0, addend))} since
+ * {@link #add(Complex) add(Complex.ofImaginary(addend))} since
* {@code -0.0 + 0.0 = 0.0}.
*
* @param addend Value to be added to this {@code Complex}.
* @return {@code this + addend}.
* @see #add(Complex)
- * @see #ofCartesian(double, double)
+ * @see #ofImaginary(double)
*/
public Complex addImaginary(double addend) {
return new Complex(real, imaginary + addend);
@@ -1062,13 +1072,13 @@ public final class Complex implements Serializable {
*
* <p>Note: This method inverts the sign of the real component {@code a} if it is {@code 0.0}.
* The sign would not be inverted if subtracting from {@code (0 + i d)} using
- * {@link #subtract(Complex) Complex.ofCartesian(0, minuend).subtract(this))} since
+ * {@link #subtract(Complex) Complex.ofImaginary(minuend).subtract(this))} since
* {@code 0.0 - 0.0 = 0.0}.
*
* @param minuend value this {@code Complex} is to be subtracted from.
* @return {@code this - subtrahend}.
* @see #subtract(Complex)
- * @see #ofCartesian(double, double)
+ * @see #ofImaginary(double)
*/
public Complex subtractFromImaginary(double minuend) {
return new Complex(-real, minuend - imaginary);
diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java
index 217588b..df6c204 100644
--- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java
+++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java
@@ -67,16 +67,6 @@ public class ComplexTest {
FINITE
}
- /**
- * Convenience constructor for testing using only and imaginary component.
- *
- * @param imaginary the imaginary part
- * @return the complex
- */
- private static Complex ofImaginary(double imaginary) {
- return Complex.ofCartesian(0, imaginary);
- }
-
@Test
public void testCartesianConstructor() {
final Complex z = Complex.ofCartesian(3.0, 4.0);
@@ -92,6 +82,13 @@ public class ComplexTest {
}
@Test
+ public void testImaginaryConstructor() {
+ final Complex z = Complex.ofImaginary(3.0);
+ Assertions.assertEquals(0.0, z.getReal());
+ Assertions.assertEquals(3.0, z.getImaginary());
+ }
+
+ @Test
public void testPolarConstructor() {
final double r = 98765;
final double theta = 0.12345;
@@ -278,7 +275,7 @@ public class ComplexTest {
Assertions.assertEquals(3.0, z.getReal());
Assertions.assertEquals(9.0, z.getImaginary());
// Equivalent
- Assertions.assertEquals(z, x.add(ofImaginary(y)));
+ Assertions.assertEquals(z, x.add(Complex.ofImaginary(y)));
}
@Test
@@ -289,7 +286,7 @@ public class ComplexTest {
Assertions.assertEquals(3.0, z.getReal());
Assertions.assertEquals(nan, z.getImaginary());
// Equivalent
- Assertions.assertEquals(z, x.add(ofImaginary(y)));
+ Assertions.assertEquals(z, x.add(Complex.ofImaginary(y)));
}
@Test
@@ -300,7 +297,7 @@ public class ComplexTest {
Assertions.assertEquals(3.0, z.getReal());
Assertions.assertEquals(inf, z.getImaginary());
// Equivalent
- Assertions.assertEquals(z, x.add(ofImaginary(y)));
+ Assertions.assertEquals(z, x.add(Complex.ofImaginary(y)));
}
@Test
@@ -311,7 +308,7 @@ public class ComplexTest {
Assertions.assertEquals(-0.0, z.getReal());
Assertions.assertEquals(9.0, z.getImaginary(), "Expected sign preservation");
// Sign-preservation is a problem: -0.0 + 0.0 == 0.0
- Assertions.assertNotEquals(z, x.add(ofImaginary(y)));
+ Assertions.assertNotEquals(z, x.add(Complex.ofImaginary(y)));
}
@Test
@@ -599,7 +596,7 @@ public class ComplexTest {
Assertions.assertEquals(3.0, z.getReal());
Assertions.assertEquals(-1.0, z.getImaginary());
// Equivalent
- Assertions.assertEquals(z, x.subtract(ofImaginary(y)));
+ Assertions.assertEquals(z, x.subtract(Complex.ofImaginary(y)));
}
@Test
@@ -610,7 +607,7 @@ public class ComplexTest {
Assertions.assertEquals(3.0, z.getReal());
Assertions.assertEquals(nan, z.getImaginary());
// Equivalent
- Assertions.assertEquals(z, x.subtract(ofImaginary(y)));
+ Assertions.assertEquals(z, x.subtract(Complex.ofImaginary(y)));
}
@Test
@@ -621,7 +618,7 @@ public class ComplexTest {
Assertions.assertEquals(3.0, z.getReal());
Assertions.assertEquals(-inf, z.getImaginary());
// Equivalent
- Assertions.assertEquals(z, x.subtract(ofImaginary(y)));
+ Assertions.assertEquals(z, x.subtract(Complex.ofImaginary(y)));
}
@Test
@@ -633,7 +630,7 @@ public class ComplexTest {
Assertions.assertEquals(-1.0, z.getImaginary());
// Equivalent
// Sign-preservation is not a problem: -0.0 - 0.0 == -0.0
- Assertions.assertEquals(z, x.subtract(ofImaginary(y)));
+ Assertions.assertEquals(z, x.subtract(Complex.ofImaginary(y)));
}
@Test
@@ -688,7 +685,7 @@ public class ComplexTest {
Assertions.assertEquals(-3.0, z.getReal());
Assertions.assertEquals(1.0, z.getImaginary());
// Equivalent
- Assertions.assertEquals(z, ofImaginary(y).subtract(x));
+ Assertions.assertEquals(z, Complex.ofImaginary(y).subtract(x));
}
@Test
@@ -699,7 +696,7 @@ public class ComplexTest {
Assertions.assertEquals(-3.0, z.getReal());
Assertions.assertEquals(nan, z.getImaginary());
// Equivalent
- Assertions.assertEquals(z, ofImaginary(y).subtract(x));
+ Assertions.assertEquals(z, Complex.ofImaginary(y).subtract(x));
}
@Test
@@ -710,7 +707,7 @@ public class ComplexTest {
Assertions.assertEquals(-3.0, z.getReal());
Assertions.assertEquals(inf, z.getImaginary());
// Equivalent
- Assertions.assertEquals(z, ofImaginary(y).subtract(x));
+ Assertions.assertEquals(z, Complex.ofImaginary(y).subtract(x));
}
@Test
@@ -721,7 +718,7 @@ public class ComplexTest {
Assertions.assertEquals(-0.0, z.getReal(), "Expected sign inversion");
Assertions.assertEquals(1.0, z.getImaginary());
// Sign-inversion is a problem: 0.0 - 0.0 == 0.0
- Assertions.assertNotEquals(z, ofImaginary(y).subtract(x));
+ Assertions.assertNotEquals(z, Complex.ofImaginary(y).subtract(x));
}
@Test