Commit 2e8f1323 authored by Mozul Rémy's avatar Mozul Rémy

ajout vue numpy d'un tableau alloué dans Fortran

parent f1d74f83
......@@ -13,3 +13,6 @@ test.pyx : module p/cython implémentant la classe python
wrappant le contenu du .pxd
pytest.py : le test
Pour comment créer une vue numpy d'un pointeur fortran :
https://gist.github.com/1249305/bd8a6922507b7e5da0f1417fdba77d5115dd12d4
cdef extern from 'type.h':
ctypedef enum param_names:
p1, p2, p3, p4
ctypedef struct myctype:
int i, j
double * s
char * chaine
double * mat
double params[4]
void print_type(myctype * ct)
......
......@@ -5,10 +5,14 @@ module test_type
implicit none
enum, bind(c)
enumerator :: p1=1, p2, p3, p4
end enum
type, bind(c) :: myftype
integer(c_int) :: i, j
type(c_ptr) :: s
type(c_ptr) :: str
type(c_ptr) :: my_mat
real(c_double), dimension(4) :: params
end type myftype
contains
......@@ -17,15 +21,14 @@ module test_type
type(myftype) :: ft
!
real(c_double), dimension(:,:), pointer :: tab
character(len=256,kind=c_char), pointer :: str
call c_f_pointer(fptr=tab,cptr=ft%s,shape=(/ft%j,ft%i/))
call c_f_pointer(fptr=str,cptr=ft%str)
call c_f_pointer(fptr=tab,cptr=ft%my_mat,shape=(/ft%j,ft%i/))
print *,'my mat : '
print *, ft%i
print *, ft%j
print *, tab
print *, str
print *,'param vector : ', ft%params
end subroutine
......@@ -36,7 +39,7 @@ module test_type
integer(c_int) :: i,j
real(c_double), dimension(:,:), pointer :: tab
call c_f_pointer(fptr=tab,cptr=ft%s,shape=(/ft%j,ft%i/))
call c_f_pointer(fptr=tab,cptr=ft%my_mat,shape=(/ft%j,ft%i/))
do i = 1, ft%i
do j = 1, ft%j
......
......@@ -9,3 +9,5 @@ t = test.pytype(z)
t.prin()
t.incr()
t.prin()
t.p_params[0] = 2.
t.prin()
......@@ -4,22 +4,37 @@ cimport numpy
import numpy
import cython
from ctest cimport myctype, increment, print_type
from ctest cimport p1, p2, myctype, increment, print_type
numpy.import_array()
p = [p1, p2]
cdef class pytype(object):
x = cython.declare(myctype)
#x = cython.declare(myctype)
cdef myctype x
cdef public numpy.ndarray p_params
@cython.locals(a=numpy.ndarray)
def __init__(self,a):
a = numpy.asfarray(a)
self.x = {'i':a.shape[0],'j':a.shape[1],'s':<double *>a.data}
self.x = {'i':a.shape[0],'j':a.shape[1],'mat':<double *>a.data}
#cdef double * params = cython.address(self.x).params
#self.p_params = numpy.asfarray(<numpy.double64_t[:4]> params)
#self.p_params = numpy.asfarray(<numpy.double64_t[:4]> cython.address(self.x).params)
cdef numpy.npy_intp s[1]
s[0] = <numpy.npy_intp> 4
self.p_params = numpy.PyArray_SimpleNewFromData(1, s, numpy.NPY_DOUBLE, cython.address(self.x).params)
self.p_params[0:4] = 0.
def dist(self):
return math.sqrt(self.x.i**2 + self.x.j**2)
def incr(self):
increment(cython.address(self.x))
self.p_params[0:4] += 1.
def prin(self):
print_type(cython.address(self.x))
......
typedef enum {
p1, p2, p3, p4
} param_names;
typedef struct {
int i, j;
double *s;
char *chaine;
double *mat;
double params[4];
} myctype;
//void init_type(int n, int i, int j);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment