Plotting algebraic surfaces using Mayavi

00_postFigureWho says math is not beautiful? Anyone who doubts the beauty in math must check out algebraic surfaces.

An implicit functions has the form:

F(x,y,z, ...)=c

where, c is an arbitrary constant.

For example, the implicit equation of the unit circle is x^2 + y^2 = 1  and the equation x^2 + y^2 + z^2 = 1  describes a sphere of radius 1.

An algebraic surface is described by an implicit function F(x,y,z) = c . It may be rendered using Mayavi by evaluating the expression F(x,y,z)-c=0  on a regular (specified) grid, and then generate isosurface of contour value equal to 0.

Here are some examples of algebraic surfaces plotted using Mayavi:

I wrote a quick function called implicit_plot for plotting algebraic surfaces using Mayavi. The most important argument to the function is of course the expression  F(x,y,z)-c=0 as a string. It is probably not the best function, but the idea is to show how to plot implicit surfaces. The code snippet is included at the end of this post. Suggestions for improvement are always welcome.

Continue reading

Advertisement

eigshow in Python

Few days back I was playing with the eigshow demo in Matlab. It is a pretty useful demonstration of eigenvalue and singular value problems for 2 by 2 matrices. A good description of the eigshow demo in Matlab is here. I was a little surprised not to find an “eigshow” implementation in my favorite computing language — Python. So I thought it might be a good idea to write my own, and here is the result.

The figure below shows the basic structure of the program which is somewhat similar to Matlab’s eigshow. There are some differences too, such as it displays the eigenvalues and eigenvectors in the eigen mode, and the singular values and singular vectors in the SVD mode. The program also displays the rank, trace, and determinant along with the matrix. It also gives a visible warning if the eigen/singular values and/or vectors are complex (as it can only plots the real values and vectors).

eigshow1

eigshow2

eigshow3

(Please continue reading to know about basic function and code)
Continue reading

Python for Scientific Computing – a collection of resources

This post is about the Python ecosystem for scientific/ technical computing. Generally, when someone says that he/she is using Python for technical computing, we must interpret it as the “Python ecosystem for scientific/ technical computing”. Vanilla Python, which is a general purpose, versatile language was not designed for and is not suitable for technical computing (such as linear algebra, symbolic computing, vectorized operations etc.) in itself. However, the language provided just the right set of tools, and a framework within which scientists and engineers could easily implement their ideas. Python was quickly embraced by the general scientific community which built several packages using Python that are quite suitable for technical computing. Currently there hundreds of different Python-based libraries. This post is meant to be a basic introduction to a core set of scientific packages in the Python ecosystem, for someone new to Python (though I highly doubt I have any such audiences).

Python is a very powerful language for doing all sorts of things, and at all stages of research — from general computing, system programming, design of experiments, building device interfaces, connecting and controlling multiple hardware/software tools, to heavy scientific number crunching, data analysis and visualization. Python is an interpreted, general purpose, object-oriented, high-level scripting language, which supports multiple programming paradigms — procedural, object-oriented, and functional programming. The core design philosophy of Python are simplicity, code readability, and expressivity.

Python is easy to learn. It is intuitive and simple, yet it is powerful, beautiful and expressive.

What makes Python particularly attractive for scientist and engineers is that it is open-source, highly portable, intuitive to use, and features dynamic and strong typing. It provides both interactive and script based programming environments like MATLAB. It also features automatic memory management and garbage collection enabling scientists and engineers (with or without strong computer science background) to direct their time and energy on their algorithm and let the interpreter handle the low-level stuffs. All the above qualities in addition to its large scientific community-support allows greater opportunity for code-sharing, open and collaborative research, and thus it supports the philosophy of reproducible research.

Python itself is a full featured programming language with a large set of tools in the standard library (sure you have heard “batteries included”!!) What is even more attractive is that there is a whole technical computing ecosystem around Python built by the different scientific communities. Most of the tools are built on top of Numpy, which itself is built on top of Python. Numpy extends Python with capabilities such as vectorization, homogeneous arrays, multi-dimensional arrays, fast element-wise operations, broadcasting and universal functions that are essential for scientific/technical computing. The figure below shows a basic landscape of the scientific python ecosystem. Please note that it is not meant to be a complete, rather a very basic reference to the most common tools around Python for scientific computing.

Scientific Python Ecosystem (simplified)

The ease and interactivity of the language coupled with the availability of good community support and specialized scientific libraries enables a newcomer to quickly learn and do meaningful work using Python. The interested reader can read more about the arguments about advantages of Python and how it compares to other languages here.

When I started learning Python I collected a number of resources related to the use of Python for scientific and numerical computation. I think these resources may help someone who is just getting started using Python for Scientific computing. For this reason I have decided to share a list of those resources here in this blog post.

Continue reading