KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > mathematics > functions > Interpolator


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.mathematics.functions;
10
11 import java.io.Serializable JavaDoc;
12 import java.util.SortedMap JavaDoc;
13
14 import javolution.lang.Immutable;
15
16 import org.jscience.mathematics.structures.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, Measure<Mass>> linear
30  * = new Interpolator<Date, Measure<Mass>>() { ... }
31  * DiscreteFunction<Date, Measure<Mass>> weight
32  * = new DiscreteFunction<Date, Measure<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 JavaDoc {
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 JavaDoc<P, V> pointValues);
48     
49     
50     /**
51      * <p> This class represents a linear interpolator for {@link Field field}
52      * instances (point and values from the same field).</p>
53      *
54      * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle </a>
55      * @version 3.0, February 13, 2006
56      */

57     public static class Linear<F extends Field<F>> implements Interpolator<F, F> {
58
59         public F interpolate(F point, SortedMap JavaDoc<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 JavaDoc<F, F> headMap = pointValues.headMap(point);
67             F x1 = headMap.lastKey();
68             F y1 = headMap.get(x1);
69             SortedMap JavaDoc<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 }
Popular Tags