fixed handling of the time-derivatives of jacobians

simplified copy and serialization

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@919480 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2010-03-05 16:38:02 +00:00
parent 81e8b4f0d1
commit 4e13f9660b
1 changed files with 108 additions and 55 deletions

View File

@ -437,15 +437,21 @@ public class FirstOrderIntegratorWithJacobians {
/** State array. */ /** State array. */
private double[] y; private double[] y;
/** State derivative array. */
private double[] yDot;
/** Jacobian with respect to initial state dy/dy0. */ /** Jacobian with respect to initial state dy/dy0. */
private double[][] dydy0; private double[][] dydy0;
/** Jacobian with respect to parameters dy/dp. */ /** Jacobian with respect to parameters dy/dp. */
private double[][] dydp; private double[][] dydp;
/** Time derivative of the state array. */
private double[] yDot;
/** Time derivative of the sacobian with respect to initial state dy/dy0. */
private double[][] dydy0Dot;
/** Time derivative of the jacobian with respect to parameters dy/dp. */
private double[][] dydpDot;
/** Simple constructor. /** Simple constructor.
* @param interpolator wrapped interpolator * @param interpolator wrapped interpolator
* @param n dimension of the original ODE * @param n dimension of the original ODE
@ -454,10 +460,12 @@ public class FirstOrderIntegratorWithJacobians {
public StepInterpolatorWrapper(final StepInterpolator interpolator, public StepInterpolatorWrapper(final StepInterpolator interpolator,
final int n, final int k) { final int n, final int k) {
this.interpolator = interpolator; this.interpolator = interpolator;
y = new double[n]; y = new double[n];
yDot = new double[n]; dydy0 = new double[n][n];
dydy0 = new double[n][n]; dydp = new double[n][k];
dydp = new double[n][k]; yDot = new double[n];
dydy0Dot = new double[n][n];
dydpDot = new double[n][k];
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -525,23 +533,23 @@ public class FirstOrderIntegratorWithJacobians {
final int n = y.length; final int n = y.length;
int start = n; int start = n;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
System.arraycopy(extendedDerivatives, start, dydy0[i], 0, n); System.arraycopy(extendedDerivatives, start, dydy0Dot[i], 0, n);
start += n; start += n;
} }
return dydy0; return dydy0Dot;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public double[][] getInterpolatedDyDpDot() throws DerivativeException { public double[][] getInterpolatedDyDpDot() throws DerivativeException {
double[] extendedDerivatives = interpolator.getInterpolatedDerivatives(); double[] extendedDerivatives = interpolator.getInterpolatedDerivatives();
final int n = y.length; final int n = y.length;
final int k = dydp[0].length; final int k = dydpDot[0].length;
int start = n * (n + 1); int start = n * (n + 1);
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
System.arraycopy(extendedDerivatives, start, dydp[i], 0, k); System.arraycopy(extendedDerivatives, start, dydpDot[i], 0, k);
start += k; start += k;
} }
return dydp; return dydpDot;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -555,40 +563,26 @@ public class FirstOrderIntegratorWithJacobians {
final int k = dydp[0].length; final int k = dydp[0].length;
StepInterpolatorWrapper copied = StepInterpolatorWrapper copied =
new StepInterpolatorWrapper(interpolator.copy(), n, k); new StepInterpolatorWrapper(interpolator.copy(), n, k);
System.arraycopy(y, 0, copied.y, 0, n); copyArray(y, copied.y);
System.arraycopy(yDot, 0, copied.yDot, 0, n); copyArray(dydy0, copied.dydy0);
for (int i = 0; i < n; ++i) { copyArray(dydp, copied.dydp);
System.arraycopy(dydy0[i], 0, copied.dydy0[i], 0, n); copyArray(yDot, copied.yDot);
} copyArray(dydy0Dot, copied.dydy0Dot);
for (int i = 0; i < n; ++i) { copyArray(dydpDot, copied.dydpDot);
System.arraycopy(dydp[i], 0, copied.dydp[i], 0, k);
}
return copied; return copied;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public void writeExternal(ObjectOutput out) throws IOException { public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(interpolator); out.writeObject(interpolator);
final int n = y.length; out.writeInt(y.length);
final int k = dydp[0].length; out.writeInt(dydp[0].length);
out.writeInt(n); writeArray(out, y);
out.writeInt(k); writeArray(out, dydy0);
for (int i = 0; i < n; ++i) { writeArray(out, dydp);
out.writeDouble(y[i]); writeArray(out, yDot);
} writeArray(out, dydy0Dot);
for (int i = 0; i < n; ++i) { writeArray(out, dydpDot);
out.writeDouble(yDot[i]);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
out.writeDouble(dydy0[i][j]);
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
out.writeDouble(dydp[i][j]);
}
}
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -596,24 +590,83 @@ public class FirstOrderIntegratorWithJacobians {
interpolator = (StepInterpolator) in.readObject(); interpolator = (StepInterpolator) in.readObject();
final int n = in.readInt(); final int n = in.readInt();
final int k = in.readInt(); final int k = in.readInt();
y = new double[n]; y = new double[n];
dydy0 = new double[n][n]; dydy0 = new double[n][n];
dydp = new double[n][k]; dydp = new double[n][k];
for (int i = 0; i < n; ++i) { yDot = new double[n];
y[i] = in.readDouble(); dydy0Dot = new double[n][n];
dydpDot = new double[n][k];
readArray(in, y);
readArray(in, dydy0);
readArray(in, dydp);
readArray(in, yDot);
readArray(in, dydy0Dot);
readArray(in, dydpDot);
}
/** Copy an array.
* @param src source array
* @param dest destination array
*/
private static void copyArray(final double[] src, final double[] dest) {
System.arraycopy(src, 0, dest, 0, src.length);
}
/** Copy an array.
* @param src source array
* @param dest destination array
*/
private static void copyArray(final double[][] src, final double[][] dest) {
for (int i = 0; i < src.length; ++i) {
copyArray(src[i], dest[i]);
} }
for (int i = 0; i < n; ++i) { }
yDot[i] = in.readDouble();
/** Write an array.
* @param out output stream
* @param array array to write
* @exception IOException if array cannot be read
*/
private static void writeArray(final ObjectOutput out, final double[] array)
throws IOException {
for (int i = 0; i < array.length; ++i) {
out.writeDouble(array[i]);
} }
for (int i = 0; i < n; ++i) { }
for (int j = 0; j < n; ++j) {
dydy0[i][j] = in.readDouble(); /** Write an array.
} * @param out output stream
* @param array array to write
* @exception IOException if array cannot be read
*/
private static void writeArray(final ObjectOutput out, final double[][] array)
throws IOException {
for (int i = 0; i < array.length; ++i) {
writeArray(out, array[i]);
} }
for (int i = 0; i < n; ++i) { }
for (int j = 0; j < k; ++j) {
dydp[i][j] = in.readDouble(); /** Read an array.
} * @param in input stream
* @param array array to read
* @exception IOException if array cannot be read
*/
private static void readArray(final ObjectInput in, final double[] array)
throws IOException {
for (int i = 0; i < array.length; ++i) {
array[i] = in.readDouble();
}
}
/** Read an array.
* @param in input stream
* @param array array to read
* @exception IOException if array cannot be read
*/
private static void readArray(final ObjectInput in, final double[][] array)
throws IOException {
for (int i = 0; i < array.length; ++i) {
readArray(in, array[i]);
} }
} }