Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Inputting Math Functions

by fredopalus (Friar)
on Dec 17, 2006 at 18:08 UTC ( #590328=perlquestion: print w/ replies, xml ) Need Help??
fredopalus has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone have any suggestions on how to read an inputted math function in order to implement it in the script?

Comment on Inputting Math Functions
Re: Inputting Math Functions
by jettero (Monsignor) on Dec 17, 2006 at 18:13 UTC
    The best plan if you want to be really robust is to choose a parser package. Parse::RecDescent seems to be very popular. I used Parse::Lex to parse SGF files for Games::Go::SGF2misc, but it's a little tricky to get installed if things don't match up right.

    -Paul

Re: Inputting Math Functions
by gaal (Parson) on Dec 17, 2006 at 18:15 UTC
    The first suggestion would be find out what expressions you want to support. Is it just the four basic arithmetic ops? What representations do you need of numbers (eg., base ten? Scientific notation like 8E6? Rationals? Arbitrary precision reals? Complex numbers?). Do you need precedence to work implicitly (eg., is "32 + 9/5 * x" "32 + ((9/5) * x)"? Or "((32 + 9)/5) * x"? Something else)? Do you need to allow arbitrary functions that you had defined earlier?

    Depending on how simple your needs, you may be able to use a simple regular expression to do the parsing for you, or you may need something more involved.

      Yeah, I would need to work within the set of reals, using transcendental functions as well as polynomials, rationals, etc..
        Then you should use a Real Parser to make sense of your expression. Parse::RecDescent was mentioned and is fine to use, if a little slow. Maybe you want to try this out in Perl 6 Rules though? You'd need Pugs for that, or perhaps stay in Perl 5 and give Pugs::Compiler::Rule a shot.
Re: Inputting Math Functions
by pKai (Priest) on Dec 17, 2006 at 20:19 UTC
Re: Inputting Math Functions
by shotgunefx (Parson) on Dec 17, 2006 at 23:19 UTC
    There are more than a few modules on CPAN to help.

    If it's simpler math, here's an old crack I took at it. Evaluate Expressions.

    -Lee
    "To be civilized is to deny one's nature."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://590328]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-08-29 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (277 votes), past polls