extending python with array functions
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Mon Feb 4 17:56:02 EST 2008
En Mon, 04 Feb 2008 18:44:42 -0200, Janwillem <jwevdijk at xs4all.nl>
escribió:
> 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[i]:=a[i]+b[i];
> 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.
--
Gabriel Genellina
More information about the Python-list
mailing list