Re: [f2py] problems with array

Pearu Peterson <[email protected]> Wed, 11 Aug 2010 21:29:57 +0300
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
On Wed, Aug 11, 2010 at 8:56 PM, Al Niessner <[email protected]> wrote:
>
> I have a function that returns an array but it crashes when I call the
> function.
>
> f2py -c kinds.f90 mathsub.f90 -m mathsub
>
> n [1]: import mathsub
>
> In [2]: i = mathsub.math_mod.eye(3)
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call
> last)
>
> /home/niessner/Scratch/Example/<ipython console> in <module>()
>
> ValueError: failed to create intent(cache|hide)|optional array-- must
> have defined dimensions but got (-1,-1,)

Currently f2py does not support wrapping functions that return
arrays. There is simple workaround to this problem: write a wrapper
subroutine that calls such functions. For example, for your case
add the following subroutine to math_mod module

        subroutine weye(r, n)
          real(8), dimension(n,n) :: r
          integer n
          !f2py intent(out) r
          r = eye(n)
        end subroutine weye

and use weye to call eye function from python:

  f2py -c kinds.f90 mathsub.f90 -m mathsub skip: eye

>>> import mathsub
>>> mathsub.math_mod.weye(3)

array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

Pearu