I'm currently working on a Calculator app and I want output to be in both a simplified expression and decimal answer form. An example would be sqrt 2 * sqrt 3 = sqrt 6 which can also be outputted as 2.44948... What is the best approach to this and is there any well-established algorithms to do this?
Yes. What you likely want is a computer algebra system, which understands formulas as artifacts to be manipulated by explicit mathematics rules.
Mathematica and Macsyma are applications which do this. However, these are quite sophisticated systems and it is not easy to see how they "work".
What you need to do is:
- Represent formulas as abstract syntax trees
- Parse text formulas (your example equations) into such trees
- Encode a set of tree-manipulation rules that represent algebra operations
- Apply these rules to your algebra trees
- Prettyprint the algebra trees back as text when done
Rules are best written in the surface syntax of algebra. (Mathematica doesn't do this; it represents formulas as trees using a kind of prefix S-expressions, and rules as the same kind of trees with special variable nodes).
One of the issues is deciding how many "algebra" rules you are wiling to encode. Mathematics is a lot more than pure 9th grade algebra, and people using such systems tend to want to extend what is there by adding more knowledge (the point of Mathematica and Macsyma: they are infinitely extendable).
Here's a very simple version of this. You can see all the "gears" and how things are described, in terms of parse trees and rewrite rules.
http://www.semdesigns.com/products/DMS/SimpleDMSDomainExample.html
来源:https://stackoverflow.com/questions/24948079/expression-simplification-algorithm