92

I just started a diploma in software development. Right now we're starting out with basic Java and such (so right from the bottom you might say) - which is fine, I have no programming experience apart from knowing how to do "Hello World" in Java.

I keep hearing that mathematics is pertinent to coding, but how is it so? What general examples would show how mathematics and programming go together, or are reliant on one another?

I apologize of my question is vague, I'm barely starting to get a rough idea of the kind of world I'm stepping into as a code monkey student...

yannis
  • 39,597
Rory
  • 237
  • 53
    Programming is mathematics. http://en.wikipedia.org/wiki/Discrete_mathematics – SK-logic Feb 26 '12 at 17:51
  • 1
    @SK-logic your link does not answer his question. Voted it up. – TheTechGuy Feb 26 '12 at 18:08
  • 2
    @Dave, my link lists all the areas of mathematics which are the programming. There is nothing else but mathematics in programming. – SK-logic Feb 26 '12 at 18:10
  • @SK-logic, the link can be useful but it does not answer his question. Answering mean you have to convince someone weather it is true or not. He is just a "Hello World" programmer. – TheTechGuy Feb 26 '12 at 18:14
  • 27
    @Dave, that's why I gave a link in comment, not as an answer. – SK-logic Feb 26 '12 at 18:15
  • 3
    I'm having a problem at this time directly related to the assumption that mathematics is only a peripheral piece of knowledge needed for programming. Because of the failure of a programmer in understanding precision and accuracy, he failed to use precision in his equations and we have major problems with anything that computes taxation. Programming is a mix of knowing language structure, logic and math. If you can't ground yourself in the basics of all three, please don't program anything for me. – Fiasco Labs Feb 26 '12 at 18:22
  • 9
    @FiascoLabs, even more than that - language structure and logic are also sub-domains of mathematics. Programming is nothing but applied math. – SK-logic Feb 26 '12 at 18:24
  • @SK-logic, apologies, I thought you downvoted it :( – TheTechGuy Feb 26 '12 at 18:24
  • 19
    On an unrelated note, starting with Java is not "right from the bottom". – Matt H Feb 26 '12 at 18:55
  • 3
    @SK-logic, I don't think it is useful to say that programming is math. I've certainly known lots of programmers who work by memorizing a set of rules, and following a few recipes. If that is doing math, then all human activity is a form of math. If that's the case then the word math doesn't have a useful meaning, because it can't be used to distinguish one activity from another. – Charles E. Grant Feb 26 '12 at 19:20
  • 6
    @CharlesE.Grant, many math students do the same (and still pass their tests). – SK-logic Feb 26 '12 at 22:11
  • @SK-logic, certain but I don't think they're necessarily doing math. I think they're a concrete example of the "Chinese Room Paradox" http://en.wikipedia.org/wiki/Chinese_room – Charles E. Grant Feb 27 '12 at 01:17
  • To succeed in both, you need attention to detail, some cleverness, some speed. I think they dance well together. Math was the precursor of CSCI – Caffeinated Feb 27 '12 at 04:15
  • @SK-logic That's a completely unhelpful one line answer masquerading a comment. Why not explain yourself and post it as a real answer? – Kirk Broadhurst Feb 27 '12 at 05:15
  • 2
    @KirkBroadhurst, others gave reasonably good answers already (although not featuring this essential link). I'm not comfortable enough trying to give comprehensive and easy to understand answers to the complete beginners, all my previous teaching experience covered only already somewhat seasoned students (either in math or cs or physics). – SK-logic Feb 27 '12 at 06:48
  • Shame there are so many answers about specific branches of maths (e.g. calculus/trig/etc) instead of the concepts of notation, set theory, etc. – John Ferguson Feb 28 '12 at 23:29
  • 1
    I believe that, those who program without having studied discrete math, are condemned to reinvent it - poorly – gcbenison Mar 30 '12 at 15:41
  • The relationship between mathematics and software is very important from a patent point of view as Mathematics is not patenable. http://www.groklaw.net/article.php?story=20110908075658894 – Jaydee Apr 04 '12 at 10:52
  • Donald Knuth. Mathematicians think that he is a programmer, programmers think that he is a matematician. He, therefore, must connect these two fields. – Val Jul 12 '14 at 06:11
  • Programming is the act of providing coded instructions. That's all it is, and it's simple to grasp and understand. If you've used a remote control, you've already programmed. Mathematics is a little trickier, it's root means learn, and it later was defined as the art of learning. They intersect at three levels, first, we learned how to make computers from math, thus learning about them involves math, second, we use math to learn how to program more effectively, and finally, we often program the computer to have it do math for us. Computer Science brings the two together into one discipline. – Didier A. Jun 09 '16 at 01:25

26 Answers26

141

First off: I am a mathematician - a professional one (in that I get paid for doing maths). I am not a programmer. I do do some programming, but very definitely of the Cargo Cult variety (see first comment to https://tex.stackexchange.com/q/451/86 and my response) and nothing of the sort that would normally bring me to this site (indeed, I registered here to post this answer after seeing a link to it in the TeX chat room).

The summary of my answer is: Mathematics is Programming.

I recently got to teach a mathematics course to a non-mathematical group of students. They were the programming section. I thought this was fantastic! At last, I was going to be able to teach mathematics to people who already understood the basic ideas and who already had a rudimentary toolkit for doing maths. I was incredibly disappointed when I asked how many of them had actually written a program and got an answer somewhere between 0 and 1.

Before I go on, I should clarify a few things. There are areas of mathematics that concern themselves directly with programming and are to do with evaluating algorithms and classifying languages and such-like. I'm not talking about those. There is also a program which is trying to translate all of mathematics into a formal language that can be evaluated by a computer. This is a bit closer to what I'm talking about, but even so to focus on that would miss the main part of what I'm trying to say. The mathematics that I do and the programming that I do are almost completely unrelated by topic. The connection between them is on a different level.

Where I'd like to start is with the comment on the main question:

If that is doing math, then all human activity is a form of math. If that's the case then the word math doesn't have a useful meaning, because it can't be used to distinguish one activity from another.

Yes, that is doing maths. But "maths" is still a useful word because, as the song says, "It ain't what you do, it's the way that you do it.". I would say that I am doing maths when I am approaching something in a mathematical way. Sometimes, that is "hard core" mathematics: formulating definitions, proving theorems. Sometimes, it isn't. Sometimes, it's writing silly little programs so that my kids can learn their spelling words.

This is what mathematics helps me with when I program:

  1. Abstraction This is probably the most important transferable skill from mathematics. By this, I mean the ability to strip away all the unnecessary stuff and focus on the important properties.

  2. Perspective If I could only choose one thing that all my students were to learn, this would be it: The ability to change ones point of view to suit the problem. We commonly treat this in linear algebra with change-of-basis formulae (that lead to horrendous matrices and horrendous complications), but it is much more applicable than that. At heart, it is the idea that just because something has been presented to you in one fashion, that doesn't have to be the way you work with it. This separates ones view of the thing itself from the way it has been presented. This can be extremely practical: it is all about making something useful or efficient. If I have a list of vectors and it is more efficient to store them as a list of x-coordinates and a list of y-coordinates, so be it.

  3. Form versus Function Leading on from the above; if a thing can be presented in many different ways then it is no longer fair to say that one particular presentation is the thing. To misquote that song again: "It ain't what you are it's what you do" that matters.

I could go on, but those are the ones that spring to mind.

Now, there are probably lots of (negative) reactions to what I've written so far. One will be "That's not maths, that's just good sense." (or bad sense) to which I refer to my remark above agreeing with the sentiment that "all human activity is a form of math". Another will be "That isn't the type of maths meant in the question.". This is almost certainly true and here I actually have a lot more sympathy with the person who said "At least I haven't touched the maths for 10 years,". He or she is wrong, of course, they have been doing maths for 10 years because whenever they wrote a program they were doing maths. They just didn't realise it. And here we get to the point about why I was delighted with the (sadly unrealised) possibility of teaching mathematics to students who were already programmers.

I do actually use some "real maths" in my programs. I recently coded a fun 3D shape explorer which involved using some maths to figure out the projections and other transformations that I had to apply to my data. I was mildly amused to find myself actually coding quaternions! But of course, the maths that was involved was trivial compared to the maths that I do when I'm working. It was "back of envelope" stuff. That type of maths, then I agree with the sentiment that you pick it up when you need it, and if you need something more complicated than you can find on Wikipedia then you find a real mathematician to do it for you. However, in order that you can pick it up when you need it then you need to have learnt something. That thing might not be anything you ever actually use, but having learnt that something makes it all the easier to pick up what you do actually use later in life. So this is where I disagree with Coder: you do need to learn some mathematics if you are ever going to use any mathematics and you need to learn it from the mathematical side (which doesn't mean proving theorems, by the way).

And so finally to the "Mathematics is Programming". You can learn all of these things from being a good programmer. And if you've learnt these things, you will find mathematics much easier because you will understand that when we talk about a vector in a vector space then it's just an instance of the class Vector which means that we can do all the things that Vector does to that instance: add, subtract, scale, and so forth. That's why I would love to teach mathematics to programmers. But, speaking as a mathematician, I would say that the first of these, "Abstraction", is easier to learn in mathematics than in programming because mathematics is the pursuit of abstraction. Whenever we see some behaviour our training is always to ask "What is it about that thing that makes it behave in that way? What if I took another thing that was similar, would it behave in the same way? How much of what that thing is would I have to lose for it to stop behaving like that?" (Taking this to the extreme leads to "centipede mathematics" - search for the term). But we don't do this with (just) "real world" objects (whatever they are), we do this with things that have already been abstracted.

This has gone on long enough, so let me close with one of the classic mathematician jokes:

A mathematician and a physicist both attended a seminar on some new model involving 24 dimensional space. Afterwards, they were discussing it and the physicist remarked: "That was really hard. I mean, how does one visualise 24-dimensional space?" to which the mathematician replied: "Oh, it's easy. Just visualise n-dimensional space and then set n = 24.".


Added 2012-03-2

There were quite a few comments on this answer expressing a variety of views. These have now been deleted by a moderator on the understanding that I would try to take into incorporate them (or respond to them) in my answer.

However, I'm not sure that I can. Reading those comments and the rest of what's on this page, I can only come to the conclusion that there is a huge misunderstanding as to what mathematics actually is. Moreover, I don't feel competent enough to explain it. Fortunately, someone has already linked to Lockhart's Lament so I'll defer the explanation to that. Whilst I might have put it differently (as I grew up in a scientific environment, I would have put more emphasis on the experimental nature of mathematics), I don't think I could put it better.

I do still think I can add something. As well as the misunderstandings as to what mathematics is, there are also misunderstandings as to what "doing mathematics" means. I see two almost contradictory stances:

  1. Mathematics is about equations and formulas. So there's no need to study it because Wikipedia exists (this is almost the converse of Euler's apocryphal challenge to Diderot).

  2. Mathematics is about theorems and definitions. So there's no need to study it as programs never prove anything (which is about as complete a fallacy as ... insert favourite fallacy here).

Whilst the two stances contradict each other, they end up in the same place: there's no point in a programmer learning any mathematics - and most assuredly not from a mathematician! After all, what do they know about anything? Anything that a programmer really needs to know can be found in Wikipedia, or cribbed off someone else.

Above, I described myself as a Cargo Cult Programmer. I bet most of you had a private giggle to yourself and thought, "Ah yes, I bet I know what your programs look like then.". You probably felt a bit smug and superior (though I'm sure you felt bad about feeling smug and superior).

What I've described just above is Cargo Cult Mathematics.

So when I say that you should learn a bit of mathematics to understand how mathematics works, I'm saying it for exactly the same reason as you might if you saw a bit of code that I'd written: "How much easier your life would be if you'd stop cut-and-pasting code from StackOverflow and learnt just a bit about how to do it properly.".

The most important thing, though, is that you should learn it from mathematicians. Why so? Here's an analogy. The language that I'm most adept at is TeX. (Says it all, really!). Now, suppose I want to learn a bit more about TeX and it just so happens that Don Knuth is in town and has offered to give some tutorials on TeX. Or I could just read about it on Wikipedia. Or maybe it's Perl and Larry Wall, or C# (is that the right one?) and Jon Skeet. It may well be that these people are not the best teachers, but they sure make up for it in the amount that they know!

And that's what mathematicians are. We're the people who write the actual language, who then write the libraries that you use. Of course, you don't have to know how to prove a theorem - you're not going to write a library! But if you know a bit about how we think, then it might help you understand why we wrote the library the way we did, and if you understand that it might help you make better use of it.

There is a middle ground between looking up equations on Wikipedia and proving the Poincaré conjecture, just as - to refer to Lockhart's lament - there is a middle ground between "I don't really know much about art, but I know what I like" and being Monet, and between "Where's the 'ANY' key?" and being Don Knuth. If you are still in university then you have an amazing opportunity to learn from people who are experts in their area and who - for some reason - are willing to spend their time explaining it to you.


The other point I wanted to expand on a bit was why as a programmer you should not be scared of learning a bit more mathematics. It's not the Deep Connections, nor the usefulness. It's that your ability to program a computer can directly help you learn mathematics. I just want to mention a few.

  1. Understanding variables. So many people get confused by simple statements like "Let n be a natural number ...". Or "Let epsilon > 0". There are places in mathematics where it's important to remember the scope of a variable. These are all commonplace in programming. Learn to translate a mathematical statement into a program and you'll find it much easier to keep track of what's what.

  2. The nature of proof. If you've ever written a test, or written a program to be used by someone else, then you understand the core of proofs. When you do that, you have to know that whatever the user puts in, you can deal with it (insert obligatory xkcd reference here). That's all a proof is! A demonstration that whatever the "user/universe" puts in, the statement will hold. Experimentalists will lean to the "If it works under normal circumstances, it's true" but programmers know that there is always that kid who will try Alt+G+Shift+ÅØÆ just to see what happens.

  3. DRY. Sorry to break this to you, but we invented it, not you. We've been "not repeating ourselves" for millennia. That's why I have a copy of Euclid's elements on my shelves and it's still useful.

And there's more. If I knew a bit more about programming, I'd write a book called "Mathematics for Programmers" where the aim wasn't to teach "The mathematics that programmers should know" but "mathematics that everyone should know, but optimised for programmers". But I'll probably never know enough about programming to write it - unless someone offers to collaborate with me!


I'll leave it there. Probably if I thought more, I'd change what I've written; hopefully I'd explain it better. In a months' time I might even disagree with parts of it. If anyone wishes to argue further, or comment otherwise, probably best not to do so in the comments here. You know where to find me.

  • 19
    This is one of the best answers I have ever read on SE; BTW, there is a grand book of "Mathematics for Programmers"; it's called Concrete Mathematics and it's co-written by the author of TeX, Donald Knuth. I just got a copy, and it's fantastic. I feel like your inverse; I got serious about programming when I realized that the best way (for me) to understand concepts in math class was to write a program to do it for me. Of course, then I wanted to use the program instead of doing rote computation by hand, and my math teachers did not like. – Jason Lewis Mar 02 '12 at 23:36
  • 1
    Dude, +100 for that. – Barry Brown Apr 03 '12 at 22:03
  • 3
    Registered just to upvote this – leo Apr 18 '12 at 01:05
  • Exactly. You cannot program well without the analytical thought that mathematics teaches you. – Fiasco Labs Apr 22 '12 at 01:56
  • Well, I like to see that things are identical essentially, at the abstract level. Yet, I am punished for that thinking. Mathematicians say that programming functions are not pure and just punish me when I ask to clarify the difference between single num and single-entry vector, http://math.stackexchange.com/questions/384927. They say that there is not single notion of vector, e.g. programmers forbid to think about objects as collectins of fields which identifies them with DB records and dynamical system state vectors. – Val Jul 12 '14 at 06:59
  • That is why they are so sure (you mock this belief out in your answer) that DRY does not exist outside the field, though it is universal principle of any truth/beauty/efficiency, aka Occam's Razor. I am tired of programmer stupidity and math arrogance. – Val Jul 12 '14 at 07:01
  • 1
    Is there a good book on the math OF programming (looking at programming mathematically)? – qed Oct 22 '14 at 14:55
  • An important point I should point out is I'm not sure how often this is really taught in a math course. E.g. I see many problems like "Solve equation X" or "Crunch out integral of Y" or whatever, but not many problems of the form "Find 3 different ways to define/describe a Z", which is REALLY what you want to get that vital abstraction capability. If you don't exercise that specifically, you won't have it. – The_Sympathizer Mar 06 '16 at 04:02
  • @mike3

    It depends. In beginning Analysis you would prove the equivalence of sequential compactness and topological compactness. Depending on the proof, the wrong choice could be difficult or impossible to use while the other is trivial.

    There is a mapping between constructive proofs and programs, https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence.

    Even the above examples you mentioned are often solved by Wolfram Alpha. How that is possible is important to both Math and Computer Science.

    – Polymer Mar 11 '16 at 16:43
  • Great answer! Thanks. I'm a programmer that found the Maths papers requisites for Comp Sci hard in university as they never related it to anything, even abstract, just a bunch or rules and corollarys. I passed but was none the wiser what to do with what I learnt. I have come back to it 10 years later and looked for better explanations and for some reason it seems to make more sense and is very very interesting. I wish I had this insight years earlier. – theringostarrs Apr 08 '18 at 15:18
26

They're not that closely related. For programming, it is important to know about mathematics- especially those branches pertaining to, for example, algorithm performance, but the simple fact is that there is no branch of mathematics that will tell you that Singletons are a horrifically bad idea, for example, or when to favour inheritance over composition, or whether or not you're really going to need that flexibility, and not to repeat yourself, and dozens of other core programming necessities.

Mathematics might be able to express what your program does, but it most certainly cannot tell you the most maintainable, human-readable, feasible way to go about it.

DeadMG
  • 36,902
  • 7
  • There is nothing wrong with singletons per se, 2) algorithmic information theory, formal languages and denotational semantics are directly relevant to those simple things you're so concerned about for some reason (they're not that much of an importance for programming in general).
  • – SK-logic Feb 26 '12 at 18:01
  • 1
    @SK-logic: Yes, Singletons are absolutely terrible, and in my experience, formal language theory had virtually nothing useful to say about building an actual language. – DeadMG Feb 26 '12 at 18:16
  • 20
    your experience must be somewhat limited then. Singletons are necessary and unavoidable, as long as they're part of a domain model. And how to build a domain model is a mathematical problem, nothing more. And yes, those who are ignorant in formal languages theory are producing horrible models and awful languages. – SK-logic Feb 26 '12 at 18:20
  • Your edit does not save your answer. Mathematics do explain which solution is the simplest, most readable and most adequate to the problem domain. Information theory is all about this sort of things. – SK-logic Feb 26 '12 at 18:21
  • 3
    @SK-logic I'm just a bit curious as to your practical programming experience given your rather firm assertions. Just where are you drawing your information from? –  Feb 26 '12 at 18:52
  • 1
    @SK-logic: I'm not interested in saving my answer from you. You and I have very different opinions about what makes a good or even a high-level programming language, and there's little question that I am never, ever going to change my answer to one that you agree with. – DeadMG Feb 26 '12 at 18:54
  • 2
    If two people have diametrically opposed and absolute views about something then, to me, the truth probably sits somewhere in the middle of the two extremes. – dave Feb 26 '12 at 19:30
  • 1
    @GlennNelson, I've been programming professionally for more than 20 years. Why are you asking? Well, my area of expertise is a bit narrow (as for everyone else, I bet) - compilers, interpreters, code analysis tools, database engines, embedded hardware. But there's nothing controversial about my statements, every computer scientist would agree. – SK-logic Feb 26 '12 at 22:06
  • 3
    Singletons are useful, as are global variables; but both should be used sparingly. See http://gamedev.stackexchange.com/questions/14217/several-classes-need-to-access-the-same-data-where-should-the-data-be-declared/14232#14232 – BlueRaja - Danny Pflughoeft Feb 26 '12 at 23:32
  • 10
    @dave: the danger in that method is that anyone can skew you away from a sensible, well-reasoned position by stating a wildly differing one, no matter how stupid. Taking averages is no useful substitute for evaluating arguments. – Michael Borgwardt Feb 26 '12 at 23:44
  • 4
    @SK-logic: Is it possible that you two are simply talking about different notions of what exactly is a "Singleton"? It seems to me that SK-logic is talking about the abstract idea of having only one instance of a certain entity in a system, which is sometimes exactly what is required. Unfortunately this abstract concept got closely coupled to the horrifically bad implementation detail of having a public static factory method because the GoF book gave that as an example of how such a Singleton might be accessed. – Michael Borgwardt Feb 26 '12 at 23:48
  • 6
    Please not more Off Topic discussion re Singtons – mattnz Feb 27 '12 at 00:18
  • 2
    Comments are not suitable for extended discussions, if any of you feel like continuing this one, please use chat instead. – yannis Feb 27 '12 at 01:13