Commit 844bf463 authored by Mozul Rémy's avatar Mozul Rémy

array management

parent a8655ca8
......@@ -2,7 +2,7 @@
cdef extern from 'type.h':
ctypedef struct myctype:
int i, j
double s
double * s
void print_type(myctype * ct)
......
......@@ -7,27 +7,39 @@ module test_type
type, bind(c) :: myftype
integer(c_int) :: i, j
real(c_double) :: s, s2
type(c_ptr) :: s
end type myftype
contains
subroutine print_type(ft) bind(c, name='print_type')
type(myftype) :: ft
!
real(c_double), dimension(:,:), pointer :: tab
call c_f_pointer(fptr=tab,cptr=ft%s,shape=(/ft%j,ft%i/))
print *, ft%i
print *, ft%j
print *, ft%s
print *, tab
end subroutine
subroutine increment(ft) bind(c,name='increment')
implicit none
type(myftype) :: ft
!
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/))
do i = 1, ft%i
do j = 1, ft%j
tab(j,i) = 0.1d0*i*j
end do
end do
ft%i = ft%i+1
ft%j = ft%j-1
ft%s = dsqrt(real(ft%i,c_double)*ft%i+real(ft%j,c_double)*ft%j)
end subroutine
end module test_type
......
import math
cimport numpy
import numpy
import cython
from ctest cimport myctype, increment, print_type
@cython.locals(x=myctype)
cdef build_type(i,j,s):
#cdef myctype x = {'i':i,'j':j,'s':s}
x = {'i':i,'j':j,'s':s}
def build_pytype(i,j,s):
build_type(i,j,s)
cdef class pytype(object):
x = cython.declare(myctype)
def __init__(self,i,j,s):
self.x = {'i':i,'j':j,'s':s}
@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}
def dist(self):
return math.sqrt(self.x.i**2 + self.x.j**2)
......
typedef struct {
int i, j;
double s, s2;
double *s;
} 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