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

minimizing simple linear equations

by beemshake (Novice)
on Mar 13, 2006 at 19:57 UTC ( [id://536361]=perlquestion: print w/replies, xml ) Need Help??

beemshake has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Anybody know of a package that will allow me to minimize a string like

(foo * 2) + (foo * 3)

and get something like 'foo * 5' in return?

thanks!

Replies are listed 'Best First'.
Re: minimizing simple linear equations
by saintmike (Vicar) on Mar 13, 2006 at 21:07 UTC
    Math::Algebra::Symbols does a pretty good job if it doesn't get too complicated:
    use Math::Algebra::Symbols my ($foo) = symbols(qw(foo)); my $result = ($foo * 2) + ($foo * 3); print "$result\n";
    prints

        5 * $foo
    

    Here's an article on it.
      Thanks for all the tips. I think Math::Algebra::Symbols was what I was looking for, but by the time I found it I had already written the script to just run the equations through mathomatic to simplify them.
Re: minimizing simple linear equations
by gjb (Vicar) on Mar 13, 2006 at 20:44 UTC

    First off, what you're trying to accomplish is called 'simplification', not minimization. I don't want to sound pedantic, but this could put your quest on the right track.

    Secondly, you could have a look at Math::Symbolic. Unfortunately, the docs are sparse, so it's not clear to me what it does, but it seems to do at least some simplification.

    Hope this helps, -gjb-

Re: minimizing simple linear equations
by Fletch (Bishop) on Mar 13, 2006 at 20:20 UTC

    That's not exactly a trivial task. If you have something like Mathematica or Maple you could open a pipe to that and let it do the heavy lifting.

Re: minimizing simple linear equations
by shotgunefx (Parson) on Mar 13, 2006 at 23:13 UTC
    You might find this helpful.
    Evaluate Expressions. This is one of my favorite things I've ever written. It parses expressions and evaluates them. It's the basis for a mini language that got put on hold for some time.

    In the evaulate sub, right after  @$ops = grep { defined $_ } @$ops; You basically have a parse tree.

    For your example, you'd have
    (foo*2) + (foo*3) $ops = [ [ 'foo', '*', '2' ], '+', [ 'foo', '*', '3' ] ];
    You could modify it there to optimize the tree the way you w ant.

    -Lee
    "To be civilized is to deny one's nature."
Re: minimizing simple linear equations
by sgt (Deacon) on Mar 13, 2006 at 23:50 UTC
    symbolic stuff is tough; one good program that you can download from nikhef is __form__ by Jos Vermasern (at least some version if free...) I think you can easily generate a small form program, call form on it and parse it back... good luck http://www.nikhef.nl/~t68/ Math::Symbolic on CPAN can handle this too... -- sgt
Re: minimizing simple linear equations
by dynamo (Chaplain) on Mar 13, 2006 at 20:29 UTC
    If you want comprehensive symbolic manipulation, I'm not sure where to point you to..

    But for the case described above (and feel free to use this as a starting point to write your own comprehensive symbolic manipulation module), you can do:

    $string =~ s/ //gs; # remove spaces $string =~ s/\((\w+)\*(\d+)\)\+\(\1\*(\d+))/($1\*(?{$2+$3})\)/gsx;

    Which should do only exactly the substitution you describe above. (Needs testing)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-24 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found