Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jmat/function/DoubleFunctionExpression.java


1   package jmat.function;
2   
3   import jmat.data.Matrix;
4   
5   import jmat.function.expressionParser.Evaluator;
6   
7   
8   /**
9    * DOCUMENT ME!
10   *
11   * @author $author$
12   * @version $Revision: 1.2 $
13   */
14  public class DoubleFunctionExpression extends DoubleFunction
15  {
16      //~ Instance fields ////////////////////////////////////////////////////////
17  
18      private Evaluator E = new Evaluator();
19      private String expression;
20      private String[] variables;
21  
22      //~ Constructors ///////////////////////////////////////////////////////////
23  
24      public DoubleFunctionExpression(String exp, String[] vars)
25      {
26          argNumber = vars.length;
27          setFunction(exp, vars);
28      }
29  
30      public DoubleFunctionExpression(String exp, String vars)
31      {
32          argNumber = 1;
33  
34          String[] variable = new String[1];
35          variable[0] = vars;
36          setFunction(exp, variable);
37      }
38  
39      //~ Methods ////////////////////////////////////////////////////////////////
40  
41      public double eval(double[] values)
42      {
43          checkArgNumber(values.length);
44          setVariableValues(values);
45  
46          return ((Double) (E.getValue())).doubleValue();
47      }
48  
49      public double eval(double value)
50      {
51          checkArgNumber(1);
52          setVariableValues(value);
53  
54          return ((Double) (E.getValue())).doubleValue();
55      }
56  
57      public double eval(Matrix values)
58      {
59          checkArgNumber(values.getRowDimension());
60          setVariableValues(values);
61  
62          return ((Double) (E.getValue())).doubleValue();
63      }
64  
65      private void setFunction(String exp, String[] vars)
66      {
67          expression = exp;
68          variables = vars;
69      }
70  
71      private void setVariableValues(double[] values)
72      {
73          for (int i = 0; i < variables.length; i++)
74          {
75              E.addVariable(variables[i], values[i]);
76          }
77  
78          E.setExpression(expression);
79      }
80  
81      private void setVariableValues(double value)
82      {
83          E.addVariable(variables[0], value);
84          E.setExpression(expression);
85      }
86  
87      private void setVariableValues(Matrix values)
88      {
89          for (int i = 0; i < variables.length; i++)
90          {
91              E.addVariable(variables[i], values.get(i, 0));
92          }
93  
94          E.setExpression(expression);
95      }
96  }
97  ///////////////////////////////////////////////////////////////////////////////
98  //  END OF FILE.
99  ///////////////////////////////////////////////////////////////////////////////