mirror of
https://github.com/apache/commons-math.git
synced 2025-02-06 18:18:56 +00:00
separated ODEWithJacobians from ParameterizedODE:
when jacobians are already available, there is no need to have a setParameter method git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@919963 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
484fc6330b
commit
7a7eb1dccc
@ -112,7 +112,7 @@ public interface EventHandlerWithJacobians {
|
|||||||
* the step handler itself is called (see below for scheduling). It
|
* the step handler itself is called (see below for scheduling). It
|
||||||
* allows the user to update his internal data to acknowledge the fact
|
* allows the user to update his internal data to acknowledge the fact
|
||||||
* the event has been handled (for example setting a flag in the {@link
|
* the event has been handled (for example setting a flag in the {@link
|
||||||
* org.apache.commons.math.ode.jacobians.ParameterizedODEWithJacobians
|
* org.apache.commons.math.ode.jacobians.ODEWithJacobians
|
||||||
* differential equations} to switch the derivatives computation in
|
* differential equations} to switch the derivatives computation in
|
||||||
* case of discontinuity), or to direct the integrator to either stop
|
* case of discontinuity), or to direct the integrator to either stop
|
||||||
* or continue integration, possibly with a reset state or derivatives.</p>
|
* or continue integration, possibly with a reset state or derivatives.</p>
|
||||||
|
@ -44,7 +44,7 @@ import org.apache.commons.math.ode.sampling.StepInterpolator;
|
|||||||
* problem has dimension n and there are p parameters, the compound problem will
|
* problem has dimension n and there are p parameters, the compound problem will
|
||||||
* have dimension n × (1 + n + k).</p>
|
* have dimension n × (1 + n + k).</p>
|
||||||
* @see ParameterizedODE
|
* @see ParameterizedODE
|
||||||
* @see ParameterizedODEWithJacobians
|
* @see ODEWithJacobians
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
@ -54,7 +54,7 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
private final FirstOrderIntegrator integrator;
|
private final FirstOrderIntegrator integrator;
|
||||||
|
|
||||||
/** Raw equations to integrate. */
|
/** Raw equations to integrate. */
|
||||||
private final ParameterizedODEWithJacobians ode;
|
private final ODEWithJacobians ode;
|
||||||
|
|
||||||
/** Maximal number of evaluations allowed. */
|
/** Maximal number of evaluations allowed. */
|
||||||
private int maxEvaluations;
|
private int maxEvaluations;
|
||||||
@ -73,7 +73,7 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
* @param hP step sizes to use for computing the jacobian df/dp, must have the
|
* @param hP step sizes to use for computing the jacobian df/dp, must have the
|
||||||
* same dimension as the original problem parameters dimension
|
* same dimension as the original problem parameters dimension
|
||||||
* @see #FirstOrderIntegratorWithJacobians(FirstOrderIntegrator,
|
* @see #FirstOrderIntegratorWithJacobians(FirstOrderIntegrator,
|
||||||
* ParameterizedODEWithJacobians)
|
* ODEWithJacobians)
|
||||||
*/
|
*/
|
||||||
public FirstOrderIntegratorWithJacobians(final FirstOrderIntegrator integrator,
|
public FirstOrderIntegratorWithJacobians(final FirstOrderIntegrator integrator,
|
||||||
final ParameterizedODE ode,
|
final ParameterizedODE ode,
|
||||||
@ -93,7 +93,7 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
* ParameterizedODE, double[], double[], double[])
|
* ParameterizedODE, double[], double[], double[])
|
||||||
*/
|
*/
|
||||||
public FirstOrderIntegratorWithJacobians(final FirstOrderIntegrator integrator,
|
public FirstOrderIntegratorWithJacobians(final FirstOrderIntegrator integrator,
|
||||||
final ParameterizedODEWithJacobians ode) {
|
final ODEWithJacobians ode) {
|
||||||
this.integrator = integrator;
|
this.integrator = integrator;
|
||||||
this.ode = ode;
|
this.ode = ode;
|
||||||
setMaxEvaluations(-1);
|
setMaxEvaluations(-1);
|
||||||
@ -107,7 +107,9 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
* @see #clearStepHandlers()
|
* @see #clearStepHandlers()
|
||||||
*/
|
*/
|
||||||
public void addStepHandler(StepHandlerWithJacobians handler) {
|
public void addStepHandler(StepHandlerWithJacobians handler) {
|
||||||
integrator.addStepHandler(new StepHandlerWrapper(handler));
|
final int n = ode.getDimension();
|
||||||
|
final int k = ode.getParametersDimension();
|
||||||
|
integrator.addStepHandler(new StepHandlerWrapper(handler, n, k));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get all the step handlers that have been added to the integrator.
|
/** Get all the step handlers that have been added to the integrator.
|
||||||
@ -149,7 +151,9 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
double maxCheckInterval,
|
double maxCheckInterval,
|
||||||
double convergence,
|
double convergence,
|
||||||
int maxIterationCount) {
|
int maxIterationCount) {
|
||||||
integrator.addEventHandler(new EventHandlerWrapper(handler),
|
final int n = ode.getDimension();
|
||||||
|
final int k = ode.getParametersDimension();
|
||||||
|
integrator.addEventHandler(new EventHandlerWrapper(handler, n, k),
|
||||||
maxCheckInterval, convergence, maxIterationCount);
|
maxCheckInterval, convergence, maxIterationCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -438,7 +442,7 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
|
|
||||||
/** Wrapper class to compute jacobians by finite differences for ODE which do not compute them themselves. */
|
/** Wrapper class to compute jacobians by finite differences for ODE which do not compute them themselves. */
|
||||||
private class FiniteDifferencesWrapper
|
private class FiniteDifferencesWrapper
|
||||||
implements ParameterizedODEWithJacobians {
|
implements ODEWithJacobians {
|
||||||
|
|
||||||
/** Raw ODE without jacobians computation. */
|
/** Raw ODE without jacobians computation. */
|
||||||
private final ParameterizedODE ode;
|
private final ParameterizedODE ode;
|
||||||
@ -487,18 +491,13 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
return ode.getParametersDimension();
|
return ode.getParametersDimension();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
public void setParameter(int i, double value) {
|
|
||||||
ode.setParameter(i, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public void computeJacobians(double t, double[] y, double[] yDot,
|
public void computeJacobians(double t, double[] y, double[] yDot,
|
||||||
double[][] dFdY, double[][] dFdP)
|
double[][] dFdY, double[][] dFdP)
|
||||||
throws DerivativeException {
|
throws DerivativeException {
|
||||||
|
|
||||||
final int n = ode.getDimension();
|
final int n = hY.length;
|
||||||
final int k = ode.getParametersDimension();
|
final int k = hP.length;
|
||||||
|
|
||||||
evaluations += n + k;
|
evaluations += n + k;
|
||||||
if (evaluations > maxEvaluations) {
|
if (evaluations > maxEvaluations) {
|
||||||
@ -531,16 +530,27 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Wrapper for step handlers. */
|
/** Wrapper for step handlers. */
|
||||||
private class StepHandlerWrapper implements StepHandler {
|
private static class StepHandlerWrapper implements StepHandler {
|
||||||
|
|
||||||
/** Underlying step handler with jacobians. */
|
/** Underlying step handler with jacobians. */
|
||||||
private final StepHandlerWithJacobians handler;
|
private final StepHandlerWithJacobians handler;
|
||||||
|
|
||||||
|
/** Dimension of the original ODE. */
|
||||||
|
private final int n;
|
||||||
|
|
||||||
|
/** Number of parameters. */
|
||||||
|
private final int k;
|
||||||
|
|
||||||
/** Simple constructor.
|
/** Simple constructor.
|
||||||
* @param handler underlying step handler with jacobians
|
* @param handler underlying step handler with jacobians
|
||||||
|
* @param n dimension of the original ODE
|
||||||
|
* @param k number of parameters
|
||||||
*/
|
*/
|
||||||
public StepHandlerWrapper(final StepHandlerWithJacobians handler) {
|
public StepHandlerWrapper(final StepHandlerWithJacobians handler,
|
||||||
|
final int n, final int k) {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
|
this.n = n;
|
||||||
|
this.k = k;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the underlying step handler with jacobians.
|
/** Get the underlying step handler with jacobians.
|
||||||
@ -553,10 +563,7 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public void handleStep(StepInterpolator interpolator, boolean isLast)
|
public void handleStep(StepInterpolator interpolator, boolean isLast)
|
||||||
throws DerivativeException {
|
throws DerivativeException {
|
||||||
handler.handleStep(new StepInterpolatorWrapper(interpolator,
|
handler.handleStep(new StepInterpolatorWrapper(interpolator, n, k), isLast);
|
||||||
ode.getDimension(),
|
|
||||||
ode.getParametersDimension()),
|
|
||||||
isLast);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@ -824,7 +831,7 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Wrapper for event handlers. */
|
/** Wrapper for event handlers. */
|
||||||
private class EventHandlerWrapper implements EventHandler {
|
private static class EventHandlerWrapper implements EventHandler {
|
||||||
|
|
||||||
/** Underlying event handler with jacobians. */
|
/** Underlying event handler with jacobians. */
|
||||||
private final EventHandlerWithJacobians handler;
|
private final EventHandlerWithJacobians handler;
|
||||||
@ -840,11 +847,12 @@ public class FirstOrderIntegratorWithJacobians {
|
|||||||
|
|
||||||
/** Simple constructor.
|
/** Simple constructor.
|
||||||
* @param handler underlying event handler with jacobians
|
* @param handler underlying event handler with jacobians
|
||||||
|
* @param n dimension of the original ODE
|
||||||
|
* @param k number of parameters
|
||||||
*/
|
*/
|
||||||
public EventHandlerWrapper(final EventHandlerWithJacobians handler) {
|
public EventHandlerWrapper(final EventHandlerWithJacobians handler,
|
||||||
|
final int n, final int k) {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
final int n = ode.getDimension();
|
|
||||||
final int k = ode.getParametersDimension();
|
|
||||||
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];
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
package org.apache.commons.math.ode.jacobians;
|
package org.apache.commons.math.ode.jacobians;
|
||||||
|
|
||||||
import org.apache.commons.math.ode.DerivativeException;
|
import org.apache.commons.math.ode.DerivativeException;
|
||||||
|
import org.apache.commons.math.ode.FirstOrderDifferentialEquations;
|
||||||
|
|
||||||
|
|
||||||
/** This interface represents {@link ParameterizedODE
|
/** This interface represents {@link ParameterizedODE
|
||||||
@ -29,8 +30,12 @@ import org.apache.commons.math.ode.DerivativeException;
|
|||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface ParameterizedODEWithJacobians
|
public interface ODEWithJacobians extends FirstOrderDifferentialEquations {
|
||||||
extends ParameterizedODE {
|
|
||||||
|
/** Get the number of parameters.
|
||||||
|
* @return number of parameters
|
||||||
|
*/
|
||||||
|
int getParametersDimension();
|
||||||
|
|
||||||
/** Compute the partial derivatives of ODE with respect to state.
|
/** Compute the partial derivatives of ODE with respect to state.
|
||||||
* @param t current value of the independent <I>time</I> variable
|
* @param t current value of the independent <I>time</I> variable
|
@ -275,7 +275,7 @@ public class FirstOrderIntegratorWithJacobiansTest {
|
|||||||
Assert.assertTrue(stopTime < 5.0 * Math.PI);
|
Assert.assertTrue(stopTime < 5.0 * Math.PI);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Brusselator implements ParameterizedODEWithJacobians {
|
private static class Brusselator implements ParameterizedODE, ODEWithJacobians {
|
||||||
|
|
||||||
private double b;
|
private double b;
|
||||||
|
|
||||||
@ -323,7 +323,7 @@ public class FirstOrderIntegratorWithJacobiansTest {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/** ODE representing a point moving on a circle with provided center and angular rate. */
|
/** ODE representing a point moving on a circle with provided center and angular rate. */
|
||||||
private static class Circle implements ParameterizedODEWithJacobians {
|
private static class Circle implements ODEWithJacobians {
|
||||||
|
|
||||||
private final double[] y0;
|
private final double[] y0;
|
||||||
private double cx;
|
private double cx;
|
||||||
@ -341,16 +341,6 @@ public class FirstOrderIntegratorWithJacobiansTest {
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setParameter(int i, double p) {
|
|
||||||
if (i == 0) {
|
|
||||||
cx = p;
|
|
||||||
} else if (i == 1) {
|
|
||||||
cy = p;
|
|
||||||
} else {
|
|
||||||
omega = p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getParametersDimension() {
|
public int getParametersDimension() {
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user