All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.meliorbis.numerics.convergence.Converger Maven / Gradle / Ivy

Go to download

A library for working with large multi-dimensional arrays and the functions they represent

There is a newer version: 1.2
Show newest version
/**
 * 
 */
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 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_);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy