Using Numerical Libraries from IronPython
Today, we have the pleasure of announcing the availability of a new IronPython interface library and over 50 samples of using our Extreme Optimization Numerical Libraries for .NET from IronPython.
Python is used more and more for numerical computing. It has always been possible to call into it from IronPython. However, IDE support was minimal and some of the more convenient features of Python, like slicing arrays, were not available.
In January, Microsoft announced the availability of Python Tools for Visual Studio. This is a big step forward in IDE’s for Python development on Windows.
Now, with our new IronPython interface library you can take advantage of the following integration features:
- Create vectors and matrices from Python lists.
- Setting and getting slices of vectors and matrices.
- Integrating Python’s complex number type with our DoubleComplex type.
- Use Python-style format specifiers.
If you want to dive right in, the download is here: IronPython Tools for Extreme Optimization Numerical Libraries for .NET.
Prerequisites
In order to use the IronPython interface library, you need the following:
- Microsoft .NET Framework version 4.0 or later.
- IronPython version 2.7 for .NET 4.0.
- Extreme Optimization Numerical Libraries for .NET version 4.2 for .NET 4.0
- Python Tools for Visual Studio. (Optional)
Installation
To install the IronPython interface library, follow these steps:
- Make sure all the prerequisites are installed.
- Download the zip archive containing the IronPython interface library for the Extreme Optimization Numerical Libraries for .NET.
- Copy the
Extreme.Numerics.IronPython27.dll
file from the zip archive to theDLLs
folder in the IronPython installation folder. - Copy the
IronPython
folder from the zip archive to theQuickStart
folder in the Extreme Optimization Numerical Libraries for .NET installation folder.
Getting Started
To use the interface library, import the numerics module:
IronPython 2.7.1 (2.7.0.40) on .NET 4.0.30319.239 Type “help”, “copyright”, “credits” or “license” for more information.
import numerics
The main types reside in the Extreme.Mathematics
namespace, so it’s a good idea to import everything from it:
>>> from Extreme.Mathematics import *
You can then start using mathematical objects like vectors, and manipulate them:
>>> a = Vector([1,2,3,4,5])
b = Vector.Create(5, lambda i: sqrt(i+1)) b Vector([1,1.4142135623730952,1.7320508075688772,2,2.23606797749979]) a+b Vector([2,3.4142135623730949,4.7320508075688767,6,7.23606797749979]) from math import * a.Apply(sin) Vector([0.8414709848078965,0.90929742682568171,0.14112000805986721,-0.7568024953079282,-0.95892427466313845])
You can use Python slicing syntax, including counting from the end:
>>> a[0] 1.0
a[-2] 4.0 a[-2:] Vector([4,5]) a[1:4] Vector([2,3,4])
Slicing works on matrices as well:
>>> H = Matrix.Create(5,5, lambda i,j: 1.0 / (1+i+j))
H Matrix([[1,0.5,0.33333333333333331,0.25,0.2] [0.5,0.33333333333333331,0.25,0.2,0.16666666666666666] [0.33333333333333331,0.25,0.2,0.16666666666666666,0.14285714285714285] [0.25,0.2,0.16666666666666666,0.14285714285714285,0.125] [0.2,0.16666666666666666,0.14285714285714285,0.125,0.11111111111111111]]) H[1,1] 0.33333333333333331 H[1,:] Vector([0.5,0.33333333333333331,0.25,0.2,0.16666666666666666]) H[:,1] Vector([0.5,0.33333333333333331,0.25,0.2,0.16666666666666666]) H[0:5:2,0:5:2] Matrix([[1,0.33333333333333331,0.2] [0.33333333333333331,0.2,0.14285714285714285] [0.2,0.14285714285714285,0.11111111111111111]])
Many linear algebra operations are supported, from the simple to the more complex:
>>> H*a Vector([5,3.55,2.8142857142857141,2.3464285714285715,2.0174603174603174])
H.Solve(a) Vector([124.99999999997976,-2880.0000000002506,14490.000000002709,-24640.000000005733,13230.000000003382]) svd = H.GetSingularValueDecomposition() svd.SingularValues Vector([1.5670506910982314, 0.20853421861101323, 0.011407491623419797, 0.00030589804015118552, 3.2879287721734089E-06])
Sample programs
We’ve converted over 60 of our QuickStart samples to Python scripts. The samples folder contains a solution that contains all the sample files. To run an individual sample, find it in Solution Explorer and choose “Set As Startup File” from the context menu. You can then run it in the interactive window by pressing Shift+Alt+F5.