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.util.List;
12: import java.util.SortedMap;
13:
14: import javolution.text.Text;
15: import javolution.util.FastList;
16:
17: /**
18: * <p> This class represents a function defined from a mapping betweem
19: * two sets (points and values).</p>
20: *
21: * <p> Instance of this class can be used to approximate continuous
22: * functions or to numerically solve differential systems.</p>
23: *
24: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
25: * @version 3.0, February 13, 2006
26: */
27: public final class DiscreteFunction<X, Y> extends Function<X, Y> {
28:
29: /**
30: * Holds the point-value entries.
31: */
32: private SortedMap<X, Y> _pointValues;
33:
34: /**
35: * Holds the variable.
36: */
37: private FastList<Variable<X>> _variables = new FastList<Variable<X>>();
38:
39: /**
40: * Holds the interpolator.
41: */
42: private Interpolator<X, Y> _interpolator;
43:
44: /**
45: * Creates the discrete function for the specified point-value entries.
46: *
47: * @param pointValues the point-value entries of this function.
48: * @param interpolator the interpolator.
49: * @param variable this function variable.
50: */
51: public DiscreteFunction(SortedMap<X, Y> pointValues,
52: Interpolator<X, Y> interpolator, Variable<X> variable) {
53: _pointValues = pointValues;
54: _variables.add(variable);
55: _interpolator = interpolator;
56: }
57:
58: /**
59: * Returns the point-value entries of this discrete function.
60: *
61: * @return the point-value mapping.
62: */
63: public SortedMap<X, Y> getPointValues() {
64: return _pointValues;
65: }
66:
67: /**
68: * Returns the interpolator used by this discrete function.
69: *
70: * @return the interpolator used to estimate the value between two points.
71: */
72: public Interpolator<X, Y> getInterpolator() {
73: return _interpolator;
74: }
75:
76: @Override
77: public Y evaluate() {
78: X point = _variables.get(0).get();
79: if (point == null) {
80: throw new FunctionException("Variable " + _variables.get(0)
81: + " not set");
82: }
83: return _interpolator.interpolate(point, _pointValues);
84: }
85:
86: @Override
87: public List<Variable<X>> getVariables() {
88: return _variables;
89: }
90:
91: private static final long serialVersionUID = 1L;
92:
93: @Override
94: public Text toText() {
95: return Text.valueOf(getClass().getName()).plus("@").plus(
96: Text.valueOf(hashCode(), 16));
97: }
98:
99: }
|