diff --git a/src/java/org/apache/commons/math/analysis/polynomials/PolynomialFunction.java b/src/java/org/apache/commons/math/analysis/polynomials/PolynomialFunction.java index 2285d768a..239695f80 100644 --- a/src/java/org/apache/commons/math/analysis/polynomials/PolynomialFunction.java +++ b/src/java/org/apache/commons/math/analysis/polynomials/PolynomialFunction.java @@ -16,6 +16,9 @@ */ package org.apache.commons.math.analysis.polynomials; +import java.io.Serializable; +import java.util.Arrays; + import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.analysis.UnivariateRealFunction; @@ -28,8 +31,13 @@ import org.apache.commons.math.analysis.UnivariateRealFunction; * * @version $Revision$ $Date$ */ -public class PolynomialFunction implements DifferentiableUnivariateRealFunction { +public class PolynomialFunction implements DifferentiableUnivariateRealFunction, Serializable { + /** + * Serializtion identifier + */ + private static final long serialVersionUID = -7726511984200295583L; + /** * The coefficients of the polynomial, ordered by degree -- i.e., * coefficients[0] is the constant term and coefficients[n] is the @@ -315,4 +323,28 @@ public class PolynomialFunction implements DifferentiableUnivariateRealFunction } + /** {@inheritDoc} */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Arrays.hashCode(coefficients); + return result; + } + + /** {@inheritDoc} */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof PolynomialFunction)) + return false; + PolynomialFunction other = (PolynomialFunction) obj; + if (!Arrays.equals(coefficients, other.coefficients)) + return false; + return true; + } + } diff --git a/src/test/org/apache/commons/math/analysis/SinFunction.java b/src/test/org/apache/commons/math/analysis/SinFunction.java index 5c2d93f9e..be1f5f8c9 100644 --- a/src/test/org/apache/commons/math/analysis/SinFunction.java +++ b/src/test/org/apache/commons/math/analysis/SinFunction.java @@ -16,8 +16,6 @@ */ package org.apache.commons.math.analysis; -import java.io.Serializable; - import org.apache.commons.math.FunctionEvaluationException; /** diff --git a/src/test/org/apache/commons/math/analysis/polynomials/PolynomialFunctionTest.java b/src/test/org/apache/commons/math/analysis/polynomials/PolynomialFunctionTest.java index 3d3d7589c..7f8224740 100644 --- a/src/test/org/apache/commons/math/analysis/polynomials/PolynomialFunctionTest.java +++ b/src/test/org/apache/commons/math/analysis/polynomials/PolynomialFunctionTest.java @@ -18,7 +18,7 @@ package org.apache.commons.math.analysis.polynomials; // commons-math import org.apache.commons.math.MathException; - +import org.apache.commons.math.TestUtils; // junit import junit.framework.TestCase; @@ -226,6 +226,11 @@ public final class PolynomialFunctionTest extends TestCase { } + public void testSerial() { + PolynomialFunction p2 = new PolynomialFunction(new double[] { 3.0, 2.0, 1.0 }); + assertEquals(p2, TestUtils.serializeAndRecover(p2)); + } + public void checkPolynomial(PolynomialFunction p, String reference) { assertEquals(reference, p.toString()); }