Programming Mathematics? 64
Posted
by
Cliff
from the codingbynumbers dept.
from the codingbynumbers dept.
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 equationsjust 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?"
Functional Languages (Score:2, Informative)
Re:Functional Languages (Score:1)
Haskell and Hugs would be good choices: the wonderful thing about functional languages is you can create a very high level of abstraction, which is great for solving maths and engineering problems. Go to: http://www.haskell.org [haskell.org]
Lisp/Scheme and ML would also be good choices.
Mathcad or Matlab (Score:1)
There are Open Source variants of these, a few of which I have used as well as some I haven't. Try them out, there are student editions of the commercial packages.
rlab [freshmeat.net]
SciLab [freshmeat.net]
Octave [freshmeat.net]
Euler [freshmeat.net]
Q [freshmeat.net]
Kalamaris [freshmeat.net]
Good luck, perhaps you can review these packages and post a response.
Re:Mathcad or Matlab (Score:2)
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+3x4=0');
solve(Y,x);
C++ is fine (Score:1)
As far as large numbers is concerned, I don't have any real experience in this. But do a google search on "infinite precision library" and you'll see that there has been some work done already in this area.
Languages for Mathematicians by Mathematicians (Score:5, Informative)
In the area of functional progamming you should also consider Common Lisp [cmu.edu] which is a well known functional language used mostly for AI.
On the properiatry side, many mathematical algorithms get coded in MatLab [mathworks.com] which provides builtin 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.
Re:Languages for Mathematicians by Mathematicians (Score:1)
and/or Mathematica. These are commerical software
though; does anyone know of free workalikes?
Re:Languages for Mathematicians by Mathematicians (Score:1)
http://www.ma.utexas.edu/maxima.html
and here is BIG list:
http://sal.kachinatech.com/A/1/
MATLAB also has a builtin implementation of the Maple kernel. There is integrated functionality, but sometimes you can see the seams, so to speak.
MATLAB is great for numerics, as is Octave a GPL'ed MATLABalike system.

alex
The Joyth of Lithp (Score:2)
A couple of years ago at the ACM collegiate programming contest, one of the problems was to write a program which would return the onesplace 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 constanttime solution to the problem. That would have impressed the judges....
Nate
Re:The Joyth of Lithp (Score:1)
Re:The Joyth of Lithp (Score:1)
switch n {
case 1: return 1;
case 2: return 2;
case 3: return 6;
case 4: return 4;
default: return 0;
}
Re:The Joyth of Lithp (Score:1)
Re:The Joyth of Lithp (Score:2)
Going to need more info... (Score:1)
If the target audience is just you, then you can do mathematics in just about any language you want. Consider it a challenge to try and write an algebraic manipulator in C  but it's possible (albeit possibly very very ugly).
Since most programming is logic (a branch of mathematics), many people think it should be easy to code arbitrary mathematical ideas in code: it's not. Mostly you need some sort of well defined problem to solve and then writing a specific solution  e.g. writing code to figure out the sum of all the primes less than 1000 isn't too hard. But trying to write a program that will accept arbitrary input and sum that given sequence is hard: e.g. the program should be able to have a user input: i want the sum of all numbers between 12 and 12^12^12 which have no 7's in them.
This later wouldn't be too hard to hardcode but would be quite a challenge to write a general engine which would accept this type of input.
To summarise the summary of the summary: Pick a specific problem, pick a specific language and go for it. Otherwise but a copy of Matlab (or go with Octave) and knock yourself out :)
 merkac
Re:Going to need more info... (Score:2)
Re:Going to need more info... (Score:1)
Godel theorem: we can't answer everything at once. (Score:1)
If you say that you want the sum of all numbers between 12 and 12^12^12 with no 7's in them, any semiproperly trained (or is it brainwashed, I'm not sure) math person like me will tell you first that they are interpreting the question thus:
Find the sum of all positive integers between 12 and 12^(12^12) inclusive which have no 7 digits in their decimal representation .
Next, they will ask you whether you want leading digits, i.e. like what a truncated floating point representation would give you, or whether you want an exact integer solution. Given that the sum would have on the order of 18*10^12 digits, they would probably assume you want a floating point representation of it.
Then they would tell you it is approximately ten to the above number, and pat themselves on the back, confident that one really doesn't need to know anything more (why the hell would one?).
All of this can be done on paper in less than 5 minutes, with the use of a calculator with base ten logarithms.
[Hint: if N=the upper bound and k=log(N) to the base ten, and ignoring the "trivial" lower bound, the fraction of numbers between 1 and N with no 7's is going to be about (0.9)^k, while the average value is going to be about (N+1)/2. This gives a sum of .5*90^k where k = 12^12 * log(12).]
The above reasoning is good enough to nail down the order of magnitude of the number; in fact, you can get a little more precise (see below).
I agree it would probably be extremely hard if not impossible to write a general engine for use with arbitrary math problems (having say a welldefined real or complex number as an answer). For one thing, to decide even whether to use asymptotics and give a realvalued numerical estimate, to solve exactly as an arbitraryprecision integer, or a "closed form" expression (using combinatorics or number theory or Fourier analysis and accepted formulas like Bessel functions or Stirling numbers as appropriate). Nevertheless, within a reasonable range of computability, programs like Mathematica already do just that. They probably don't solve this problem, because they are engineered to solve using known algorithms for commonly needed types of solutions.
(Now I am going to just dream:) To really solve such problems efficiently, one would need a database or research query mechanism to classify problems by known methods of solution in specific problem domains. That would really be cool if such an infrastructure existed that a program could tell you what methods are known that even apply to your problem, beyond a mere predefined set of problem ranges. Does anyone know if such 'expert systems' exist or are conceivable in the foreseeable future?
P.S. A slightly titillating but related problem is the sum of all kdigit positive integers containing at least one 7, allowing leading 0's for simplicity. For example, if this sum is T_k, then the sum S above is approximately
where the approximation stems from the fact that k is not integer. One can give a ballpark estimate for T_k of aboutThe point? A question: does anyone forsee the day that a computer will be able identify, on the fly, what sort of reasoning is possible and appropriate to general mathematical questions??? My quess is that we are far too idealistic about questions like this, which involve AI, because we are far too naive (and unconscious) about the sophistication of our own thinking. However, it is very conceivable that an expert system can account for known methods of reasoning and be able to discriminate as to when they are appropriate for given questions.
Lastly, lest the original poster may think we have forgotten him or her, I'll have to agree with others that many languages are useful, but in math, often a pencil and paper (or a desktop calculator)  and some mathematical reasoning  is all you're going to need to solve a problem or to entertain yourself. It does help to have a little background in computability and numerical analysis, but playing around with ANY computing language will also give you background that would help in learning these subjects. Personally I have used C/C++, mathematica, maple, fortran, assembly, Commodore basic, quickbasic, a TI82, an old HP calculator, or even javascript or the windows NT command shell(!) to explore, compute, demonstrate or entertain myself with math questions (depending on what was at hand).
Programming maths (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 tomaintain code. The compilers (though not Free as of yet, but see the G95 homepage [sourceforge.net]) 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 nonnumerical stuff, macsyma is quite nice
Macsyma and Maxima (Score:1)
http://www.ma.utexas.edu/users/wfs/maxima.html
A very sad side note to this is that the individual largely responsible for spearheading this piece of work, William Schelter, has recently passed away. He was also the maintainer of GNU Common Lisp. Rest In Peace.
Mike
Python is perfect for programming Math (Score:1)
APL  A mathematical programming language (Score:3, Insightful)
Re:APL  A mathematical programming language (Score:3, Insightful)
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.
Re:APL  A mathematical programming language (Score:1)
Re:APL  A mathematical programming language (Score:1)
I'll second that.
Morgan Stanley has a GPLed implementation for most Unices called A+ [aplusdev.org].
For a reference, pick up APL: the Language and its Usage, Learning APL: An Array Processing Language,
or Ken Iverson's A Programming Language, from which the name of the language is derived.
A modern variant which uses ASCII characters is K [kx.com]. Free as in beer, for FreeBSD, AIX, Linux, Win32.
Re:APL  A mathematical programming language (Score:1)
fortran (Score:5, Informative)
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.
Re:fortran (Score:2)
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 (Nbody 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
Re:fortran (Score:1)
Matlab and Mathematica (Score:1)
Both have their own language backed by a huge collection of builtin functions. Mathematica's strength is in symbolic calculations: Solve[a x^2 + b x + c ==0,x] will give you the standard formula for a quadratic in a,b, and c. Matlab excels in numerical computations, especially on vectors and matrices. You can do numerical stuff in Mathematica and symbolic stuff in Matlab, but those aren't their strengths.
I would not say that either is easier to program than the other. You can write programs for either using your favorite text editor. One thing common to both languages is that though they have "for" loops, such loops are generally an inefficient way of doing things. Applying functions to entire lists in Mathematica or to vectors in Matlab will be a lot more efficient. You will have to get used to this "vector" way of thinking.
Whether either of these is applicable to the ask slashdotter's situation depends very much on his goals. Solutions to algebraic equations become oneline commands in these languages; if you are seeking the thrill of programming the solution yourself from first principles, use one of the standard programming languages others have described. If you want to make deeper investigations, you will likely get there sooner using one of these programs than starting from scratch.
Both packages also allow you to call (and call from) C programs  useful for intensive stuff that is better done directly in C.
Also worth noting is that both these software packages cost money! Your university may have them on their lab computers, but if you want to use them (legally) at home, you will have to shell out $100$150 for a student license. Thankfully the student versions are pretty much fullfeatured. Your university might have special deals with these companies; mine sells student Matlab for about $30. Single nonstudent licenses are very expensive (about $1000 for Mathematica and $2300 for Matlab, I believe)
Re:Matlab and Mathematica (Score:1)
I don't know about a mathematica clone (since it's much more complex to program symbolic stuff)
but there are free programs following the same style (as mupad, khalamaris, etc)
Smalltalk (Score:1)
Smalltalk is OO and has some better alternatives for arbitrary long integers. All numbers are objects, and understand appropriate operations (methods/or messages in ST) no problem calculating something as "12090 factorial"
You can download [squeak.org] OSS at Squeak [squeak.org]
Re:Smalltalk (Score:1)
if you have (1/3) * (1/9) you get (1/27), not
Javascript (Score:1)
http://www.uiowa.edu/~itsarcs/jni/seosl/
ML (Score:1)
Why? (Score:3, Insightful)
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).
you have taken numerical mythods right? (Score:2)
Just in case (and for any nonmath 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.
Re:you have taken numerical mythods right? (Score:3, Insightful)
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.
Re:you have taken numerical mythods right? (Score:1)
Oh definitely. I found playing around with Hugs (the Haskell interpreter/compiler) way cool. Of course, it helps to get an education in numerical methods (at the least so you know how the tools work in the background), but getting a feel for things in real time helps alot
Depends... (Score:2)
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.
Re:you have taken numerical mythods right? (Score:1)
It looks like it has escaped your notice that MatLab, and a 'few [you] missed' like Mathematica ad Pari, are quite capable doing symbolic mathematics  such as eg finding the factors of polynomials, integration, differentiation, etc. See here to get started: http://www.symbolicnet.org/
It is also quite possible to work with represetations of numbers as intervals instead of plain ol' floats, so that the results of the calculation give you error bounds, in a more automated way (without as much need for a numerical method course, in other words). Plenty of research cited here: http://liinwww.ira.uka.de/bibliography/Math/intar
See also the XSC system which builds this error calculation into languages like Pascal:
http://www.xsc.de/
I find it mystifying that you would consider an understanding of 'Gurdels'(sic) incompleteness theorem necessary to write decent computer programs.
Baz
UBASIC (Score:1)
It doesn't give the greatest performance it the world, but I've used to for everything from prime searching to looking for narcissistic numbers [geocities.com].
scheme (Score:2)
Maple from U of Waterloo (Score:2, Informative)
If you're looking for something on the numerical analysis end, try Octave [octave.org]. It's like MatLab but free.
Maple! (Score:2, Informative)
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...
perl and Math::Pari (Score:4, Informative)
ok, I just looked it up, here's a blurb from the documentation:
One thing I would advise youuse visualization aggressively. There was a tendency in mathematics for a long time to deemphasize the geometrical/physical aspects of systems as being sort of extraneousi.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
Re:perl and Math::Pari (Score:2)
A little OT here, but I've just got to comment on this ...
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.
Re:perl and Math::Pari (Score:2)
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.
C/C++ for sure (Score:1)
For example, all serious games out there that use OpenGL or DirectX are all programmed in C/C++. Why? Because its the best language for the job. And since everyone knows that games are very mathematically intensive, and most of their languages are C/C++, you can make the logical postulate.
Vic
Re:C/C++ for sure (Score:1)
Re:C/C++ for sure (Score:1)
On the topic of OpenGL, your logic doesn't follow. The mathematical content of the 3D graphics routines is implemented within the OpenGL library (not so much the game itself), and these routines are implemented in C, assembly, and hardware, not because these media provide for the most eloquent expression of mathematics, but because they provide for the fastest implementation, and speed is of primary importance in 3D graphics.
You are right that C++ has some interesting applications in mathematics. See for example Yet Another Computer Algebra System [xs4all.nl].
Fortran for most, Matlab and C for some (Score:1)
Another possibility... (Score:1)
Scheme and SICP. (Score:2)
Additionally, one of the best Computer Science books ever written, Structure and Interpretation of Computer Programs [mit.edu], 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 [mit.edu].
I use good old bc (Score:1)
This old chestnut is an arbitrary precision desk calculator with a Clike programming syntax. So, if it's the sort of stuff that you would do in C/C++ or Java but to play with really large numbers, the bc is the way to go.