[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

extending python with array functions

Janwillem

2/4/2008 8:45:00 PM

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.

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

Gabriel Genellina

2/4/2008 10:56:00 PM

0

En Mon, 04 Feb 2008 18:44:42 -0200, Janwillem <jwevdijk@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....
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-... 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

Marc 'BlackJack' Rintsch

2/5/2008 7:29:00 AM

0

On Mon, 04 Feb 2008 20:56:02 -0200, Gabriel Genellina wrote:

> - the array module http://docs.python.org/lib/module-... 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

Gabriel Genellina

2/5/2008 5:07:00 PM

0

En Tue, 05 Feb 2008 05:28:33 -0200, Marc 'BlackJack' Rintsch
<bj_666@gmx.net> escribi�:

> On Mon, 04 Feb 2008 20:56:02 -0200, Gabriel Genellina wrote:
>
>> - the array module http://docs.python.org/lib/module-... 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.

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

--
Gabriel Genellina

Janwillem

2/5/2008 7:57:00 PM

0

Gabriel Genellina wrote:
> En Tue, 05 Feb 2008 05:28:33 -0200, Marc 'BlackJack' Rintsch
> <bj_666@gmx.net> escribi�:
>
>> On Mon, 04 Feb 2008 20:56:02 -0200, Gabriel Genellina wrote:
>>
>>> - the array module http://docs.python.org/lib/module-... 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.
>
> 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