Added method multiply(int) to FieldElement, and updated implementing classes and tests accordingly. Solves MATH-684.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1178715 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2898bc6403
commit
309dd689eb
|
@ -38,6 +38,16 @@ public interface FieldElement<T> {
|
|||
*/
|
||||
T subtract(T a);
|
||||
|
||||
/** Compute n × this. Multiplication by an integer number is defined
|
||||
* as the following sum
|
||||
* <center>
|
||||
* n × this = ∑<sub>i=1</sub><sup>n</sup> this.
|
||||
* </center>
|
||||
* @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
|
||||
|
|
|
@ -424,6 +424,25 @@ public class Complex implements FieldElement<Complex>, 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.
|
||||
|
|
|
@ -246,6 +246,11 @@ public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, 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<BigReal>, Comparable<BigReal>, Seri
|
|||
public Field<BigReal> getField() {
|
||||
return BigRealField.getInstance();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue