commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1180312 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/ main/java/org/apache/commons/math/complex/ main/java/org/apache/commons/math/dfp/ main/java/org/apache/commons/math/util/ test/java/org/apache/commons/math/com...
Date Sat, 08 Oct 2011 04:21:02 GMT
Author: celestin
Date: Sat Oct  8 04:21:00 2011
New Revision: 1180312

URL: http://svn.apache.org/viewvc?rev=1180312&view=rev
Log:
Added FieldElement.negate() and FieldElement.reciprocal() (MATH-686).

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/FieldElement.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/dfp/Dfp.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/dfp/DfpTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/BigRealTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/FieldElement.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/FieldElement.java?rev=1180312&r1=1180311&r2=1180312&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/FieldElement.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/FieldElement.java Sat
Oct  8 04:21:00 2011
@@ -38,6 +38,12 @@ public interface FieldElement<T> {
      */
     T subtract(T a);
 
+    /**
+     * Returns the additive inverse of {@code this} element.
+     * @return the opposite of {@code this}.
+     */
+    T negate();
+
     /** Compute n &times; this. Multiplication by an integer number is defined
      * as the following sum
      * <center>
@@ -62,9 +68,14 @@ public interface FieldElement<T> {
      */
     T divide(T a) throws ArithmeticException;
 
+    /**
+     * Returns the multiplicative inverse of {@code this} element.
+     * @return the inverse of {@code this}.
+     */
+    T reciprocal();
+
     /** Get the {@link Field} to which the instance belongs.
      * @return {@link Field} to which the instance belongs
      */
     Field<T> getField();
-
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java?rev=1180312&r1=1180311&r2=1180312&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java Sat
Oct  8 04:21:00 2011
@@ -294,6 +294,31 @@ public class Complex implements FieldEle
                              imaginary  / divisor);
     }
 
+    /** {@inheritDoc} */
+    public Complex reciprocal() {
+        if (isNaN) {
+            return NaN;
+        }
+
+        if (real == 0.0 && imaginary == 0.0) {
+            return NaN;
+        }
+
+        if (isInfinite) {
+            return ZERO;
+        }
+
+        if (FastMath.abs(real) < FastMath.abs(imaginary)) {
+            double q = real / imaginary;
+            double scale = 1. / (real * q + imaginary);
+            return createComplex(scale * q, -scale);
+        } else {
+            double q = imaginary / real;
+            double scale = 1. / (imaginary * q + real);
+            return createComplex(scale, -scale * q);
+        }
+    }
+
     /**
      * Test for the equality of two Complex objects.
      * If both the real and imaginary parts of two complex numbers
@@ -1192,4 +1217,5 @@ public class Complex implements FieldEle
     public String toString() {
         return "(" + real + ", " + imaginary + ")";
     }
+
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/dfp/Dfp.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/dfp/Dfp.java?rev=1180312&r1=1180311&r2=1180312&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/dfp/Dfp.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/dfp/Dfp.java Sat Oct 
8 04:21:00 2011
@@ -1937,6 +1937,11 @@ public class Dfp implements FieldElement
 
     }
 
+    /** {@inheritDoc} */
+    public Dfp reciprocal() {
+        return field.getOne().divide(this);
+    }
+
     /** Compute the square root.
      * @return square root of the instance
      */

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java?rev=1180312&r1=1180311&r2=1180312&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java Sat
Oct  8 04:21:00 2011
@@ -237,10 +237,20 @@ public class BigReal implements FieldEle
     }
 
     /** {@inheritDoc} */
+    public BigReal negate() {
+        return new BigReal(d.negate());
+    }
+
+    /** {@inheritDoc} */
     public BigReal divide(BigReal a) throws ArithmeticException {
         return new BigReal(d.divide(a.d, scale, roundingMode));
     }
 
+    /** {@inheritDoc}} */
+    public BigReal reciprocal(){
+        return new BigReal(BigDecimal.ONE.divide(d, scale, roundingMode));
+    }
+
     /** {@inheritDoc} */
     public BigReal multiply(BigReal a) {
         return new BigReal(d.multiply(a.d));

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java?rev=1180312&r1=1180311&r2=1180312&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
Sat Oct  8 04:21:00 2011
@@ -300,6 +300,47 @@ public class ComplexTest {
     }
 
     @Test
+    public void testReciprocal() {
+        Complex z = new Complex(5.0, 6.0);
+        Complex act = z.reciprocal();
+        double expRe = 5.0 / 61.0;
+        double expIm = -6.0 / 61.0;
+        Assert.assertEquals(expRe, act.getReal(), FastMath.ulp(expRe));
+        Assert.assertEquals(expIm, act.getImaginary(), FastMath.ulp(expIm));
+    }
+
+    @Test
+    public void testReciprocalReal() {
+        Complex z = new Complex(-2.0, 0.0);
+        Assert.assertEquals(new Complex(-0.5, 0.0), z.reciprocal());
+    }
+
+    @Test
+    public void testReciprocalImaginary() {
+        Complex z = new Complex(0.0, -2.0);
+        Assert.assertEquals(new Complex(0.0, 0.5), z.reciprocal());
+    }
+
+    @Test
+    public void testReciprocalInf() {
+        Complex z = new Complex(neginf, inf);
+        Assert.assertTrue(z.reciprocal().equals(Complex.ZERO));
+
+        z = new Complex(1, inf).reciprocal();
+        Assert.assertEquals(z, Complex.ZERO);
+    }
+
+    @Test
+    public void testReciprocalZero() {
+        Assert.assertEquals(Complex.ZERO.reciprocal(), Complex.NaN);
+    }
+
+    @Test
+    public void testReciprocalNaN() {
+        Assert.assertTrue(Complex.NaN.reciprocal().isNaN());
+    }
+
+    @Test
     public void testMultiply() {
         Complex x = new Complex(3.0, 4.0);
         Complex y = new Complex(5.0, 6.0);
@@ -1273,5 +1314,4 @@ public class ComplexTest {
         }
 
     }
-
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/dfp/DfpTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/dfp/DfpTest.java?rev=1180312&r1=1180311&r2=1180312&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/dfp/DfpTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/dfp/DfpTest.java Sat Oct
 8 04:21:00 2011
@@ -55,14 +55,14 @@ public class DfpTest {
         qnan    = null;
     }
 
-    // Generic test function.  Takes params x and y and tests them for 
+    // Generic test function.  Takes params x and y and tests them for
     // equality.  Then checks the status flags against the flags argument.
     // If the test fail, it prints the desc string
     private void test(Dfp x, Dfp y, int flags, String desc)
     {
         boolean b = x.equals(y);
 
-        if (!x.equals(y) && !x.unequal(y))  // NaNs involved 
+        if (!x.equals(y) && !x.unequal(y))  // NaNs involved
             b = (x.toString().equals(y.toString()));
 
         if (x.equals(field.newDfp("0")))  // distinguish +/- zero
@@ -114,197 +114,197 @@ public class DfpTest {
     public void testAdd()
     {
         test(field.newDfp("1").add(field.newDfp("1")),      // Basic tests   1+1 = 2
-             field.newDfp("2"), 
+             field.newDfp("2"),
              0, "Add #1");
 
         test(field.newDfp("1").add(field.newDfp("-1")),     // 1 + (-1) = 0
-             field.newDfp("0"), 
+             field.newDfp("0"),
              0, "Add #2");
 
         test(field.newDfp("-1").add(field.newDfp("1")),     // (-1) + 1 = 0
-             field.newDfp("0"), 
+             field.newDfp("0"),
              0, "Add #3");
 
         test(field.newDfp("-1").add(field.newDfp("-1")),     // (-1) + (-1) = -2
-             field.newDfp("-2"), 
+             field.newDfp("-2"),
              0, "Add #4");
 
         // rounding mode is round half even
 
         test(field.newDfp("1").add(field.newDfp("1e-16")),     // rounding on add
-             field.newDfp("1.0000000000000001"), 
+             field.newDfp("1.0000000000000001"),
              0, "Add #5");
 
         test(field.newDfp("1").add(field.newDfp("1e-17")),     // rounding on add
-             field.newDfp("1"), 
+             field.newDfp("1"),
              DfpField.FLAG_INEXACT, "Add #6");
 
         test(field.newDfp("0.90999999999999999999").add(field.newDfp("0.1")),     // rounding
on add
-             field.newDfp("1.01"), 
+             field.newDfp("1.01"),
              DfpField.FLAG_INEXACT, "Add #7");
 
         test(field.newDfp(".10000000000000005000").add(field.newDfp(".9")),     // rounding
on add
-             field.newDfp("1."), 
+             field.newDfp("1."),
              DfpField.FLAG_INEXACT, "Add #8");
 
         test(field.newDfp(".10000000000000015000").add(field.newDfp(".9")),     // rounding
on add
-             field.newDfp("1.0000000000000002"), 
+             field.newDfp("1.0000000000000002"),
              DfpField.FLAG_INEXACT, "Add #9");
 
         test(field.newDfp(".10000000000000014999").add(field.newDfp(".9")),     // rounding
on add
-             field.newDfp("1.0000000000000001"), 
+             field.newDfp("1.0000000000000001"),
              DfpField.FLAG_INEXACT, "Add #10");
 
         test(field.newDfp(".10000000000000015001").add(field.newDfp(".9")),     // rounding
on add
-             field.newDfp("1.0000000000000002"), 
+             field.newDfp("1.0000000000000002"),
              DfpField.FLAG_INEXACT, "Add #11");
 
         test(field.newDfp(".11111111111111111111").add(field.newDfp("11.1111111111111111")),
// rounding on add
-             field.newDfp("11.22222222222222222222"), 
+             field.newDfp("11.22222222222222222222"),
              DfpField.FLAG_INEXACT, "Add #12");
 
         test(field.newDfp(".11111111111111111111").add(field.newDfp("1111111111111111.1111")),
// rounding on add
-             field.newDfp("1111111111111111.2222"), 
+             field.newDfp("1111111111111111.2222"),
              DfpField.FLAG_INEXACT, "Add #13");
 
         test(field.newDfp(".11111111111111111111").add(field.newDfp("11111111111111111111")),
// rounding on add
-             field.newDfp("11111111111111111111"), 
+             field.newDfp("11111111111111111111"),
              DfpField.FLAG_INEXACT, "Add #14");
 
         test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("-1e131052")),
// overflow on add
-             field.newDfp("9.9999999999999999998e131071"), 
+             field.newDfp("9.9999999999999999998e131071"),
              0, "Add #15");
 
         test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("1e131052")),
// overflow on add
-             pinf, 
+             pinf,
              DfpField.FLAG_OVERFLOW, "Add #16");
 
         test(field.newDfp("-9.9999999999999999999e131071").add(field.newDfp("-1e131052")),
// overflow on add
-             ninf, 
+             ninf,
              DfpField.FLAG_OVERFLOW, "Add #17");
 
         test(field.newDfp("-9.9999999999999999999e131071").add(field.newDfp("1e131052")),
// overflow on add
-             field.newDfp("-9.9999999999999999998e131071"), 
+             field.newDfp("-9.9999999999999999998e131071"),
              0, "Add #18");
 
         test(field.newDfp("1e-131072").add(field.newDfp("1e-131072")), // underflow on add
-             field.newDfp("2e-131072"), 
+             field.newDfp("2e-131072"),
              0, "Add #19");
 
         test(field.newDfp("1.0000000000000001e-131057").add(field.newDfp("-1e-131057")),
// underflow on add
-             field.newDfp("1e-131073"), 
+             field.newDfp("1e-131073"),
              DfpField.FLAG_UNDERFLOW, "Add #20");
 
         test(field.newDfp("1.1e-131072").add(field.newDfp("-1e-131072")), // underflow on
add
-             field.newDfp("1e-131073"), 
+             field.newDfp("1e-131073"),
              DfpField.FLAG_UNDERFLOW, "Add #21");
 
         test(field.newDfp("1.0000000000000001e-131072").add(field.newDfp("-1e-131072")),
// underflow on add
-             field.newDfp("1e-131088"), 
+             field.newDfp("1e-131088"),
              DfpField.FLAG_UNDERFLOW, "Add #22");
 
         test(field.newDfp("1.0000000000000001e-131078").add(field.newDfp("-1e-131078")),
// underflow on add
-             field.newDfp("0"), 
+             field.newDfp("0"),
              DfpField.FLAG_UNDERFLOW, "Add #23");
 
         test(field.newDfp("1.0").add(field.newDfp("-1e-20")), // loss of precision on alignment?
-             field.newDfp("0.99999999999999999999"), 
+             field.newDfp("0.99999999999999999999"),
              0, "Add #23.1");
 
         test(field.newDfp("-0.99999999999999999999").add(field.newDfp("1")), // proper normalization?
-             field.newDfp("0.00000000000000000001"), 
+             field.newDfp("0.00000000000000000001"),
              0, "Add #23.2");
 
         test(field.newDfp("1").add(field.newDfp("0")), // adding zeros
-             field.newDfp("1"), 
+             field.newDfp("1"),
              0, "Add #24");
 
         test(field.newDfp("0").add(field.newDfp("0")), // adding zeros
-             field.newDfp("0"), 
+             field.newDfp("0"),
              0, "Add #25");
 
         test(field.newDfp("-0").add(field.newDfp("0")), // adding zeros
-             field.newDfp("0"), 
+             field.newDfp("0"),
              0, "Add #26");
 
         test(field.newDfp("0").add(field.newDfp("-0")), // adding zeros
-             field.newDfp("0"), 
+             field.newDfp("0"),
              0, "Add #27");
 
         test(field.newDfp("-0").add(field.newDfp("-0")), // adding zeros
-             field.newDfp("-0"), 
+             field.newDfp("-0"),
              0, "Add #28");
 
         test(field.newDfp("1e-20").add(field.newDfp("0")), // adding zeros
-             field.newDfp("1e-20"), 
+             field.newDfp("1e-20"),
              0, "Add #29");
 
         test(field.newDfp("1e-40").add(field.newDfp("0")), // adding zeros
-             field.newDfp("1e-40"), 
+             field.newDfp("1e-40"),
              0, "Add #30");
 
         test(pinf.add(ninf), // adding infinities
-             nan, 
+             nan,
              DfpField.FLAG_INVALID, "Add #31");
 
         test(ninf.add(pinf), // adding infinities
-             nan, 
+             nan,
              DfpField.FLAG_INVALID, "Add #32");
 
         test(ninf.add(ninf), // adding infinities
-             ninf, 
+             ninf,
              0, "Add #33");
 
         test(pinf.add(pinf), // adding infinities
-             pinf, 
+             pinf,
              0, "Add #34");
 
         test(pinf.add(field.newDfp("0")), // adding infinities
-             pinf, 
+             pinf,
              0, "Add #35");
 
         test(pinf.add(field.newDfp("-1e131071")), // adding infinities
-             pinf, 
+             pinf,
              0, "Add #36");
 
         test(pinf.add(field.newDfp("1e131071")), // adding infinities
-             pinf, 
+             pinf,
              0, "Add #37");
 
         test(field.newDfp("0").add(pinf), // adding infinities
-             pinf, 
+             pinf,
              0, "Add #38");
 
         test(field.newDfp("-1e131071").add(pinf), // adding infinities
-             pinf, 
+             pinf,
              0, "Add #39");
 
         test(field.newDfp("1e131071").add(pinf), // adding infinities
-             pinf, 
+             pinf,
              0, "Add #40");
 
         test(ninf.add(field.newDfp("0")), // adding infinities
-             ninf, 
+             ninf,
              0, "Add #41");
 
         test(ninf.add(field.newDfp("-1e131071")), // adding infinities
-             ninf, 
+             ninf,
              0, "Add #42");
 
         test(ninf.add(field.newDfp("1e131071")), // adding infinities
-             ninf, 
+             ninf,
              0, "Add #43");
 
         test(field.newDfp("0").add(ninf), // adding infinities
-             ninf, 
+             ninf,
              0, "Add #44");
 
         test(field.newDfp("-1e131071").add(ninf), // adding infinities
-             ninf, 
+             ninf,
              0, "Add #45");
 
         test(field.newDfp("1e131071").add(ninf), // adding infinities
-             ninf, 
+             ninf,
              0, "Add #46");
 
         test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("5e131051")),
 // overflow
@@ -345,19 +345,19 @@ public class DfpTest {
 
         test(field.newDfp("-0").add(field.newDfp("-0")),
              field.newDfp("-0"),
-             0, "Add #56"); 
+             0, "Add #56");
 
         test(field.newDfp("0").add(field.newDfp("-0")),
              field.newDfp("0"),
-             0, "Add #57"); 
+             0, "Add #57");
 
         test(field.newDfp("-0").add(field.newDfp("0")),
              field.newDfp("0"),
-             0, "Add #58"); 
+             0, "Add #58");
 
         test(field.newDfp("0").add(field.newDfp("0")),
              field.newDfp("0"),
-             0, "Add #59"); 
+             0, "Add #59");
     }
 
     ////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -403,7 +403,7 @@ public class DfpTest {
         cmptst(field.newDfp("-1"), field.newDfp("0"), "equal", false, 7);        // -1 ==
0
         cmptst(field.newDfp("0"), field.newDfp("-1"), "equal", false, 8);        // 0 ==
-1
         cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "equal", false, 9); // 0 ==
1e-131072
-        // check flags 
+        // check flags
         if (field.getIEEEFlags() != 0)
             Assert.fail("assersion failed.  compare flags = "+field.getIEEEFlags());
 
@@ -491,7 +491,7 @@ public class DfpTest {
         cmptst(field.newDfp("-1"), field.newDfp("0"), "unequal", true, 7);        // -1 ==
0
         cmptst(field.newDfp("0"), field.newDfp("-1"), "unequal", true, 8);        // 0 ==
-1
         cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "unequal", true, 9); // 0 ==
1e-131072
-        // check flags 
+        // check flags
         if (field.getIEEEFlags() != 0)
             Assert.fail("assersion failed.  compare flags = "+field.getIEEEFlags());
 
@@ -582,7 +582,7 @@ public class DfpTest {
         cmptst(field.newDfp("-1"), field.newDfp("0"), "lessThan", true, 7);        // -1
< 0
         cmptst(field.newDfp("0"), field.newDfp("-1"), "lessThan", false, 8);        // 0
< -1
         cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "lessThan", true, 9); // 0 <
1e-131072
-        // check flags 
+        // check flags
         if (field.getIEEEFlags() != 0)
             Assert.fail("assersion failed.  compare flags = "+field.getIEEEFlags());
 
@@ -673,7 +673,7 @@ public class DfpTest {
         cmptst(field.newDfp("-1"), field.newDfp("0"), "greaterThan", false, 7);        //
-1 > 0
         cmptst(field.newDfp("0"), field.newDfp("-1"), "greaterThan", true, 8);        //
0 > -1
         cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "greaterThan", false, 9); //
0 > 1e-131072
-        // check flags 
+        // check flags
         if (field.getIEEEFlags() != 0)
             Assert.fail("assersion failed.  compare flags = "+field.getIEEEFlags());
 
@@ -756,62 +756,62 @@ public class DfpTest {
     public void testMultiply()
     {
         test(field.newDfp("1").multiply(field.newDfp("1")),      // Basic tests   1*1 = 1
-             field.newDfp("1"), 
+             field.newDfp("1"),
              0, "Multiply #1");
 
         test(field.newDfp("1").multiply(1),             // Basic tests   1*1 = 1
-             field.newDfp("1"), 
+             field.newDfp("1"),
              0, "Multiply #2");
 
         test(field.newDfp("-1").multiply(field.newDfp("1")),     // Basic tests   -1*1 =
-1
-             field.newDfp("-1"), 
+             field.newDfp("-1"),
              0, "Multiply #3");
 
         test(field.newDfp("-1").multiply(1),            // Basic tests   -1*1 = -1
-             field.newDfp("-1"), 
+             field.newDfp("-1"),
              0, "Multiply #4");
 
         // basic tests with integers
         test(field.newDfp("2").multiply(field.newDfp("3")),
-             field.newDfp("6"), 
+             field.newDfp("6"),
              0, "Multiply #5");
 
         test(field.newDfp("2").multiply(3),
-             field.newDfp("6"), 
+             field.newDfp("6"),
              0, "Multiply #6");
 
         test(field.newDfp("-2").multiply(field.newDfp("3")),
-             field.newDfp("-6"), 
+             field.newDfp("-6"),
              0, "Multiply #7");
 
         test(field.newDfp("-2").multiply(3),
-             field.newDfp("-6"), 
+             field.newDfp("-6"),
              0, "Multiply #8");
 
         test(field.newDfp("2").multiply(field.newDfp("-3")),
-             field.newDfp("-6"), 
+             field.newDfp("-6"),
              0, "Multiply #9");
 
         test(field.newDfp("-2").multiply(field.newDfp("-3")),
-             field.newDfp("6"), 
+             field.newDfp("6"),
              0, "Multiply #10");
 
         //multiply by zero
 
         test(field.newDfp("-2").multiply(field.newDfp("0")),
-             field.newDfp("-0"), 
+             field.newDfp("-0"),
              0, "Multiply #11");
 
         test(field.newDfp("-2").multiply(0),
-             field.newDfp("-0"), 
+             field.newDfp("-0"),
              0, "Multiply #12");
 
         test(field.newDfp("2").multiply(field.newDfp("0")),
-             field.newDfp("0"), 
+             field.newDfp("0"),
              0, "Multiply #13");
 
         test(field.newDfp("2").multiply(0),
-             field.newDfp("0"), 
+             field.newDfp("0"),
              0, "Multiply #14");
 
         test(field.newDfp("2").multiply(pinf),
@@ -836,19 +836,19 @@ public class DfpTest {
 
         test(field.newDfp("5e131071").multiply(2),
              pinf,
-             DfpField.FLAG_OVERFLOW, "Multiply #19");        
+             DfpField.FLAG_OVERFLOW, "Multiply #19");
 
         test(field.newDfp("5e131071").multiply(field.newDfp("1.999999999999999")),
              field.newDfp("9.9999999999999950000e131071"),
-             0, "Multiply #20");        
+             0, "Multiply #20");
 
         test(field.newDfp("-5e131071").multiply(2),
              ninf,
-             DfpField.FLAG_OVERFLOW, "Multiply #22");        
+             DfpField.FLAG_OVERFLOW, "Multiply #22");
 
         test(field.newDfp("-5e131071").multiply(field.newDfp("1.999999999999999")),
              field.newDfp("-9.9999999999999950000e131071"),
-             0, "Multiply #23");        
+             0, "Multiply #23");
 
         test(field.newDfp("1e-65539").multiply(field.newDfp("1e-65539")),
              field.newDfp("1e-131078"),
@@ -919,11 +919,11 @@ public class DfpTest {
     public void testDivide()
     {
         test(field.newDfp("1").divide(nan),      // divide by NaN = NaN
-             nan, 
+             nan,
              0, "Divide #1");
 
         test(nan.divide(field.newDfp("1")),      // NaN / number = NaN
-             nan, 
+             nan,
              0, "Divide #2");
 
         test(pinf.divide(field.newDfp("1")),
@@ -1036,10 +1036,50 @@ public class DfpTest {
     }
 
     @Test
+    public void testReciprocal()
+    {
+        test(nan.reciprocal(),
+             nan,
+             0, "Reciprocal #1");
+
+        test(field.newDfp("0").reciprocal(),
+             pinf,
+             DfpField.FLAG_DIV_ZERO, "Reciprocal #2");
+
+        test(field.newDfp("-0").reciprocal(),
+             ninf,
+             DfpField.FLAG_DIV_ZERO, "Reciprocal #3");
+
+        test(field.newDfp("3").reciprocal(),
+             field.newDfp("0.33333333333333333333"),
+             DfpField.FLAG_INEXACT, "Reciprocal #4");
+
+        test(field.newDfp("6").reciprocal(),
+             field.newDfp("0.16666666666666666667"),
+             DfpField.FLAG_INEXACT, "Reciprocal #5");
+
+        test(field.newDfp("1").reciprocal(),
+             field.newDfp("1"),
+             0, "Reciprocal #6");
+
+        test(field.newDfp("-1").reciprocal(),
+             field.newDfp("-1"),
+             0, "Reciprocal #7");
+
+        test(pinf.reciprocal(),
+             field.newDfp("0"),
+             0, "Reciprocal #8");
+
+        test(ninf.reciprocal(),
+             field.newDfp("-0"),
+             0, "Reciprocal #9");
+    }
+
+    @Test
     public void testDivideInt()
     {
         test(nan.divide(1),      // NaN / number = NaN
-             nan, 
+             nan,
              0, "DivideInt #1");
 
         test(pinf.divide(1),
@@ -1158,7 +1198,7 @@ public class DfpTest {
              field.newDfp("-0"),
              DfpField.FLAG_UNDERFLOW|DfpField.FLAG_INEXACT, "Next After #12");
 
-        test(field.newDfp("1e-131092").nextAfter(ninf), 
+        test(field.newDfp("1e-131092").nextAfter(ninf),
              field.newDfp("0"),
              DfpField.FLAG_UNDERFLOW|DfpField.FLAG_INEXACT, "Next After #13");
 

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/BigRealTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/BigRealTest.java?rev=1180312&r1=1180311&r2=1180312&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/BigRealTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/BigRealTest.java
Sat Oct  8 04:21:00 2011
@@ -16,7 +16,6 @@
  */
 package org.apache.commons.math.util;
 
-
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.math.MathContext;
@@ -29,23 +28,46 @@ public class BigRealTest {
 
     @Test
     public void testConstructor() {
-        Assert.assertEquals(1.625, new BigReal(new BigDecimal("1.625")).doubleValue(), 1.0e-15);
-        Assert.assertEquals(-5.0, new BigReal(new BigInteger("-5")).doubleValue(), 1.0e-15);
-        Assert.assertEquals(-5.0, new BigReal(new BigInteger("-5"), MathContext.DECIMAL64).doubleValue(),
1.0e-15);
-        Assert.assertEquals(0.125, new BigReal(new BigInteger("125"), 3).doubleValue(), 1.0e-15);
-        Assert.assertEquals(0.125, new BigReal(new BigInteger("125"), 3, MathContext.DECIMAL64).doubleValue(),
1.0e-15);
-        Assert.assertEquals(1.625, new BigReal(new char[] { '1', '.', '6', '2', '5' }).doubleValue(),
1.0e-15);
-        Assert.assertEquals(1.625, new BigReal(new char[] { 'A', 'A', '1', '.', '6', '2',
'5', '9' }, 2, 5).doubleValue(), 1.0e-15);
-        Assert.assertEquals(1.625, new BigReal(new char[] { 'A', 'A', '1', '.', '6', '2',
'5', '9' }, 2, 5, MathContext.DECIMAL64).doubleValue(), 1.0e-15);
-        Assert.assertEquals(1.625, new BigReal(new char[] { '1', '.', '6', '2', '5' }, MathContext.DECIMAL64).doubleValue(),
1.0e-15);
+        Assert.assertEquals(1.625,
+                            new BigReal(new BigDecimal("1.625")).doubleValue(),
+                            1.0e-15);
+        Assert.assertEquals(-5.0,
+                            new BigReal(new BigInteger("-5")).doubleValue(),
+                            1.0e-15);
+        Assert.assertEquals(-5.0, new BigReal(new BigInteger("-5"),
+                                              MathContext.DECIMAL64)
+            .doubleValue(), 1.0e-15);
+        Assert
+            .assertEquals(0.125,
+                          new BigReal(new BigInteger("125"), 3).doubleValue(),
+                          1.0e-15);
+        Assert.assertEquals(0.125, new BigReal(new BigInteger("125"), 3,
+                                               MathContext.DECIMAL64)
+            .doubleValue(), 1.0e-15);
+        Assert.assertEquals(1.625, new BigReal(new char[] {
+            '1', '.', '6', '2', '5'
+        }).doubleValue(), 1.0e-15);
+        Assert.assertEquals(1.625, new BigReal(new char[] {
+            'A', 'A', '1', '.', '6', '2', '5', '9'
+        }, 2, 5).doubleValue(), 1.0e-15);
+        Assert.assertEquals(1.625, new BigReal(new char[] {
+            'A', 'A', '1', '.', '6', '2', '5', '9'
+        }, 2, 5, MathContext.DECIMAL64).doubleValue(), 1.0e-15);
+        Assert.assertEquals(1.625, new BigReal(new char[] {
+            '1', '.', '6', '2', '5'
+        }, MathContext.DECIMAL64).doubleValue(), 1.0e-15);
         Assert.assertEquals(1.625, new BigReal(1.625).doubleValue(), 1.0e-15);
-        Assert.assertEquals(1.625, new BigReal(1.625, MathContext.DECIMAL64).doubleValue(),
1.0e-15);
+        Assert.assertEquals(1.625, new BigReal(1.625, MathContext.DECIMAL64)
+            .doubleValue(), 1.0e-15);
         Assert.assertEquals(-5.0, new BigReal(-5).doubleValue(), 1.0e-15);
-        Assert.assertEquals(-5.0, new BigReal(-5, MathContext.DECIMAL64).doubleValue(), 1.0e-15);
+        Assert.assertEquals(-5.0, new BigReal(-5, MathContext.DECIMAL64)
+            .doubleValue(), 1.0e-15);
         Assert.assertEquals(-5.0, new BigReal(-5l).doubleValue(), 1.0e-15);
-        Assert.assertEquals(-5.0, new BigReal(-5l, MathContext.DECIMAL64).doubleValue(),
1.0e-15);
+        Assert.assertEquals(-5.0, new BigReal(-5l, MathContext.DECIMAL64)
+            .doubleValue(), 1.0e-15);
         Assert.assertEquals(1.625, new BigReal("1.625").doubleValue(), 1.0e-15);
-        Assert.assertEquals(1.625, new BigReal("1.625", MathContext.DECIMAL64).doubleValue(),
1.0e-15);
+        Assert.assertEquals(1.625, new BigReal("1.625", MathContext.DECIMAL64)
+            .doubleValue(), 1.0e-15);
     }
 
     @Test
@@ -72,7 +94,16 @@ public class BigRealTest {
     public void testSubtract() {
         BigReal a = new BigReal("1.2345678");
         BigReal b = new BigReal("8.7654321");
-        Assert.assertEquals( -7.5308643, a.subtract(b).doubleValue(), 1.0e-15);
+        Assert.assertEquals(-7.5308643, a.subtract(b).doubleValue(), 1.0e-15);
+    }
+
+    @Test
+    public void testNegate() {
+        BigReal a = new BigReal("1.2345678");
+        BigReal zero = new BigReal("0.0000000");
+        Assert.assertEquals(a.negate().add(a), zero);
+        Assert.assertEquals(a.add(a.negate()), zero);
+        Assert.assertEquals(zero, zero.negate());
     }
 
     @Test
@@ -83,6 +114,18 @@ public class BigRealTest {
     }
 
     @Test
+    public void testReciprocal() {
+        BigReal a = new BigReal("1.2345678");
+        double eps = FastMath.pow(10., -a.getScale());
+        BigReal one = new BigReal("1.0000000");
+        BigReal b = a.reciprocal();
+        BigReal r = one.subtract(a.multiply(b));
+        Assert.assertTrue(FastMath.abs(r.doubleValue()) <= eps);
+        r = one.subtract(b.multiply(a));
+        Assert.assertTrue(FastMath.abs(r.doubleValue()) <= eps);
+    }
+
+    @Test
     public void testMultiply() {
         BigReal a = new BigReal("1024.0");
         BigReal b = new BigReal("0.0009765625");
@@ -98,9 +141,11 @@ public class BigRealTest {
 
     @Test
     public void testBigDecimalValue() {
-        BigDecimal pi = new BigDecimal("3.1415926535897932384626433832795028841971693993751");
+        BigDecimal pi = new BigDecimal(
+                                       "3.1415926535897932384626433832795028841971693993751");
         Assert.assertEquals(pi, new BigReal(pi).bigDecimalValue());
-        Assert.assertEquals(new BigDecimal(0.5), new BigReal(1.0 / 2.0).bigDecimalValue());
+        Assert.assertEquals(new BigDecimal(0.5),
+                            new BigReal(1.0 / 2.0).bigDecimalValue());
     }
 
     @Test
@@ -121,13 +166,11 @@ public class BigRealTest {
     @Test
     public void testSerial() {
         BigReal[] Reals = {
-            new BigReal(3.0), BigReal.ONE, BigReal.ZERO,
-            new BigReal(17), new BigReal(FastMath.PI),
-            new BigReal(-2.5)
+            new BigReal(3.0), BigReal.ONE, BigReal.ZERO, new BigReal(17),
+            new BigReal(FastMath.PI), new BigReal(-2.5)
         };
         for (BigReal Real : Reals) {
             Assert.assertEquals(Real, TestUtils.serializeAndRecover(Real));
         }
     }
-
 }



Mime
View raw message