particular, the a array argument




Download 410,71 Kb.
Pdf ko'rish
bet9/12
Sana16.11.2023
Hajmi410,71 Kb.
#100091
1   ...   4   5   6   7   8   9   10   11   12
Bog'liq
cython cise


particular, the a array argument.
To do this,
we set the array dimensions and then create the
array, filling it with random values. To simplify
matters, we set all array dimensions equal to m:
>>> import numpy as np
>>> from numpy.random import rand
>>> m = 10
>>> rand_array = rand(m, m)
>>> a = np.asfortranarray(rand_array,
... dtype=np.double)
The asfortranarray() function is important –
this ensures that the array a is laid out in column-
major ordering, also known as “fortran ordering.”
This ensures that no copying is required when
passing arrays to Fortran subroutines.
Any subroutine argument that is an INTENT(OUT)
array needs to be passed to the subroutine. The
subroutine will modify the array in place; no
copies are made for arrays of numeric types. This
is not required for scalar INTENT(OUT) arguments,
such as the INFO argument. This is how one would
create three empty arrays of appropriate dimen-
sions:
>>> s = np.empty(m, dtype=np.double,
... order=’F’)
>>> u = np.empty((m, m), dtype=np.double,
... order=’F’)
>>> vt = np.empty((m, m), dtype=np.double,
... order=’F’)
The order=’F’ keyword argument serves the
same purpose as the asfortranarray() function.
The extension module is named fw dgesdd.so
(the file extension is platform-dependent).
We
import dgesdd from it and call it from Python:
>>> from fw_dgesdd import dgesdd
# specify that we want all the output vectors
>>> jobz = ’A’
>>> (a, s, u, vt, info) = dgesdd(
... jobz, m, n, a, m, s, u, m, vt, m)
The return value is a tuple that contains all ar-
guments that were declared intent out, inout
or with no intent spec. The a argument (intent
inout) is in both the argument list and the return
tuple, but no copy has been made.
We can verify that the result is correct:
>>> s_diag = np.diag(s)
>>> a_computed = np.dot(u,
...
np.dot(s_diag, vt))
>>> np.allclose(a, a_computed)
True
7


Here we create a computed which is equivalent
to the matrix product u * s diag * vt, and we
verify that a and a computed are equal to within
machine precision.
When calling the routine from within Cython
code, the invocation is identical, and the argu-
ments can be typed to reduce function call over-
head. Again, please see the documentation for
details and examples.
Fwrap handles any kind of Fortran array declara-
tion, whether assumed-size (like the above exam-
ple), assumed-shape or explicit shape. Options
exist for hiding redundant arguments (like the ar-
ray dimensions LDA, LDU and LDVT above) and are
covered in Fwrap’s documentation.
This
example
covers
just
the
basics
of
what Fwrap can do.
For more informa-
tion,
downloads and help using Fwrap,
see
http://fwrap.sourceforge.net/
.
You
can
reach
other
users
and
the
Fwrap
devel-
opers
on
the
the
fwrap-users
mailing
list,
http://groups.google.com/group/fwrap-users
.

Download 410,71 Kb.
1   ...   4   5   6   7   8   9   10   11   12




Download 410,71 Kb.
Pdf ko'rish