解析数学方程 [英] Parsing math equations

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

问题描述

只是为了踢球,我试图创建一个可以简化,因子和展开代数方程的应用程序。如果我可以将方程式变成一个可行的格式,编程规则看起来好像是直截了当的。解析方程式被证明是一件麻烦事,目前正在使用Python,但我并不反对学习新的东西。



是否有任何库(对于任何语言)会使此项目变得非常简单,或者是管道梦想?

[用Haskell标记,因为我觉得'simple'是这样的]

解决方案

是的,Haskell有许多许多库,可以使解析器合理简单。 Parsec 是一个很好的开始,它甚至还有其他语言的克隆,包括 Python (该文章也链接到 pyparsing ,它看起来像也可能工作)。



是一个例子(注意,它可能不是一流的Parsec或Haskell):它表示的是Haskell的解析库,正好有4行代码实现了整个解析器。



您也可以浏览旧问题和答案,以了解各种图书馆和技术,例如 parsec parsing+haskell 解析+ python

为大型项目制定攻击线的最佳方式是从小处着手,直到适应您的工具为止:选择一个并尝试实现一个相对简单的解析器,就像解析带有数字的表达式 + * ,或者甚至只是使用括号来解析数字和 + ...有点小(但不能太小;这两个例子每个都有不重要的地方,第一个具有运算符优先级,第二个具有递归嵌套)。如果您不太喜欢这个图书馆,请尝试其他图书馆。


Just for kicks, I'm trying to create an application that can simplify, factor, and expand algebra equations. Programming the rules seems as if it will be straight forward if I can get the equations into a good workable format. Parsing the equations is proving to be a hassle, Currently working with Python but I'm not against having to learn something new.

Are there any libraries (for any language) that would make this project pretty simple, or is that a pipe dream?

[Tagging this with Haskell because I have a feeling that's where the 'simple' is]

解决方案

Yes, Haskell has many many libraries that make writing parsers reasonably easy. Parsec is a good start, and it even has clones in other languages, including Python (that article also links to pyparsing which looks like it might also work).

This answer of mine is an example (note, it's probably not top-notch Parsec or Haskell): it's indicative of the power of Haskell's parsing libraries, precisely 4 lines of code implement the whole parser.

You could also browse old questions and answers to get a feel for the various libraries and techniques, e.g. , parsing+haskell and parsing+python.

The best way to work out your line of attack for the larger project would be to start small and just try stuff until you're comfortable with your tools: choose a library and try to implement a relatively simple parser, like parsing expressions with just numbers, + and *, or even just parsing numbers and + with bracketing... something small (but not too small; those two examples each have non-trivialities, the first has operator precedence and the second has recursive nesting). If you don't like the library much, try a different library.

这篇关于解析数学方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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