Ask Your Question

Revision history [back]

You can use C libraries from Sage via Cython. Cython is a Python-like language that makes it easy to write Python programs that manipulate pure C data types allowing for faster computations and the like. In you're unfamiliar with Cython you should take a look at these tutorials for more information.

I'm not familiar with ATLAS or GOTOBLAS but I can provide an example of how to link to some BLAS routines. Suppose I wanted to add two vectors using BLAS, which is included with Sage. To do $y \leftarrow \alpha x + y$ where $x$ and $y$ are double vectors and $\alpha$ is a double scalar, I start by extern-ing the appropriate function, cblas_daxpy() into my Cython code. (i.e. #import-ing) In the Sage notebook, I begin a cell with the following:

%cython

cdef extern from "gsl/gsl_cblas.h":
    void cblas_daxpy    (int N, double alpha, double *x, int incX, double *y, int incY)

Then, to perform this addition I add the following (to the same cell)

cdef double x[3]
cdef double y[3]
cdef double alpha = -1

x[0] = 1.0; x[1] = 1.0; x[2] = 1.0
y[0] = 1.0; y[1] = 2.0; y[2] = 3.0

print "x =",x[0],x[1],x[2]
print "y =",y[0],y[1],y[2]
print "a =",alpha

cblas_daxpy(3,alpha,x,1,y,1)

print "alpha x + y =",y[0],y[1],y[2]

The output is

x = 1.0 1.0 1.0
y = 1.0 2.0 3.0
a = -1.0
alpha x + y = 0.0 1.0 2.0

You might need to compile GOTOBLAS in the Sage shell (type "sage -sh") in the terminal but it might also work if you have a system-wide install.

I hope this is a decent enough introduction to using C libraries in Sage. You can fine more information at the Cython website and documentation at http://www.cython.org