Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Math Solver

by beakerboy (Novice)
on Jul 01, 2003 at 22:06 UTC ( [id://270657]=perlquestion: print w/replies, xml ) Need Help??

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

I'm in the process of moving some of my company's chemical prediction models out of MS Excel and into a more friendly perl environment. The one thing I'm having trouble with is some worksheets rely on the Excel solver. I'm guessing the Generalized Reduced Gradient (GRG2) nonlinear optimization code it uses is not available, but something similar HAS to be out there. I've checked the web and CPAN to no avail. I did find a Diophantine Equation Solver in the archives here, but this only can find integer results from integer inputs.
Thanks for any help,
Kevin Nowaczyk

Replies are listed 'Best First'.
Re: Math Solver
by chunlou (Curate) on Jul 02, 2003 at 00:17 UTC
    I don't think you should use Perl for solving math. Rather use Perl as a glue to interact with some outside specialized math program.

    R is a good program for statistics; Octave for matrix algebra. They are GNU software.

    Omegahat (also free) offers R-Perl bidirectional interface.

    Or, to interact with R, you can just do this in Perl:
    my $Rpath = "\\user\\R\\rw\\bin\\" ; my $Rcmd = $Rpath . "rterm --vanilla --quiet --slave" ; my $Rscript = "whatever R commands"; $Rscript =~ s/(\r|;\r)/ ;/gm ; $Rscript =~ s/<-/=/gm; # \r or <- will break "echo" print `echo $Rscript | $Rcmd`; # execute R commands & print results

    _____________________
    Update: an informative benchmark of various math packages at SciViews.
Re: Math Solver
by traveler (Parson) on Jul 01, 2003 at 23:27 UTC
    Based on this from Microsoft, they use code from these guys. That code does not appear to be free, but you may be able to call it using inline::c or some other mechanism. Or mabye you can get the GRG2 code from UT Austin which looks like the source. Oh, and GRG2 may be a trademark of "Windward Associates".

    HTH, --traveler

Re: Math Solver
by dragonchild (Archbishop) on Jul 01, 2003 at 22:59 UTC
    To expand on Cody Pendant's suggestion - you can use Spreadsheet::WriteExcel and Spreadsheet::ParseExcel to automate the communication. Or, if you're on a windows machine, you can use the various Win32:: and OLE:: modules to talk directly to Excel.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      perhaps if excel is the destination grab an xml-formatted copy of an excel spreadsheet, gut it, then borrow its functionality to reproduce it.
        Uh, no. That's a bad plan. You're assuming that Microsoft wants to keep anything in their applications similar from version to version. Microsoft has a vested interest in not maintaining outside compatibility between versions. That way, you have to use their software.

        Better is to use the modules whose maintainers are willing to handle this issue. If you want, write a XLS -> XML converter, using Spreadsheet::WriteExcel and your favorite XML writer. At least, you'll know that the DTD won't change just cause the version of Excel you saved the file in changed.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Math Solver
by Cody Pendant (Prior) on Jul 01, 2003 at 22:37 UTC
    Most of your post is "blah blah blah Ginger blah blah blah" to me because I don't know the subject, but just a thought:
    >this only can find integer results from >integer inputs

    Might it be possible to multiply each number until it no longer has a decimal, do the calculations, then re-correct?

    Also, if it does turn out that only Excel can do what you want, could you approach it another way: get Perl to talk to a local copy of Excel for that portion of the processing?



    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: Math Solver
by hsmyers (Canon) on Jul 02, 2003 at 13:02 UTC

    If there isn't any other way to put together a solution than to roll your own, then keep two things in mind:

    • There are more available math libraries than probably any other subject on the 'net.
    • paying forward is a good thing---when you've got something that works, publish!

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: Math Solver
by hossman (Prior) on Jul 05, 2003 at 08:33 UTC

    I don't know what kind of hooks are available, or if there's a perl API ... but gnumeric has a Solver you might be able to use.

    UPDATE: check out this page and CORBA::ORBit.

Re: Math Solver
by toma (Vicar) on Jul 03, 2003 at 04:22 UTC
    Another solver to take a look at is MAXIMA (a relative of MACSYMA). I have used this going in the other direction, where I have a set of equations and I want to turn the solution of these equations into a spreadsheet, where I can type in the knowns to find the unknowns.

    Your problems is difficult, since you have presumably working spreadsheets, and any other given solver may or may not be able to converge on the same solution. It might be a good idea to find a few problems that they weren't able to solve in Excell and see if any solver that you come up with can do better.

    The advantage of a computer algebra system such as MAXIMA is that it can come up with a closed-form solution, so no iterations are necessary. The disadvantage is that it may not solve the problem, where an iterating approach could.

    It should work perfectly the first time! - toma

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-03-28 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found