RHEMUtils.java [src/java/rhem/utils] Revision: 2fc86a2f22dc734fb9261ed74e35bbf6fe3b2fa2 Date: Fri Jan 29 17:32:53 MST 2021
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API, and application suite.
*
* 2012-2020, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package rhem.utils;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
/**
*
* @author rumpal
*/
public class RHEMUtils {
private static final String DEFAULT_DOUBLE_PRECISION_FORMAT = "%.14f";
private static final String DEFAULT_DOUBLE_FORMAT = "%.6f";
public static final String[] MONTH_NAMES_LIST = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};
//Round the value of a float or double
public static double roundValues(double value, int places) {
double power = Math.pow(10, places);
return (Math.round(value * power) / power);
}
public static double interpolate(double pointToEvaluate, double[] functionValuesX, double[] functionValuesY) {
double result = 0;
int index = findIntervalLeftBorderIndex(pointToEvaluate, functionValuesX);
if (index == functionValuesX.length - 1) {
index--;
}
result = linearInterpolation(pointToEvaluate, functionValuesX[index], functionValuesY[index],
functionValuesX[index + 1], functionValuesY[index + 1]);
return result;
}
public static int findIntervalLeftBorderIndex(double point, double[] intervals) {
if (point < intervals[0]) {
return 0;
}
if (point > intervals[intervals.length - 1]) {
return intervals.length - 1;
}
int leftBorderIndex = 0;
int indexOfNumberToCompare;
int rightBorderIndex = intervals.length - 1;
while ((rightBorderIndex - leftBorderIndex) != 1) {
indexOfNumberToCompare = leftBorderIndex
+ (int) Math.floor(((rightBorderIndex - leftBorderIndex) / 2));
if (point >= intervals[indexOfNumberToCompare]) {
leftBorderIndex = indexOfNumberToCompare;
} else {
rightBorderIndex = indexOfNumberToCompare;
}
}
return leftBorderIndex;
}
public static double linearInterpolation(double x, double x0, double y0, double x1, double y1) {
double a = (y1 - y0) / (x1 - x0);
double b = -a * x0 + y0;
return a * x + b;
}
public String formatPrecisionDouble(double value) {
return formatDoubleString(value, DEFAULT_DOUBLE_PRECISION_FORMAT);
}
public static String formatDouble(double value) {
return formatDoubleString(value, DEFAULT_DOUBLE_FORMAT);
}
private static String formatDoubleString(double value, String format) {
return String.format(format, value);
}
public static String fmt(double d) {
DecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
df.setMaximumFractionDigits(340);
return df.format(d);
}
}