by qsort · 3y ago · 0 comments
Math is equational and declarative. When you write:
S = \sum_{i=1}^{100} (2i + 1)
you are declaring that we shall call S the sum of the elements of a certain set. You are not prescribing how this sum ought to be calculated: S is a pure value that doesn't depend on what practical operations you do -- be them on paper or on a CPU -- to actually find out what S is.For example, using equational reasoning, we may write:
S = \sum_{i=1}^{100} 1 + 2 \sum_{i=1}^{100} i = 100 + (100 * 101) = 101^2 - 1
Or, without computing anything at all, we may prove by induction that: (1) \sum_{i=1}^{n} (2i + 1) = (n + 1)^2 - 1
The base case is obvious, and to prove the inductive step it is sufficient to observe that indeed: (n + 1)^2 - 1 + (2n + 3) = n^2 + 4n + 3 = (n + 2)^2 - 1
From which it follows: S = 101^2 - 1
Tying math to code is more like tying your own hands behind your back.All of this is basically an excuse to point out that the code for the product is wrong, the initial value should be 1 and not 0 (or, in general, your monoid identity).
I don't see a problem, if eg it helps with getting some intuition.
Sure there is a formula for this example. But the point in the article of writing this sum as a for loop is to explain what the notation means.
For a programmer who is not familiar with sigma/pi notation for sum/product, explaining it with a for loop is a quick way to understand.
It feels a bit like how I found it impossible to memorise all the countries of the world as an adolescent, but then when I discovered Seterra as an adult I was able to memorise them all in less than a week. I need an equivalent perspective shift, but for math.
> it takes me multiple days to understand a single formula
This is not weird. Papers are not hard because notation is hard but because the ideas behind them are difficult to understand. It usually takes me several hours of uninterrupted work to read a paper; I'm not a researcher and have never been in academia, but everyone I know says the same thing, so I'm comfortable reassuring you that you're definitely not the odd one out.
A few pointers to go faster:
- Write down your observations (on the paper itself, if you can print it). Ask yourself questions and see if you got things right. Try to replicate their computation steps. Actively engage the topic.
- Keep a dictionary of symbols. What do the authors mean with this ridiculous scribble? Ah, that thing.
- Mathematical notation suffers from catastrophic overloading. For example, if A and B are numbers, then AB is multiplication in their set. If they are matrices, it's matrix multiplication. If A is a matrix and B is a vector, it's the image of B through A. If one is a vector and the other is a number it's multiplication by a scalar, and so on. Try to undo this process and figure out what the virtual method call resolves to :)
- Try to assign "types" to variables. Notation is "dynamically typed", in the sense that a variable could (syntactically) be anything. If you have trouble understanding a formula, try to understand what kind of object each variable represents. Is this squiggle a set or an element? If an element, from what set does it come from? What are the arguments of this function? What is its image? Is this "i" a real variable or a mute variable? To what quantifier is this variable bound?
- Sometimes notation is abused. E.g. we may write:
10n^2 + n = O(n^2)
This is, strictly speaking, nonsense: O(n^2) is a set, and we're saying it's equal to some undefined stuff. What even is "n"? Obviously, it "compiles" to: f(n) = 10n^2 + n
f(n) \in O(n^2)
It's normal to abuse notation, but it may cause confusion if one is unfamiliar with the topic. Try to undo this process if you find it's preventing you from understanding a formula.Hopefully I am wrong and there is a way to learn with a game as with Seterra.
Not to pick fights, but your example seems sort of orthogonal to that aspect. What seems more relevant is how you are choosing to structure your code, and how mutable your state is.
Either is fine for explaining something to a human, as long as it makes sense from the context.
\sum i. lower upper t[i]
and then write the above sum as \sum i. 1 100 2 * i + 1
(after * and + and numbers have been defined as well)There is no reason why the above cannot work both as math notation and as code.
\sum_{n=1}^{\infty} \frac{1}{n^2}
Note that I am not saying that math is code. Practal is based on logic, and logic transcends code. What I am saying is that code is math. At least, that's obviously true for purely functional code. So there is no reason to write the subset of code which is purely functional not in math notation.
Translating math to code in itself is an art (or rather, a technical discipline called Numerical Methods). That's (at least one) full course at any real engineering school.
The article is good, but I would expect anyone calling himself a software engineer to have already studied and mastered the material covered.