commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r348791 - in /jakarta/commons/proper/math/branches/MATH_1_1: src/java/org/apache/commons/math/analysis/NewtonSolver.java src/test/org/apache/commons/math/analysis/NewtonSolverTest.java xdocs/changes.xml
Date Thu, 24 Nov 2005 19:50:40 GMT
Author: psteitz
Date: Thu Nov 24 11:50:36 2005
New Revision: 348791

URL: http://svn.apache.org/viewcvs?rev=348791&view=rev
Log:
Made NewtonSolver derivative field transient and implemented readObject to
initialize. 
BZ #36491
Reported by FindBugs

Modified:
    jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/analysis/NewtonSolver.java
    jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/analysis/NewtonSolverTest.java
    jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml

Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/analysis/NewtonSolver.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/analysis/NewtonSolver.java?rev=348791&r1=348790&r2=348791&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/analysis/NewtonSolver.java
(original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/analysis/NewtonSolver.java
Thu Nov 24 11:50:36 2005
@@ -16,6 +16,7 @@
 
 package org.apache.commons.math.analysis;
 
+import java.io.IOException;
 import org.apache.commons.math.ConvergenceException;
 import org.apache.commons.math.FunctionEvaluationException; 
 
@@ -33,7 +34,7 @@
     private static final long serialVersionUID = 2606474895443431607L;
     
     /** The first derivative of the target function. */
-    private UnivariateRealFunction derivative;
+    private transient UnivariateRealFunction derivative;
     
     /**
      * Construct a solver for the given function.
@@ -97,5 +98,17 @@
         throw new ConvergenceException
             ("Maximum number of iterations exceeded " + i);
     }
-
+    
+    /**
+     * Custom deserialization to initialize transient deriviate field.
+     * 
+     * @param in serialized object input stream
+     * @throws IOException if IO error occurs 
+     * @throws ClassNotFoundException if instantiation error occurs
+     */
+    private void readObject(java.io.ObjectInputStream in)
+    throws IOException, ClassNotFoundException {
+        in.defaultReadObject();
+        derivative = ((DifferentiableUnivariateRealFunction) f).derivative();
+    }    
 }

Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/analysis/NewtonSolverTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/analysis/NewtonSolverTest.java?rev=348791&r1=348790&r2=348791&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/analysis/NewtonSolverTest.java
(original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/analysis/NewtonSolverTest.java
Thu Nov 24 11:50:36 2005
@@ -18,6 +18,8 @@
 package org.apache.commons.math.analysis;
 
 import org.apache.commons.math.MathException;
+import org.apache.commons.math.TestUtils;
+
 
 import junit.framework.TestCase;
 
@@ -84,5 +86,80 @@
 
         result = solver.solve(0.85, 5);
         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+    }
+    
+    /**
+     * Test Serialization and Recovery
+     */
+    public void testSerialization() throws MathException {
+        DifferentiableUnivariateRealFunction f = new QuinticFunction();
+        double result;
+        
+        NewtonSolver solver = new NewtonSolver(f);
+        NewtonSolver solver2 = (NewtonSolver)TestUtils.serializeAndRecover(solver);
+        
+        result = solver.solve(-0.2, 0.2);
+        assertEquals(result, 0, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(-0.2, 0.2), result, solver2.getAbsoluteAccuracy());
+        
+        result = solver.solve(-0.1, 0.3);
+        assertEquals(result, 0, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(-0.1, 0.3), result, solver2.getAbsoluteAccuracy());
+        
+        result = solver.solve(-0.3, 0.45);
+        assertEquals(result, 0, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(-0.3, 0.45), result, solver2.getAbsoluteAccuracy());
+        
+        result = solver.solve(0.3, 0.7);
+        assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(0.3, 0.7), result, solver2.getAbsoluteAccuracy());
+        
+        result = solver.solve(0.2, 0.6);
+        assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(0.2, 0.6), result, solver2.getAbsoluteAccuracy());
+        
+        result = solver.solve(0.05, 0.95);
+        assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(0.05, 0.95), result, solver2.getAbsoluteAccuracy());
+        
+        result = solver.solve(0.85, 1.25);
+        assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(0.85, 1.25), result, solver2.getAbsoluteAccuracy());
+        
+        result = solver.solve(0.8, 1.2);
+        assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(0.8, 1.2), result, solver2.getAbsoluteAccuracy());
+        
+        result = solver.solve(0.85, 1.75);
+        assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(0.85, 1.75), result, solver2.getAbsoluteAccuracy());
+        
+        result = solver.solve(0.55, 1.45);
+        assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(0.55, 1.45), result, solver2.getAbsoluteAccuracy());
+        
+        result = solver.solve(0.85, 5);
+        assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
+        assertEquals(solver2.solve(0.85, 5), result, solver2.getAbsoluteAccuracy());
+        
+        /* Test Reset */
+        double newValue = 1.0e-2;
+        f = new QuinticFunction();
+        solver = new NewtonSolver(f);
+        
+        double oldValue = solver.getRelativeAccuracy();
+        solver.setRelativeAccuracy(newValue);
+        solver.resetRelativeAccuracy();
+        assertEquals(oldValue, solver.getRelativeAccuracy(), 1.0e-2);
+        
+        solver2 = (NewtonSolver)TestUtils.serializeAndRecover(solver); 
+        
+        assertEquals(oldValue, solver2.getRelativeAccuracy(), 1.0e-2);
+        
+        solver2.setRelativeAccuracy(newValue);
+        solver2.resetRelativeAccuracy();
+        
+        assertEquals(oldValue, solver2.getRelativeAccuracy(), 1.0e-2);
+        
     }
 }

Modified: jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml?rev=348791&r1=348790&r2=348791&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml (original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml Thu Nov 24 11:50:36 2005
@@ -45,13 +45,17 @@
        and numerical utilities, and a PRNG pluggability framework making it
        possible to replace the JDK-supplied random number generator in
        commons-math (and elsewhere) with alternative PRNG implementations.">
-      <action dev="psteitz" type="update">
-        Added polar2Complex method to ComplexUtils to create Complex numbers
-        from polar representations.
+      <action dev="psteitz" type="fix" issue="36491">
+        Made NewtonSolver derivative field transient and implemented readObject to
+        initialize.
       </action>
       <action dev="psteitz" type="fix" issue="36491">
        Made sampleStats field private and changed getUpperBounds to return a fresh
        copy in EmpiricalDistributionImpl.
+      </action>
+      <action dev="psteitz" type="update">
+        Added polar2Complex method to ComplexUtils to create Complex numbers
+        from polar representations.
       </action>
       <action dev="psteitz" type="fix" issue="37162" due-to="Elliotte Harold">
         Made all serialVersionUIDs private.



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message