Skip to content
Snippets Groups Projects
Commit 06acbd9b authored by David Weir's avatar David Weir
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
run.py 0 → 100644
import scipy.integrate
import test
# Turn the Cython C function into a LowLevelCallable
integrand = scipy.LowLevelCallable.from_cython(test,
'integrand')
# 'integrand' here is A*x*x + b
# - see test.pyx
A = 0.0
b = 0.0
res = scipy.integrate.quad(integrand, 0, 4, args=(A, b))
print('Should be 0: %g' % res[0])
A = 0.0
b = 1.0
res = scipy.integrate.quad(integrand, 0, 4, args=(A, b))
print('Should be 4: %g' % res[0])
A = 1.0
b = 0.0
res = scipy.integrate.quad(integrand, 0, 4, args=(A, b))
print('Should be 21.333...: %g' % res[0])
setup.py 0 → 100644
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
setup(
ext_modules=cythonize(Extension("test",
sources=["test.pyx"],
include_dirs=['./']),
annotate=True)
)
# This is sort of a header file
# The 'int' is the number of arguments in the double (should be 3)
# and the double array contains the x-coordinate followed by
# your optional arguments, here we have just two.
cdef double integrand(int, double[3])
test.pyx 0 → 100644
# The integrand here is A*x*x + b
# NB: this has to be a cdef, not a cpdef or def, because these add
# extra stuff to the argument list to help python. LowLevelCallable
# does not like these things...
# You can however increase the number of arguments (remember also to
# update test.pxd)
cdef double integrand(int n, double[3] args):
# x-coordinate
x = args[0]
# coefficients
A = args[1]
# coefficients
b = args[2]
return A*x*x + b
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment