01: /*
02: * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
03: * Copyright (C) 2006 - JScience (http://jscience.org/)
04: * All rights reserved.
05: *
06: * Permission to use, copy, modify, and distribute this software is
07: * freely granted, provided that this notice is preserved.
08: */
09: package org.jscience.mathematics.function;
10:
11: import java.io.Serializable;
12: import java.util.SortedMap;
13:
14: import javolution.lang.Immutable;
15:
16: import org.jscience.mathematics.structure.Field;
17:
18: /**
19: * <p> This interface represents an estimator of the values at a certain point
20: * using surrounding points and values. Interpolators are typically used
21: * with {@link DiscreteFunction discrete functions}.</p>
22: *
23: * <p> As a convenience {@link Interpolator.Linear linear} interpolator class
24: * for point-values of the same {@link Field field} is provided.</p>
25: *
26: * <p> Custom interpolators can be used between Java objects of different kind.
27: * For example:[code]
28: * // Creates a linear interpolator between the java.util.Date and Measures<Mass>
29: * Interpolator<Date, Amount<Mass>> linear
30: * = new Interpolator<Date, Amount<Mass>>() { ... }
31: * DiscreteFunction<Date, Amount<Mass>> weight
32: * = new DiscreteFunction<Date, Amount<Mass>>(samples, linear, t);
33: * [/code]</p>
34: *
35: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle </a>
36: * @version 3.0, February 13, 2006
37: */
38: public interface Interpolator<P, V> extends Immutable, Serializable {
39:
40: /**
41: * Estimates the value at the specified point.
42: *
43: * @param point the point for which the value is estimated.
44: * @param pointValues the point-value entries.
45: * @return the estimated value at the specified point.
46: */
47: V interpolate(P point, SortedMap<P, V> pointValues);
48:
49: /**
50: * <p> This class represents a linear interpolator for {@link Field field}
51: * instances (point and values from the same field).</p>
52: *
53: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle </a>
54: * @version 3.0, February 13, 2006
55: */
56: public static class Linear<F extends Field<F>> implements
57: Interpolator<F, F> {
58:
59: public F interpolate(F point, SortedMap<F, F> pointValues) {
60: // Searches exact.
61: F y = pointValues.get(point);
62: if (y != null)
63: return y;
64:
65: // Searches surrounding points/values.
66: SortedMap<F, F> headMap = pointValues.headMap(point);
67: F x1 = headMap.lastKey();
68: F y1 = headMap.get(x1);
69: SortedMap<F, F> tailMap = pointValues.tailMap(point);
70: F x2 = tailMap.firstKey();
71: F y2 = tailMap.get(x2);
72:
73: // Interpolates.
74: final F x = point;
75: F deltaInv = (x2.plus(x1.opposite())).inverse();
76: F k1 = (x2.plus(x.opposite())).times(deltaInv);
77: F k2 = (x.plus(x1.opposite())).times(deltaInv);
78: return ((y1.times(k1))).plus(y2.times(k2));
79: }
80:
81: private static final long serialVersionUID = 1L;
82: }
83:
84: }
|