Scientific computing with PYTHON on Windows (1/2)
Why Python?
In the scientific community, python is becoming the de facto scripting language.
The obvious reasons are:
1. It is accessible, download and install in minutes.
2. The syntax is simple, just about anyone could program, read it.
3. It is dynamic, thus making it easier to test while you program. This is the so called, test-driven development.
4. In addition to its comprehensive set of standard libraries, there are amazing amount of packages available. The Python eco-system is flourishing, as more packages are wrapped using swig and other tools.
5. Cross-platform support is yet another huge advantage of python; most programs will run on mac, Linux and windows with almost no additional work.
6. Extreme productivity. I have seen network, fully functional GUI applications written in less than a few hundred lines.
I am sure there are other benefits of python that I have not mentioned above. It is difficult to find another language that can beat Python on all of these points.
How it works.
Like any scripting (dynamic) programming languages. Python itself is slow, you would not want to write a simulation that requires hours of cpu time in pure python. However, there's a cure. You can wrap your existing C/C++ code using SWIG, by generating a python interface for your C code. SWIG is pretty strait forward to use, it pretty much amounts to defining an interface using SWIG's interface language, generate a loadable c module, load it into python and call the function.
Example:
Say, you have a C program example.c
#include<stdio.h>
void my_C_function(){
printf(" Calling the C function ");
}
Step 1. Write your interface (example.i)
%module example
%{
extern void my_C_function();
}
Step2. Generate the Python interface
swig -python example.i
You will find example_wrap.c in your directory.
Step3. compile both the wrapper and the original c program.
gcc -c example.c example_wrap.c -I/yourpythonIncludeDIR
Step 4. dynamically link your example.
ld -shared example.o example_wrap.o -o _example.so
Step 5. run Python
>>> import example
>>> example.my_C_function()
'calling the C function'
>>>
Where to look?
Using swig, package developers are able to bring tremendous amount of value to the Python community.
Some of the most famous python packages for scientific computing includes:
Scipy, numPY: solvers, lapack, blas, optimization, and much more.
PyTables: built on top of hdf5, it is basically an efficient database store package for HUGE amounts of data.
There are simply too many to mention, a good place to look is:
https://www.scipy.org/Topical_Software
Python runs well on windows
Some of the windows developer may not have used python in the past. The good news is that MOST of these packages are NOT only available on windows, but they run better on Windows. One of the most frustrating things about running Python and utilizing its full power and packages is installation. I have spent hours if not days trying to get packages installed on Linux. The issue is that you will need to not only worry about versions of Linux, but also the various dependencies, and their versions. Sometimes RPMs or .DEB packages are not available, then you are on your own... building a tool Chain from the latest source is simply not accessible to most end users. Of course, this is getting better on Linux, but for most of us mere mortals, we just need a double click installation process.
Luckily, the windows support on python even surprises me. python.org provides an MSI installation python2.5.2, and various packages can be loaded using the easy_install script. In the next blog, I will demonstrate how i was able to get enThought's scientific 3d scientific Visualization tool working in minutes on Windows. Though it may not be fair to compare here, but the last time I got it entirely working on Linux took me nearly two days to build all the packages.
Python and .NET
IronPython is a version of python that targets the .NET framework, that means you can script .NET code in PYTHON! That means that you can script WCF, WPF, Silverlight, and even XNA. You would be able to find out more at https://www.codeplex.com/IronPython Since mono is the .net equivalent on Linux, so are these extra functionality.
Conclusion
Python is the best cross-platform, scripting language for scientific computing. It runs surprisingly well on windows, if not better than other platforms. As a python user, I would like to see more windows developers start utilizing the benefits of python, especially when it comes to writing tests. In the next blog, I will show you how to start doing scientific computing, serious 3d Visualization with Python in 30 minutes or less on the windows platform.