We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › use math formulas from file
Page Index Toggle Pages: 1
use math formulas from file (Read 298 times)
use math formulas from file
Feb 5th, 2009, 5:20am
 
is it possible to have a XML file with different math formulas and have processing load it and use the formulas inside?

the formulas would have the variables using the same name as the ones i have in processing, and i would only change operators...

with this i would like to build an application but make it open to changes.
Re: use math formulas from file
Reply #1 - Feb 5th, 2009, 12:44pm
 
I see two ways (at least) to do it.
- Use a formula parser library, there are several for Java (or write your own, interesting but long and prone to bugs);
- Include a scripting language (ultimate flexibility! but quite heavier solution).
Re: use math formulas from file
Reply #2 - Feb 5th, 2009, 7:36pm
 
If processing doesn't support a PERL-style (or ruby-style) "eval" function, then writing your own parser is probably the way to go.

If you insist on writing a parser for infix notation (i.e. "sin(x) + cos(y^3)" type expressions), then bgustavocosta is right; that's a huge pile of tedium to implement and is error-prone.

But if you implement a parser for postfix/reverse-polish notation (i.e. "x sin y 3 ^ cos +") then the job becomes tremendously easier.  You just iterate over all the tokens in the expression and:
* if the token is a constant or a variable whose value you know, you push it onto a stack of values
* if the token is an operator, you pop the necessary number of arguments for it off the stack, perform the calculation, and push the result onto the stack.

For well formed expressions, the final result will be a stack with exactly one value in it that is the result of the entire expression.  See also:

http://en.wikipedia.org/wiki/Reverse_Polish_notation

which goes into it in much more detail.
Page Index Toggle Pages: 1