CogPar
A versatile parser for mathematical expressions.
 All Classes Functions Variables
FunctionExpressionNode.java
1 /*
2  * This software and all files contained in it are distrubted under the MIT license.
3  *
4  * Copyright (c) 2013 Cogito Learning Ltd
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 package uk.co.cogitolearning.cogpar;
26 
32 public class FunctionExpressionNode implements ExpressionNode
33 {
35  public static final int SIN = 1;
37  public static final int COS = 2;
39  public static final int TAN = 3;
40 
42  public static final int ASIN = 4;
44  public static final int ACOS = 5;
46  public static final int ATAN = 6;
47 
49  public static final int SQRT = 7;
51  public static final int EXP = 8;
52 
54  public static final int LN = 9;
56  public static final int LOG = 10;
58  public static final int LOG2 = 11;
59 
61  private int function;
62 
65 
75  {
76  super();
77  this.function = function;
78  this.argument = argument;
79  }
80 
84  public int getType()
85  {
87  }
88 
98  public static int stringToFunction(String str)
99  {
100  if (str.equals("sin"))
102  if (str.equals("cos"))
104  if (str.equals("tan"))
106 
107  if (str.equals("asin"))
109  if (str.equals("acos"))
111  if (str.equals("atan"))
113 
114  if (str.equals("sqrt"))
116  if (str.equals("exp"))
118 
119  if (str.equals("ln"))
120  return FunctionExpressionNode.LN;
121  if (str.equals("log"))
123  if (str.equals("log2"))
125 
126  throw new ParserException("Unexpected Function " + str + " found");
127  }
128 
137  public static String getAllFunctions()
138  {
139  return "sin|cos|tan|asin|acos|atan|sqrt|exp|ln|log|log2";
140  }
141 
148  public double getValue()
149  {
150  switch (function)
151  {
152  case SIN:
153  return Math.sin(argument.getValue());
154  case COS:
155  return Math.cos(argument.getValue());
156  case TAN:
157  return Math.tan(argument.getValue());
158  case ASIN:
159  return Math.asin(argument.getValue());
160  case ACOS:
161  return Math.acos(argument.getValue());
162  case ATAN:
163  return Math.atan(argument.getValue());
164  case SQRT:
165  return Math.sqrt(argument.getValue());
166  case EXP:
167  return Math.exp(argument.getValue());
168  case LN:
169  return Math.log(argument.getValue());
170  case LOG:
171  return Math.log(argument.getValue()) * 0.43429448190325182765;
172  case LOG2:
173  return Math.log(argument.getValue()) * 1.442695040888963407360;
174  }
175 
176  throw new EvaluationException("Invalid function id "+function+"!");
177  }
178 
188  public void accept(ExpressionNodeVisitor visitor)
189  {
190  visitor.visit(this);
191  argument.accept(visitor);
192  }
193 
194 }