KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
12 import java.util.SortedMap JavaDoc;
13 import javolution.context.Realtime;
14 import javolution.util.FastList;
15
16 /**
17  * <p> This class represents a function defined from a mapping betweem
18  * two sets (points and values).</p>
19  *
20  * <p> Instance of this class can be used to approximate continuous
21  * functions or to numerically solve differential systems.</p>
22  *
23  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
24  * @version 3.0, February 13, 2006
25  */

26 public final class DiscreteFunction<X, Y> extends Function<X, Y> {
27
28     /**
29      * Holds the point-value entries.
30      */

31     private SortedMap JavaDoc<X, Y> _pointValues;
32
33     /**
34      * Holds the variable.
35      */

36     private FastList<Variable<X>> _variables = new FastList<Variable<X>>();
37
38     /**
39      * Holds the interpolator.
40      */

41     private Interpolator<X, Y> _interpolator;
42
43     /**
44      * Creates the discrete function for the specified point-value entries.
45      *
46      * @param pointValues the point-value entries of this function.
47      * @param interpolator the interpolator.
48      * @param variable this function variable.
49      */

50     public DiscreteFunction(SortedMap JavaDoc<X, Y> pointValues, Interpolator<X, Y> interpolator, Variable<X> variable) {
51         _pointValues = pointValues;
52         _variables.add(variable);
53         _interpolator = interpolator;
54     }
55
56     /**
57      * Returns the point-value entries of this discrete function.
58      *
59      * @return the point-value mapping.
60      */

61     public SortedMap JavaDoc<X, Y> getPointValues() {
62         return _pointValues;
63     }
64
65     /**
66      * Returns the interpolator used by this discrete function.
67      *
68      * @return the interpolator used to estimate the value between two points.
69      */

70     public Interpolator<X, Y> getInterpolator() {
71         return _interpolator;
72     }
73
74     @Override JavaDoc
75     public Y evaluate() {
76         X point = _variables.get(0).get();
77         if (point == null) {
78             throw new FunctionException("Variable " + _variables.get(0) + " not set");
79         }
80         return _interpolator.interpolate(point, _pointValues);
81     }
82
83     @Override JavaDoc
84     public boolean move(ObjectSpace os) {
85         if (super.move(os)) {
86             _variables.move(os);
87             if (_pointValues instanceof Realtime) {
88                 ((Realtime) _pointValues).move(os);
89             }
90             return true;
91         }
92         return false;
93     }
94
95     @Override JavaDoc
96     public List JavaDoc<Variable<X>> getVariables() {
97         return _variables;
98     }
99
100     private static final long serialVersionUID = 1L;
101
102 }
Popular Tags