Commit a8655ca8 authored by Mozul Rémy's avatar Mozul Rémy
Browse files

init from hackaton

parents
Le fichier mod_type.f90 contient
cdef extern from 'type.h':
ctypedef struct myctype:
int i, j
double s
void print_type(myctype * ct)
void increment(myctype * ct)
all: test.so
libtype.so: mod_type.f90
gfortran -shared -o $@ $<
test.so: test.pyx libtype.so
python setup.py build_ext -i
clean:
@rm -f *.so *.c *.mod
module test_type
use iso_c_binding
implicit none
type, bind(c) :: myftype
integer(c_int) :: i, j
real(c_double) :: s, s2
end type myftype
contains
subroutine print_type(ft) bind(c, name='print_type')
type(myftype) :: ft
print *, ft%i
print *, ft%j
print *, ft%s
end subroutine
subroutine increment(ft) bind(c,name='increment')
implicit none
type(myftype) :: ft
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 test
test.build_pytype(1,2,4.)
t = test.pytype(2,3,5.)
print t.dist()
t.incr()
t.prin()
import os
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(name='sprint',
ext_modules=[Extension('test',['test.pyx'],libraries=['type'],library_dirs=[os.getcwd()])],
cmdclass={'build_ext':build_ext}
)
import math
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}
def dist(self):
return math.sqrt(self.x.i**2 + self.x.j**2)
def incr(self):
increment(cython.address(self.x))
def prin(self):
print_type(cython.address(self.x))
# @cython.returns(cython.struc)
# def get_x(self):
# return self.x
typedef struct {
int i, j;
double s, s2;
} myctype;
//void init_type(int n, int i, int j);
void print_type(myctype * ct);
void increment(myctype * ct);
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