package ua.ansymo.hintco

import java.util.HashMap
import org.eclipse.core.runtime.Assert

class MathUtils {
	def static isApproximatelyEqual(double d1, double d2, double delta) {
		return d1 == d2 || delta > Math.abs(d1-d2) / Math.max(Math.abs(d1), Math.abs(d2));
	}
	
	def static converged(HashMap<OutputPortInstance, Double> newOuts, HashMap<OutputPortInstance, Double> oldOuts, double tol) {
		Assert.isTrue(newOuts !== null || oldOuts !== null)
		if (newOuts === null || oldOuts === null) {
			return false
		}
		for (outP : newOuts.keySet){
			Assert.isTrue(oldOuts.containsKey(outP))
			val nV = newOuts.get(outP)
			val oV = oldOuts.get(outP)
			if (! MathUtils.isApproximatelyEqual(oV, nV, tol)){
				return false
			}
		}
		return true
	}
	
}