Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

RFC: Module for evaluation math. expressions

by moritz (Cardinal)
on Sep 07, 2007 at 09:44 UTC ( [id://637614]=perlmeditation: print w/replies, xml ) Need Help??

Dear fellow monks,

I'm preparing my first CPAN module, which is a parser and evaluator for mathematical expressions.

I know there are some modules like that no CPAN, but Math::Expr says in section Bugs "The parses does not handle bad strings in a decent way. If you try to parse a string that does not follow the specification above, all strange things might happen..." which scares me. And Math::Expression and Math::Calculus::Expression seem both fine, but they are overkill when I simply want to parse and evaluate numerical expressions.

Anyway, what I am looking for is a good name. Currently I consider Math::Lite::Expression or Math::Eval. Do you have better suggestions, or even better ideas?

Secondly I am looking for advise on the interface.

Currently it looks like this (in examples):

# does what you think: print Math::Light::Expression->new("2*3")->val(); # force the statement delimiter to be ';'. # otherwise it's optional, and whitespaces # delimit expressions my $m = Math::Light::Expression->new({force_semicolon => 1}); $m->parse("a = 2; a^(a+b)")->val({ b => 3}); # arguments to val() overwrite variables in statements: $m->parse("a=1; a*2")->val({ a => -1 }); # result: -2

Is there anything that could be improved?

(Update: fixed typo)

Update: thank you for your input, everybody.

I named my module Math::Expression::Evaluator now and uploaded it to cpan.

Replies are listed 'Best First'.
Re: RFC: Module for evaluation math. expressions
by planetscape (Chancellor) on Sep 07, 2007 at 09:59 UTC
      I know about CAS, but that's not what I'm doing.

      The module does no algebraic calculations, it just defers the evaluation of symbols (but it doesn't simplify, transform etc.).

      That's why I don't want to use terms such as "Symbolic" or "Algebra" in the name, it would be misleading.

Re: RFC: Module for evaluation math. expressions
by Limbic~Region (Chancellor) on Sep 07, 2007 at 13:15 UTC
    moritz,
    I know there are some modules like that no CPAN

    s/no/on/

    You may find Breaking The Rules II of moderate interest. I would kind of like to see the parsing and the evaluation as different steps. That way, someone could come along and use it as a way to teach order of operations to a school kid. You hand them the parse tree and they can step through the evaluation.

    Cheers - L~R

      Breaking The Rules II is of interest indeed.

      My parser consists of a self made lexer and a recursive descending parser. The code is quite lengthy, but not really complicated.

      Parsing and evaluation are different steps indeed, you can do something like

      use Data::Dumper; my $m = Math::Light::Expression->new; $m->parse($expression); print Dumper $m->{ast}; for (0 .. 9) { print $val->({ a => $_ }), "\n"; # evaluate multiple times }

      I experimented with a LaTeX emitting backend, but that goes against the grain of keeping modules small and doing exactly what they are meant for.

      I considered a PIR emitter as well, but PIR handles precedence on its on, so there is now real need to for my parser ;-)

        Math::Symbolic::Custom::LaTeXDumper can dump Math::Symbolic trees to LaTeX, but while the basic transformation is trivial, generating good-looking formulas automatically is extremely hard. That aforementioned module doesn't extend a lot of cycles on being too smart, but it works okay for reasonably simple formulas.

        Cheers,
        Steffen

Re: RFC: Module for evaluation math. expressions
by shotgunefx (Parson) on Sep 07, 2007 at 23:30 UTC
    Not sure what would be the best name. Is there a Math::Expression::Simple? If you were going with Light, I think personally, I find Math::Expression::Light better semantically.

    I did something similar a while back. It had some non-math functions, though it didn't support multiple statements per expression. Though I might go fix that now. ;)

    I started wrapping it up into a module and I too had some trouble thinking of an appropriate package, then other events caused it to get dropped by the way side, so I never did come up with what I thought was an appropriate name.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://637614]
Approved by planetscape
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-25 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found