Skip to content

Add a new example for using scipy with the Python module #26696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/library/packages/Python/examples/scipy/CLEANFILES
1 change: 1 addition & 0 deletions test/library/packages/Python/examples/scipy/COMPOPTS
1 change: 1 addition & 0 deletions test/library/packages/Python/examples/scipy/EXECENV
48 changes: 48 additions & 0 deletions test/library/packages/Python/examples/scipy/integrateQuad.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use Python;

var interp = new Interpreter();

var sp = interp.importModule("scipy");
var spi = interp.importModule("scipy.integrate");

// integrate a function using scipy.integrate.quad
proc integrate(f: borrowed Value, a: real, b: real): real {
return spi.call("quad", f, a, b).call(real, "__getitem__", 0);
}

{
writeln("Integrating x**3, defined in Python");
var f = interp.compileLambda("lambda x,: x**3");
writeln(integrate(f, 0.0, 2.0));
writeln(integrate(f, 0.0, 4.0));
writeln(integrate(f, 0.0, 8.0));
}


proc chapelProcToPython(f:proc(_: real):real): owned Value {
use CTypes;
// get a c_ptr to a Chapel proc
var f_ptr = c_ptrTo(f):c_ptr(void);

// create a ctypes object from the function pointer
// another way to do this would be to support the pycapsule api in some form
var ctypes = interp.importModule("ctypes");
var ctypes_double = ctypes.get("c_double");
var cfunctype = ctypes.call("CFUNCTYPE", ctypes_double, ctypes_double);
var f_ptrPy = cfunctype(f_ptr:c_intptr);

// using the python representation of the function pointer,
// create a LowLevelCallable for scipy usage
var fPy = sp.call("LowLevelCallable", f_ptrPy,
kwargs=["signature"=>"double (double)"]);
return fPy;
}

{
writeln("Integrating x**3, defined in Chapel");
proc chplFunc(x: real): real do return x;
var f = chapelProcToPython(cChplFunc);
writeln(integrate(f, 0.0, 2.0));
writeln(integrate(f, 0.0, 4.0));
writeln(integrate(f, 0.0, 8.0));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
4.0
64.0
1024.0
10 changes: 10 additions & 0 deletions test/library/packages/Python/examples/scipy/integrateQuad.skipif
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

# skip valgrind testing: if CHPL_TEST_VGRND_EXE is set and 'on'
if [ -n "$CHPL_TEST_VGRND_EXE" ] && [ "$CHPL_TEST_VGRND_EXE" == "on" ]; then
echo "True"
exit 0
fi

FILE_DIR=$(cd $(dirname ${BASH_SOURCE[0]}) ; pwd)
$FILE_DIR/../../skipIfAndInstallPackage.sh $FILE_DIR scipy
Empty file.