commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gilles (Commented) (JIRA)" <>
Subject [jira] [Commented] (MATH-742) Please make PolynomialSplineFunction Serializable
Date Thu, 16 Feb 2012 23:17:01 GMT


Gilles commented on MATH-742:

The following is what you need to serialize a {{PolynomialSplineFunction}} object:

import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;

public class SerialPolynomialSpline extends PolynomialSplineFunction
    implements Serializable {

    public SerialPolynomialSpline(double[] knots,
                                  PolynomialFunction[] coeff) {
        super(knots, coeff);

    private Object writeReplace() {
        return new SerializationProxy(this);

    private static class SerializationProxy
        implements Serializable {
        final double[] knots;
        final double[][] coefficients;

        public SerializationProxy() {
            knots = null;
            coefficients = null;
        public SerializationProxy(SerialPolynomialSpline spline) {
            knots = spline.getKnots();

            final PolynomialFunction[] p = spline.getPolynomials();
            coefficients = new double[p.length][];
            for (int i = 0; i < p.length; i++) {
                coefficients[i] = p[i].getCoefficients();

        Object readResolve() {
            final PolynomialFunction[] p = new PolynomialFunction[coefficients.length];
            for (int i = 0; i < p.length; i++) {
                p[i] = new PolynomialFunction(coefficients[i]);
            return new SerialPolynomialSpline(knots, p);

If you only need it for {{PolynomialSplineFunction}}, you'll write that once, you'll test
it (or not) and off you go.
In the case of a library, consistency is an important quality; thus, _we_ would need to write
that for all the classes, and test them all because by implementing "Serializable", we advertise
that the class can be used robustly in any application that would make use of that feature.
We did not make that promise, and one of the developers indeed pointed out that supporting
"Serializable" is not trivial in terms of maintenance.

As a matter fact, your issue raised the question of the CM policy with respect to "Serializable".
I said that it could be that we implement "Serializable" for everything, _in the right way_.
Waiving that point as unrelated to your issue was not very constructive. ;)
However, if you want to go in that direction, you are welcome to contribute.
> Please make PolynomialSplineFunction Serializable
> -------------------------------------------------
>                 Key: MATH-742
>                 URL:
>             Project: Commons Math
>          Issue Type: Improvement
>    Affects Versions: 2.2
>            Reporter: Neil Roeth
>            Priority: Minor
>         Attachments:
> PolynomialSplineFunction is not Serializable, while the very similar PolynomialFunction
class in the same package is. All that needs to be done is to add the import:
> {{import;}}
> and change this:
> {{public class PolynomialSplineFunction implements DifferentiableUnivariateRealFunction}}
> to this:
> {{public class PolynomialSplineFunction implements DifferentiableUnivariateRealFunction,
> I made exactly that modification to a local copy and it serialized successfully.  Before
the change, I got serialization errors.
> Thanks.

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:!default.jspa
For more information on JIRA, see:


View raw message