001/*
002 * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
003 * Copyright (C) 2006 - JScience (http://jscience.org/)
004 * All rights reserved.
005 * 
006 * Permission to use, copy, modify, and distribute this software is
007 * freely granted, provided that this notice is preserved.
008 */
009package org.jscience.mathematics.function;
010
011import java.util.List;
012import java.util.SortedMap;
013
014import javolution.text.Text;
015import javolution.util.FastList;
016
017/**
018 * <p> This class represents a function defined from a mapping betweem 
019 *     two sets (points and values).</p>
020 *     
021 * <p> Instance of this class can be used to approximate continuous
022 *     functions or to numerically solve differential systems.</p>
023 *     
024 * @author  <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
025 * @version 3.0, February 13, 2006
026 */
027public final class DiscreteFunction<X, Y> extends Function<X, Y> {
028
029    /**
030     * Holds the point-value entries.
031     */
032    private SortedMap<X, Y> _pointValues;
033
034    /**
035     * Holds the variable.
036     */
037    private FastList<Variable<X>> _variables = new FastList<Variable<X>>();
038
039    /**
040     * Holds the interpolator.
041     */
042    private Interpolator<X, Y> _interpolator;
043
044    /**
045     * Creates the discrete function for the specified point-value entries.
046     * 
047     * @param  pointValues the point-value entries of this function.
048     * @param  interpolator the interpolator.
049     * @param  variable this function variable.
050     */
051    public DiscreteFunction(SortedMap<X, Y> pointValues,
052            Interpolator<X, Y> interpolator, Variable<X> variable) {
053        _pointValues = pointValues;
054        _variables.add(variable);
055        _interpolator = interpolator;
056    }
057
058    /**
059     * Returns the point-value entries of this discrete function.
060     *
061     * @return the point-value mapping.
062     */
063    public SortedMap<X, Y> getPointValues() {
064        return _pointValues;
065    }
066
067    /**
068     * Returns the interpolator used by this discrete function.
069     *
070     * @return the interpolator used to estimate the value between two points.
071     */
072    public Interpolator<X, Y> getInterpolator() {
073        return _interpolator;
074    }
075
076    @Override
077    public Y evaluate() {
078        X point = _variables.get(0).get();
079        if (point == null) {
080            throw new FunctionException("Variable " + _variables.get(0)
081                    + " not set");
082        }
083        return _interpolator.interpolate(point, _pointValues);
084    }
085
086    @Override
087    public List<Variable<X>> getVariables() {
088        return _variables;
089    }
090
091    private static final long serialVersionUID = 1L;
092
093    @Override
094    public Text toText() {
095        return Text.valueOf(getClass().getName()).plus("@").plus(
096                Text.valueOf(hashCode(), 16));
097    }
098
099}