Monthly Archives: August 2011

SciPy for .NET & IronPython

This week Enthought and Microsoft are announcing the availability of NumPy and SciPy for IronPython and the .NET Framework, released in conjunction with the release of Microsoft’s Python Tool for Visual Studio release. These packages implement a fast and flexible multi-dimensional array package (NumPy) and a large collection of scientific and numerical algorithms built on top of the array (SciPy) andare two fundamental building blocking for technical computing in Python. CPython users are well familiar with both but up until now they have not been available on other Python environments. This is because both packages include many heavily optimized native-code (typically C or FORTRAN) implementation of the algorithms, thus requiringcustom wrappers to integrate them into each Python environment.

The result of the project isIronPython ports of NumPy and SciPy, which are full .NET ports and include custom C#/C interfaces to a common native C core. This means that the full functionality is available not only to IronPython but to all .NET languages such as C# or F# by directly accessing the C# interface objects.

Completing the port required considerably more work than just writing new interfaces IronPython. To start with large parts of the NumPy C code were written specifically for CPython and made free use of the CPython APIs. To support additional Python environments the codebase had to be refactored into a Python-independent core library plus a CPython-specific interface layer. A big advantage to this architectural separation is that the core NumPy library can now be used from applications written in C or C++ much more easily and ports to other Python environments will require less work.

The second supporting project was an extension of Cython to support IronPython. Cython, not to be confused with CPython, is a tool that allows C extensions to be written for Python using a language based on Python with extensions for interacting with native code libraries. Up until now Cython has only supported the CPython environment. We have extended Cython to be able to generate C++/CLI code interfaces for .NET, and thus IronPython. Using a single Cython input file it is now possible to target both CPython and IronPython environments.

The main driver for the Cython project was the nature of the SciPy library. SciPy consists of dozens of different packages, many of which have a hand-written C interface for CPython. One option was to write a matching C# interface for each of these to support IronPython. While this would have taken less time, the result would have been a significant duplication of code and more maintenance work in the future. Instead we chose to port Cython to .NET and then rewrite each C module in Cython so one a single source file is needed.

The first release of SciPy and NumPy for .NET are available now as binary distributions from SciPy.org or directly from Enthought. All of the code for these and the supporting projects are open source and available at the links below.