Slashdot Mirror


Programming Mathematics?

Adam asks: "I'm an undergraduate math and CS major, and as such, I would like to write some programs that do basic math, from finding perfect numbers to solving basic algebraic equations--just for fun. However, I only have experience with Java, and BigInteger and BigDecimal suck pretty hard as far as writing equations with them is concerned. So, to all you mathematicians and math lovers, what languages do you program mathematics in, and why?"

22 of 64 comments (clear)

  1. Functional Languages by Tomah4wk · · Score: 2, Informative

    Funtional languages like haskell are very good for doing maths, especially large dynamic sizedd matrix math. The language has a built in list type (thing self memory managing dynamic array) and the language is functional, so the programming style is very mathematical. If you use an environment like hugs (see google), you can do all your programming and use in the same system, which i personally like alot. Fortran is also popular for engineering types, as i believe it has built in matrix math functions, howeverdont quote me on that.

  2. Languages for Mathematicians by Mathematicians by epsalon · · Score: 5, Informative

    Mathematiticians have invented a language called ML (Meta Language) which is a functional language in which you can write mathematical formulas almost as you would mathematically define them.
    In the area of functional progamming you should also consider Common Lisp which is a well known functional language used mostly for AI.

    On the properiatry side, many mathematical algorithms get coded in MatLab which provides built-in matrix manipulation and lots of additional libraries (you'll probably find out most of the stuff you want to write is already there...)

    In any case, the progamming language should be tightly fitted to the application.

  3. Programming maths by d-Orb · · Score: 3, Informative

    I know I'm going to get flamed for this, but here it goes anyway...

    I do use a mix'n'match approach to mathematical programming. Usually, I deal with numerical methods, and in principle, all languages are good. However, I find that using Fortran95 (see, you were going to flame me) I code faster and easily to-maintain code. The compilers (though not Free as of yet, but see the G95 homepage) produce fast code, which is easy to port between my Linux box and the Tru64, Solaris boxen in some of the labs.

    Another good option is python. The numerical extension and the many modules already developed make it really nice (and quite fast). Additionally, you can add C and Fortran routines to it.

    For profiling, I tend to use either Octave or Scilab, and then convert that on to F95

    For non-numerical stuff, macsyma is quite nice

  4. APL - A mathematical programming language by jcwren · · Score: 3, Insightful

    I'm surprised APL hasn't been mentioned. I wrote a few lines of APL a hundred years ago, and it's certainly not as popular as it used to be, but it was designed as a mathematical programming language. A couple of resources, obtained with Google, using this search rule. http://whatis.techtarget.com/definition/0,,sid9_gc i213454,00.html http://www.engin.umd.umich.edu/CIS/course.des/cis4 00/apl/apl.html

    1. Re:APL - A mathematical programming language by renehollan · · Score: 3, Insightful
      Ah yes, APL... the only language in which you can invert a matrix with three characters, one of which is a backspace. (quad, backspace, divide; also known as domino).

      Seriously, though, APL let me do my arithmetical stats course assignments in the 30 minutes before class, many, many moons ago.

      That said, I think that Mathematica or even MatLab would be better suited for what you want to do.

      --
      You could've hired me.
  5. fortran by tony_gardner · · Score: 5, Informative

    Depends entirely on what you want to do:

    Mathcad or mathematica can to calculations from a graphical interface, but are difficult to program and slow for anything requiring big loops.

    matlab is a higher level language like the two above, but isn't a graphical interface, so it's easier to do things a little more complicated.

    fortran is the mathematical workhorse for small to medium programs with hard maths. The style is reasonably intuitive. In addition, a familiarity with fortran will never go to waste, since the scientific community has been using it for 35 years, and there's a lot of legacy code. There's free compilers too.

    c, c++ are the mathematical workhorses for medium to large programs. In general, better data structure handling than fortran, and fewer mathematical libraries. Most CFD code and indeed most finite element code is written in some brand of c. I think that it would be fair to say that professional programmers know about c, where scientists who do some programming know fortran. There are free compilers for c as well.

    Choose one to meet your project size and execution speed required.

    1. Re:fortran by drudd · · Score: 2

      The latest version of matlab that I've seen (6) has a graphical interface, although I don't use it.

      As other posters have mentioned, it really comes down to what you are doing. If I want to solve a single ODE, or integrate a single function numerically, matlab is the way I would go.

      When I start solving very large systems of equations (N-body simulations) it is usually rewarding to develop software tuned to that task in a lower level language such as C or Fortran.

      Java might not be bad either if you're just playing around, but I know that they place artificial limits on floating point operations in order to try to maintain absolute consistency (i.e. intel chips have somthing like 80 bit internal acuracy which jvm's are forced to artificially limit in order to match other systems).

      Doug

      --
      Venn ist das nurnstuck git und Slotermeyer? Ya! Beigerhund das oder die Flipperwaldt gersput!
  6. Why? by coyote-san · · Score: 3, Insightful

    Why are you writing these programs?

    If it's to help you understand the problem, you can use any language... and ML or applications like MathLab and Maple are probably best since they allow you to focus on the math, not the programming.

    If it's because you are interested in working as a scientific programmer then you need to focus on the primary languages used in the field: Fortran, C and possibly Ada.

    Fortran, as others have pointed out, isn't *that* bad. Unfortunately most Fortran programmers *are*. Too many people with a scientific background thought "it can't be that hard to write code" and they're right -- it's not hard to write code. It's hard to write good code.

    C is the probably the standard language now, and has the benefit that the skills are portable.

    Ada is now pretty much a niche language, but you may see it at defense companies and it has a cleaner OO implementation than C++. For the same reason, you might see java compiled into native code (e.g., with gjc).

    --
    For every complex problem there is an answer that is clear, simple, and wrong. -- H L Mencken
  7. you have taken numerical mythods right? by bluGill · · Score: 2

    Just in case (and for any non-math people reading this) Do not write programs to do math brefore you take a numerical mythods class, and a theory of computing class.

    You need to understand the limits of numbers on computers, what the error is, and so on before you can write a program that is worth touching.

    Likewise you need to understand the halting problem (though not as deep), and other problems in fundamentals. (Gurdel incompletness).

    Without the proper background (which a good CS program will make avaiable for you, and probably require), you cannot do this right. Of course once you have the right background language is an implimentation detail. While ML, APL, and MatLab (and a few I missed) have a lot of things to help with math, you can use any language you want to and get the job done.

    1. Re:you have taken numerical mythods right? by msouth · · Score: 3, Insightful
      Just in case (and for any non-math people reading this) Do not write programs to do math brefore you take a
      numerical mythods class, and a theory of computing class.

      You need to understand the limits of numbers on computers, what the error is, and so on before you can write
      a program that is worth touching.


      quite to the contrary, there is no better time to experiement than the present, with your current level of knowledge. You should be messaing around, trying things, seeing what works. Maybe you shouldn't attempt to sell your work as a fast, small competitor to Mathematica, but, by all means, experiment.


      The more you have played around with this stuff on your own, the more you will get out of those classes when you take them. Never let anyone succeed in scaring you away from trying.


      When I was taking numerical methods, I was in there because I had to be. There was another student in there with way less "raw mathematical talent" than me, but who was doing much better at "getting it" than I was because he was going home and messing around with the stuff on his computer. In fact, during the course he noticed an interesting pattern in his graphs, and brought it to the professor. If I recall correctly they published a paper on the result.


      Theory is important, too, but the real way that theory is developed is that people mess around with stuff for a long time, develop some intuition, and then try to formally show that their intuition is correct. In classes they try to do this in the opposite direction--"here's the theory, try to develop some intuition about it".


      You will be way ahead of the game if you already have experience trying stuff.

      --
      Liberty uber alles.
  8. scheme by austad · · Score: 2

    As much as I hate Scheme, it worked nicely for writing mathematics programs when I went to the U of MN.

    --
    Need Free Juniper/NetScreen Support? JuniperForum
  9. Maple from U of Waterloo by Capt_Napalm · · Score: 2, Informative

    The University of Waterloo has a program called Maple. It's fairly easy to pick up, available for Windows, UNIX, Linux and Macs, not horribly expensive, and very powerful. There's also piles of add-on packages created by other mathematicians.

    If you're looking for something on the numerical analysis end, try Octave. It's like MatLab but free.

  10. perl and Math::Pari by msouth · · Score: 4, Informative
    There's a mathematician who used to be quite active in developing Perl's regular expression code (Ilya za[something]) who created a Perl module that lets you use a very powerful and extensive mathematical library called PARI. I believe the extension is called Math::Pari, and it's available on CPAN.

    ok, I just looked it up, here's a blurb from the documentation:


    Package Math::Pari is a Perl interface to famous library PARI for
    numerical/scientific/number-theoretic calculations. It allows use of
    most PARI functions (>500) as Perl functions, and (almost) seamless merging
    of PARI and Perl data.


    One thing I would advise you--use visualization aggressively. There was a tendency in mathematics for a long time to de-emphasize the geometrical/physical aspects of systems as being sort of extraneous--i.e., it doesn't matter what the parabola looks like, just what its mathematical properties are. Well, in short, this is stupid. Your visual cortex is an amazingly powerful processor, and it's dumb to tie one of your brain's hands behind its back just because someone a few centuries back had a theoretical axe to grind.

    Always ask yourself "is there some way I can visualize what's going on here?". You will leap far ahead of where you would be otherwise.

    good luck.

    mike
    --
    Liberty uber alles.
    1. Re:perl and Math::Pari by Daniel+Dvorkin · · Score: 2

      A little OT here, but I've just got to comment on this ...

      One thing I would advise you--use visualization aggressively. There was a tendency in mathematics for a long time to de-emphasize the geometrical/physical aspects of systems as being sort of extraneous--i.e., it doesn't matter what the parabola looks like, just what its mathematical properties are. Well, in short, this is stupid. Your visual cortex is an amazingly powerful processor, and it's dumb to tie one of your brain's hands behind its back just because someone a few centuries back had a theoretical axe to grind.

      Well, sometimes that's true and sometimes it's not. The fact is, some people are more visually oriented and some people are more numerically and symbolically oriented -- the way one of my Dad's professors (back when he was a math student, they had to draw their graphs, by God!) was that there are two types of people in the world, geometers and algebraists. Me, I'm an algebraist: sometimes it's useful for me to see a visual representation of a problem, but more often than not, not only don't I care what it looks like, trying to think about it visually makes it harder for me to solve.

      Ultimately, what matters is solving the problem (or proving the theorem, or whatever.) In most branches of mathematics, there are both algebraic and geometric ways to get a solution. Neither one is "better" in some abstract sense, but one or the other will generally work better for an individual.

      --
      The correlation between ignorance of statistics and using "correlation is not causation" as an argument is close to 1.
    2. Re:perl and Math::Pari by msouth · · Score: 2

      You are right. I didn't intend to go to the other extreme. What I should have emphasized is that visualization can often make a remarkably valuable contribution towards insight or understanding, and often it does so in places that are not traditionally approached through visualization.

      I can certainly believe that there are situations or mental dispositions for which this is not the case, and they may even be the majority.

      --
      Liberty uber alles.
  11. Depends... by coyote-san · · Score: 2

    That depends on the problems he's interested in.

    If he's trying to solve continuous problems, he needs to get a copy of Numerical Recipes and start working through it. Ideally solving real problems that can be checked analytically or by running simulations - it's important to learn deep in your gut just how easy it is to write code that looks good but produces garbage.

    But if he's interested in discrete math problems, something that the original question hinted, then he needs to get an arbitrary math package and learn an entirely different type of programming. In this case a numeric methods class is irrelevant.

    --
    For every complex problem there is an answer that is clear, simple, and wrong. -- H L Mencken
  12. Re:Going to need more info... by divbyzero · · Score: 2

    Your answer, while true for edit-compile-run languages (C, C++, Java, Fortran, etc), is not true for fully interactive languages (Lisp, ML, Perl, Python, TCL, etc). In an interactive language, there's really no such thing as hardcoding; the program is not run monolithically, but defined in small reusable pieces which you can recombine at runtime using the full expressiveness of the language.

    --
    But my grandest creation, as history will tell,
    Was Firefrorefiddle, the Fiend of the Fell.
  13. Maple! by bje2 · · Score: 2, Informative

    I went to Lehigh University and we had Maple V available on our on campus network...it's an extremely powerful mathematics application...

    it's not really a programming language, but it does allow you the ability to create variables, your own functions, etc...by far the most powerful featues are it's graphing capabilities and it's ability to find integrals and derivatives of equations that you'd never dream of trying to solve by paper and pencil...

    the only downsides are it takes a while to get used to (the manual is huge!), and the program itself is a bit of a memory hog...other then that it's great...

    --

    "Facts are meaningless. You could use facts to prove anything that's even remotely true." - Homer Simpson
  14. The Joyth of Lithp by n8willis · · Score: 2
    This reminds me of something cool you get for free with Common Lisp: arbitrary precision integers. Very nice treat for discrete or algebraic problems.

    A couple of years ago at the ACM collegiate programming contest, one of the problems was to write a program which would return the ones-place digit of n! for any n between, like 1 and 10,000 (or something huge like that). One of our guys came up with a solution, but afterwards our professor reminded us that we could have just generated the list with a simple CL program, slapped it into a lookup table, and had a constant-time solution to the problem. That would have impressed the judges....

    Nate

    --
    -- Watch the REAL Jon Katz.
    1. Re:The Joyth of Lithp by n8willis · · Score: 2

      Yeah yeah, I guess what I meant was "least significant digit" or something like that. I forget how it was actually expressed.

      --
      -- Watch the REAL Jon Katz.
  15. Scheme and SICP. by Christopher+Cashell · · Score: 2

    I'd give Scheme a try. It's a functional language, and a dialect of lisp. It's design makes mathematically oriented programming fairly intuitive.

    Additionally, one of the best Computer Science books ever written, Structure and Interpretation of Computer Programs, utilizes Scheme. Ths book takes a strong math styled for programming, particularly in the first chapter, and I think it'd be a great way to get yourself started.

    Also, the book is available online, full text, for free here.

    --
    Topher
  16. Re:Mathcad or Matlab by AntiNorm · · Score: 2

    Mathematica can do symbolic mathematics, Matlab doesn't.

    Actually, Matlab can do symbolic math. It's kind of a pain though, IMO. It's been a while since I've worked with Matlab, but IIRC the syntax is something close to:

    Y=sym('x^2+3x-4=0');
    solve(Y,x);

    --

    I pledge allegiance to the flag...
    of the Corporate States of America...