Mercurial > dive4elements > river
diff artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java @ 8581:073ea4bcea58
(issue1755) Interpolate BedQuality Results
This adds an interpolation function to each various bedQuality result class.
Imho this is ok as the interpolation function can be seen as part of the result.
The interpolation function is initalized on first use and can be accessed through
get.*Interpol functions.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 16 Mar 2015 15:36:38 +0100 |
parents | e4606eae8ea5 |
children | 07c9ac22f611 |
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java Mon Mar 16 14:37:30 2015 +0100 +++ b/artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java Mon Mar 16 15:36:38 2015 +0100 @@ -10,6 +10,13 @@ import org.dive4elements.river.artifacts.math.Linear; +import org.apache.commons.math.analysis.interpolation.LinearInterpolator; +import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; + +import org.apache.commons.math.exception.DimensionMismatchException; +import org.apache.commons.math.exception.NumberIsTooSmallException; +import org.apache.commons.math.exception.NonMonotonousSequenceException; + import gnu.trove.TDoubleArrayList; import java.util.Arrays; @@ -305,5 +312,23 @@ } } } + + /** Convieniance function for results to get an interpolator. + * This is basically a static wrapper to for LinearInterpolator.interpolate + * with error handling. Returns null on error.*/ + public static PolynomialSplineFunction getLinearInterpolator(TDoubleArrayList x, TDoubleArrayList y) { + LinearInterpolator lpol = new LinearInterpolator(); + try { + return lpol.interpolate(x.toNativeArray(), y.toNativeArray()); + } catch (DimensionMismatchException e) { + log.error("KMs and Result values have different sizes. Failed to interpolate: " + + e.getMessage()); + } catch (NonMonotonousSequenceException e) { + log.error("KMs are not monotonous. Failed to interpolate: " + e.getMessage()); + } catch (NumberIsTooSmallException e) { + log.error("Result is to small. Failed to interpolate: " + e.getMessage()); + } + return null; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :