PyNAG is a Python wrapper for the NAG C library that is currently at release 0.16.
- Latest Version (Linux) – PyNAG 0.16 (32 and 64 bit Linux for use with Mark 8 of the NAG C library)
- Latest Version (Windows) – PyNAG 0.16 (32bit Windows for use with Mark 8 of the NAG C library)
- PyNAG 0.15 (32 and 64 bit Linux for use with Mark 8 of the NAG C library)
- PyNAG 0.12 (32 and 64 bit Linux for use with Mark 8 of the NAG C library)
Installation – Windows XP
I’ll assume that you have installed and licensed the CLDLL084ZL NAG C library implementation and that you are using 32bit Windows XP. There will never be Vista instructions but there will be Windows 7 specific instructions in the future.
- Download and install Python 2.6.5 (others may work but this is what I tested PyNAG with) for Windows from http://www.python.org/download/
- Add the following to your windows PATH Environment Variable C:\Python26 (You can get to the Environment variable dialogue by going to Start->Control Panel->System (assuming you are in classic view) and then clicking on the Advanced tab. Click on Environment variables, select ‘Path’ from the list of ‘System Variables’ and then click Edit. Note that each element in the Path is separated from the others by a semicolon.)
- Check that Python is on your path by opening a command window (Start->Run and then type cmd) and typing python followed by Return. This will start the Python interpreter. Type exit() to exit.
- Install Numpy (provides matrices,linear algebra etc) from http://sourceforge.net/projects/numpy/files/
- Install matplotlib (provides graphics)http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-0.99.1/
- Download and install PyNAG. Unzip it somewhere (e.g. C:\PyNAG-0.16) and open a command prompt. Execute the following commands
cd C:\PyNAG-0.16 python setup.py install
This will install PyNAG to the standard location C:\Python26\Lib\site-packages\PyNAG
A graphical example for the minimisation of the Rosenbrock function can be found at C:\Python26\Lib\site-packages\PyNAG\examples\graphical Double click on rosenbrock.py to run it. Then click anywhere on the plot to change the starting position.
If that works then you are good to go. The console examples will be in C:\Python26\Lib\site-packages\PyNAG\examples\examples_source but these will need to be run from the command line at the moment. As an example just do
cd C:\Python26\Lib\site-packages\PyNAG\examples\examples_source python e04fcce.py
Installation – Linux
Download the installation file, open up a terminal, ensure that the installation file is in your current working directory and type the following:
tar -xvzf ./PyNAG-0.16.tar.gz cd PyNAG-0.16/src
edit the following line in the file src/__init__.py so that it points to your installation of the library.
Move back into the root PyNAG directory:
run the following command with administrator privileges
python setup.py install
A simple example
There are 30 examples included with PyNAG that can be found in <PyNAG root directory>/src/examples/examples_source and the corresponding expected output (for a 32bit machine) for each one is in <PyNAG root directory>/src/examples/expected_output. One of these examples is s14aace.py (shown below) which uses the NAG library to evaluate Gamma(x) for x=1.25 using the NAG C-function s14aac. I have kept it very simple on purpose to allow you to see how to do the basics.
#!/usr/bin/env python #Example for s14aac using PyNAG #Evaluates Gamma(x) for x=1.25 from ctypes import * from PyNAG import s14aac,NAGERR_DEFAULT x = c_double(1.25) y= s14aac( x, NAGERR_DEFAULT) print "x= %s, result= %s" % (x.value,y)
If you are interested in a NAG function for which there is currently no example script then let me know and (time permitting) I will see what I can do for you.
- What versions of the NAG C library have been tested with PyNAG?
- cldll08084zl – 32bit Windows
- cllux08dgl -32bit Linux compiled with GNU gcc.
- cll6a08dgl – 64bit Linux compiled with GNU gcc
- I am using a version of the NAG C library that isn’t included in the list above. Can I still use PyNAG? Possibly. Let me know which version you are using and I’ll take a look.
- What version of Python does PyNAG need? Version 0.16 was developed with Python 2.6.5. It may work on other versions – it may not.
- How many NAG functions does PyNAG support? That depends on how you define ‘support’. PyNAG has basic definitions for 170 functions as of version 0.16 but may be missing Python definitions for some of the C-structures that are required by certain NAG routines. If you are unsure whether or not PyNAG supports a particular function then drop me a line and I’ll let you know (and maybe even whip up an example).
- Do you work for NAG? No – I am just a very satisfied customer that likes working WITH them.
- What is PyNAG’s license? It’s a ‘do whatever you like’ license. I’d appreciate that you acknowledge that I wrote it if you decide to rip it off but I won’t come after you if you don’t – life’s too short.
- What is the license for the NAG Library? It’s commercial. Check out their website if you want to buy it.
- Will you release a version for Mac OS X or <insert implementation> here? Possibly, if you ask for it. I reserve the right not to.
- Will you add support for <insert function here>? Possibly, if you ask for it. I reserve the right not to.
- Are you interested in collaboration? Yes, drop me a message and we’ll talk.
- Why is development of PyNAG so slow? I do as much as I need to in order to ‘scratch my own itch’ and no more. PyNAG is a project in my spare time and I don’t have much spare time.
- I have used PyNAG as part of a research project that will be written up as a paper. How should I cite it? I am flexible but suggest something like ‘PyNAG – A Python Wrapper for the NAG C library v0.16′ by M.P Croucher.
- Is PyNAG supported by NAG? I understand that if you have a supported licence from NAG and need helping calling it from Python then they will try to help you. NAG answered many of my questions during PyNAG’s development and a couple of people there have played with it. It is not, however, a fully supported interface.
- Why Python and NAG? Someone I know wanted it so I built it. I had fun building it so I built some more. A couple of people said it was useful but could I please add some stuff…so I did. I’m learning a lot, NAG seem to like it and it’s turning out to be useful. So why not?
- How can I contact you? Email me, write a comment here (anonymous comments may be ignored) or send me a message on twitter.
A while ago now, I was asked by a postgraduate student at the University of Manchester if I had any idea how to call a function from the NAG library (In either Fortran or C) from Python. I didn’t! So, I rolled up my sleeves, called in some favours at NAG and sorted out a solution for him.
Since I like programming in Python and I also enjoy using the NAG library, I decided to write up my investigations as a series of blog articles right here on Walking Randomly. Eventually I got fed up with writing a lot of standard, boilerplate code at the beginning of my examples and so I wrote a simple Python module that took care of the basics. Thus, PyNAG was born.
The blog articles that led to its creation along with several that were written afterwards can be found in the links below.
- Making simple NAG C functions available to Python
- Plotting the Cosine Integral using NAG and matplotlib
- Callback functions (The example code given is for finding the zero’s of an equation)
- Structures (The example code given is for Numerical Integration)
- Callback functions with communication (The example code solves a simple 1d minimization problem)
- PyNAG 0.12 – The first public release of PyNAG
- PyNAG 0.15 – The second public release of PyNAG
- Interfacing the NAG C library and Python on Windows – Includes PyNAG 0.16 release
- PyNAG is free and always will be. You’ll obviously need to buy a copy of the NAG C library though.
- It includes a definition of one of the largest C-Structures I have ever seen – Nag_E04_Opt – which has over 130 members. This took me quite a while to get working and now you don’t need to go through the same pain I did. All the C structures mentioned in these tutorials have been included to save your typing fingers.
- PyNAG has been tested on 32bit and 64bit flavours of Linux and 32bit Windows and seems to work fine.
- It is relatively easy to add definitions for NAG functions that are not already currently included. In the best case scenario all you need to do is add two lines of code per NAG function. (They’re not all this easy though!)
- It comes with a set of fully functional examples (32 so far with more on the way) along with expected output. This output has been checked against the original C versions of the code to ensure that the results are correct
- It only works on Linux and Windows at the moment. Depending on interest levels (mine and yours) I may get it working on Mac OS X.
- It only includes some of the boilerplate needed to interface with 170 NAG functions at the time of writing. Of these, only 30 have been tested but all test programs have been included in the distribution.
- It doesn’t include all of the structures you might need to use throughout the entire library at the moment. I add structure definitions as I find I need them so many are missing.
- It doesn’t include any of the enumerations that the NAG C library uses. I plan to include some of the most commonly used ones but will simply have to define them as variables since ctypes doesn’t support enumerations as far as I know.
- This is my first ever Python package so things may be a bit rough around the edges. I welcome advice from more experienced Pythonistas concerning things like package deployment.
- The Python version of the NagError structure has a member called pprint rather than the original print. This is because print is a reserved keyword in Python so I couldn’t use it.
- It has only been tested on Python versions 2.5.x and 2.6.x. I have no idea if it will work on other versions at the moment.
- Documentation is rather sparse at the moment. I haven’t had time to do a better job.
- All PyNAG really does is save you from having to type in a load of boilerplate. It doesn’t properly ‘Pythonise’ the NAG C library and so it is painfully obvious that your code is talking to a C library and not a Python library. Ideally I would like to change this and make the user interface a lot more user-friendly. Any input on how best to go about this would be gratefully received.