com.meliorbis.numerics.convergence.Converger Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Numerics Show documentation
Show all versions of Numerics Show documentation
A library for working with large multi-dimensional arrays and the functions they represent
/**
*
*/
package com.meliorbis.numerics.convergence;
import java.util.LinkedList;
import java.util.List;
import com.meliorbis.utils.Pair;
/**
* @author Tobias Grasl
*/
public class Converger
{
public interface Callback {
void notify(Pair,Criterion> state_, int period_);
}
private final List _callbacks = new LinkedList();
public S converge(Convergable convergable_,
S initial_, double target_) throws E
{
Pair state = new Pair(initial_, Criterion.INITIAL);
int period = 0;
while(state.getRight().getValue() > target_)
{
final Pair newState = convergable_.performIteration(state.getLeft());
state = newState;
final int periodForCallbacks = ++period;
_callbacks.forEach(c -> c.notify(newState, periodForCallbacks));
}
return state.getLeft();
}
public void addCallback(Callback cb_)
{
assert cb_ != null : "Callback must not be null";
_callbacks.add(cb_);
}
}