473,382 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

need an example of Python numarray to C++ and back again, Boost / SWIG?

PL
I want to pass a 2D array from Python to C++, manipulate it in C++ (for
example, add 1 to each element) and pass it back to Python.

With these building blocks I will be able to figure out all the rest of
what I need to do for my project. I am very familiar with Python, but
less so with C++ and Boost or SWIG.

Does anyone have an example with all steps that I can follow? More
specifically I am looking for the C++ code, ".i" file for SWIG and/or
the analagous setup files that Boost would need to do this.

Thanks in advance for any help you can provide,

Nov 9 '05 #1
3 3373
PL wrote:
I want to pass a 2D array from Python to C++, manipulate it in C++ (for
example, add 1 to each element) and pass it back to Python.

With these building blocks I will be able to figure out all the rest of
what I need to do for my project. I am very familiar with Python, but
less so with C++ and Boost or SWIG.

Does anyone have an example with all steps that I can follow? More
specifically I am looking for the C++ code, ".i" file for SWIG and/or
the analagous setup files that Boost would need to do this.


You may want to look into weave.inline or weave.blitz, from scipy. Typemaps for
conversion to blitz++ were recently posted on the scipy list:

http://aspn.activestate.com/ASPN/Mai...ussion/2883831

In particular look at Stefan's post.

For info on weave, here you can find some old slides and example code:

http://amath.colorado.edu/faculty/fperez/python/

Cheers,

f

Nov 10 '05 #2
PL
I looked at Stefan's post - but he remarks that "Unfortunately, Blitz
jealously guards its data (restricted pointers), so that it is not so
easy to do the conversion in the other direction. If anyone knows an
answer to this problem, I'd be glad to hear it"

I've previously looked at Phillip Austin's 'num_util' and Paulo J. S.
Silva's 'COIN' example, but even from those two, I can't figure out a
way to do: Python 2D numarray --> C++ (process array) --> Python 2D
numarray.

I forgot about "weave" - I had looked there before and will revisit it
to see if it will work. But I was intending to do this with a compiled
extension. I wish there was a simple example of this in either the
SWIG or Boost docs or a faq/howto posted somewhere . . .

-Paul
Fernando Perez wrote:
PL wrote:
I want to pass a 2D array from Python to C++, manipulate it in C++ (for
example, add 1 to each element) and pass it back to Python.

With these building blocks I will be able to figure out all the rest of
what I need to do for my project. I am very familiar with Python, but
less so with C++ and Boost or SWIG.

Does anyone have an example with all steps that I can follow? More
specifically I am looking for the C++ code, ".i" file for SWIG and/or
the analagous setup files that Boost would need to do this.


You may want to look into weave.inline or weave.blitz, from scipy. Typemaps for
conversion to blitz++ were recently posted on the scipy list:

http://aspn.activestate.com/ASPN/Mai...ussion/2883831

In particular look at Stefan's post.

For info on weave, here you can find some old slides and example code:

http://amath.colorado.edu/faculty/fperez/python/

Cheers,

f


Nov 10 '05 #3
PL wrote:
I looked at Stefan's post - but he remarks that "Unfortunately, Blitz
jealously guards its data (restricted pointers), so that it is not so
easy to do the conversion in the other direction. If anyone knows an
answer to this problem, I'd be glad to hear it"

I've previously looked at Phillip Austin's 'num_util' and Paulo J. S.
Silva's 'COIN' example, but even from those two, I can't figure out a
way to do: Python 2D numarray --> C++ (process array) --> Python 2D
numarray.


I may be missing something, but what I've done in the past for this is have the
C++ code simply reuse the Numeric data pointer. This way, when I exit the C++
extension (I've used blitz++ for the job), the array as seen from the python
side has been 'magically' modified. Obviously this means that I can't allocate
new arrays in C++ which can be transfered over to python without paying the
price of a copy, but in my cases that hasn't been a problem: I do all
'allocations' in python (via arr=Numeric.empty(...)) and let the blitz code
fill in the arrays.

This has the advantage that the blitz array creation is extremely cheap, as only
the shape tuple needs to be copied (not the data region). The following little
snippet is pretty much all that's needed if the above description happens to
work for you. This code is mostly taken from weave's internals:

// -*- C++ -*-
#ifndef PY_TO_BLITZ_H
#define PY_TO_BLITZ_H

#include "Python.h"
#include "Numeric/arrayobject.h"
#include "blitz/array.h"

using namespace blitz;

// Convert a Numpy array to a blitz one, using the original's data (no copy)
template<class T, int N>
static Array<T,N> py_to_blitz(const PyArrayObject* arr_obj)
{
const int T_size = sizeof(T);
TinyVector<int,N> shape(0);
TinyVector<int,N> strides(0);
int *arr_dimensions = arr_obj->dimensions;
int *arr_strides = arr_obj->strides;

for (int i=0;i<N;++i) {
shape[i] = arr_dimensions[i];
strides[i] = arr_strides[i]/T_size;
}
return Array<T,N>((T*) arr_obj->data,shape,strides,neverDeleteData);
}
#endif // PY_TO_BLITZ_H
Cheers,

f

Nov 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ray Schumacher | last post by:
Hi all, I'm trying to port Hongjie Xie's IDL/ENVI Implementation of the FFT Based Algorithm for Automatic Image Registration ftp://ftp.iamg.org/VOL29/v29-08-10.zip...
37
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any...
2
by: Carl Gilbert | last post by:
Hi I am trying to serialize an assembly into a byte array. I am using a BinaryFormatter to serialize the assembly into a stream. Once in the stream, would then save this to the database in a...
8
by: Kenny ODell | last post by:
I do not know how to convert from a byte array to a float, and back again. I read data from a serial port into a byte (entire command structure which I parse). I am able to sift the data and...
0
by: KathyB | last post by:
HI, I've posted this type of question before so please don't be annoyed, but I still need example(s) of how to build dynamic controls and post them back to the xmldocument on the server. I...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
4
by: movieknight | last post by:
Hi, I have an application that stores raw .wav files (and also jpgs/bitmaps) within strings, and I need to sometimes convert these strings to byte arrays, and sometimes go from byte arrays back...
14
by: mistral | last post by:
Need compile python code, source is in html and starts with parameters: #!/bin/sh - "exec" "python" "-O" "$0" "$@" I have installed ActivePython for windows.
0
by: phlip | last post by:
Nick Keighley wrote: CC'd to the correct newsgroup. Yes, the destructor of the shared pointer will delete the object. Then its former address will convert to a reference. At some point -...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.