自定义的帧间preTER数学EX pressions [英] Custom interpreter for mathematical expressions

查看:172
本文介绍了自定义的帧间preTER数学EX pressions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要评价含有大量的变量前pressions和我正在考虑写一个小自定义的帧间preTER保持快速编译和小。不过,我有这个主题上没有经验,并有几个问题。

I have to evaluate a large number of expressions containing variables and I am thinking about writing a small custom interpreter to keep compilation fast and small. However I have no experience with this topic and have a few questions.

假设我们有一个数学前pressions文件和一组有限的对象。该文件可能是这样的:

Say we have a file with mathematical expressions and a limited set of objects. The file could look like:

expr[x,y,z] = 2*x*y + x^2 + 28/14*z*(x*y^2 + 15*z) + ...

我想分析这个不知何故,所以我可以在我的应用程序数值评估前pressions
通过简单地调用一个函数 EXPR(浮点X,浮Y,浮动Z)。参数的数量不应该是固定的(编辑:每前pression将有适当的参数数自己的定义或将接受一个数组)和括号的嵌套应该被允许保持输入文件相当小。

I'd like to parse this somehow so I can evaluate the expressions numerically in my application by simply calling a function expr(float x, float y, float z). The number of parameters should not be fixed ( every expression would have its own definition with the appropriate number of parameters or would accept an array) and nesting of parenthesis should be allowed to keep the input files reasonably small.

由于前pressions都是多项式的类型,我能想到的数据结构应该什么样子,但是看起来解析困难。使用Lua我已经找到了一些答案有些类似的问题在这里所以,举例来说。

Since the expressions are all of polynomial type, I can think of how the data structure should look like, but parsing looks difficult. I have already found some answers to somewhat similar questions here on SO, for instance using Lua.

最大的问题,但是,是创造和相比,直接编译自动生成的C code这些前pressions调用这些对象时的性能损失将是什么。

The biggest question, however, is what the performance penalty would be when creating and calling those objects as compared to directly compile these expressions from automatically generated C code.

在此先感谢!

编辑:请考虑 EXPR()以上仅作为这样的例子。我想最好的办法是有一个持有系数和稀疏数组变量的权力模板化类的对象。

Please consider the example of expr() above only as such. I guess the best way would be to have objects of a templated class that holds coefficients and powers of the variables in sparse arrays.

推荐答案

性能是有点长度的一计件的串一个问题。国米preTED语言pretty多少总是比编译的C code,以评估算术前pressions慢。但是没有那么多的程序花费大部分时间做算术,所以大部分表示无所谓的时间。这也使得有差别是否解析前pression每次评估,或(如从你说的话似乎更容易)时,它解析成某种中间形式。

Performance is a bit of a length-of-a-piece-of-string issue. Interpreted languages pretty much always are slower than compiled C code to evaluate arithmetic expressions. But not that many programs spend the majority of their time doing arithmetic, so most of the time that doesn't matter. It also makes a difference whether you parse the expression every time you evaluate it or (as seems more likely from what you say), parse it into some intermediate form.

这是不可能的告诉一下你所说的,它是否会关系到你,或者如何快速跨preTER你会写,但我不会期望它要慢一些更好的10倍以上,如据花时间评估前pressions关注。在国米pretation首先尝试已经差远了。

It's impossible to tell from what you've said, whether it will matter to you, or how fast an interpreter you will write, but I wouldn't expect it to be better than 10 times slower, as far as time spent evaluating the expressions is concerned. First attempts at interpretation have been far worse.

至于说中间形式 - 通常开始的地方是使用Dijkstra的分流码算法到你的缀前pressions转换为逆波兰形式。这就给了你符号的序列,字节codeS,叫他们你喜欢什么,而且很容易写一个前pression评估为形式 - 每个运营商只是弹出它的操作数从堆栈中,执行运算,然后推结果压入堆栈,直到前pression的终值是在最后留下的唯一的东西。数字文字和变量名就这样突然没有操作数,并把他们的价值经营者。

As for that intermediate form - the usual place to start is to use Dijkstra's "shunting-yard" algorithm to convert your infix expressions to a reverse Polish form. That gives you a sequence of "symbols", "byte codes", call them what you like, and it's easy to write an expression evaluator for that form - each operator just pops its operands from a stack, performs the op, then pushes the result onto the stack, until the final value of the expression is the only thing left at the end. Numeric literals and variable names are just like "operators" that pop no operands, and push their value.

这篇关于自定义的帧间preTER数学EX pressions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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