Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Advanced Math?

by afkaos (Novice)
on Jul 26, 2003 at 13:12 UTC ( [id://278090]=perlquestion: print w/replies, xml ) Need Help??

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

I am writing a program for mine and my friends use that does math problems for us, the problem is I'm getting into the Geometric portions (Gotta start somewhere) and I really don't want to have to include all the specific tangents of angles, so I was wondering if there was some sort of Module/etc... that would let me do something like
chomp(my $angle = <STDIN>); $angle = sin $angle;
Or mabye something else that can do somewhat advanced math fairly simply?

Replies are listed 'Best First'.
Re: Advanced Math?
by PERLscienceman (Curate) on Jul 26, 2003 at 13:35 UTC
    Greetings afkaos: If you want to start with : "Geometric Proportions", geometry/trigonometic related functions can be accessed by using Math::Trig, this is a core module and covers things such as tan, cot, sin, cos, ...etc.; it also has a predefined constant pi. Math::Trig is a core module. You may also want to have a look at Math::Geometry.
Re: Advanced Math?
by Zaxo (Archbishop) on Jul 26, 2003 at 14:07 UTC

    What you show will work fine, except that it needs the angle to be expressed in radians. That's not what most people think of first, so a conversion from degrees to radians is in order,

    my $pi = 4 * atan2 1, 1; while (1) { print "Angle? "; chomp ( my $angle = <STDIN>); last if $angle eq ''; print sin( $angle * $pi / 180 ), $/; }
    That is for fractional degrees expressed as decimal or float notation. Your users may think of dms notation, which I leave as an exercise ;-)

    As you've been advised, Math::Trig may be very helpful.

    After Compline,
    Zaxo

Re: Advanced Math?
by chunlou (Curate) on Jul 26, 2003 at 15:38 UTC

    PDL (Perl Data Language), Pari (number theory) or Inline::Octave (matrix algebra) are good advanced math modules. Other good open source math software are R (statistics) and Maxima (symbolic math). A Perl/R interface is available at Omegahat.


    __________________
    Update: Forgot to mention Math::MatrixReal. It's a pure Perl implementation of matrix algebra for real numbers (hence solves eigensystem for only symmetric case, not general case, which may take complex values). In addition, Math::LP if you ever need linear programming. Graph was also mentioned in a recent post "recursive complex structure or something like that?" (for visual display of "graph," go with GraphViz).

Re: Advanced Math?
by SyN/AcK (Scribe) on Jul 26, 2003 at 14:22 UTC

    You know, I fear to suggest anything but Perl on a Perl board, but what about Maple? It's great for solving all sorts of math problems, and it can double as a programming language (albeit not your normal programming language).

    I used to loathe Maple because all of your stupid low level math classes would make you use it, when you could do the problem directly in your head. Then I used it in my System State Modelling class (I know, it sounds like it wouldn't be the best of ideas for that), we were forced to, and while I thought there was better things we could've used, it did force me to learn Maple.

    As I learned it, I realized I should've been using it in my higher level math classes, like Linear Algebra, Differential Equations, etc. I'm certain it has what you're looking for.

    Just a suggestion... feel free to flame.

Re: Advanced Math?
by Coruscate (Sexton) on Jul 26, 2003 at 23:12 UTC

    You are creating a script that gives answers to sample problems after you've given it a try, and not creating a script to do your math homework for you? Right?


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

      I remember a time when a maths instructor was horribly upset with the entire class and gave an assignment wherein the correct answer was required (else flunk the course). Since I'd been slacking I had no clue as to how to attack the challenge---but I did have a computer! Wrote code until the wee hours and until I finally understood what in hell was going on. At the point the solution was obvious (at least to my new found vantage point) so I wrote it up and turned it in, one of two who actually solved the silly thing. He didn't flunk anyone, as he had calmed down but it was a fun ride while it lasted!

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
        Reminds me of the time I solved this problem:

        A man walks towards his house at 4 miles an hour. At the time he sets off, a fly leaves the man's house, flying at 8 miles an hour. When it reaches the man, it turns around and flies back to the house. It keeps repeating this journey until the man reaches the house an hour later. How far did the fly fly?

        I spent ages working out where their paths intersected and how far the fly flew each circuit, ending up with an infinite series. On summing the series, I slapped my head and let out the oh so classic exclamation of "d'oh".

        cLive ;-)

        PS, for those of you as clueless as me, highlight below:
        The fly flew 8 miles, since the man walked for one hour and the fly flew for one hour.

        You wouldn't happen to have a copy of the math problem available, would you?

        Looking for a challenge,
        Petras
        Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

        -Howard Aiken
Re: Advanced Math?
by DuncDude (Initiate) on Jul 27, 2003 at 11:15 UTC
    I'm very new to Perl but it seems to me you've got it right anyway.
    Perl has a number of intrinsic functions available for performing mathematical calculations.

    abs() # return the absolute value
    atan2(y/x) # return the arctangent of y/x
    cos() # return the cosine
    hex() # return decimal value of a hexidecimal string
    oct() # return decimal value of an octal string
    sin() # return the sine
    sqrt() # return the square root

    so you would require...
    $angle = sin($angle)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 17:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found