简体   繁体   English

将数学方程表示为Java对象

[英]Representing Math Equations as Java Objects

I am trying to design a way to represent mathematical equations as Java Objects. 我试图设计一种将数学方程表示为Java对象的方法。 This is what I've come up with so far: 这是我到目前为止所提出的:

  • Term 术语
  • -Includes fields such as coefficient (which could be negative), exponent and variable (x, y, z, etc). - 包括诸如系数(可以是负数),指数和变量(x,y,z等)之类的字段。 Some fields may even qualify as their own terms alltogether, introducing recursion. 有些字段甚至可能完全符合他们自己的条件,引入递归。
  • -Objects that extend Term would include things such as TrigTerm to represent trigonometric functions. - 扩展Term的对象将包括诸如TrigTerm之类的东西来表示三角函数。

  • Equation 方程

  • -This is a collection of Term s - 这是Term的集合
  • -The toString() method of Equation would call the toString() method of all of its Term s and concatenate the results. - Equation的toString()方法将调用其所有Term的toString()方法并连接结果。

The overall idea is that I would be able to programmatically manipulate the equations (for example, a dirivative method that would return an equation that is the derivative of the equation it was called for, or an evaluate method that would evaluate an equation for a certain variable equaling a certain value). 总的想法是我能够以编程方式操纵方程式(例如,一个能够返回方程式的方向式方法,这个方程式是它所要求的方程式的导数,或者是一个评估方程式来评估一个方程式的方程式变量等于某个值)。


What I have works fine for simple equations: 我对简单方程式的效果很好:
x ^ 2 + 3
This is just two Term s: one with a variable "x" and an exponent "2" and another which is just a constant "3." 这只是两个Term s:一个带有变量“x”和一个指数“2”,另一个带有一个常量“3”。


But not so much for more complex equations: 但对于更复杂的方程式而言并非如此:
替代文字
Yes, this is a terrible example but I'm just making a point. 是的,这是一个可怕的例子,但我只是说明一点。


So now for the question: what would be the best way to represent math equations as Java objects? 那么现在问题是:将数学方程表示为Java对象的最佳方法是什么? Are there any libraries that already do this? 有没有图书馆已经这样做了?

what would be the best way to represent math equations as Java objects? 将数学方程表示为Java对象的最佳方法是什么?

I want you to notice, you don't have any equations. 我要你注意,你没有任何方程式。 Equations look like this; 方程看起来像这样;

x = 3 x = 3

What you have are expressions: collections of symbols that could, under some circumstances, evaluate out to some particular values. 你所拥有的是表达式:在某些情况下可以评估某些特定值的符号集合。

You should write a class Expression. 你应该写一个类Expression。 Expression has three subclasses: Constant (eg 3), Variable (eg x), and Operation. 表达式有三个子类:常量(例如3),变量(例如x)和操作。

An Operation has a type (eg "exponentiation" or "negation") and a list of Expressions to work on. 操作具有类型(例如“取幂”或“否定”)以及要处理的表达式列表。 That's the key idea: an Operation, which is an Expression, also has some number of Expressions. 这是关键的想法:一个操作,它一种表达,也表达的一些数字。

So your 所以你的 is SUM(EXP(X, 2), 3) -- that is, the SUM Operation, taking two expressions, the first being the Exponentiation of the Expressions Variable X and Constant 2, and the second being the Constant 3. 是SUM(EXP(X,2),3) - 即SUM操作,取两个表达式,第一个是表达式变量X和常量2的指数,第二个是常量3。

This concept can be infinitely elaborated to represent any expression you can write on paper. 这个概念可以无限细化,以表示您可以在纸上书写的任何表达。

The hard part is evaluating a string that represents your expression and producing an Expression object -- as someone suggested, read some papers about parsing. 困难的部分是评估表示表达式的字符串并生成一个Expression对象 - 正如有人建议的那样,阅读一些关于解析的文章。 It's the hardest part but still pretty easy. 这是最难的部分,但仍然很容易。

Evaluating an Expression (given fixed values for all your Variables) and printing one out are actually quite easy. 评估表达式(给出所有变量的固定值)并打印出一个实际上很容易。 More complicated transforms (like differentiation and integration) can be challenging but are still not rocket science. 更复杂的变换(如差异化和整合)可能具有挑战性,但仍然不是火箭科学。

Consult a good compiler book for details about how to write the part of a compiler that converts input into an expression tree. 有关如何编写将输入转换为表达式树的编译器部分的详细信息,请参阅一本好的编译器书。

You might find this series inspirational: http://compilers.iecc.com/crenshaw/ 您可能会发现这个系列鼓舞人心: http//compilers.iecc.com/crenshaw/

If you "just" want to evaluate an input string, then have a look at the snippet compiler in the Javassist library. 如果您“只是”想要评估输入字符串,那么请查看Javassist库中的代码段编译器。

Here I described the representation of parsed math expressions as Abstract Syntax Trees in the Symja project. 在这里,我将解析的数学表达式的表示描述为Symja项目中的抽象语法树

The D[f,x] function in the D.java file implements a derivative function by reading the initial Derivative[] rules from the System.mep file. D.java文件中的D[f,x]函数通过从System.mep文件中读取初始Derivative[]规则来实现派生函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM