Tuesday, July 31, 2012

mathematical mishaps: local vs. global variables

Yesterday I had a long discussion with my professor on the meaning of |aij| <= sumj(sumi(aij 2))1/2.  When he saw that written in my homework, even without a long discussion on what it might mean, his immediate reaction was that it was incorrect.  However, if I simply substituted in the subscripts kl on the right hand side so |aij| <= sum(sum(akl2))1/2, it would be fine.*

It is at moments like these that it becomes clear to me that math is a language with its own ambiguities, and that I am far from fluent.  Also, because I learned to program years ago, coding feels "natural" to me - but my programming instincts can lead me astray with mathematical notation.  Let me explain.

When I wrote the above equation, I meant to say, "The value (on the left) of any entry in a matrix is less than (on the right) the square root of the sum of all the squared values in the matrix."  To break it down even more, that very compact statement is a set of directions, and if I was a computer, I would read them something like this:
Take the absolute value of some entry in a matrix (on the left hand side).  On the right hand side, square all the entries any given column of the matrix and take their sum.  You end up with a row of those values.  Then add up all the sums of squares to get a single value, and take its square root.  Now compare the two values.  The left hand side is less than or equal to the right hand side.  
When I asked my instructor what was wrong with my original statement, he said that (on the left hand side) by using i and j in the subscript I had implicitly said "for all i and j," that is, "for any and every given entry in the matrix, compare this to the right hand side."  But on the right hand side I was summing over all of the entries to yield an actual value, an upper boundary on what any individual entry in the matrix could be, which conflicts with my left hand side definition of i and j as a single entry would be.

At the moment, he couldn't quite articulate why it was wrong, and I was still a bit unclear on whether it was strictly wrong or merely ambiguous and confusing.  But I think I know where I went wrong and why: global versus local variables.
 * * *
In programming, I can declare a variable**, for example, a=3.  Then for the computer, every time it sees a, it will substitute in 3 and compute whatever it is supposed to compute.  So you could think of my program as its own little country in which every variable a is interpreted as 3.  Yet inside my program I can create a function which has local variables that are only applicable inside the function.  You could think of this as a local region inside the world which has its own rules and language, but that they don't apply outside that region.

This is analogous to the following situation: if you heard the sound "NYNE"  in most of the United States you would interpret it as the number "nine," but if you happened to step inside a German-speaking household you would interpret it as "nein," which means "no" in German.  Yet as soon as you left the house, you would assume that everyone spoke English again and that you should interpret the sound "NYNE" as the number 9.  (This is, incidentally, why pilots will say "niner" - pronounced NYN-ER - on the radio instead of "nine," to avoid confusion with the German word for "no."  Thus, the numerals 190 would be read "one-niner-zero.")

In this situation, stepping from the outside world into a German-speaking household is like entering a function.  Within that function, the sound "NYNE" is a local variable with the meaning "no."  When you exit the function, like leaving the household to walk on the street, you leave behind the meaning of the sound "NYNE" and would interpret it as whatever the outside world has set it to be (the numeral 9).

However, I can also declare a global variable.  This means that no matter where in the program I am, whether inside a function or outside a function, the value of that variable holds.  Going back to the language analogy, it would be a word that would be interpreted the same way whether inside the German-speaking household or on the street.  For example, the word "iPod" (or "McDonald's") could be thought to have universal meaning.  Regardless of whether I was inside the German-speaking household or outside on an American street, I would interpret the sound to mean a small electronic music-playing device (or fast-food restaurant chain).

* * *
So returning to the question of what was wrong with |aij| <= sumj(sumi(aij 2))1/2: My professor was reading the subscripts i and j as global variables which, like the word "iPod," should have meaning throughout the statement, including both the right- and left-hand sides.  I had imagined i and j on the right-hand side as local variables inside functions, which, like the sound "NYNE" in a German-speaking household, should only be interpreted as "no" in the context of the sums, but after exiting, no longer held that local meaning but should revert to the English meaning of the numeral 9 for the rest of the statement.

Of course the easy solution is to remove the ambiguity by changing the subscripts on the right hand side to make clear that they are local variables, hence my professor's suggestion to change them to kl. But I wanted to understand why I did not read the statement the way my teacher or a more experienced mathematician would.  Because I spend a lot of time programming, I was too quick to think that indexes on a sum were local variables, not considering that they could also be read as global variables (like not realizing someone might interpret "NYNE" as the numeral 9 everywhere, even inside a house that might speak German, creating confusion).  My cultural mishap came because, it seems, mathematicians assume all variables by default are global variables, but I as a programmer - and one with the bad habit of using the same variable names inside a local function to have different meanings than for the overall program - brought those local variable interpretations to the math equation.  Lesson learned: from here on out, I will assume unless told otherwise that all variables will by default be assumed to be global variables, and try to write my equations in a way that is not ambiguous or confusing.

* For the non-mathematically inclined, think of a matrix as a rectangular grid of numbers, much like an Excel spreadsheet.  You can reference the location of any entry in this grid by its row and column.  For example, I could tell you to find the entry in the third row and second column.  In this particular mathematical notation, I could write this as a31 (which you should not interpret as the 31st entry, as would be my first instinct as a non-trained person).  If I wanted to tell you to an entry in any row or any column, instead of writing the actual number, I would write a letter (such as i for rows, j for columns) as a placeholder to tell you to substitute in any number in the existing number of rows: look at the entry in the ith row and jth column.  I would write this as aij.

**Again, if you are someone for whom the sudden introduction of letters (or variables) in algebra was really confusing and puzzling ("How the heck am I supposed to add x and y?  I thought I could only add numbers..."), you could think of the letter as a little empty box that could be filled with any number that you want.  For example, x + y = 0 means "The sum of some number and some other number is 0." 0 The trivial answer is that 0 + 0 = 0.  But two other numbers that add up to zero are 3 and -3.  Another set of numbers that work is -25 and 25.  Even 2,345,236.25 and -2,345,236.25 will work!  As you can see, any number minus itself (or added to the negative of itself) is 0.  So when you solve that equation to find that x = -y, what you are really saying is, If I want to know what two numbers can be added to yield zero, I can pick any number and its negative value, and the sum of that pair will be zero.  The letters are just a shorthand way of saying "any number."

No comments:

Post a Comment