(agi.foundation.core-2022r2.jar)

## Class RungeKuttaAlgorithm

• All Implemented Interfaces:

```public class RungeKuttaAlgorithm
extends Object

The basic Runge-Kutta integration algorithm used by the integrators, it's Butcher Tableau, and the derivative information computed during integration.

y[n+1] = y[n] + h * Sum(i=0 to s){ b[i]*k[i] }

k[i] = f(t[n] + c[i]*h, y[n] + Sum(j=0 to i-1){ a[i,j] * k[j] } )

• ### Constructor Summary

Constructors
Modifier Constructor and Description
` ` ```RungeKuttaAlgorithm(double[] stages, double[] weights, double[][] coefficients)```
Initializes a new instance of a Runge-Kutta algorithm with the given Butcher Tableau.
`protected ` ```RungeKuttaAlgorithm(RungeKuttaAlgorithm existingInstance, CopyContext context)```
Initializes a new instance as a copy of an existing instance.
• ### Method Summary

All Methods
Modifier and Type Method and Description
`Object` `clone(CopyContext context)`
Clones this object using the specified context.
`double[][]` `getCoefficients()`
Gets the coefficients "a".
`DependentVariableDerivatives` `getDerivativeFunction()`
Gets the delegate which defines the first order differential equation representing the derivative of the dependent variables as a function of independent variable and dependent variables.
`double[][]` `getDerivatives()`
Gets the derivative information used in the algorithm.
`boolean` `getIsThreadSafe()`
Gets a value indicating whether the methods on this instance are safe to call from multiple threads simultaneously.
`int` `getNumberOfStages()`
Gets the total number of stages in this instance of the Runge-Kutta integrator.
`double[]` `getStages()`
Gets the coefficients "c".
`double[]` `getWeights()`
Gets the coefficients "b".
`protected double[]` `getWorkingArray()`
Gets the working array used as a means to efficiently update the intermediate state information when calling the derivative `DerivativeFunction` (`get` / `set`).
`void` ```initialize(int dimension, DependentVariableDerivatives function)```
Initializes the derivative `DerivativeFunction` (`get` / `set`) and the working arrays used during integration.
`void` ```integrate(double step, double initialIndependentVariableValue, double[] initialDependentVariableValues, double[] finalDependentVariableValues)```
Compute an integration step and update the final dependent variables and store the `Derivatives` (`get`).
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Constructor Detail

• #### RungeKuttaAlgorithm

```public RungeKuttaAlgorithm(double[] stages,
double[] weights,
double[][] coefficients)```

Initializes a new instance of a Runge-Kutta algorithm with the given Butcher Tableau.

y[n+1] = y[n] + h * Sum(i=0 to s){ b[i]*k[i] }

k[i] = f(t[n] + c[i]*h, y[n] + Sum(j=0 to i-1){ a[i,j] * k[j] } )

Parameters:
`stages` - The coefficients "c". These represent the factors multiplying the step size by which to advance the independent variable at each stage.
`weights` - The coefficients "b". These represent the coefficients of the "k" stages summed together to represent the slope.
`coefficients` - The coefficients "a". These represent the factors multiplying the derivatives to provide the intermediate state information.
• #### RungeKuttaAlgorithm

```protected RungeKuttaAlgorithm(@Nonnull
RungeKuttaAlgorithm existingInstance,
@Nonnull
CopyContext context)```
Initializes a new instance as a copy of an existing instance.

See `ICloneWithContext.clone(CopyContext)` for more information about how to implement this constructor in a derived class.

Parameters:
`existingInstance` - The existing instance to copy.
`context` - A `CopyContext` that controls the depth of the copy.
Throws:
`ArgumentNullException` - Thrown when `existingInstance` or `context` is `null`.
• ### Method Detail

• #### clone

`public final Object clone(CopyContext context)`
Clones this object using the specified context.

This method should be implemented to call a copy constructor on the class of the object being cloned. The copy constructor should take the `CopyContext` as a parameter in addition to the existing instance to copy. The copy constructor should first call `CopyContext.addObjectMapping(T, T)` to identify the newly constructed instance as a copy of the existing instance. It should then copy all fields, using `CopyContext.updateReference(T)` to copy any reference fields.

A typical implementation of `ICloneWithContext`:

``````public static class MyClass implements ICloneWithContext {
public MyClass(MyClass existingInstance, CopyContext context) {
someReference = context.updateReference(existingInstance.someReference);
}

@Override
public final Object clone(CopyContext context) {
return new MyClass(this, context);
}

private Object someReference;
}``````

In general, all fields that are reference types should be copied with a call to `CopyContext.updateReference(T)`. There are a couple of exceptions:

1. Fields that are aggregate parts of the object should always be copied. A referenced object is an aggregate if properties or methods on the object being copied can modify the externally-visible value of the referenced object.
2. If the semantics of the referenced object require that it have a single parent, owner, context, etc., and the object being copied is that parent, owner, or context, then the referenced object should always be copied.

If one of these exceptions applies, the `CopyContext` should be given an opportunity to update the reference before the reference is copied explicitly. Use `CopyContext.updateReference(T)` to update the reference. If `CopyContext.updateReference(T)` returns the original object, indicating that the context does not have a replacement registered, then copy the object manually by invoking a Clone method, a copy constructor, or by manually constructing a new instance and copying the values.

``````alwaysCopy = context.updateReference(existingInstance.alwaysCopy);
if (existingInstance.alwaysCopy != null && alwaysCopy == existingInstance.alwaysCopy) {
alwaysCopy = (AlwaysCopy) existingInstance.alwaysCopy.clone(context);
}``````

If you are implementing an evaluator (a class that implements `IEvaluator`), the `IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)` method shares some responsibilities with the copy context constructor. Code duplication can be avoided by doing the following:

1. For references to other evaluators ONLY, simply assign the reference in the constructor instead of calling `CopyContext.updateReference(T)`. You should still call `CopyContext.updateReference(T)` on any references to non-evaluators.
2. Call `IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)` as the last line in the constructor and pass it the same `CopyContext` passed to the constructor.
3. Implement `IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)` as normal. See the reference documentation for `IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)` for more information on implementing that method.
``````public MyClass(MyClass existingInstance, CopyContext context) {
super(existingInstance, context);
someReference = context.updateReference(existingInstance.someReference);
evaluatorReference = existingInstance.evaluatorReference;
updateEvaluatorReferences(context);
}

@Override
public void updateEvaluatorReferences(CopyContext context) {
evaluatorReference = context.updateReference(evaluatorReference);
}

@Override
public Object clone(CopyContext context) {
return new MyClass(this, context);
}

private Object someReference;
private IEvaluator evaluatorReference;``````
Specified by:
`clone` in interface `ICloneWithContext`
Parameters:
`context` - The context to use to perform the copy.
Returns:
A new instance which is a copy of this object.

`public final boolean getIsThreadSafe()`
Gets a value indicating whether the methods on this instance are safe to call from multiple threads simultaneously.

If this property is `true`, all methods and properties are guaranteed to be thread safe. Conceptually, an object that returns `true` for this method acts as if there is a lock protecting each method and property such that only one thread at a time can be inside any method or property in the class. In reality, such locks are generally not used and are in fact discouraged. However, the user must not experience any exceptions or inconsistent behavior that would not be experienced if such locks were used.

If this property is `false`, the behavior when using this class from multiple threads simultaneously is undefined and may include inconsistent results and exceptions. Clients wishing to use multiple threads should call `CopyForAnotherThread.copy(T)` to get a separate instance of the object for each thread.

Specified by:
`getIsThreadSafe` in interface `IThreadAware`
• #### getNumberOfStages

`public final int getNumberOfStages()`
Gets the total number of stages in this instance of the Runge-Kutta integrator.
• #### getStages

`public final double[] getStages()`
Gets the coefficients "c". These represent the factors multiplying the step size by which to advance the independent variable at each stage.
• #### getWeights

`public final double[] getWeights()`
Gets the coefficients "b". These represent the coefficients of the "k" stages summed together to represent the slope.
• #### getCoefficients

`public final double[][] getCoefficients()`
Gets the coefficients "a". These represent the factors multiplying the derivatives to provide the intermediate state information.
• #### getDerivatives

`public final double[][] getDerivatives()`
Gets the derivative information used in the algorithm. These represent the values of "k" in the Butcher Tableau for each dependent variable, with the first index representing the stage "i" and the second representing the index of the dependent variable.
• #### getWorkingArray

`protected final double[] getWorkingArray()`
Gets the working array used as a means to efficiently update the intermediate state information when calling the derivative `DerivativeFunction` (`get` / `set`).
• #### getDerivativeFunction

`public final DependentVariableDerivatives getDerivativeFunction()`
Gets the delegate which defines the first order differential equation representing the derivative of the dependent variables as a function of independent variable and dependent variables.
• #### initialize

```public final void initialize(int dimension,
DependentVariableDerivatives function)```
Initializes the derivative `DerivativeFunction` (`get` / `set`) and the working arrays used during integration.
Parameters:
`dimension` - The dimension of the state.
`function` - The first order differential function representing the derivative of the dependent variables as a function of independent variable and dependent variables.
• #### integrate

```public final void integrate(double step,
double initialIndependentVariableValue,
double[] initialDependentVariableValues,
double[] finalDependentVariableValues)```
Compute an integration step and update the final dependent variables and store the `Derivatives` (`get`).
Parameters:
`step` - The size of the step "h" to take.
`initialIndependentVariableValue` - The initial independent variable value "t[n]".
`initialDependentVariableValues` - The initial dependent variable value "y[n]".
`finalDependentVariableValues` - The final dependent variable value "y[n+1]" which will be updated.