Re: [f2py] F2PY issue with array arguments, results in unexpected sizes

"Michelle Gill" <michelle-X+ovDskamAd4hsYFU5B8wFaTQe2KTcn/@public.gmane.org> Sat, 11 Jan 2014 11:06:14 -0500
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
--===============2100314824799961107==
Content-Type: multipart/alternative;
 boundary="=_MailMate_04CEC69E-E2BE-4066-8296-FBC88B400892_="


--=_MailMate_04CEC69E-E2BE-4066-8296-FBC88B400892_=
Content-Type: text/plain; format=flowed; markup=markdown
Content-Transfer-Encoding: quoted-printable

Dear David,

You were correct. I forgot the python function inputs are not =

necessarily identical to those in the fortran function. Upon examining =

them once the module was imported, this is obvious. This also fixes my =

issue with the more complicated function I have been working on. I've =

spent the better part of three days fighting with this issue, so I'm =

extremely appreciative of your help. Thank you so much.

Kind regards,
Michelle Gill

On 11 Jan 2014, at 5:00, [email protected] wrote:
> From: David Ochoa <[email protected]>
> Subject: Re: [f2py] F2PY issue with array arguments,	results in
> 	unexpected sizes
> To: For users of the f2py program <f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]>
> Message-ID:
> 	<CADFoYWon35GK-EAQhRg150YzTrjgCLnQ8_=3Dbj-DDCsa5M+_C2g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>=

> Content-Type: text/plain; charset=3D"iso-8859-1"
>
> Dear Michelle:
>
> I've been using f2py lately and i think that you don't need to send =

> the
> size parameters to functions created with f2py (or at least that's how =

> I'm
> coding).
>
> So it's just change the call:
> v2 =3D test2(r)
>
> And it should work (in my computer it does).
>
> in test1 is different because you are not passing a array, only =

> creating
> and sending it back.
>
> David
>
>
>
>
> On Fri, Jan 10, 2014 at 2:27 PM, Michelle Gill <
> michelle-X+ovDskamAd4hsYFU5B8wFaTQe2KTcn/@public.gmane.org> wrote:
>
>> Dear f2py users,
>>
>> I am attempting to use f2py to speed up some python calculations and =

>> am
>> getting some unexpected results. I made two sample fortran functions =

>> to
>> illustrate the issue. These two functions compile fine with f2py. =

>> When
>> accessed within python, the respective returned rank-2 arrays are
>> different sized even though their sizes should be the same.
>>
>> In the case of the first function, "test1", the size of the returned
>> array is 3x1001, which is as expected. In the case of the second =

>> function,
>> "test2", the size of the returned array is 3x1, which is not correct.
>>
>> This issue has arisen in more complicated function that I'm =

>> attempting
>> to debug and I'd appreciate any assistance in solving it. My fortran
>> skills are a little rusty, so it's possible there is an issue with =

>> the
>> way the arrays are being passed or declared. I've tried searching =

>> this
>> mailing list, StackOverflow, and general web searches with no luck.
>>
>> I'm using gfortran 4.7.3, python 2.7.5, numpy 1.8.0, all built with
>> MacPorts, on a Mac running OS X 10.9.1.
>>
>> If I can provide further details, let me know.
>>
>> Thanks,
>> Michelle Gill
>> Contents of file test1.f90
>>
>> ! -*- f90 -*-
>> subroutine test1(simdim, v)
>>
>> integer, intent(in) :: simdim
>> ! f2py integer, intent(in) :: simdim
>>
>> real, dimension(3,simdim+1), intent(out):: v(3,simdim+1)
>> ! f2py real, dimension(3,simdim+1), intent(out) :: v(3,simdim+1)
>>
>> v =3D spread( (/ 45.2, 31.2, 41.2 /), 2, simdim+1)
>>
>> print *, "v shape ", size(v)
>>
>> return
>> end
>> Contents of file test2.f90
>>
>> ! -*- f90 -*-
>> subroutine test2(simdim, r, v)
>>
>> integer, intent(in) :: simdim
>> ! f2py integer, intent(in) :: simdim
>>
>> real, dimension(simdim), intent(in) :: r(simdim)
>> ! f2py real, dimension(simdim), intent(in) :: r(simdim)
>>
>> real, dimension(3,simdim+1), intent(out):: v(3,simdim+1)
>> ! f2py real, dimension(3,simdim+1), intent(out) :: v(3,simdim+1)
>>
>> v =3D spread( (/ 45.2, 31.2, 41.2 /), 2, simdim+1)
>>
>> print *, "r shape ", size(r)
>> print *, "v shape ", size(v)
>>
>> return
>> end
>> Compile each with f2py
>>
>> f2py --verbose -m test1 -c test1.f90
>> f2py --verbose -m test2 -c test2.f90
>> Compare the functions in IPython
>>
>> In [1]: from test1 import test1
>>
>> In [2]: from test2 import test2
>>
>> In [3]: import numpy as np
>>
>> In [4]: simdim =3D 1000
>>
>> In [5]: r =3D np.random.random(simdim)
>>
>> In [6]: print r.shape
>> (1000,)
>>
>> In [7]: v1 =3D test1(simdim)
>> v shape 3003
>>
>> In [8]: v2 =3D test2(simdim, r)
>> r shape 0
>> v shape 3
>>
>> In [9]: print v1.shape, v2.shape
>> (3, 1001) (3, 1)
>>
>> _______________________________________________
>> f2py-users mailing list
>> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>> http://cens.ioc.ee/mailman/listinfo/f2py-users
>>
>>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: =

> http://cens.ioc.ee/pipermail/f2py-users/attachments/20140110/6970388b/a=
ttachment-0001.htm
>
> ------------------------------
>
> _______________________________________________
> f2py-users mailing list
> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
>
> End of f2py-users Digest, Vol 97, Issue 2
> *****************************************

--=_MailMate_04CEC69E-E2BE-4066-8296-FBC88B400892_=
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

<div class=3D"markdown">
<p dir=3D"auto">Dear David,</p>

<p dir=3D"auto">You were correct. I forgot the python function inputs are=
 not necessarily identical to those in the fortran function. Upon examini=
ng them once the module was imported, this is obvious. This also fixes my=
 issue with the more complicated function I have been working on. I've sp=
ent the better part of three days fighting with this issue, so I'm extrem=
ely appreciative of your help. Thank you so much.</p>

<p dir=3D"auto">Kind regards,<br>
Michelle Gill</p>

<p dir=3D"auto">On 11 Jan 2014, at 5:00, <a href=3D"mailto:ochoadavid@gma=
il.com">[email protected]</a> wrote:</p>

<blockquote>
<p dir=3D"auto">From: David Ochoa <a href=3D"mailto:[email protected]"=
>[email protected]</a><br>
Subject: Re: [f2py] F2PY issue with array arguments,    results in<br>
    unexpected sizes<br>
To: For users of the f2py program <a href=3D"mailto:[email protected]=
e">f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]</a><br>
Message-ID:<br>
    <br>
Content-Type: text/plain; charset=3D"iso-8859-1"</p>

<p dir=3D"auto">Dear Michelle:</p>

<p dir=3D"auto">I've been using f2py lately and i think that you don't ne=
ed to send the<br>
size parameters to functions created with f2py (or at least that's how I'=
m<br>
coding).</p>

<p dir=3D"auto">So it's just change the call:<br>
v2 =3D test2(r)</p>

<p dir=3D"auto">And it should work (in my computer it does).</p>

<p dir=3D"auto">in test1 is different because you are not passing a array=
, only creating<br>
and sending it back.</p>

<p dir=3D"auto">David</p>

<p dir=3D"auto">On Fri, Jan 10, 2014 at 2:27 PM, Michelle Gill <<br>
<a href=3D"mailto:michelle-X+ovDskamAd4hsYFU5B8wFaTQe2KTcn/@public.gmane.org">michelle@michellelynngil=
l.com</a>> wrote:</p>

<blockquote>
<p dir=3D"auto">Dear f2py users,</p>

<p dir=3D"auto">I am attempting to use f2py to speed up some python calcu=
lations and am<br>
getting some unexpected results. I made two sample fortran functions to<b=
r>
illustrate the issue. These two functions compile fine with f2py. When<br=
>
accessed within python, the respective returned rank-2 arrays are<br>
different sized even though their sizes should be the same.</p>

<p dir=3D"auto">In the case of the first function, "test1", the size of t=
he returned<br>
array is 3x1001, which is as expected. In the case of the second function=
,<br>
"test2", the size of the returned array is 3x1, which is not correct.</p>=


<p dir=3D"auto">This issue has arisen in more complicated function that I=
'm attempting<br>
to debug and I'd appreciate any assistance in solving it. My fortran<br>
skills are a little rusty, so it's possible there is an issue with the<br=
>
way the arrays are being passed or declared. I've tried searching this<br=
>
mailing list, StackOverflow, and general web searches with no luck.</p>

<p dir=3D"auto">I'm using gfortran 4.7.3, python 2.7.5, numpy 1.8.0, all =
built with<br>
MacPorts, on a Mac running OS X 10.9.1.</p>

<p dir=3D"auto">If I can provide further details, let me know.</p>

<p dir=3D"auto">Thanks,<br>
Michelle Gill<br>
Contents of file test1.f90</p>

<p dir=3D"auto">! -<em>- f90 -</em>-<br>
subroutine test1(simdim, v)</p>

<p dir=3D"auto">integer, intent(in) :: simdim<br>
! f2py integer, intent(in) :: simdim</p>

<p dir=3D"auto">real, dimension(3,simdim+1), intent(out):: v(3,simdim+1)<=
br>
! f2py real, dimension(3,simdim+1), intent(out) :: v(3,simdim+1)</p>

<p dir=3D"auto">v =3D spread( (/ 45.2, 31.2, 41.2 /), 2, simdim+1)</p>

<p dir=3D"auto">print *, "v shape ", size(v)</p>

<p dir=3D"auto">return<br>
end<br>
Contents of file test2.f90</p>

<p dir=3D"auto">! -<em>- f90 -</em>-<br>
subroutine test2(simdim, r, v)</p>

<p dir=3D"auto">integer, intent(in) :: simdim<br>
! f2py integer, intent(in) :: simdim</p>

<p dir=3D"auto">real, dimension(simdim), intent(in) :: r(simdim)<br>
! f2py real, dimension(simdim), intent(in) :: r(simdim)</p>

<p dir=3D"auto">real, dimension(3,simdim+1), intent(out):: v(3,simdim+1)<=
br>
! f2py real, dimension(3,simdim+1), intent(out) :: v(3,simdim+1)</p>

<p dir=3D"auto">v =3D spread( (/ 45.2, 31.2, 41.2 /), 2, simdim+1)</p>

<p dir=3D"auto">print *, "r shape ", size(r)<br>
print *, "v shape ", size(v)</p>

<p dir=3D"auto">return<br>
end<br>
Compile each with f2py</p>

<p dir=3D"auto">f2py --verbose -m test1 -c test1.f90<br>
f2py --verbose -m test2 -c test2.f90<br>
Compare the functions in IPython</p>

<p dir=3D"auto">In [1]: from test1 import test1</p>

<p dir=3D"auto">In [2]: from test2 import test2</p>

<p dir=3D"auto">In [3]: import numpy as np</p>

<p dir=3D"auto">In [4]: simdim =3D 1000</p>

<p dir=3D"auto">In [5]: r =3D np.random.random(simdim)</p>

<p dir=3D"auto">In [6]: print r.shape<br>
(1000,)</p>

<p dir=3D"auto">In [7]: v1 =3D test1(simdim)<br>
v shape 3003</p>

<p dir=3D"auto">In [8]: v2 =3D test2(simdim, r)<br>
r shape 0<br>
v shape 3</p>

<p dir=3D"auto">In [9]: print v1.shape, v2.shape<br>
(3, 1001) (3, 1)</p>

<hr>

<p dir=3D"auto">f2py-users mailing list<br>
<a href=3D"mailto:f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]">f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]</a><br>
<a href=3D"http://cens.ioc.ee/mailman/listinfo/f2py-users">http://cens.io=
c.ee/mailman/listinfo/f2py-users</a></p>
</blockquote>

<p dir=3D"auto">-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <a href=3D"http://cens.ioc.ee/pipermail/f2py-users/attachments/20140=
110/6970388b/attachment-0001.htm">http://cens.ioc.ee/pipermail/f2py-users=
/attachments/20140110/6970388b/attachment-0001.htm</a></p>

<hr>

<hr>

<p dir=3D"auto">f2py-users mailing list<br>
<a href=3D"mailto:f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]">f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]</a><br>
<a href=3D"http://cens.ioc.ee/mailman/listinfo/f2py-users">http://cens.io=
c.ee/mailman/listinfo/f2py-users</a></p>

<p dir=3D"auto">End of f2py-users Digest, Vol 97, Issue 2</p>

<hr>
</blockquote>

</div>
--=_MailMate_04CEC69E-E2BE-4066-8296-FBC88B400892_=--


--===============2100314824799961107==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
f2py-users mailing list
f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
http://cens.ioc.ee/mailman/listinfo/f2py-users

--===============2100314824799961107==--