0

Say I have a module to be compiled via f2py:

test.f90

module test
implicit none

integer, parameter :: q = 2
real(8), parameter, dimension(-q:q) :: vec = (/ 1, 2, 3, 4, 5 /)

contains

subroutine writevec()
    write(*,*) vec
end subroutine

end module

Upon running f2py -c -m test test.f90, I get the error

/tmp/tmp6X6gsD/src.linux-x86_64-2.7/testmodule.c:176:17: error: expected expression before ‘)’ token
{"vec",1,{{-(-)+1}},NPY_DOUBLE},

On the other hand, if I declare vec with dimension(2*q+1), it works. Sort of. When I import into python:

>>> from test import test
>>> test.writevec()
>>>    1.0000000000000000        2.0000000000000000        3.0000000000000000        4.0000000000000000        5.0000000000000000     

>>> test.vec
>>> array([ 1.,  2.]) # ???

What is going on here??

Matt Hancock
  • 3,870
  • 4
  • 30
  • 44

1 Answers1

0

You can create a signature file in order to get the array dimensions right. This creates the signature file 'sign.pyf' for the python module 'mymod':

f2py -m mymod -h sign.pyf test.f90

Then, use this to compile:

f2py -c sign.pyf test.f90

The library can be imported and used in Python as:

>>>import mymod
>>>mymod.test.writevec()

Note, that the array bounds are shifted and the first element has the index 0 in Python:

>>>import mymod
>>>mymod.test.vec #output: array([ 1.,  2.,  3.,  4.,  5.])
>>>mymod.test.vec[0] #output: 1.0
Niklas M.
  • 91
  • 1
  • 7