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);
|
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.
|
/** Compute this × a.
|
||||||
* @param a element to multiply
|
* @param a element to multiply
|
||||||
* @return a new element representing this × a
|
* @return a new element representing this × a
|
||||||
|
|
|
@ -424,6 +424,25 @@ public class Complex implements FieldElement<Complex>, Serializable {
|
||||||
real * factor.imaginary + imaginary * factor.real);
|
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}
|
* Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor}
|
||||||
* interpreted as a real number.
|
* 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));
|
return new BigReal(d.multiply(a.d));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
public BigReal multiply(final int n) {
|
||||||
|
return new BigReal(d.multiply(new BigDecimal(n)));
|
||||||
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public int compareTo(BigReal a) {
|
public int compareTo(BigReal a) {
|
||||||
return d.compareTo(a.d);
|
return d.compareTo(a.d);
|
||||||
|
@ -288,5 +293,4 @@ public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, Seri
|
||||||
public Field<BigReal> getField() {
|
public Field<BigReal> getField() {
|
||||||
return BigRealField.getInstance();
|
return BigRealField.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -313,10 +313,12 @@ public class ComplexTest {
|
||||||
Complex x = new Complex(3.0, 4.0);
|
Complex x = new Complex(3.0, 4.0);
|
||||||
Complex z = x.multiply(Complex.NaN);
|
Complex z = x.multiply(Complex.NaN);
|
||||||
Assert.assertSame(Complex.NaN, z);
|
Assert.assertSame(Complex.NaN, z);
|
||||||
|
z = Complex.NaN.multiply(5);
|
||||||
|
Assert.assertSame(Complex.NaN, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultiplyInInf() {
|
public void testMultiplyInfInf() {
|
||||||
// Assert.assertTrue(infInf.multiply(infInf).isNaN()); // MATH-620
|
// Assert.assertTrue(infInf.multiply(infInf).isNaN()); // MATH-620
|
||||||
Assert.assertTrue(infInf.multiply(infInf).isInfinite());
|
Assert.assertTrue(infInf.multiply(infInf).isInfinite());
|
||||||
}
|
}
|
||||||
|
@ -351,6 +353,9 @@ public class ComplexTest {
|
||||||
double yDouble = 2.0;
|
double yDouble = 2.0;
|
||||||
Complex yComplex = new Complex(yDouble);
|
Complex yComplex = new Complex(yDouble);
|
||||||
Assert.assertEquals(x.multiply(yComplex), x.multiply(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
|
@Test
|
||||||
|
|
|
@ -87,6 +87,8 @@ public class BigRealTest {
|
||||||
BigReal a = new BigReal("1024.0");
|
BigReal a = new BigReal("1024.0");
|
||||||
BigReal b = new BigReal("0.0009765625");
|
BigReal b = new BigReal("0.0009765625");
|
||||||
Assert.assertEquals(1.0, a.multiply(b).doubleValue(), 1.0e-15);
|
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
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue