extending python with array functions

J

Janwillem

I want to make numerical functions that can be called from python.
I am programming in pascal the last few decades so I had a look at
"python for delphi" (P4D). The demo09 gives as example add(a,b) using
integers and pyarg_parsetuple. That works!

However, I cannot figure out what to do when a, b and the result are
arrays (or matrices) of float (for i:=0 to high(a) do c:=a+b;
and then return c to python). Although from the ALGOL60 school and
always tried to keep far from pointers, I might also understand advise in C.

Please get me started e.g. by giving a simple example.
Many thanks,
Janwillem
 
G

Gabriel Genellina

I want to make numerical functions that can be called from python.
I am programming in pascal the last few decades so I had a look at
"python for delphi" (P4D). The demo09 gives as example add(a,b) using
integers and pyarg_parsetuple. That works!

However, I cannot figure out what to do when a, b and the result are
arrays (or matrices) of float (for i:=0 to high(a) do c:=a+b;
and then return c to python). Although from the ALGOL60 school and
always tried to keep far from pointers, I might also understand advise
in C.


First: do you know NumPy? http://numpy.scipy.org/
NumPy provides powerful functions to work with numeric arrays. Maybe using
this library you don't even have to write an extension and you can keep
all your code in Python with reasonable speed. It may be the best option
depending on your needs.

In case you still have to write an extension:

- the "usual" container for Python objects is a "list"; it's a generic
container and can hold any kind of objects, but there are other
alternatives too. To access its elements from Delphi, use the
PySequence_XXX family of functions (or PyList_XXX if you know it is an
actual list).

- the array module http://docs.python.org/lib/module-array.html provides
homogeneuos arrays that may be more efficient for your application. arrays
don't have a special API, you have to import the module and use its
functions the same as one would do in pure Python.
 
M

Marc 'BlackJack' Rintsch

- the array module http://docs.python.org/lib/module-array.html provides
homogeneuos arrays that may be more efficient for your application. arrays
don't have a special API, you have to import the module and use its
functions the same as one would do in pure Python.

There's one special thing about it: the `buffer_info()` method returns a
tuple with the memory address and length (in items) of the current
underlying buffer. Pretty useless information in Python but handy in
extensions that can directly access the "raw" memory.

To the OP: Since Python 2.5 the `ctypes` module is another way to
interface with "native" code in dynamic libraries from the standard
library.

Ciao,
Marc 'BlackJack' Rintsch
 
G

Gabriel Genellina

En Tue, 05 Feb 2008 05:28:33 -0200, Marc 'BlackJack' Rintsch
There's one special thing about it: the `buffer_info()` method returns a
tuple with the memory address and length (in items) of the current
underlying buffer. Pretty useless information in Python but handy in
extensions that can directly access the "raw" memory.

Good to know! I didn't notice it the (only) time I had to use arrays from
C code.
 
J

Janwillem

Gabriel said:
En Tue, 05 Feb 2008 05:28:33 -0200, Marc 'BlackJack' Rintsch


Good to know! I didn't notice it the (only) time I had to use arrays
from C code.
Thanks for the advice. I think I will read the cstype stuff because it
might mean that my calculation intensive functions (non-linear systems
and Monte Carlo stuff) can be kept unchanged ans stay compatible with
e.g. the Excel interfaces I have for them. My first attempt with
sum(i,j) was successful but sum(x,y) (FORTRAN typecast thinking) needs
apparently understanding ctypes.
Janwillem
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top