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

nl.tno.bim.nmd.scaling.NmdTableScaler Maven / Gradle / Ivy

The newest version!
package nl.tno.bim.nmd.scaling;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * Table scaler is in pricniple a linear scaler with a series of reference dimensions
 * @author vijj
 *
 */
public class NmdTableScaler extends NmdBaseScaler {
	
	/**
	 * reference dimensions stating the dimension as key and the reference value belonging to that dim as value
	 */
	private Map refDims = null;
	private Double[] orderedDims = null;
	
	/** 
	 * Factory method for creating a table sclaer. WIllc reate a new Table sclaer object based on input parameters
	 * @param unit
	 * @param refDims
	 * @param currentValues
	 * @return
	 */
	public static NmdTableScaler getInstance(String unit,Map refDims, Double[] currentValues) {
		Double[] x_vals = refDims.keySet().toArray(new Double[0]);
		Arrays.sort(x_vals);
		Double[] bounds = new Double[] {
				x_vals[0], x_vals[x_vals.length - 1],
				currentValues.length > 1 ? x_vals[0] : 0d,
				currentValues.length > 1 ? x_vals[x_vals.length - 1] : 0d};
		
		NmdTableScaler scaler = new NmdTableScaler(unit, null, bounds, currentValues);
		scaler.refDims = refDims;
		scaler.setOrderedDims(refDims.keySet());
		return scaler;
	}
	
	private NmdTableScaler(String unit, Double[] coefficients, Double[] bounds, Double[] currentValues) {
		super(unit, coefficients, bounds, currentValues);
	}
	
	@Override
	protected Double calculate(Double x) {
		// find relevant values to interpolate between (x values closest above and below the input value)
		Optional x_min = Arrays.stream(orderedDims).filter(ref -> x >= ref).reduce((first, second) -> second);
		Optional x_max = Arrays.stream(orderedDims).filter(ref -> x <= ref).findFirst();
		// apply interpolation on the refDims and values.
		if (x_max.isPresent() && x_min.isPresent()) {
			double x0 = x_min.get();
			double x1 = x_max.get();
			double y0 = refDims.get(x0);
			double y1 = refDims.get(x1);
			return x1 == x0 ? y0 : y0 + (x - x0)  * (y1 - y0) / (x1 - x0);
		} else {
			return Double.NaN;
		}
	}
		
	protected void setOrderedDims(Set dims) {
		this.orderedDims = dims.toArray(new Double[0]);
		Arrays.sort(this.orderedDims);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy