Sunday, December 27, 2009

STARTING OFF IN RUBY

MY FIRST RUBY PROGRAM !

Dabbling in python and its variants I came to try ruby ! The syntax is very similar to python.

Fig1. Ruby version 1.8.7

My first program was straight from the ruby website,

Fig2. Seen on Ruby website

The output was as desired !

Fig3. I love Ruby !


Friday, November 27, 2009

SINE INTEGRAL IN SCIPY

THE SINE INTEGRAL

The Sine Integral is a very important function in Physics, Astronomy, Electrodynamics, Mathematical Physics,Optics and Signal Processing.

A fundamental result in the sine integral is;

This result is analytically proven using contour integrals concept from complex theory.


TRYING IT IN MATLAB



MATLAB gives excellent results, particularly for the special case of (0-inf )it gives correct value.

TRYING IT IN SCIPY


In Scipy the sine integral (and the cosine integral) is via (si,ci) = sici function. It yields excellent values for numbers, however for infinity it yields nan (not a number). This should probably be corrected with an exception in the sici module.

It is worth noting that for sufficiently high values (which tend to infinity) the desired result of 1.57.... ( = pi/2) is obtained, which confirms the numerical evaluation is correct


REFERENCES

(1)
sici
(2) sine integral




Wednesday, November 25, 2009

RAISING IT TO THE POWER OF ......

LAWS OF EXPONENT

In the laws of exponents, a number can never be raised to an exponent to yield negative values. Only using complex exponents can negative values be obtained.


Trying the same formulation in python, it is worth noting that the formulation fails for a = 1 hence a special case output for a = 1.


Some sample output is ;


Similar treatments in MATLAB is also fruitful



The visible change is that iota in MATLAB it is i, while in Python it is j.


Saturday, November 7, 2009

BIZARRE BIZARRE PYTHON

IS THAT SOME WITCH CRAFT ?

Python .... maybe the one of the best programming languages has just gone crazy !

In the interpreter mode I got these crazy results trying to get numbers starting with zero...


Only on referring to Hetland that I got to know that the interpreter does an OCTAL !

REFERENCES
(1) Hetland

Monday, October 19, 2009

SQUARE ROOT OF IOTA

SQUARE ROOT OF COMPLEX NUMBERS

i, the square root of -1 the fundamental complex number. Working out the square-root of i;


ON MATLAB

Trying it on Matlab


Fig 1. Matlab 1



Fig 2. Matlab 2

Matlab gives very precise result both by 'power of 0.5' and 'sqrt function'.

USING CMATH

Using cmath module in Python;


Fig 3. cmath 1


Fig 4. cmath 2

cmath also gives wonderful results, however it is worth noting that the real and complex parts are different in the last 2 digits ( 0.70710678118654757 in the real part while 0.70710678118654746 in the complex part); which should not be so as they both represent the same number !

USING SCIPY

Using Scipy, scientific and numerical module in python



Fig 5. Using Scipy

Similar results to that of cmath.

SQUARING THE ROOT !

Squaring the square root often confirms to the accuracy and resolution of the software.


Fig 6. Squaring the root in Matlab


Fig 7. Squaring the root in cmath


Fig 8. Squaring the root in scipy

SOME OBSERVATIONS

Matlab on squaring the root, gives precise results

cmath and scipy on squaring the root gives precise results for the complex part but odd results for the real part (2.2204460492503131e-16 for scipy and -2.2204460492503131e-16 for cmath).

Using (1j)**0.5 and sqrt(1j) in scipy yields different results in real parts (2.2204460492503131e-16 for (1j)**0.5 and -2.2204460492503131e-16 for sqrt(1j)).


For developing scipy there should be a sense of consistency with cmath and the resolution (digits in the answer) should be controlled at the discretion of the user( It really looks sleek in Matlab). Further it looks odd and conveys a sense of inconsistency if the complex part tallies completely with the expected result while the real part has an inconsistency.

Sunday, October 11, 2009

EULER'S GAMMA !

EULER'S GAMMA

Once again ! .... we meet Leonhard Euler ... a constant named after him. Euler-Mascheroni constant which runs as .... 0.57721 … called 'gamma' ,denoted by the Greek alphabet 'gamma' and is one of the important constants of mathematics.

From an abinitio, 'gamma' is defined as;

IN SCIPY

Trying it out in scipy
yields a very accurate gamma.....

Fig 1. gamma in scipy

Should gamma be build into scipy as pi and e ?

Fig 2. pi and e in scipy

IN MATLAB

Trying it in MATLAB


MATLAB recognises the integral as a special integral ! ...... with a vpa, the value is obtained.

Gamma and other mathematical constants should be build into Scipy and Scipy should be intelligent enough to identify these expressions and integrals.







REFERENCES
(1) Murray Spiegel

Thursday, October 1, 2009

AN EXOTIC INTEGRAL IN SCIPY

ADVENTURES IN SCIPY

Scipy is a module in python which allows for mathematical and scientific functions and tools. Trying to evaluate an exotic integral , using contour integration and complex analysis it can be shown that ;


Trying out the integral in scipy,
the function is introduced using lambda and the scipy.integrate.quad is used over 0 to infinity to obtain the results. The result comes up with a warning on infinite recursions and a recommendation to use a special-purpose integrator and the numeric value is 1.5708678849453777, which is with 0.0015587759422623915 of the correct value (~0.025% accurate).

Fig 1. The integral in scipy






REFERENCES
(1) Scipy
(2) Scipy mini anthology

Sunday, September 20, 2009

I AM FAMOUS !

GOING PLACES ..... AGAIN !

Now, I am famous ! ...... my game Pygame Toss has been published in famouswhy ..... I would guess it is the simplicity of the game than its achievements , that makes it 'famous' !


Fig 1. I am famous !

REFERENCES
(1)
Pygame Toss

Saturday, September 19, 2009

VISUAL PYTHON

VPYTHON - FIRST PROGRAM !

Trying out visual python (version 5.12) came rather easy.


My first program was mere 2 lines and I could do a fair deal with that.




Thursday, September 17, 2009

AN ODE TO 'pi'

3.141592653589793......

The ratio of circumference to diameter, may be no other number has intrigued and troubled mathematicians any more. The Bible puts pi as 3 ..... while many fanatics have spend the prime of their lives computing the 'little' that lies beyond 3 ... Ludolph van Ceulen from Leiden is sure worth a mention .... and he took the 35 digits of his computation to his tombstone after his death in 1610 ..... the exoticism of pi doesn't end here ..... and Feynman point is another interesting aspect of the unending digits of pi....

It must be appreciated that pi is not just another constant .....and is far from the likes of physical constants as G,h and c ..... and is also distinctly different from root 2, gamma, e and iota .... the physical constants are structured by the physical theory and are found to vary with time..... while e, iota, gamma etc can be said to be product of our chosen number system and the bias of our prevalent mathematical structure ....... while, pi is engraved in mother nature ..... it is ubiquitous and omnipresent .... pi enunciates why every circle mimics every other circle .... and so is true for every sphere..... Various physical theories as electrostatics, fluid-dynamics and gravitation have confirmed the presence of pi in their formulation , i.e: Stokes Equation, Gauss's Law, Kepler's law .....

The only other constant which may compete for similar prominence is phi , though phi is more subtle and not really as often visible as pi .... well..... I must stop with these rhetorics ... and get to business ...... 2 python recursions (1) Ramanujan's formulation (2) Wallis Product.

(1) Ramanujan's formulation

One of the most exotic and 'very fast converging' series for computing pi was given by Ramanujan...

A corresponding python program is ....

Fig 1. piramanujan.py

The program gives very accurate value of pi (3.14159265381), however the limit of recursion is reached in about 20 terms ...

(2) Wallis Product

An evaluation of pi in a 'product' form of an infinite series was given by English mathematician John Wallis.

The python program is ....

Fig 2. piwallis.py

The limit of recursion is at about 995 terms .

Though life can be made much easier ... away from these recursions by importing pi from math .....

Fig 2. pisimple.py

Wednesday, September 16, 2009

PYTHON RECURSIONS FOR 'pi' AND 'e'

FROM THE BASEL PROBLEM

The problem which put Euler on the path of immortality was the BASEL PROBLEM solved in 1735, 91 years since it was proposed . Succinctly put Euler's proof was ;

Using this result to obtain a recursion for pi in Python !

Pic 1. pi.py

At 500 terms the result is 3.13968..... not too far from the sinister 3.1415.....The recursions are good till about 995 terms (3.14063326091) after which one comes across .... RuntimeError: maximum recursion depth exceeded

Similar programs may be devised on more series as given by Euler, as .....


RECURSIONS FOR e

e the base of natural logarithm is yet another important constant in mathematics .... and once again related to Euler .....

A python program on this recursion is designed via the factorial function ....

Pic 2. e.py

The limit of recursion is around n = 170 which yields a very accurate value of e.

Thus in a nutshell .....

"Read Euler, read Euler, he is the master of us all."
-- Pierre-Simon Laplace

REFERENCES
(1) Recursive functions for phi and root two
(2) Other pi recursions in Python

STARRY THINGS !

ONCE UPON A STAR LIT SKY

I discuss 2 programs which try to mimic stars in the sky.

Program 1 (stars.py)
This program comes along with examples in the official pygame package , very nice introductory example. With every mouse click the supposed center of the screen moves to that point. Creates a good illusion and can be used as the start-up for further futuristic starry games and animations.

Video 1. Program 1

Program 2 (parallaxstars.py)
I came across this example from Chapter 8 of Will's book, this example creates an illusion that the stars close to the observer are moving faster than those in the background, unlike last example ... a mouse click does no good to the program ! once again a good program and can be used as background for futuristic games.


Video 2. Program 2

It is worth nothing that both these Pygame GUI's generate random stars and does not use any images to blit the screen.

Monday, September 7, 2009

CODEPAD

....AND THEN THERE WERE ALL ..... !

Other than Netbeans, Scite and Eclipse there is hardly another IDE which supports a wide array of languages

! VOILA ! ..... CODEPAD ..... AN ONLINE COMPILER / INTERPRETER FOR 12 LANGUAGES

Python works cool , but lacks the interpreter mode. Another online interpreter specifically for Python is Try Python which preserves the Python Chevron (>>>) and is in the interpreter mode. Zamplizer is another multi-lingual online platform.

Fig 1. The pad of codepad

Fig 2. Simple Ruby code in codepad

Sunday, September 6, 2009

FERMAT'S LAST THEOREM !

FERMAT'S LAST THEOREM !

Allen Downey in his book Think Python poses the reader with a problem !

Write a function named check_fermat that takes four parameters — a, b, c and n — and that
checks to see if Fermat’s theorem holds.

Fig 1. Fermat's last Theorem

A suitable program may be made as;

Fig 2. Python Code for check_fermat

However, it must be noted that this sort of treatment may never form some basis of an alternative proof of Fermat's last Theorem, which was the holy grail of mathematics till 1995 when Andrew Wiles established the proof for the 358 year old puzzle.


REFERENCES
(1) Simon Singh

PALINDROMES !

PALINDROMES !

A simple program to check for palindromes .

Fig 1. The code for palindrome check

Fig 2. Sample output