Math easier to express in programming language

Started by
9 comments, last by Daerax 14 years, 10 months ago
Maybe it's because I'm a programmer, but I find that a lot of mathematical formulas are easier to express in a programming language than in the actual language that mathematicians use! For example this: x ≡ 1 mod n They use a triple equal sign and place the mod n behind the 1 while actually they're taking the modulo of the x instead of the 1. In programming language, it'd be: x mod n == 1 Personally I find the second much clearer, because the mod operator is applied to x instead of looking like it's applied to 1, and there's a more "normal" equal sign indicating the identity of two numbers. I understand why they write the first form like that in mathematics, but for some reason I can "think" better with the second form, because I see the mod operator being applied more properly there. They call it "congruence" in mathematics, but that doesn't really stop the fact that in reality there's a modulo operator taken and the term "congruence" doesn't really help to make that fact clear, on the contrary. Also, things like for loops can sometimes indicate summations much more clearly, and, using mathematical functions in a programming language with arguments inside the () is sometimes clearer to me than various mathematical notations for various functions. And of course, in a programming language you always write out the multiplication operator, while in mathematical notation, it's sometimes not clear if two symbols are being multiplied or they're just forming one symbol together or they're applying the first symbol to the second. So what do you find clearer?
Advertisement
Let us assume, for a second, that "x ≡ y mod n" can be accurately represented as "(x mod n) = y".

Substituting for x, y, and n, we know that the two following are true:

10 ≡ 3 mod 7
10 ≡ 10 mod 7

Now, using your equivalent representation:

(10 mod 7) = 3
(10 mod 7) = 10

Since the left-hand member is the same in both equalities, and the equality relationship is transitive, we therefore deduce that:

3 = 10

Which, I'm certain you will agree, is not true. The correct interpretation of "x ≡ y mod n" in terms of equality is "(x mod n) = (y mod n)".
I used to feel like you, but I have since been convinced of the superior expressiveness of mathematics assuming you have the adequate mathematical experience.

Quote:x ≡ 1 mod n

They use a triple equal sign and place the mod n behind the 1 while actually they're taking the modulo of the x instead of the 1.

In programming language, it'd be:

x mod n == 1

While the two statements are logically equivalent they state subtly different results. The first asserts that x and 1 belong to the same residue class. The second asserts that the representative of x in the residue class is 1. Most people (even many programmers) aren't that comfortable working with equivalence classes, residue classes and cosets, but by stating the result in terms of a concrete representative in the integers you make the statement more easily digestible for most. Thus the gain in understanding in my opinion comes from your rewording, not the actual syntactical differences. Also both would be perfectly valid in a mathematical context, but for mathematicians the first is as easy if not easier to comprehend and allows for some flexibility (for instance x ≡ 19 mod n <==> x mod n == 19 mod n, but the first is IMO easier to understand since we don't need to take the mod of 19). I believe the chief reason for mathemticians using congruences is that once you get used to it, it's easier to think of x and 1 being equal with a somewhat unusual definition of equal, but it takes some time getting used to.

Quote:but that doesn't really stop the fact that in reality there's a modulo operator taken

What does it mean that there really is an operator applied? I mean sure the way you rephrased it the equation involves the modulo operator applied, but I could instead phrase it n | x-1 so in my opinion what it really hides is that a division is taken, or perhaps it's a statement that (x-1)/n is an integer so what it really is, is:
is_int((x-1)/n))
but then it really is is a sequence of arithmetic operations followed by a test of whether a real number is integral.
The beauty of the mathematical approach is that it can be understood in a number of ways and that you're allowed to consider it in any way logically equivalent to the original form. To many people in elementary number theory this means thinking in terms of remainders as you seem to do, but an experienced mathematician has a much broader repertoire of tools to approach it including congruence classes, divisibility, algorithmic description and modulo operators.

Quote:Also, things like for loops can sometimes indicate summations much more clearly,

Often a description written using summations or for loops only contains superficial difference and as such the real difference comes in your prior exposure to the syntactical constructs. A for loop is a more natural way to express yourself as an imperative programmer, but as a mathematician you often find that a summation is a much clearer representation of your thought pattern. The only difference is experience.

Quote:and, using mathematical functions in a programming language with arguments inside the () is sometimes clearer to me than various mathematical notations for various functions.

Again simply a result of your experience.

Quote:And of course, in a programming language you always write out the multiplication operator, while in mathematical notation, it's sometimes not clear if two symbols are being multiplied or they're just forming one symbol together or they're applying the first symbol to the second.

It should ALWAYS be clear in decent mathematical writing what happens if you understand the objects involved. Context matters. Can you give an example? Because while I have heard people voice similar complaints I don't remember ever being confused over whether multiplication or function application was meant. Notation is carefully specified and it would be considered bad mathematics to use confusing notation. It's possible to abuse mathematics and due to the informal specification of notation it can be easier, but such an abuse is the mathematical equivalent of confusing function names and excessive operator overloading.
Vector3 x,y;Vector3 z = x(y);

What does this do? Translate x by y? Possibly, but the point remains that it's easy to be confusing in both cases and if you don't understand the subject field (programming or mathematics) then surely you will make mistakes.

I see mathematics as a much more general, flexible and elegant framework for expressing ideas. That said it can be somewhat elitist and rarely do much to ease the learning curve. You think C++ is hard to get started with because you don't get concrete results? Try algebraic topology. However once you have that BSc or whatever you will find it much easier to express ideas mathematically than algorithmically. However as many programmers never get the necessary mathematical experience you're right that for most programming notation will probably be easier.
Quote:Original post by ToohrVyk
Let us assume, for a second, that "x ≡ y mod n" can be accurately represented as "(x mod n) = y".

Substituting for x, y, and n, we know that the two following are true:

10 ≡ 3 mod 7
10 ≡ 10 mod 7

Now, using your equivalent representation:

(10 mod 7) = 3
(10 mod 7) = 10

Since the left-hand member is the same in both equalities, and the equality relationship is transitive, we therefore deduce that:

3 = 10

Which, I'm certain you will agree, is not true. The correct interpretation of "x ≡ y mod n" in terms of equality is "(x mod n) = (y mod n)".


Hey, I wrote x ≡ 1 mod n, not some other number. If I had used x ≡ y mod n I'd indeed have written (x mod n) == (y mod n).

But in my course almost all those formulas are with "1", e.g.

a^(p-1) ≡ 1 mod n

Quote:Original post by CTar
It should ALWAYS be clear in decent mathematical writing what happens if you understand the objects involved. Context matters. Can you give an example? Because while I have heard people voice similar complaints I don't remember ever being confused over whether multiplication or function application was meant.


It's just that not all slides and books made by professors are all that clear, sometimes they just throw formulas with various symbols in there without any explanation what they mean. Today we have Wikipedia, but when I had those courses Wikipedia was still in its infancy and I had to find out the meanings of the symbols from various PDFs about the same subject found on Google, if they'd even use the same symbols in there.
If you know any group theory, it's best to think of the statement

10 ≡ 3 (mod 7)

as representing the fact that 10 and 3 lie in the same residue class modulo 7, where the residue classes modulo 7 form a group under addition. Thinking of this as the application of the modulo operator is, in my opinion, not a very productive way of thinking about it.
Quote:Original post by CTar
I see mathematics as a much more general, flexible and elegant framework for expressing ideas. That said it can be somewhat elitist and rarely do much to ease the learning curve. You think C++ is hard to get started with because you don't get concrete results? Try algebraic topology. However once you have that BSc or whatever you will find it much easier to express ideas mathematically than algorithmically. However as many programmers never get the necessary mathematical experience you're right that for most programming notation will probably be easier.


Exactly. Mathematics is just a language -- surprisingly expressive if you consider what mind-bending things you can represent using even the simplest notations. Take exponentiation, for example -- you can say "Look here, this is a basket of twenty apples -- and I've got as many these basket as there are apples in this basket." An overcomplicated way of saying I've got 202 apples. And that's only squaring a natural number -- you try yourself expressing something non-trivial without using mathematical constructs. (Expressing the above example without even using numbers would be interesting, too.)

But there's one thing about mathematics that I never got: single-letter symbols. Being a programmer I always try to name my objects (variables, constants, functions, ...) expressively, not resorting to single-letter names (with some exceptions, of course) -- reading through a theorem that uses ten letters to denote ten different things does get quite confusing for me.

One could argue that saying apples = 20 could be understood as an equation saying p2 * a * e * l * s = 20, but I don't see why nobody in the years that this notation has been used has ever standardised notation like, “apples” = 20, simply saying that whatever appears inside quotes is supposed to be understood as a single name. (Quotes could be substituted for any other weird symbol, of course -- doesn't matter much.)

And yeah, I know that there are multi-letter names for some common things -- sin, for instance. Also, I've seen things like Mmax = ..., where the subscript is not understood as a product of three things. But this is the exception rather than the rule.
Quote:Original post by Oxyd
But there's one thing about mathematics that I never got: single-letter symbols. Being a programmer I always try to name my objects (variables, constants, functions, ...) expressively, not resorting to single-letter names (with some exceptions, of course) -- reading through a theorem that uses ten letters to denote ten different things does get quite confusing for me.

One could argue that saying apples = 20 could be understood as an equation saying p2 * a * e * l * s = 20, but I don't see why nobody in the years that this notation has been used has ever standardised notation like, “apples” = 20, simply saying that whatever appears inside quotes is supposed to be understood as a single name. (Quotes could be substituted for any other weird symbol, of course -- doesn't matter much.)

And yeah, I know that there are multi-letter names for some common things -- sin, for instance. Also, I've seen things like Mmax = ..., where the subscript is not understood as a product of three things. But this is the exception rather than the rule.

If you chose your letters good and stay consistent, then single letter names are rarely a problem. Use a symbol and define what it is, and stay consistent. Not only in what symbols you use, but what groups of symbols you use for what purpose. For example, my common definition is to use letters K to Q for integer counts, and the corresponding small letter for counting (that is, k counts over K, and l counts over L).

It is also a space issue when writing. If you have a journal with double, or triple, column typesetting, then you just don't have enough space for anything but the shortest possible name. Had I used descriptive names for symbols in my publications, the names themselves would be longer than the available width of the column. Try split an A4 or letter into three columns and see how much, or little, you can actually fit into it.
Quote:Original post by Lode
I find that a lot of mathematical formulas are easier to express in a programming language than in the actual language that mathematicians use!

my entire thesis is based on this. there is more mathematicians can get from programmers. like partial verification and types.

Doron Zeilberger is how do you say, quite opinionated. But the core of what he says is the general direction I go. mathematical notation is left overs from the past. it does have its place as a useful short hand i suppose.

[Edited by - Daerax on June 21, 2009 9:30:09 AM]
I will say that the one issue I've ever had with mathematical notation is that assuming you have been formally trained in the topic being drawn out, it's easy to translate the mathematical shorthand and understand what's going on. However, many people simply aren't, and so seeing in a more familiar form is generally easier for them to get the basic idea. It's for this reason, that I generally discourage one or two letter shorthand variable names even in math-style code, because then only those who are already experts in the area will understand the shorthand-style notation, even if it is in code.
Read the section on remainders in Wikipedia's Modular Arithmetic article. I actually hadn't seen the notation with equality instead of congruence until I read that article. There's a funny thing about that notation. One usually writes
a ≡ b mod c
with 0 ≤ b < c (with -c < b < 0 being the second most common), but if one wrote
a = b mod c
then you're required to have 0 ≤ a < c.

Anyway, the reason the notation is different is because the meanings, while related, are different. If
a ≡ b mod c
then
a + nc ≡ b + mc mod c
where n and m are any integer. In other words, it's referring to a whole set of numbers, all of which behave the same in modulo arithmetic (for example, adding (c-1) is the same as adding -1, modulo c). The notation found in programming languages is referring only to the least positive element or the element that is the remainder after division (the C89 standard, for example, left that implementation defined).

Quote:
It's just that not all slides and books made by professors are all that clear, sometimes they just throw formulas with various symbols in there without any explanation what they mean.


Perhaps I had better professors because I could always find the symbol either in the textbook or in my notes from that class. The only cases I can think of where that might not have been the case are when they were using a standard notation (e.g. lambda for wavelength in physics, or sigma for the standard deviation in statistics) in a junior or later level course where a student could be expected to recognize the notation from context.

This topic is closed to new replies.

Advertisement