简体   繁体   English

用变量评估数学表达式。 (java 8)

[英]Evaluating a math expression with variables. (java 8)

I would like additional help on a answer to this question, Evaluating a math expression given in string form . 我想对这个问题的答案提供额外帮助, 评估以字符串形式给出的数学表达式 The user @Boann answered the question with a very interesting algorithm that he also points out can be altered to accept variables. 用户@Boann用一个非常有趣的算法回答了这个问题,他也指出可以改变它来接受变量。 I've managed to alter it and get it to work, but dont know how he separates the compilation and evaluation . 我已经设法改变它并让它工作, 但不知道他如何分离编译和评估 Here's my code: 这是我的代码:

import java.util.HashMap;
import java.util.Map;

public class EvaluateExpressionWithVariabels {

@FunctionalInterface
interface Expression {
    double eval();
}

public static void main(String[] args){
    Map<String,Double> variables = new HashMap<>();     
    for (double x = 100; x <= +120; x++) {
        variables.put("x", x);
        System.out.println(x + " => " + eval("x+(sqrt(x))",variables).eval());
    }
}

public static Expression eval(final String str,Map<String,Double> variables) {
    return new Object() {
        int pos = -1, ch;

        //if check pos+1 is smaller than string length ch is char at new pos
        void nextChar() {
            ch = (++pos < str.length()) ? str.charAt(pos) : -1;
        }

        //skips 'spaces' and if current char is what was searched, if true move to next char return true
        //else return false
        boolean eat(int charToEat) {
            while (ch == ' ') nextChar();
            if (ch == charToEat) {
                nextChar();
                return true;
            }
            return false;
        }


        Expression parse() {
            nextChar();
            Expression x = parseExpression();
            if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
            return x;
        }

        // Grammar:
        // expression = term | expression `+` term | expression `-` term
        // term = factor | term `*` factor | term `/` factor
        // factor = `+` factor | `-` factor | `(` expression `)`
        //        | number | functionName factor | factor `^` factor

        Expression parseExpression() {
            Expression x = parseTerm();
            for (;;) {
                if (eat('+')) { // addition
                    Expression a = x, b = parseTerm();                      
                    x = (() -> a.eval() + b.eval());
                } else if (eat('-')) { // subtraction
                    Expression a = x, b = parseTerm();
                    x = (() -> a.eval() - b.eval());
                } else {
                    return x;
                }
            }
        }

        Expression parseTerm() {
            Expression x = parseFactor();
            for (;;) {
                if (eat('*')){
                     Expression a = x, b = parseFactor(); // multiplication
                     x = (() -> a.eval() * b.eval());
                }
                else if(eat('/')){
                     Expression a = x, b = parseFactor(); // division
                     x = (() -> a.eval() / b.eval());
                }
                else return x;
            }
        }

        Expression parseFactor() {
            if (eat('+')) return parseFactor(); // unary plus
            if (eat('-')){
                Expression b = parseFactor(); // unary minus
                return (() -> -1 * b.eval());
            }

            Expression x;
            int startPos = this.pos;
            if (eat('(')) { // parentheses
                x = parseExpression();
                eat(')');
            } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
                while ((ch >= '0' && ch <= '9') || ch == '.'){                     
                    nextChar();
                }
                double xx = Double.parseDouble(str.substring(startPos, this.pos));
                x = () -> xx;
            } else if (ch >= 'a' && ch <= 'z') { // functions
                while (ch >= 'a' && ch <= 'z') nextChar();
                String func = str.substring(startPos, this.pos);

                if ( variables.containsKey(func)){
                    x = () -> variables.get(func);
                }else{
                    double xx = parseFactor().eval();
                    if (func.equals("sqrt")) x = () -> Math.sqrt(xx);
                    else if (func.equals("sin")) x = () -> Math.sin(Math.toRadians(xx));
                    else if (func.equals("cos")) x = () -> Math.cos(Math.toRadians(xx));
                    else if (func.equals("tan")) x = () -> Math.tan(Math.toRadians(xx));                    
                    else throw new RuntimeException("Unknown function: " + func);
                }
            } else {
                throw new RuntimeException("Unexpected: " + (char)ch);
            }

            if (eat('^')){ 
                x = () -> {
                    double d =  parseFactor().eval();
                    return Math.pow(d,d); // exponentiation
                };
            }

            return  x;
        }
    }.parse();
}
}

If you take a look at his answer his main 如果你看看他的答案他的主要

public static void main(String[] args) {
    Map<String,Double> variables = new HashMap<>();
    Expression exp = parse("x^2 - x + 2", variables);
    for (double x = -20; x <= +20; x++) {
        variables.put("x", x);
        System.out.println(x + " => " + exp.eval());
    }
}

He calls function parse on this line Expression exp = parse("x^2 - x + 2", variables); 他在这一行调用函数parse Expression exp = parse("x^2 - x + 2", variables); to compile the expression once and uses the for to evaluate it multiple times with a unique x values. 编译表达式一次并使用for来使用唯一的x值多次计算它。 What does the parse function refer to. parse函数引用了什么。

ps: I have commented on the users question with no reply. ps:我对用户问题发表了评论,没有回复。

I think the actual code of the Expression which holds the map reference was not published on the answer. 我认为持有地图参考的Expression的实际代码没有在答案中公布。

In order for that sample code to work the expression must have some kind of memory. 为了使该示例代码起作用,表达式必须具有某种内存。 Actually the code manipulates the map and as the expression holds a reference to it, each subsequent call to the expression's eval -method will take the adjusted value. 实际上代码操纵地图,并且当表达式保存对它的引用时,对表达式的eval -method的每次后续调用都将采用调整后的值。

So in order to get the code working you will first need an expression implementation, which holds a map reference. 因此,为了使代码工作,您首先需要一个表达式实现,它包含一个map引用。 But beware of any sideeffects, which such an expression may have. 但要注意这种表达可能带来的任何副作用。

So for that specific example, the code must be something like the following (don't have time to completely look whether it will work, but you will get the idea: the important thing is, that the expression holds a reference which is altered from outside): 因此,对于该特定示例,代码必须类似于以下内容(没有时间完全查看它是否可行,但是您将得到这个想法:重要的是,表达式包含一个参考,该参考由外):

static Expression parse(String expression, Map<String, String> variables) {
    return new PseudoCompiledExpression(expression, variables);
}
static class PseudoCompiledExpression implements Expression {
    Map<String, String> variables;
    Expression wrappedExpression;
    PseudoCompiledExpression(String expression, Map<String, String> variables) {
       this.variables = variables;
       wrappedExpression = eval(expression, variables);
    }
   public double eval() {
        // the state of the used map is altered from outside... 
        return wrappedException.eval();
   }

Sorry for the confusion. 对困惑感到抱歉。 The " parse " function I referred to is simply the existing eval function, but renamed since it returns an Expression object. 我所提到的“ parse ”函数只是现有的eval函数,但由于它返回了一个Expression对象而重命名。

So you'd have: 所以你有:

public static Expression parse(String str, Map<String,Double> variables) { ... }

And invoke it by: 并通过以下方式调用它:

Map<String,Double> variables = new HashMap<>();
Expression exp = parse("x+(sqrt(x))", variables);
for (double x = 100; x <= +120; x++) {
    variables.put("x", x);
    System.out.println(x + " => " + exp.eval());
}

One other thing: It's necessary to know at parse time whether a name refers to a variable or a function, in order to know whether or not it takes an argument, but you can't call containsKey on the variables map during the parse, since the variables might not be present in the map until exp.eval() is called! 另一件事:在解析时需要知道名称是指变量还是函数,以便知道它是否需要参数,但是在解析期间你不能在变量映射上调用containsKey ,因为在调用exp.eval()之前,变量可能不会出现在映射中! One solution is to put functions in a map instead, so you can call containsKey on that: 一种解决方案是将函数放在映射中,因此您可以在其上调用containsKey

    } else if (ch >= 'a' && ch <= 'z') { // functions and variables
        while (ch >= 'a' && ch <= 'z') nextChar();
        String name = str.substring(startPos, this.pos);
        if (functions.containsKey(name)) {
            DoubleUnaryOperator func = functions.get(name);
            Expression arg = parseFactor();
            x = () -> func.applyAsDouble(arg.eval());
        } else {
            x = () -> variables.get(name);
        }
    } else {

And then somewhere at class level, initialize the functions map: 然后在类级别的某处,初始化functions映射:

private static final Map<String,DoubleUnaryOperator> functions = new HashMap<>();
static {
    functions.put("sqrt", x -> Math.sqrt(x));
    functions.put("sin", x -> Math.sin(Math.toRadians(x)));
    functions.put("cos", x -> Math.cos(Math.toRadians(x)));
    functions.put("tan", x -> Math.tan(Math.toRadians(x)));
}

(It would also be okay to define the functions map as a local variable inside the parser, but that adds a little bit more overhead during each parse.) (将functions map定义为解析器中的局部变量也是可以的,但是在每次解析时会增加一些开销。)

I found the above grammar to not work for exponentiation. 我发现上面的语法不能用于取幂。 This one works: 这个工作:

public class BcInterpreter {

static final String BC_SPLITTER = "[\\^\\(\\/\\*\\-\\+\\)]";
static Map<String,Double> variables = new HashMap<>();
private static final Map<String,DoubleUnaryOperator> functions = new HashMap<>();

static {
    functions.put("sqrt", x -> Math.sqrt(x));
    functions.put("sin", x -> Math.sin(Math.toRadians(x)));
    functions.put("cos", x -> Math.cos(Math.toRadians(x)));
    functions.put("tan", x -> Math.tan(Math.toRadians(x)));
    functions.put("round", x -> Math.round(x));
    functions.put("abs", x -> Math.abs(x));
    functions.put("ceil", x -> Math.ceil(x));
    functions.put("floor", x -> Math.floor(x));
    functions.put("log", x -> Math.log(x));
    functions.put("exp", x -> Math.exp(x));
    // TODO: add more unary functions here.
}

/**
 * Parse the expression into a lambda, and evaluate with the variables set from fields
 * in the current row.  The expression only needs to be evaluated one time.
 * @param recordMap
 * @param fd
 * @param script
 * @return
 */
static String materialize(Map<String, String> recordMap, FieldDesc fd, String script){
    // parse the expression one time and save the lambda in the field's metadata
    if (fd.get("exp") == null) {
        fd.put("exp", parse(script, variables));
    }
    // set the variables to be used with the expression, once per row
    String[] tokens = script.split(BC_SPLITTER);
    for(String key : tokens) {
        if (key != null) {
            String val = recordMap.get(key.trim());
            if (val != null)
                variables.put(key.trim(), Double.parseDouble(val));
        }
    }
    // evaluate the expression with current row's variables
    return String.valueOf(((Expression)(fd.get("exp"))).eval());
}

@FunctionalInterface
interface Expression {
    double eval();
}

static Map<String,Double> getVariables(){
    return variables;
}

public static Expression parse(final String str,Map<String,Double> variables) {
    return new Object() {
        int pos = -1, ch;

        //if check pos+1 is smaller than string length ch is char at new pos
        void nextChar() {
            ch = (++pos < str.length()) ? str.charAt(pos) : -1;
        }

        //skips 'spaces' and if current char is what was searched, if true move to next char return true
        //else return false
        boolean eat(int charToEat) {
            while (ch == ' ') nextChar();
            if (ch == charToEat) {
                nextChar();
                return true;
            }
            return false;
        }

        Expression parse() {
            nextChar();
            Expression x = parseExpression();
            if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
            return x;
        }

        // Grammar:
        // expression = term | expression `+` term | expression `-` term
        // term = factor | term `*` factor | term `/` factor
        // factor = base | base '^' base
        // base = '-' base | '+' base | number | identifier | function factor | '(' expression ')'

        Expression parseExpression() {
            Expression x = parseTerm();
            for (;;) {
                if (eat('+')) { // addition
                    Expression a = x, b = parseTerm();
                    x = (() -> a.eval() + b.eval());
                } else if (eat('-')) { // subtraction
                    Expression a = x, b = parseTerm();
                    x = (() -> a.eval() - b.eval());
                } else {
                    return x;
                }
            }
        }

        Expression parseTerm() {
            Expression x = parseFactor();
            for (;;) {
                if (eat('*')){
                    Expression a = x, b = parseFactor(); // multiplication
                    x = (() -> a.eval() * b.eval());
                } else if(eat('/')){
                    Expression a = x, b = parseFactor(); // division
                    x = (() -> a.eval() / b.eval());
                } else {
                    return x;
                }
            }
        }

        Expression parseFactor(){
            Expression x = parseBase();
            for(;;){
                if (eat('^')){
                    Expression a = x, b = parseBase();
                    x = (()->Math.pow(a.eval(),b.eval()));
                }else{
                    return x;
                }
            }
        }
        Expression parseBase(){
            int startPos = this.pos;
            Expression x;
            if (eat('-')){
                Expression b = parseBase();
                x = (()-> (-1)*b.eval());
                return x;
            }else if (eat('+')){
                x = parseBase();
                return x;
            }
            if (eat('(')) { // parentheses
                x = parseExpression();
                eat(')');
                return x;
            } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
                while ((ch >= '0' && ch <= '9') || ch == '.'){
                    nextChar();
                }
                double xx = Double.parseDouble(str.substring(startPos, this.pos));
                x = () -> xx;
                return x;
            } else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') { // functions and variables
                while (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') nextChar();
                String name = str.substring(startPos, this.pos);
                if (functions.containsKey(name)) {
                    DoubleUnaryOperator func = functions.get(name);
                    Expression arg = parseFactor();
                    x = () -> func.applyAsDouble(arg.eval());
                } else {
                    x = () -> variables.get(name);
                }
                return x;
            }else {
                throw new RuntimeException("Unexpected: " + (char)ch);
            }
        }
    }.parse();
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM