diff --git a/src/java/org/apache/commons/math/ode/nonstiff/DormandPrince853StepInterpolator.java b/src/java/org/apache/commons/math/ode/nonstiff/DormandPrince853StepInterpolator.java index 9c2b8baf2..fb598b6ba 100644 --- a/src/java/org/apache/commons/math/ode/nonstiff/DormandPrince853StepInterpolator.java +++ b/src/java/org/apache/commons/math/ode/nonstiff/DormandPrince853StepInterpolator.java @@ -259,8 +259,9 @@ class DormandPrince853StepInterpolator } catch (DerivativeException e) { throw MathRuntimeException.createIOException(e); } - out.writeInt(currentState.length); - for (int i = 0; i < currentState.length; ++i) { + final int dimension = (currentState == null) ? -1 : currentState.length; + out.writeInt(dimension); + for (int i = 0; i < dimension; ++i) { out.writeDouble(yDotKLast[0][i]); out.writeDouble(yDotKLast[1][i]); out.writeDouble(yDotKLast[2][i]); @@ -279,9 +280,9 @@ class DormandPrince853StepInterpolator // read the local attributes yDotKLast = new double[3][]; final int dimension = in.readInt(); - yDotKLast[0] = new double[dimension]; - yDotKLast[1] = new double[dimension]; - yDotKLast[2] = new double[dimension]; + yDotKLast[0] = (dimension < 0) ? null : new double[dimension]; + yDotKLast[1] = (dimension < 0) ? null : new double[dimension]; + yDotKLast[2] = (dimension < 0) ? null : new double[dimension]; for (int i = 0; i < dimension; ++i) { yDotKLast[0][i] = in.readDouble(); diff --git a/src/java/org/apache/commons/math/ode/nonstiff/GraggBulirschStoerStepInterpolator.java b/src/java/org/apache/commons/math/ode/nonstiff/GraggBulirschStoerStepInterpolator.java index 2677d799b..ab05356ab 100644 --- a/src/java/org/apache/commons/math/ode/nonstiff/GraggBulirschStoerStepInterpolator.java +++ b/src/java/org/apache/commons/math/ode/nonstiff/GraggBulirschStoerStepInterpolator.java @@ -358,7 +358,7 @@ class GraggBulirschStoerStepInterpolator public void writeExternal(final ObjectOutput out) throws IOException { - final int dimension = currentState.length; + final int dimension = (currentState == null) ? -1 : currentState.length; // save the state of the base class writeBaseExternal(out); @@ -380,7 +380,7 @@ class GraggBulirschStoerStepInterpolator // read the base class final double t = readBaseExternal(in); - final int dimension = currentState.length; + final int dimension = (currentState == null) ? -1 : currentState.length; // read the local attributes final int degree = in.readInt(); diff --git a/src/java/org/apache/commons/math/ode/nonstiff/RungeKuttaStepInterpolator.java b/src/java/org/apache/commons/math/ode/nonstiff/RungeKuttaStepInterpolator.java index 60c5078a6..0cf959b07 100644 --- a/src/java/org/apache/commons/math/ode/nonstiff/RungeKuttaStepInterpolator.java +++ b/src/java/org/apache/commons/math/ode/nonstiff/RungeKuttaStepInterpolator.java @@ -133,9 +133,11 @@ abstract class RungeKuttaStepInterpolator writeBaseExternal(out); // save the local attributes - out.writeInt(yDotK.length); - for (int k = 0; k < yDotK.length; ++k) { - for (int i = 0; i < currentState.length; ++i) { + final int n = (currentState == null) ? -1 : currentState.length; + final int kMax = (yDotK == null) ? -1 : yDotK.length; + out.writeInt(kMax); + for (int k = 0; k < kMax; ++k) { + for (int i = 0; i < n; ++i) { out.writeDouble(yDotK[k][i]); } } @@ -153,22 +155,27 @@ abstract class RungeKuttaStepInterpolator final double t = readBaseExternal(in); // read the local attributes + final int n = (currentState == null) ? -1 : currentState.length; final int kMax = in.readInt(); - yDotK = new double[kMax][]; + yDotK = (kMax < 0) ? null : new double[kMax][]; for (int k = 0; k < kMax; ++k) { - yDotK[k] = new double[currentState.length]; - for (int i = 0; i < currentState.length; ++i) { + yDotK[k] = (n < 0) ? null : new double[n]; + for (int i = 0; i < n; ++i) { yDotK[k][i] = in.readDouble(); } } equations = null; - try { - // we can now set the interpolated time and state - setInterpolatedTime(t); - } catch (DerivativeException e) { - throw MathRuntimeException.createIOException(e); + if (currentState != null) { + try { + // we can now set the interpolated time and state + setInterpolatedTime(t); + } catch (DerivativeException e) { + throw MathRuntimeException.createIOException(e); + } + } else { + interpolatedTime = t; } } diff --git a/src/java/org/apache/commons/math/ode/sampling/AbstractStepInterpolator.java b/src/java/org/apache/commons/math/ode/sampling/AbstractStepInterpolator.java index e9077d756..68abd232d 100644 --- a/src/java/org/apache/commons/math/ode/sampling/AbstractStepInterpolator.java +++ b/src/java/org/apache/commons/math/ode/sampling/AbstractStepInterpolator.java @@ -351,14 +351,20 @@ public abstract class AbstractStepInterpolator protected void writeBaseExternal(final ObjectOutput out) throws IOException { - out.writeInt(currentState.length); + if (currentState == null) { + out.writeInt(-1); + } else { + out.writeInt(currentState.length); + } out.writeDouble(previousTime); out.writeDouble(currentTime); out.writeDouble(h); out.writeBoolean(forward); - for (int i = 0; i < currentState.length; ++i) { - out.writeDouble(currentState[i]); + if (currentState != null) { + for (int i = 0; i < currentState.length; ++i) { + out.writeDouble(currentState[i]); + } } out.writeDouble(interpolatedTime); @@ -393,15 +399,19 @@ public abstract class AbstractStepInterpolator h = in.readDouble(); forward = in.readBoolean(); - currentState = new double[dimension]; - for (int i = 0; i < currentState.length; ++i) { - currentState[i] = in.readDouble(); + if (dimension < 0) { + currentState = null; + } else { + currentState = new double[dimension]; + for (int i = 0; i < currentState.length; ++i) { + currentState[i] = in.readDouble(); + } } // we do NOT handle the interpolated time and state here interpolatedTime = Double.NaN; - interpolatedState = new double[dimension]; - interpolatedDerivatives = new double[dimension]; + interpolatedState = (dimension < 0) ? null : new double[dimension]; + interpolatedDerivatives = (dimension < 0) ? null : new double[dimension]; finalized = true;