Ask HN: Which general programming language is good for learning/exploring math?

43 points by mathpadawan ↗ HN
Which general purpose programming language is good for learning and exploring math?

Here is what I am looking for from the programming language:

* Good standard library that helps in exploring math concepts. Python fits this bill. Python's standard library has functions like math.comb(), math.gcd(), math.factorial(), etc. They make it easy to write down many closed-form expressions without reinventing the wheel.

* Speed. If I am exploring a new concept and I want to test a conjecture for large numbers, such iterate-and-test loops in Python run 30 times slower than equivalent loops in C++. Here C/C++ fits the bill and Python does not.

* Expressibility. While I am exploring mathematics, it should not feel like I am fighting the syntax of the language. Python and Java fit the bill due to their simplicity. C++ is manageable. Rust feels like too much work for quick and dirty hacks to test conjectures.

* Longevity. The language should be stable and not prone to too many breaking changes. Some code I write now should run without modifications ten years later. C, C++, Go fit the bill. Python does not.

* Open source implementations. The language must have popular free and open source implementations. I don't want to be paying large sums of money for something like MATLAB or Mathematica. Most languages popular here on Hacker News like Python, Go, Rust, etc. fit the bill.

Now if there is no language that do not meet all the requirements above, that is fine. Something that comes to close to supporting most of the features above is going to be okay.

65 comments

[ 3.4 ms ] story [ 131 ms ] thread
Thoughts on Haskell?
Yep; see The Haskell Road to Logic, Maths and Programming.
I’m learning Haskell right now and had the same question until I found Hindley’s Lambda-Calculus and Combinators: an Introduction book. Many here are recommending Julia but I’ve used Julia enough now to conclude that it isn’t as mature as Haskell and can be frustrating at times. Haskell, when it’s foundations and history are well understood not only challenges how you think about programming but also of mathematics.
If what you want is to quickly iterate on short experimental math programs, you want an APL. Otherwise, if you need syntactic macros there's Racket.

I suggest J (jsoftware.com)

Honestly? None.

You can definitely explore mathematics experimentally, we all do - computer or not, but to really learn the mathematics just read the books and do the exercises.

Being able to work things out symbolically on paper is probably one of the biggest skills you can have beyond your usual repertoire of programming skills - is there any worse feeling that guessing your way through a problem you don't understand?

> is there any worse feeling that guessing your way through a problem you don't understand?

Isn't this called science?

Is mathematics science though?
Mathematics is the exploration of the a priori. Historically some axioms have been deemed more real than others. I think Gauss rejected non-eulidean geometry for instance. But this point of view has changed. With abstract algebra, and I believe also computer science, modern mathematics is about exploring connections between structures such as they emerge from stipulated axioms and rules of inference. It is science in the sense that ideas and hypotheses can be tested experimentally. But a proof requires more than non-falsification. Then there is the complication of potentially irreducible computation problems, where essentially a kind of mining of the computational space is the only way forward. This is the new kind of science Stephen Wolfram speaks of.
A very small fraction of experimental science is about just doing something and seeing what happens without a strong prior. You are expected to do your homework first. As a scientist, your role is typically to read and understand all the relevant prior work in the area first, then using this theory to derive a hypothesis for the outcome of your experiment. If you are lucky, the way the experiment unfolds might be outside your understanding, given that your understanding of the prior theory was correct, you have made a new scientific discovery which will be used to create new theory.

The fragment of uncertainty in this is typically very small and the process is very far from guessing your way through, hoping to find something new (whatever you found using such a method will most likely be either already known or incorrect).

Yes, I agree with you. My comment looks dull with your explanation. Of course, I also don't advocate doing something without any hypothesis. Though, I think hands-on practice on a subject can help you learn things better. For example, I find it very useful for linear algebra (maybe this is not applicable to other subjects). I can analyze my flawed intuitions.

When I first heard about the Monty Hall problem, I didn't understand it and tried it myself. It was way easier for me to understand the flawed intuition by analyzing each line as opposed to, say, for example, the explanation of Judea Pearl (which is also good).

What I wanted to emphasize is that it is not bad guessing your way through a problem you don't understand. But yeah, of course, you should have some knowledge.

I wish I could say I have been doing science already, not yet (hopefully), but I was referring to (say) programming problems like (for example) one recently where I could see that it boiled down to asking whether there existed a linear combination of a input set of vectors with a single second vector - I was able to spot the connection, I'm not convinced I would've been able to get it without the mathematics under my belt to spot the pattern.

Regardless, I plan to be learning until the day I drop, science or not.

I was inclined to write something similar myself as I agree in general. Mathematics is much more about the thinking than the doing, programming is more about the doing.

However, I do think that programming can be an excellent complement and aid when understanding certain concepts. Sometimes, writing a numerical simulation of a hard to grasp concept can make things fall into place. In particular in probability theory it can be quite helpful to generate a lot of random samples to validate and clarify some counterintuitive concepts.

what's the highest level math that one can theoretically get to through "just [reading] the books and [doing] the exercises"?
Field's Medal? Is that high enough?

What do you think mathematicians do?

I meant in terms of mathematical field/topic. Like the progression I did to get an engineering BS was roughly

* basic math > algebra/trig > calculus, linear algebra, differential equations

As a non-mathematician, my impression has been that mathematicians do a lot of proof-based work since (to my impression) the goal is to make new findings, not just learn concepts and formulas that they can apply to their work like an engineer. From my limited experience with proof-based work, it seems like something where you would need guidance and discussion. Whereas when I learned things like derivatives and integrals, it was very procedural and you just needed to learn a process, which is then built on by a more advanced process, etc.

Can one realistically self-learn up to low-dimensional topology or algebraic geometry just by reading books and doing the exercises?

The likelihood that you will succeed in self-learning any field of mathematics without outside help goes down as the material gets more advanced, but there's nothing fundamentally impossible about the endeavor. You can definitely get to graduate level that way if you're willing to put in the work and study the right things in the right order.

Luckily, there are plenty of resources available to someone self-studying online on various math forums.

what would you class as "graduate level" in this context? Would that be stuff like PDEs? As I said I'm not very familiar with what the 'progression' is past undergrad
Based on my experience either Julia or Wolfram Mathematica (the command line version with fewer libraries should be free, as in beer)

Or if you want to go all in you can try a proof assistant like Lean which is being developed with a focus toward application in mathematics rather than CS/logic.

I'm doing the same thing right now to get better at ML, and I chose Python.

I partially disagree with you on longevity since not much math-heavy code would have been hit by the breaking changes in Python 3.

In the ML domain I also disagree with your take on performance since there are plenty good enough GPU accelerated libraries for Python.

Python also has the advantage of knowledge reuse in gainful employment since it's the preferred language for ML.

Take a look at https://www.idris-lang.org/

While I think it doesn’t check most of your boxes, it’s one of a kind in that it lets you write mathematical proofs for your functions.

It pushes the limits of type driven development. As I understand, the big idea is that tests can only show a program is faulty, not that it’s correct. On the other hand mathematical proofs can actually prove your program is correct.

That said I’ve never used it myself but I know a close-knit community of extremely smart mathematicians/programmers using it. It’s certainly a language with huge potential. I plan to play with it / learn it some day.

No matter what others might think of universities but if there is a place to learn math, for most people it will be there.

If you actually want to learn math, then I would recommend to take courses and do the homework. You will notice how rarely you need the computer for it. I cannot remember that we used a computer even once in the first few semesters of studying math (I am physicist but we shared courses with actual math students. We just had less math courses than they did).

Fortran
Interesting suggestion. I have not programmed a lot in Fortran but with my limited exposure to it I like the first class support multidimensional arrays. It has a simple and clean syntax too. Definitely wins in speed and longevity.

Can someone with more experience of Fortran explain if this language is still relevant today and if it makes sense to get started with it in this day and age? If so, which Fortran standard to use? Fortran 90? Which compiler implementation to use?

I have experience with modern Fortran. The features that make Fortran relevant today are the array operations, simpler memory management than C/C++ (the allocate statement), and the ability to change floating point and integer precision easily. If you are doing math on very large arrays, Fortran is one of the best ways to go. If you are just starting to learn it, try to follow at least the Fortran 2003 standard and up, because it introduces many more modern conveniences. Many people still use Fortran 77 but honestly I see no reason to look at it at all. Fortran 90 really was only the start of the modernization of Fortran. Most compilers (GNU/gfortran and Intel/ifort) already support the Fortran 2003 standard. For a free software solution, stick to GNU (unless you want to pay for the Intel compiler).
Modern Fortran is a bliss, very high level comparable to MATLAB and Python, yet lightening fast, only a professional careful C programmer could get to its speed. Meanwhile, the only high-level programming language that provides a very simple native builtin syntax for one the most difficult types of distributed parallel computing. If you begin to learn Fortran, start from Fortran 2008 and beyond. Most major compilers support all of Fortran 2008 and and most of Fortran 2018, including Intel Fortran, NAG, GNU, Fujitsu, Cray, PGI, ...
> only a professional careful C programmer could get to its speed.

And a professional careful C programmer would not get to Fortran's library of numerically stable libraries for a very long time.

Julia[1] It has Python-like syntax, macros, and is general purpose while being designed for maths/science.It runs on LLVM so performance is great [2]. It supports easily writable matrices and Unicode maths symbols.

You may be interested in a free online MIT course about 'computational thinking' that's being run right now using Julia. [3]

In not affiliated with Julia or anything, just think it's a really good language.

[1] https://julialang.org/

[2] https://benchmarksgame-team.pages.debian.net/benchmarksgame/... (graph at bottom of page)

[3] https://computationalthinking.mit.edu/Fall20/

Julia, the only bad thing is that last I checked a few months ago, time to first plot in the stable release is terrible, though I hear the master branch has made some major improvements.
Julia is a great one to learn for this and will take a long way, both educationally and professionally.
Just go with C++; see books like Numerical Recipes and Matters Computational.
Fortran is good for math and physics.
I think it really depends what kind of mathematical exploration you want to do.

If you are interested in something similar to Mathematica or Matlab, then probably Python, because it is used as a language in SageMath: https://www.sagemath.org/

If you're interested in numerical computing, then there might be other choices such as Julia.

If you're interested in theory programming languages, logic or category theory, then Haskell or even more esoteric language (theorem prover) like Coq or Lean could be interesting for you.

"math" is a massive area, arguably bigger than all of computing (just because it has a longer history).

If you wanted (for example) to learn about group theory, the best tool is gap (https://www.gap-system.org). It satisfies your requirements -- an open source system, with a language which hasn't broken backwards compatbility in over 20 years.

Right now I would start with Typescript with Deno. You learn to code the syntax of the web (javascript) without much of the headaches of building and tooling of ts/js/node.
comb, gcd, factorial are very trivial. They are like 5 lines each or so. I think it should hardly matter whether or not the standard library has them.
I'd recommend scheme. It hits all of your points, and also makes manipulating symbolic expressions very easy, so you can do things like differentiating functions with very little code