[Numpy-discussion] Numpy and C++ integration...

Kent-Andre Mardal kent-and@simula...
Tue Feb 5 12:16:02 CST 2008

Vince Fulco <vfulco1 <at> gmail.com> writes:

> Dear Numpy Experts-  I find myself working with Numpy arrays and
> wanting to access *simple* C++ functions for time series returning the
> results to Numpy.  As I am a relatively new user of Python/Numpy, the
> number of paths to use in incorporating C++ code into one's scripts is
> daunting.  I've attempted the Weave app but can not get past the
> examples.  I've also looked at all the other choices out there such as
> Boost, SIP, PyInline, etc.  Any trailheads for the simplest approach
> (assuming a very minimal understanding of C++) would be much
> appreciated.  At this point, I can't release the code however for
> review.  Thank you.

We have created a small Python module Instant (www.fenics.org/instant)  on top
of SWIG, which makes integration of C/C++ and NumPy arrays easy in some cases. 

Its use: 

import numpy
from instant import inline_with_numpy

c_code = """
double sum (int n1, double* array1){
  double tmp = 0.0; 
  for (int i=0; i<n1; i++) {  
      tmp += array1[i]; 
  return tmp; 

sum_func = inline_with_numeric(c_code, arrays = [['n1', 'array1']])

a = numpy.arange(10000000); a = numpy.sin(a)

sum1 = sum_func(a)


More information about the Numpy-discussion mailing list