comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/extreme/Curve.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/extreme/Curve.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.model.extreme;
2
3 import org.dive4elements.river.artifacts.math.Function;
4 import org.dive4elements.river.artifacts.math.NaNFunction;
5 import org.dive4elements.river.artifacts.math.UnivariateRealFunctionFunction;
6
7 import org.dive4elements.river.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 /** An extrapolating W/Q function/curve. */
20 public class Curve
21 implements Serializable, Function
22 {
23 private static Logger log = Logger.getLogger(Curve.class);
24
25 protected double [] qs;
26 protected double [] ws;
27 protected String function;
28 protected double [] coeffs;
29 protected double chiSqr;
30
31 /** Suggested maximum value for q to input. */
32 protected double suggestedMaxQ;
33
34 // The spline is pretty heavy weight so cache it with a soft ref only.
35 protected transient SoftReference<Function> spline;
36 protected transient Function extrapolation;
37
38 public Curve() {
39 }
40
41 public Curve(
42 double [] qs,
43 double [] ws,
44 String function,
45 double [] coeffs,
46 double chiSqr
47 ) {
48 this.qs = qs;
49 this.ws = ws;
50 this.function = function;
51 this.coeffs = coeffs;
52 this.suggestedMaxQ = Double.MAX_VALUE;
53 }
54
55 public double [] getQs() {
56 return qs;
57 }
58
59 public double [] getWs() {
60 return ws;
61 }
62
63 public String getFunction() {
64 return function;
65 }
66
67 public double [] getCoeffs() {
68 return coeffs;
69 }
70
71
72 public void setSuggestedMaxQ(double newMaxQ) {
73 this.suggestedMaxQ = newMaxQ;
74 }
75
76
77 public double getSuggestedMaxQ() {
78 return this.suggestedMaxQ;
79 }
80
81
82 /** Calculate value at given x. */
83 @Override
84 public double value(double x) {
85 if (qs == null || x < qs[0]) return Double.NaN;
86 return (x <= qs[qs.length-1]
87 ? getSpline()
88 : getExtrapolation()).value(x);
89 }
90
91 protected synchronized Function getExtrapolation() {
92 if (extrapolation == null) {
93 org.dive4elements.river.artifacts.math.fitting.Function
94 f = FunctionFactory.getInstance().getFunction(function);
95
96 extrapolation = f != null
97 ? f.instantiate(coeffs)
98 : NaNFunction.INSTANCE;
99 }
100 return extrapolation;
101 }
102
103 /**
104 * Gets the chiSqr for this instance.
105 *
106 * @return The chiSqr.
107 */
108 public double getChiSqr() {
109 return this.chiSqr;
110 }
111
112 /**
113 * Sets the chiSqr for this instance.
114 *
115 * @param chiSqr The chiSqr.
116 */
117 public void setChiSqr(double chiSqr) {
118 this.chiSqr = chiSqr;
119 }
120
121 protected synchronized Function getSpline() {
122 Function sp;
123 if (spline != null) {
124 if ((sp = spline.get()) != null) {
125 return sp;
126 }
127 }
128 spline = new SoftReference<Function>(sp = createSpline());
129 return sp;
130 }
131
132 protected Function createSpline() {
133 SplineInterpolator interpolator = new SplineInterpolator();
134 try {
135 return new UnivariateRealFunctionFunction(
136 interpolator.interpolate(qs, ws));
137 }
138 catch (MathIllegalArgumentException miae) {
139 log.debug("creation on spline failed", miae);
140 return NaNFunction.INSTANCE;
141 }
142 }
143 }
144 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org