简体   繁体   中英

Pattern Matching in Java

I'm working on java program that can compute the differentiation of any given mathematical expression. When User types the expression say Sin(Cos(x^2)) I need to check whether it is mathematically valid or not. Of course we can do it by manual code writing but that would be tedious work. Can I do it using regex package of java??

How??

thanx..

No, regexes aren't sufficient... (in fact, this can be mathematically proved).

To parse mathematical expressions, you can either write your own parser (which isn't actually that hard) or use an existing one, such as Jep .

Here are some previous SO questions on parsing mathematical expressions:

Best algorithm for evaluating a mathematical expression?

Reading and running a mathematical expression in Python

Evaluating mathematical expressions

JEV是一个不错的选择,因为JEP现在已获得商业许可。

if you are trying to accept any mathematical expression then regex will not work for you.

you will need to use a parser to check your expression and decide if its valid or not base on a specified mathematical grammar.

have a lot at antlr

如果您想自己编码,那么您可以使用广泛用于此目的的波兰表示法

Another alternative is the JScience library. The abstract class Function includes a differentiate() method, which is implemented for discrete, polynomial, and rational functions.

Computer algebra systems (like for example Mathematica) use rule notations like the following for differentiation:

  D[Cos[x_],y_]:=(-1)*Sin[x]*D[x,y]
  D[Sin[x_],y_]:=Cos[x]*D[x,y]

these transformation rules are also sometimes called "pattern-matching rules", but they have nothing to do with regex pattern-matching for strings. See Introduction to Patterns for more information.

I know these open source Java projects, which contain a similar pattern-matching rule engine:

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