comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/extreme/Curve.java @ 3786:4adc35aa655c

merged flys-artifacts/2.9.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:47 +0200
parents 467efea19d15
children 5cc9453456a7
comparison
equal deleted inserted replaced
3719:e82acd5c86f7 3786:4adc35aa655c
1 package de.intevation.flys.artifacts.model.extreme;
2
3 import de.intevation.flys.artifacts.math.Function;
4 import de.intevation.flys.artifacts.math.NaNFunction;
5 import de.intevation.flys.artifacts.math.UnivariateRealFunctionFunction;
6
7 import de.intevation.flys.artifacts.math.fitting.FunctionFactory;
8
9 import java.io.Serializable;
10
11 import java.lang.ref.SoftReference;
12
13 import org.apache.commons.math.analysis.interpolation.SplineInterpolator;
14
15 import org.apache.commons.math.exception.MathIllegalArgumentException;
16
17 import org.apache.log4j.Logger;
18
19 public class Curve
20 implements Serializable, Function
21 {
22 private static Logger log = Logger.getLogger(Curve.class);
23
24 protected double [] qs;
25 protected double [] ws;
26 protected String function;
27 protected double [] coeffs;
28
29 // The spline is pretty heavy weight so cache it with a soft ref only.
30 protected transient SoftReference<Function> spline;
31 protected transient Function extrapolation;
32
33 public Curve() {
34 }
35
36 public Curve(
37 double [] qs,
38 double [] ws,
39 String function,
40 double [] coeffs
41 ) {
42 this.qs = qs;
43 this.ws = ws;
44 this.function = function;
45 this.coeffs = coeffs;
46 }
47
48 public double [] getQs() {
49 return qs;
50 }
51
52 public double [] getWs() {
53 return ws;
54 }
55
56 public String getFunction() {
57 return function;
58 }
59
60 public double [] getCoeffs() {
61 return coeffs;
62 }
63
64 @Override
65 public double value(double x) {
66 if (x < qs[0]) return Double.NaN;
67 return (x <= qs[qs.length-1]
68 ? getSpline()
69 : getExtrapolation()).value(x);
70 }
71
72 protected synchronized Function getExtrapolation() {
73 if (extrapolation == null) {
74 de.intevation.flys.artifacts.math.fitting.Function
75 f = FunctionFactory.getInstance().getFunction(function);
76
77 extrapolation = f != null
78 ? f.instantiate(coeffs)
79 : NaNFunction.INSTANCE;
80 }
81 return extrapolation;
82 }
83
84 protected synchronized Function getSpline() {
85 Function sp;
86 if (spline != null) {
87 if ((sp = spline.get()) != null) {
88 return sp;
89 }
90 }
91 spline = new SoftReference<Function>(sp = createSpline());
92 return sp;
93 }
94
95 protected Function createSpline() {
96 SplineInterpolator interpolator = new SplineInterpolator();
97 try {
98 return new UnivariateRealFunctionFunction(
99 interpolator.interpolate(qs, ws));
100 }
101 catch (MathIllegalArgumentException miae) {
102 log.debug("creation on spline failed", miae);
103 return NaNFunction.INSTANCE;
104 }
105 }
106 }
107 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org