Hacking Math I--Math Precision of Python

©Fernando Caracena 2014

In a previous blog, "A beautiful Math Maze", I used python to explore various ideas in pure mathematics. The idea is that programming languages are math machines that contain within them the accumulated experience and knowledge of generations of mathematicians. We learn by doing, know by reflecting, and understand by sifting through knowledge and exploring its connections.  Computers, therefore, can serve to tease out an undertanding of math ideas and relations, in other words, to hack math itself. If there were some popular demand for this, I might write a book on "Hacking Math and Physics using computers".

When we used mnemonics

I remember during my early days in physics, maybe in high school learning pi to about 14 decimal places, by using the mnemonic phrase:"how I need a drink alcoholic of course after the heavy chapters involving quantum mechanics". All you needed to do was count the number of characters  in each word [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9], and remember where to put the decimal point. Of course, in those days such knowledge was redundant because slide rule accuracy was sufficient for most calculation, and even then fast math in one's head was almost as accurate, but much faster. Still, we were kids who wanted to dazzle the teachers and pull the A+ rabbit out of the grade hat, instead of just an A or mere B+. The development of computers and modern computer languages have rendered moot all of that intellectual prestidigitation. Now our kids can get on with fast learning, provided that they are not presided over by professional morons who call themselves educators, who are really latter day Luddites. Our kids have so much to learn to be able to manage the rapid advance of technology that we do then a disservice by holding them in trying to teach them arithmetic through complicated procedures. We should teach them how to use technology it self to bootstrap their rapid learning and gaining of understanding.

I am impressed with the advance of technology and how its incorporation of voice input and visual output together give the whole population potential access to the entire intelligence of the human race.  For example, I used the phrase,je ne sais quoi in front of my grandson, who promptly spoke it into his smart phone to get the instant definition. Now what is wrong with that? It is faster than going to the huge dictionary and thumbing through the thin sheets. People voice concern over the dangers to the human race on over-reliance on technology to solve mental exercises as a crutch that could give way to a new Dark Age given some large-scale disaster that produces nepenthe. The worst case scenario would be that some insane world leader could release some destructive force that destroys the whole intellectual-technological network and plunges the world into a super nepenthe that resets the whole, (by then) starving human race back to the stone age. When all the batteries run out, we would all then be in deep trouble. But then, we would not have any paper to use for math calculations either; and of course, we would be too busy surviving to care anyway. So let us be optimistic and use the best that we have today, and preserve our historical knowledge of scientific ascent in more primitive print media, just in case.

Continuing the discussion of python

First, let us explore some of the properties of the python programming language by using in its most basic form. Initialize interactive python containing matplot libraries with the command,

python --pylab

Next at the prompt In [1]: type an addition statement

In [1]: 3+2                             .

and hit return. The computer instantly responds as follows:

Out[1]: 5                                .

Next, try a fraction,

In [2]: 3/2
Out[2]: 1                    ,

but wait(!?), we know that this is not the answer that we would expect. That is because the computer sees the quotient of two integers, and gives the correct answer that the result is the integer, 1   . So how do we inform the python interpreter that the above is not a quotient of two integers? The simplest way is to put decimal points after each number, as follows:

In [3]: 3./2.
Out[3]: 1.5                         .

Now let us define a string variable,

In [4]: aa='Hi there!'
Next, print it out by simply naming it and hitting return,

In [5]: aa
Out[5]: 'Hi there!'

How simple and intuitive is all this?

We can define an array by a simple command,

In [6]: b=[1, 2, 3]

and verify that that is what happened, as follows:

In [7]: b
Out[7]: [1, 2, 3]              .

Similarly, we can define a string array as follows:

In [8]: cc=['a', 'b', 'c']

In [9]: cc
Out[9]: ['a', 'b', 'c']                          .

In verifying the mnemonic for pi we type it as a string array and get the following exchange:

In [10]: qq=['how', 'I', 'need',  'a', 'drink', 'alcoholic', 'of', 'course', 'after', 'the', 'heavy', 'chapIn [10]: qq=['how', 'I', 'need',  'a', 'drink', 'alcoholic', 'of', 'course', 'after', 'the', 'heavy', 'chapters', 'involving', 'quantum', 'mechanics', 'end']
In [11]:qq
Out[11]:
['how',
'I',
'need',
'a',
'drink',
'alcoholic',
'of',
'course',
'after',
'the',
'heavy',
'chapters',
'involving',
'quantum',
'mechanics',
'end']                                  .

Note that I added 'end' as a last string, instead of a period. Also, the strings are separated by commas, which cannot be included in the quotes, which is the logical choice that prompts me to reverse the ancient typesetter's quirky conventions.

It is easy to count the number of characters in a string in python by applying the built in function, len, as follows:

In [12]: len('alcoholic')
Out[12]: 9                                                .

A quick way to count characters of the various string elements is by applying the length operator, len, to each element. We could apply a loop to do this task, but a faster way is to use the built-in map operator, which acts as follows:

nq=map(len, qq)

nq prints out as [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3]

All this is redundant in python for getting pi, because you can get it directly by the simple command,

In [13]: pi
Out[13]: 3.141592653589793

Notice how python has undercut so many memory short cuts and mnemonics?

A pretty good approximation for pi in any language, in case you are using a programming language that does not have it built in is the evaluation of a fraction formed from the double sequence of successive, odd numbers, 113355, which is

In [14]: 355./113.
Out[14]: 3.1415929203539825           .

NB, the following sequence shows that the error of the above fraction is small:

In [15]: (355./113.-pi)/pi
Out[15]: 8.49136787674061e-08

(355./113.-pi)/pi                    .

Python as a language is disarmingly simple, but a very powerful object oriented language. I use it together with the matlab library to generate all the figures for my blog on ghyzmo.com, where I also pass along the code in case someone wants to verify the figures on their own computers. To do this they must of course, have the proper python software and Matplotlib loaded, as above.

Complex variables in python

Python supports complex variables, but not in the traditional math notation, which is as follows
C=R+i Im ,

where R is the real part and Im is the pure imaginary part; however, in python notation is

C=R+1j * Im ,

where R and Im have assigned numerical values and 1j represents the square root of -1.

NB, there is a symbolic extension of python under development, called SymPy, which I have yet to explore. If I a do, I may report back about it on Ghyzmo.

Let me give you some examples that use complex notation in python.

Define a complex number and find its complex conjugate in the following sequence:

In [16]: c=3+4j

In [17]: conjugate(c)
Out[17]: (3-4j)               .

Now the product of a complex number with its complex conjugate results in its a  real number, which represents the square of its amplitude:

In [18]: c*conjugate(c)
Out[18]: (25+0j)

We recognize the above as the correct result because

32+42=52

and the imaginary part is zero, indicating a real number.

 In the blog  "A beautiful Math Maze"

  we presented what some mathematicians consider the most beautiful equation , Euler's identity,

e+1=0.                                 (ABMM.17)

Since python has all the the above functions, let us see if python verifies the above through the following exchange:

In [19]: exp(pi*1J)+1
Out[19]: 1.2246063538223773e-16j                                         .

You might wonder why the result is not exactly zero as the theory would indicate; but remember that python works to within a certain number of decimal places. It is not an infinite precision engine. The above result, which is pure imaginary, represents the difference of two complex numbers that each are very close to the order 1. The error is in the imaginary part somewhere around the 16th decimal place, which is a PD Good approximation.

Conclusion

The point in the above discussion is to show how one can use a sophisticated modern language in such a way that we can explore pure math with a device that is constructed to conform to the principles and logic of math. A secondary point was to give absolute newbies a primer in using python. The rest they can fill in by looking python up with a good search engine.

Ciao, buona fortuna.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *