最好的算法用于评估数学EX pression? [英] Best algorithm for evaluating a mathematical expression?

查看:116
本文介绍了最好的算法用于评估数学EX pression?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是用于评估数学EX pression最好的算法?我希望能够优化这一点的,我可以有一个公式的各种变量,我可能需要评估使用不同的变量数百倍的感觉。所以基本上,如果我可以初步解析公式,使其优化以某种方式,然后我可以在变量传递给该优化的版本很多次,因为我需要,每次它产生了我的结果。

What's the best algorithm for evaluating a mathematical expression? I'd like to be able to optimize this a little in the sense that I may have one formula with various variables, which I may need to evaluate hundreds of times using different variables. So basically if I can initially parse the formula so that it is optimized in some way, and I can then pass in the variables to this optimized version as many times as I need, each time it produces a result for me.

我会在任何Delphi或C#写这个。我已经写了使用调车场算法类似的东西,但每次我需要计算相同的公式时,我不必通过解析阶段。必须有一个更好的方式来做到这一点。

I'll be writing this in either Delphi or C#. I have already written something similar by using the shunting yard algorithm, but each time I need to calculate the same formula, I'm having to go through the parsing stage. There must be a better way to do this.

推荐答案

如果你想用Delphi做到这一点,你可以看看如何使用 JclEx preVAL 单位工程中, JEDI code库的一部分。我写这在几年前(这是一个有点过度设计);它解析函数和变量,可以交给你回来的方法指针,快速评估EX pression。通过引用传递的变量,你可以直接改变他们和重新评估EX pression会相应计算。

If you want to do it with Delp you could look into how the JclExprEval unit works, part of the JEDI Code Library. I wrote it several years ago (it's a little over-engineered); it parses functions and variables and can hand you back a method pointer which evaluates the expression quickly. Pass the variables in by reference, and you can change them directly and the re-evaluated expression will be calculated accordingly.

在任何情况下,它是如何工作的基础知识可能对你有所帮助。使前pressions递归下降解析是容易的,通过建立一棵树,你可以评价多次,而无需重新解析。 JclEx preVAL实际上产生了一个简单的堆栈机code,使之能工作比树间pretation快一点;堆机在很大程度上限制了其内存操作阵列,并使用开关操作codeS,而树间pretation如下整个堆的联系,常常使用虚拟讯(或双调度)的运算codeS,所以他们通常最终会慢一些。

In any case, the basics of how it works may be helpful for you. Recursive-descent parsing of expressions is easy, and by building a tree you can evaluate multiple times without re-parsing. JclExprEval actually generates code for a simple stack machine, so that it can work a little faster than tree interpretation; stack machines largely restrict their memory operations to arrays and use switches for opcodes, while tree interpretation follows links throughout the heap and often uses virtual dispatch (or double-dispatch) for opcodes, so they usually end up slower.

采取同样的方法,因为 JclEx preVAL 的解析,但用C#编写,并建立了一个防爆pression ,像马克建议,是另一种非常有效的方法。在JIT编译EX pression应该比一个跨preTED EX pression程序或树,它本身有很多比分析更快相当快一点。

Taking the same approach as JclExprEval in parsing but written in C#, and building up an Expression, like Marc suggests, is another perfectly valid approach. The JIT-compiled expression ought to be quite a bit faster than an interpreted expression program or tree, which themselves are a lot faster than parsing.

这篇关于最好的算法用于评估数学EX pression?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆