1 9 package org.jscience.mathematics.functions; 10 11 import java.io.Serializable ; 12 import java.util.SortedMap ; 13 14 import javolution.lang.Immutable; 15 16 import org.jscience.mathematics.structures.Field; 17 18 38 public interface Interpolator<P, V> extends Immutable, Serializable { 39 40 47 V interpolate(P point, SortedMap <P, V> pointValues); 48 49 50 57 public static class Linear<F extends Field<F>> implements Interpolator<F, F> { 58 59 public F interpolate(F point, SortedMap <F, F> pointValues) { 60 F y = pointValues.get(point); 62 if (y != null) 63 return y; 64 65 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 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 |