Slashdot videos: Now with more Slashdot!

View

Discuss

Share

We've improved Slashdot's video section; now you can view our video interviews, product close-ups and site visits with all the usual Slashdot options to comment, share, etc. No more walled garden! It's a work in progress -- we hope you'll check it out (Learn more about the recent updates).

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?"

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.

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]

As far as programming mathematics go, Mathematica and Matlab seem to be the most accepted programming languages. Mathematica seems to be used somewhat more by mathematicians, Matlab somewhat more by engineers. I assume that by programming mathematics you mean to apply mathematics to solve some problem. I've used Matlab extensively and it has a relatively C like syntax, but it understands things like matrices, complex variables and has a slew of built in mathematical operators. Mathematica can do symbolic mathematics, Matlab doesn't.

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.

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) [bell-labs.com] 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 [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 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.

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....

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

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.

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

S = N*(N+1)/2 - 11*12/2 - T_k

where the approximation stems from the fact that k is not integer. One can give a ballpark estimate for T_k of about.5*90^k using the above heuristic, but a precise formula can also be given (either using the inclusion-exclusion principle and binomial coeffiecient formulas, or some other combinatorics argument). T_k is a given ratio of the sum S_k of all k-digit integers 00...0 to 99...9:

S_k = (10^k)*(10^k - 1)/2,

T_k / S_k = 10*(.1)^k + 2d/81 * (.9)^k; (d=7)

What all this shows is that the dominant term of T_k is 14/81 * (90)^k, which vanishes compared to S_k and S, so that not only the magnitude or logarithm, but the actual mantissa of the number in question (S), can in fact be computed fairly accurately if we ignore the fact that k is not an integer.

The 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).

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 [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

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.

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'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.

J is free, neat! Last time I looked at it, it was shareware. I've never run into a limitation on K, but I haven't written that many large programs in it. The commercial K license is $7500, or so last I heard. Nowadays KX seems to be marketing the database, not the language.

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.

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).

I think that when he said "gui", the first poster was referring to "typeset mathematics". Mathematica and Maple have this, but Matlab does not (but matlab does have a maple plugin). Yes, though, the latest version of matlab has a windowed user interface.

I agree with the parent that the tool for the job depends on what you want to do, but I must disagree with the parent's evaluation of Matlab and Mathematica. I have used both, and here are my thoughts on them.

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)

there's a free clone-like of matlab called octave
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)

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

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]

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.

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).

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.

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.

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.

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

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.

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/intari th.html

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

Version 8 of UBASIC has the high precision real and complex arithmetic (up to 2600 digits) of previous versions, but adds exact rational arithmetic and arithmetic of polynomials with complex, rational, or modulo p coefficients, as well as string handling and limited list handling capabilities. In addition UBASIC has context-sensitive on-line documentation (read ubhelp.doc for information).

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].

The University of Waterloo has a program called Maple [maplesoft.com]. 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 [octave.org]. It's like MatLab but free.

I went to Lehigh University and we had Maple V [maplesoft.com] 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...

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.

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.

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.

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.

It doesn't sound to me like the poster wants optimized code to do one thing thousands of times. It does sound like they want a framework to try out different problems and learn mathematical algorithms and programming. Many good suggestions have been mentioned. One other, similar to Matlab or Maple, is MuPad (http://www.sciface.com), which has free trial versions and supports many OS's.

How did NR ever lead you to believe that numerical programs can be cleanly written in C? The C code in Numerical Recipes is terrible. The authors basically transplanted their Fortran code directly to C, resulting in unreadable programs. They ignore C conventions, for example by using Fortan-style arrays based at 1 instead C's 0-based arrays. Variable names are most often uninformative, at most two letters. Furthermore their algorithms often implement special rather than general cases of the problems they seek to solve. Finally, while the prose can enlightening, it's been questioned whether the authors are even qualified to write such a book. But don't take my word for it, read NASA/JPL's page `Why not use Numerical Recipes [nasa.gov],' which begins with the statement, ``We have found Numerical Recipes to be generally unreliable.''

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.

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

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].

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.