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?"
C
Post Comment
Lameness filter encountered. Post aborted!
Reason: You can type more than that for your comment.
http://www.nr.com/
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.
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
SciLab
Octave
Euler
Q
Kalamaris
Good luck, perhaps you can review these packages and post a response.
Chris Kuivenhoven is a thief, beware
When I wrote my graphing calculator (and other math functions) calculator, I used C++. Keep in mind that you probably don't need the precision of anything more than a 32 bit number. Having 15 decimal places is just fine for nearly all applications, and it's overkill for most.
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.
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.
Make even shorter URLs - 8LN.org
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
Cool, but useless.
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
Hello. I am a mathematician and physicist who uses Python. Python is very easy to learn and use, but is also extremely powerful, especially whre math is oncerned. Python comes with built-in libraries that do all of the hard work. Num.py is a built in library that handles all the sophisticated mathematical functions you will ever need, and does so quickly, easily, and efficiently. Really, you should check it out. You can learn the core Python language in two or three hours by doing an online tutorial that can be found at http://www.awaretek.com/plf.html which site also contains links to everything you need to learn and use Python ;-)))
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
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.
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 OSS at Squeak
Ceci n'est pas un sig
The Macsyma company has gone out of business, but there's a good free (open source) version known as Maxima.
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
Well there is always Javascript...
http://www.uiowa.edu/~itsarcs/jni/seosl/
Fortran is used a lot traditionally by Maths and Physics types, in British universities at least. The language I would reccomend however is ML, I just graduated from uni and I remember that this was the language used by all the lecturers who would describe Computer Science as a branch of Mathematics. It's very easy to use if you're used to mathemtical formulae, logic and all that stuff, I remember it being very easy to define Finite Autonoma and Grammar Parsers (I didn't get into it big style but if there's one language I was going to code a Turing machine in it'd be ML) I imagine you'll find it easy enough to define your problem in the context of one of these.
J-aims
--
Yo, whatever happened to peas? Join T( H)GS
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
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.
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.
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
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.
I mean, for simple student code F77 isn't too bad, but it sure would be nice to get the newer features of the language (like free formatting, just as an example) without paying an arm + leg. By free I mean free speech or free beer, if it was free beer I couldn't talk about that'd be OK too :-). [student budgets being what they are and all...]
ok, I just looked it up, here's a blurb from the documentation:
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.
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
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
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.
Both have their own language backed by a huge collection of built-in 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 one-line 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 full-featured. Your university might have special deals with these companies; mine sells student Matlab for about $30. Single non-student licenses are very expensive (about $1000 for Mathematica and $2300 for Matlab, I believe)
After reading a lot of the book Numerical Recipes in C, I have to say that C/C++ are the best languages to program mathematics in. They are both fast, and with the OOP part of C++, you can really get some cool results.
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
Most of the numerical stuff I write is in Fortran90, and a lot of the older code you find is also in Fortran. Sometime C and Matlab comes in handy for specific task. Depending on what you do, I would say C and Fortran would be your best bet.
... even though it's more of a data language than anything else, is IDL. (Forgot the company that produces it, though, sorry.) It's fairly intuitive, has a lot of great functions and makes some of the greatest plots of any language I've ever seen. (Which is why it's perfect for data visualization...) Truthfully, as a theoretical scientist, I've used FORTRAN, C++, Java, IDL, MatLab, Mathematica, Maple, and Pascal for varying projects. It all really depends on what you want to do. And since other have mentioned much of that, I'll leave it at that. -Jellisky
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
If the kinds of math you want to do is calculator style stuff then `bc' is my choice.
This old chestnut is an arbitrary precision desk calculator with a C-like 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.
If you say that you want the sum of all numbers between 12 and 12^12^12 with no 7's in them, any semi-properly 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 well-defined real or complex number as an answer). For one thing, to decide even whether to use asymptotics and give a real-valued numerical estimate, to solve exactly as an arbitrary-precision 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 k-digit 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 TI-82, 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).
It's a fantasticly flexible language even if it's syntax is a bit different from most other languages. It has fantastic support for manipulating symbolic expressions directly, so you don't have to spend time writing symbol libraries before you get started.
I had to write routines to do algebraic simplification and simple symbolic derivatives for a class once and it was *maybe* 30 lines of common lisp code. Check out CMU CL, GCL, clisp, or any of the scheme implementations for details. Paul Graham has published some good books on lisp to get you started (although one is out of print).
Enjoy!
fyi,
ac