diff --git a/src/main/java/org/apache/commons/math/FieldElement.java b/src/main/java/org/apache/commons/math/FieldElement.java index 5bbcb50a0..f3771d3b8 100644 --- a/src/main/java/org/apache/commons/math/FieldElement.java +++ b/src/main/java/org/apache/commons/math/FieldElement.java @@ -38,6 +38,16 @@ public interface FieldElement { */ T subtract(T a); + /** Compute n × this. Multiplication by an integer number is defined + * as the following sum + *
+ * n × this = ∑i=1n this. + *
+ * @param n Number of times {@code this} must be added to itself. + * @return A new element representing n × this. + */ + T multiply(int n); + /** Compute this × a. * @param a element to multiply * @return a new element representing this × a diff --git a/src/main/java/org/apache/commons/math/complex/Complex.java b/src/main/java/org/apache/commons/math/complex/Complex.java index ac31e4b1b..d3a3cb8bb 100644 --- a/src/main/java/org/apache/commons/math/complex/Complex.java +++ b/src/main/java/org/apache/commons/math/complex/Complex.java @@ -424,6 +424,25 @@ public class Complex implements FieldElement, Serializable { real * factor.imaginary + imaginary * factor.real); } + /** + * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor} + * interpreted as a integer number. + * + * @param factor value to be multiplied by this {@code Complex}. + * @return {@code this * factor}. + * @see #multiply(Complex) + */ + public Complex multiply(final int factor) { + if (isNaN) { + return NaN; + } + if (Double.isInfinite(real) || + Double.isInfinite(imaginary)) { + return INF; + } + return createComplex(real * factor, imaginary * factor); + } + /** * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor} * interpreted as a real number. diff --git a/src/main/java/org/apache/commons/math/util/BigReal.java b/src/main/java/org/apache/commons/math/util/BigReal.java index ddf62900a..902fa14b7 100644 --- a/src/main/java/org/apache/commons/math/util/BigReal.java +++ b/src/main/java/org/apache/commons/math/util/BigReal.java @@ -246,6 +246,11 @@ public class BigReal implements FieldElement, Comparable, Seri return new BigReal(d.multiply(a.d)); } + /** {@inheritDoc} */ + public BigReal multiply(final int n) { + return new BigReal(d.multiply(new BigDecimal(n))); + } + /** {@inheritDoc} */ public int compareTo(BigReal a) { return d.compareTo(a.d); @@ -288,5 +293,4 @@ public class BigReal implements FieldElement, Comparable, Seri public Field getField() { return BigRealField.getInstance(); } - } diff --git a/src/test/java/org/apache/commons/math/complex/ComplexTest.java b/src/test/java/org/apache/commons/math/complex/ComplexTest.java index 00700f9e7..0380e15f9 100644 --- a/src/test/java/org/apache/commons/math/complex/ComplexTest.java +++ b/src/test/java/org/apache/commons/math/complex/ComplexTest.java @@ -129,7 +129,7 @@ public class ComplexTest { Assert.assertTrue(Double.isNaN(x.add(z).getReal())); } - + @Test public void testScalarAdd() { Complex x = new Complex(3.0, 4.0); @@ -150,7 +150,7 @@ public class ComplexTest { public void testScalarAddInf() { Complex x = new Complex(1, 1); double yDouble = Double.POSITIVE_INFINITY; - + Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.add(yComplex), x.add(yDouble)); @@ -288,11 +288,11 @@ public class ComplexTest { yDouble = Double.NEGATIVE_INFINITY; yComplex = new Complex(yDouble); TestUtils.assertEquals(x.divide(yComplex), x.divide(yDouble), 0); - + x = new Complex(1, Double.NEGATIVE_INFINITY); TestUtils.assertEquals(x.divide(yComplex), x.divide(yDouble), 0); } - + @Test public void testScalarDivideZero() { Complex x = new Complex(1,1); @@ -313,10 +313,12 @@ public class ComplexTest { Complex x = new Complex(3.0, 4.0); Complex z = x.multiply(Complex.NaN); Assert.assertSame(Complex.NaN, z); + z = Complex.NaN.multiply(5); + Assert.assertSame(Complex.NaN, z); } @Test - public void testMultiplyInInf() { + public void testMultiplyInfInf() { // Assert.assertTrue(infInf.multiply(infInf).isNaN()); // MATH-620 Assert.assertTrue(infInf.multiply(infInf).isInfinite()); } @@ -340,7 +342,7 @@ public class ComplexTest { w = negInfNegInf.multiply(oneNaN); Assert.assertTrue(Double.isNaN(w.getReal())); Assert.assertTrue(Double.isNaN(w.getImaginary())); - + z = new Complex(1, neginf); Assert.assertSame(Complex.INF, z.multiply(z)); } @@ -351,6 +353,9 @@ public class ComplexTest { double yDouble = 2.0; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); + int zInt = -5; + Complex zComplex = new Complex(zInt); + Assert.assertEquals(x.multiply(zComplex), x.multiply(zInt)); } @Test @@ -367,7 +372,7 @@ public class ComplexTest { double yDouble = Double.POSITIVE_INFINITY; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); - + yDouble = Double.NEGATIVE_INFINITY; yComplex = new Complex(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); @@ -417,7 +422,7 @@ public class ComplexTest { x = new Complex(neginf, 0); Assert.assertTrue(Double.isNaN(x.subtract(z).getReal())); } - + @Test public void testScalarSubtract() { Complex x = new Complex(3.0, 4.0); @@ -445,7 +450,7 @@ public class ComplexTest { Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); } - + @Test public void testEqualsNull() { Complex x = new Complex(3.0, 4.0); @@ -507,7 +512,7 @@ public class ComplexTest { Assert.assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode()); Assert.assertEquals(imaginaryNaN.hashCode(), Complex.NaN.hashCode()); } - + @Test public void testAcos() { Complex z = new Complex(3, 4); diff --git a/src/test/java/org/apache/commons/math/util/BigRealTest.java b/src/test/java/org/apache/commons/math/util/BigRealTest.java index 9830572f8..8053c5f99 100644 --- a/src/test/java/org/apache/commons/math/util/BigRealTest.java +++ b/src/test/java/org/apache/commons/math/util/BigRealTest.java @@ -87,6 +87,8 @@ public class BigRealTest { BigReal a = new BigReal("1024.0"); BigReal b = new BigReal("0.0009765625"); Assert.assertEquals(1.0, a.multiply(b).doubleValue(), 1.0e-15); + int n = 1024; + Assert.assertEquals(1.0, b.multiply(n).doubleValue(), 1.0e-15); } @Test