如何操作数学符号?
这更多的是一个“教育”问题。 :)
虽然,我最终可能想做这样的事情。
所以,假设我有一个方程。任何形式的方程都可以,只要它不是荒谬的,而且是一个擅长数学的人,都可以解出来。
比方说... 0 = (x-1)(x+2)
或... y = (x^2), y = 1/x
或正弦函数等。基本上,就像我们在学校做的那样做数学。
问题是,我该如何编写计算机程序来解决这个问题?我知道这是可能的,因为像 Mathematica、Maple 等程序几十年来一直在这样做!但我找不到任何关于如何制作简单方程求解器的好的文档。
我不期望答案告诉我“这正是你要做的”,因为当然这样的事情是一个完整的大型程序,而不仅仅是一个代码片段。
但只是一般概述,或者一些好的文档的链接?那太好了!谢谢:)
尤其是所需的数据结构和算法。
如果做不到这一点,我只需要弄清楚如何解方程,然后对其进行编码。但这实际上需要几个月的时间才能完成(我以前做过这种事情,将我自己的思维过程形式化为代码,它可以工作,但速度很慢)。
This is more of an "educational" question. :)
Although, I probably would like to do something like this eventually.
So, let's say I got an equation. Could be any kind of equation, as long as it's not ridiculous and also a human who was good at math, could solve it.
Let's say... 0 = (x-1)(x+2)
or... y = (x^2), y = 1/x
Or sine functions, etc. Basically, doing math like we did in school.
the question is, how would I write a computer program to solve this? I know it's possible, because programs like Mathematica, Maple, etc, have been doing this for decades! But I can't find any good documentation on how to make even a simple equation solver.
I don't expect answers that tell me "this is exactly how you do it" because of course such a thing is an entire large program, not just a code snippet.
But just a general overview, or links to some good documents? That would be great! Thanks :)
Especially the kind of data structures and algorithms needed.
Failing that, I'll just have to figure out HOW I SOLVE EQUATIONS, and encode that. But that takes literally months to get right (I've done this sort of thing before, formalising my own thinking process into code, it works but it's slow).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看一些关于符号操作的论文。
Peter Norvig 的 PAIP 书涵盖了一个非常简单的符号操作和方程求解系统,所以这是值得的一读。它介绍了名为 MacSyma 的人工智能程序的基础知识,该程序最终形成了 Mathematica。
Have a look at some papers on symbolic manipulation.
Peter Norvig's PAIP book covers a very simple system for symbolic manipulation and solving of equations, so that'd be worth a read. It introduces the basics of an AI program called MacSyma which eventually formed the basis of Mathematica.
Wolfram Alpha 将是您最容易获得的基准测试。
您的输入是字符串,因此第一步是编写词法分析器/解析器以将这些字符串分解为标记并将它们放入抽象语法树 (AST) 中。
您没有说明要使用哪种语言来实现此功能,但我建议您查看 ANTLR。它是一个解析器生成器,可以帮助您创建 AST。你必须为你的方程想出一个语法。
一旦你有了 AST,你的解算器就会遍历树,并将更具体的操作与“+”、“-”等符号关联起来。你可以处理的运算符越多,你的解算器就会越强大、越全面。
但是,您必须处理或排除许多复杂情况:
我建议您从简单的算术和多项式开始,然后逐步提高。史蒂芬·沃尔夫勒姆 (Stephen Wolfram) 并不是一天就能写出 Mathematica 的。
Wolfram Alpha would be the benchmark that is most easily available to you.
Your inputs are strings, so the first step is to write a lexer/parser to break those strings into tokens and put them into an abstract syntax tree (AST).
You don't say what language you want to implement this in, but I'd recommend looking at ANTLR. It's a parser generator that can help you create the AST. You'll have to come up with a grammar for your equations.
Once you have the AST, your solver will walk the tree and associate more specific operations with symbols like "+", "-", etc. The more operators you can handle, the more powerful and all-encompassing your solver will be.
But there are a lot of complexities that you'll have to either deal with or exclude:
I'd recommend that you start with simple arithmetic and polynomials and work your way up. Stephen Wolfram didn't write Mathematica in a day.
基本技术是在计算机程序中表示数学方程的结构。这与编译器的想法非常相似,但编译器主要将其输入转换为机器可读的格式,而计算机代数系统主要产生与其输入相同格式的输出,但以某种有趣的方式进行转换。无论哪种情况,立即输出都是抽象语法树。下一步将应用一些模式匹配技术(类似于正则表达式的工作原理)以及一些机械转换,以某种有用的方式重写树。
如果您想了解这实际上是如何完成的,SymPy 是一个 Python 符号数学库,它既是开源的,又恰好主要关注该主题的符号操作方面。
The basic technique is to represent the structure of the mathematical equation in the computer program. This is a very similar idea to what compilers do, but compilers mostly transform their inputs into a machine readable format, but Computer Algebra Systems mostly produce output of the same format as their inputs, but transformed in some interesting way. In either case, the immediate output is an abstract syntax tree. The next step will be to apply some pattern matching techniques (similar to how regular expressions work) as well as some mechanical transformations to rewrite the tree in some useful way.
If you want to see how this might actually be done, SymPy is a python symbolic math library that's both open source and happens to focus on mainly the symbol manipulation aspects of the topic.
除了其他人的有用答案之外:此链接似乎很有趣: http://en.wikipedia.org/ wiki/Pattern_matching 另外,“抽象树语法”似乎很有趣。基本上,它是在语法树上进行“模式匹配”!有点像正则表达式,但用于代码。
我实际上已经编写了自己的“抽象树语法”:) 所以我已经在符号操纵器的道路上走了一小段路。
In addition to the useful answers by other people: This link seems interesting: http://en.wikipedia.org/wiki/Pattern_matching also, the "abstract tree syntax" seems interesting. Basically, it is about doing "pattern matching" on a syntax tree! Sort of like regex but for code.
I've actually written my own "abstract tree syntax" already :) So I'm already a little way down the path to a symbolic manipulator.