
Understanding Mathematical Notation as Code - mattdesl
https://github.com/Jam3/math-as-code#
======
j2kun
This is a nice little document, but you have to understand that mathematical
notation is syntactically closer to spoken language than to code, and it's
only expected to be semantically rigorous after context is taken into account.

------
brudgers
Ken Iverson's Turing essay was _Notation as a Tool of Thought_ [1]. Both his
languages, APL and J [2] were based on the idea of mapping mathematical and a
programming language. Iverson's _Math for the Layman_ presents that link
explicitly despite being an enjoyable read. [3]

[1]:
[http://www.jsoftware.com/papers/tot.htm](http://www.jsoftware.com/papers/tot.htm)

[2]: [http://www.jsoftware.com](http://www.jsoftware.com)

[3]: [http://www.cs.trinity.edu/About/The_Courses/cs301/math-
for-t...](http://www.cs.trinity.edu/About/The_Courses/cs301/math-for-the-
layman/)

------
hasenj
I've always thought (ever since university) that math notation should take
inspiration from code. Instead of writing Xi, they should really write X[i];
because Xi is ambiguous. I find that dealing with all the ambiguity in syntax
can be rather frustrating.

~~~
john_b
> _" Instead of writing Xi, they should really write X[i]; because Xi is
> ambiguous. I find that dealing with all the ambiguity in syntax can be
> rather frustrating."_

This is a feature of math, not a bug. The potential for ambiguity is the cost
that one pays to have a flexible and extensible notation which can be adapted
to concepts yet undiscovered. This is generally not the case with code [1].
When you are exploring new ideas you want the ability to redefine your
notation to match the nature and structure of the abstactions you are
examining.

There is a finite number of symbols in the set of all human languages, and
thus far we know of no reason that there should be a finite set of concepts in
mathematics. Enforcing a one-to-one mapping from a given sequence of symbols
to a given concept forces you to either limit the space of concepts you can
consider or to eventually deal with impractically large sequences of symbols
for relatively simple concepts.

[1] Yes, Lisp and DSLs are a thing, but you still have to define what a given
sequence of symbols means. In an interpreted language, the interpreter
computes the meaning using inputs and any necessary state. In math, the
meaning is necessarily dependent on context as well.

~~~
hasenj
> deal with impractically large sequences of symbols for relatively simple
> concepts.

I don't think they would ever have to be impractically large.

Right now they are just impractically weird.

------
kej
The part about using = for definition doesn't seem to reflect the way that
works in mathematics.

The mathematical meaning of _x = 2kj_ isn't var x = 2 * k * j but var x =
function(k, j) { return 2 * k * j; };

~~~
mattdesl
I'm working on improving this part; feel free to add more discussion here:
[https://github.com/Jam3/math-as-code/issues/17](https://github.com/Jam3/math-
as-code/issues/17)

------
refrigerator
I don't like being negative, but this seems really patronising and unhelpful.
Just because someone is a 'self-taught game/graphics programmer' doesn't mean
that describing maths concepts to them as code is at all intuitive, especially
for things relating to geometric concepts like vectors, for which a simple
diagram would speak volumes.

~~~
SEMW
I agree, sadly. I'm not sure anyone who didn't already understand vector
products is going to be helped by staring at "var rx = ay * bz - az * by; var
ry = az * bx - ax * bz; var rz = ax * by - ay * bx" (as opposed to, say, [0]).
Sure, it's precise and unambiguous, but it's missing the context that make it
possible for a human to actually understand what it is as opposed to just
calculating it (which the javascript interpreter is capable of doing fine on
its own).

To say nothing of "var determinant = require('gl-mat2/determinant');
determinant(matrix)"! I guess the point is to go read the source code of the
library, but I can't imagine many worse ways of learning linear algebra than
reading the source code of optimized linear algebra routines.

[0]
[https://upload.wikimedia.org/wikipedia/commons/6/6e/Cross_pr...](https://upload.wikimedia.org/wikipedia/commons/6/6e/Cross_product.gif)

~~~
mattdesl
The point is not to understand linear algebra in a few lines of code. The
point is to present the language of mathematics in another light; and
hopefully demystify intimidating-looking equations that often appear in
literature surrounding games, graphics and other fields of programming.

The audience is hobbyists and self-taught developers with no formal background
in mathematic notation. This audience might have no problem with a for loop,
but the Summation symbol is (literally) just Greek to them.

------
contravariant
Not sure if I agree with the statement that "=" is used for definitions. It's
true meaning is to simply state that two things are equal. I can see where the
confusion comes from since mathematicians have a tendency to just state "Let x
= 2kj", instead of the more explicit "Let us assume that the statement 'x =
2kj', is true". It's important to note the distinction though, since
introducing a new variable by simply stating it's properties is more powerful
than just defining it to be equal to something. For instance it's equally
valid to define a symbol x by simply stating "Let x∈A". This is used quite a
lot since anything you can then prove about "x" must automatically be true for
all elements of A.

~~~
arsenide
I like the idea of using the compound symbol ":=" for "is defined to be". So,
you can say something like "For x∈A, y := 2x". When I take math notes, it
helps a lot using this shorthand. I'm able to remove some ambiguity between
what is and is not a definition.

~~~
contravariant
I definitely agree with the use of ":=", but "=" simply doesn't mean the same
thing. For example, the similar statement "For x∈A, y = 2x" could even be
interpreted as using y to define x.

~~~
arsenide
I don't understand -- you say "=" and ":=" are not the same thing, but give an
example where they act the same?

Practically speaking, using ":=" removes any doubt when quickly skimming over
old notes. It is nice to be able to easily separate what is defined and what
is asserted in each of dozens of statements.

------
jwp729
Surprised there's no mention of the Curry-Howard correspondence between proofs
and programs.

~~~
mlitchard
Mention? The title led me to believe that was the topic.

------
aikah
Nice, it would make a great book : "Math for coders: from code to math
notation.". Seriously ;)

------
rectangletangle
This is amazing; It's always annoying having to Google arcane mathematical
notation, in order to translate something into code.

------
32h8
Math is pure = not mutable, and that code is mutable. Damn barbarians try to
desecrate math!

------
ilaksh
Math notation is just obfuscated code in a language that doesn't compile or
run.

~~~
paulmd
It's not really code - more of a pseudocode, because it leaves the details of
the implementation to the reader.

Might be what you were getting at with "doesn't compile or run".

------
bogomipz
This is neat. I hope to see more!

