Parsing math equations [closed]

2019-08-02 19:22发布

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]

4条回答
劫难
2楼-- · 2019-08-02 19:24

Python or matlab would be my suggestions. Are you planning on storing the whole equation in a string, and then split it up, to factor and simplify?

Give some more information, kindof a cool project.

查看更多
倾城 Initia
3楼-- · 2019-08-02 19:30

This is an old question, but I'd like to suggest you MathParseKit.

This is a C++ library that given a string like "2*3/4" gives you a Tree of functions/variable/constants that defines the expression.

You can solve it, but you can even change it and put it again in string format.

You can find it at:

https://github.com/B3rn475/MathParseKit

查看更多
戒情不戒烟
4楼-- · 2019-08-02 19:35

It's been done in just about every language.

Python has a library for parsing algebraic equations and symbolic mathematics all ready to go:

http://code.google.com/p/sympy/

I'd recommend reusing, unless your purpose is to learn how to write such a thing.

查看更多
你好瞎i
5楼-- · 2019-08-02 19:38

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.

查看更多
登录 后发表回答