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

Advertisements

Accessing ZEMAX/ OpticStudio from Python using PyZDDE

PyZDDEIconCodeAtGithub

ZEMAX (also known as OpticStudio), a leading tool for design and analysis of optical systems, provides an in-built DDE server that enables access to ZEMAX’s features externally using any Windows application. The ability to access and control ZEMAX externally allows it to be used in many interesting and powerful ways, some of which has been documented in the “ZEMAX Extensions” chapter of the ZEMAX manual. An excellent toolbox called MZDDE, developed by Derek Griffith at CSIR, already exists for users of MATLAB. When I tried to look for an equivalent toolbox in Python, I couldn’t find one, so I decided to write one myself.

PyZDDE is a Python-based toolbox for communicating with ZEMAX using the DDE messaging protocol. It is similar to MZDDE and very much inspired by it. Currently PyZDDE is not as extensive as MZDDE as it is a work in progress. However, it has some distinguishing features — it can be used with regular Python scripts and also in an interactive environment such as an IPython shell, QtConsole or IPython notebook. The ability to interact with ZEMAX from an IPython notebook using PyZDDE can be a powerful tool for both teaching and documentation. Please visit the github repository for downloading and using the code. The github repository also contains an accompanying wiki page which describes briefly on how to use the PyZDDE toolbox.

Here I would like to briefly document the communication process between ZEMAX and Python using PyZDDE. But before that I would like to show you how simple it is to communicate with Zemax using PyZDDE using a “Hello world” example (it gets and prints the running Zemax version):

import pyzdde.zdde as pyz
link = pyz.createLink()
print("Hello Zemax version: ", link.zGetVersion())
link.close()

Opensource software for research – a 15 mins talk

All PhD students in our department (EE), in the Lyle school of engineering (SMU), are required to give a 15-min lunch-time talk in front of fellow PhD students and professors at least once every semester. This short-time talk/presentation, called the “Brown Bag Talk,” is meant to help PhD students in many ways, such as improving presentation skills, networking skills, etc. In addition, it encourages interaction among students, who are usually working in their own problems, to know about others’ work. However, the talk is not necessarily limited to one’s own PhD problem.

So, when it was my turn to lecture, I decided to talk about the use of open-source tools in research. In particular, I wanted to concentrate on two open-source tools that I regularly use in my everyday work — Python and Zotero. Zotero and its other free and open-source alternative, Mendeley, are very powerful and easy to use citation management system with lots of other useful capabilities. I also demonstrated few of the useful features of Zotero and some of the interesting language capabilities of Python during the presentation.

Here are the slides (images) from the presentation. The presentation contain animations,  so I have provided a link  to download the slides (powerpoint) from slideboom at the end of this post. Please feel free to use it as it may seem fit.

slide01 slide02

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