All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.core.MathematicalExpression Maven / Gradle / Ivy

Go to download

The Waikato Environment for Knowledge Analysis (WEKA), a machine learning workbench. This version represents the developer version, the "bleeding edge" of development, you could say. New functionality gets added to this version.

There is a newer version: 3.9.6
Show newest version
/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see .
 */

/*
 *    MathematicalExpression.java
 *    Copyright (C) 2008-2012 University of Waikato, Hamilton, New Zealand
 */

package weka.core;

import java.io.ByteArrayInputStream;
import java.util.HashMap;

import java_cup.runtime.DefaultSymbolFactory;
import java_cup.runtime.SymbolFactory;
import weka.core.mathematicalexpression.Parser;
import weka.core.mathematicalexpression.Scanner;

/**
 * Class for evaluating a string adhering the following grammar:
* *
 * expr_list ::= expr_list expr_part | expr_part ;
 * expr_part ::= expr ;
 * expr      ::=   NUMBER
 *               | ( expr )
 *               | opexpr
 *               | varexpr
 *               | funcexpr
 *               ;
 * 
 * opexpr    ::=   expr + expr
 *               | expr - expr
 *               | expr * expr
 *               | expr / expr
 *               ;
 * 
 * varexpr  ::=  VARIABLE ;
 * 
 * funcexpr ::=    abs ( expr )
 *               | sqrt ( expr )
 *               | log ( expr )
 *               | exp ( expr )
 *               | sin ( expr )
 *               | cos ( expr )
 *               | tan ( expr )
 *               | rint ( expr )
 *               | floor ( expr )
 *               | pow ( expr , expr )
 *               | ceil ( expr )
 *               | ifelse ( boolexpr , expr (if true) , expr (if false) )
 *               ;
 * 
 * boolexpr ::=    BOOLEAN
 *               | true
 *               | false
 *               | expr < expr
 *               | expr <= expr
 *               | expr > expr
 *               | expr >= expr
 *               | expr = expr
 *               | ( boolexpr )
 *               | ! boolexpr
 *               | boolexpr & boolexpr
 *               | boolexpr | boolexpr
 *               ;
 * 
* * Code example 1: * *
 * String expr = "pow(BASE,EXPONENT)*MULT";
 * HashMap symbols = new HashMap();
 * symbols.put("BASE", new Double(2));
 * symbols.put("EXPONENT", new Double(9));
 * symbols.put("MULT", new Double(0.1));
 * double result = MathematicalExpression.evaluate(expr, symbols);
 * System.out.println(expr + " and " + symbols + " = " + result);
 * 
* * Code Example 2 (uses the "ifelse" construct): * *
 * String expr = "ifelse(I<0,pow(BASE,I*0.5),pow(BASE,I))";
 * MathematicalExpression.TreeNode tree = MathematicalExpression.parse(expr);
 * HashMap symbols = new HashMap();
 * symbols.put("BASE", new Double(2));
 * for (int i = -10; i <= 10; i++) {
 *   symbols.put("I", new Double(i));
 *   double result = MathematicalExpression.evaluate(expr, symbols);
 *   System.out.println(expr + " and " + symbols + " = " + result);
 * }
 * 
* * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 10203 $ */ public class MathematicalExpression implements RevisionHandler { /** * Parses and evaluates the given expression. Returns the result of the * mathematical expression, based on the given values of the symbols. * * @param expr the expression to evaluate * @param symbols the symbol/value mapping * @return the evaluated result * @throws Exception if something goes wrong */ @SuppressWarnings("deprecation") public static double evaluate(String expr, HashMap symbols) throws Exception { SymbolFactory sf; ByteArrayInputStream parserInput; Parser parser; sf = new DefaultSymbolFactory(); parserInput = new ByteArrayInputStream(expr.getBytes()); parser = new Parser(new Scanner(parserInput, sf), sf); parser.setSymbols(symbols); parser.parse(); return parser.getResult(); } /** * Returns the revision string. * * @return the revision */ @Override public String getRevision() { return RevisionUtils.extract("$Revision: 10203 $"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy