简体   繁体   中英

Representing Math Equations as Java Objects

I am trying to design a way to represent mathematical equations as Java Objects. 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). 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.

  • Equation

  • -This is a collection of s 的集合
  • -The toString() method of Equation would call the toString() method of all of its s and concatenate the results. 的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 s: one with a variable "x" and an exponent "2" and another which is just a constant "3." 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? Are there any libraries that already do this?

what would be the best way to represent math equations as Java objects?

I want you to notice, you don't have any equations. Equations look like this;

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 has three subclasses: Constant (eg 3), Variable (eg x), and Operation.

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.

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. 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/

If you "just" want to evaluate an input string, then have a look at the snippet compiler in the Javassist library.

Here I described the representation of parsed math expressions as Abstract Syntax Trees in the Symja project.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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