mirror of
https://github.com/boostorg/python.git
synced 2026-01-23 17:52:17 +00:00
numpy python extension - added basic SCons build system, started on unit tests
This commit is contained in:
5
libs/python/numpy/src/SConscript
Normal file
5
libs/python/numpy/src/SConscript
Normal file
@@ -0,0 +1,5 @@
|
||||
Import("env")
|
||||
|
||||
lib = env.SharedLibrary("boost_python_numpy", Glob("*.cpp"))
|
||||
|
||||
Return("lib")
|
||||
17
libs/python/numpy/test/SConscript
Normal file
17
libs/python/numpy/test/SConscript
Normal file
@@ -0,0 +1,17 @@
|
||||
Import("env")
|
||||
import os
|
||||
|
||||
test_env = env.Clone()
|
||||
test_env.Append(LIBS="boost_python_numpy")
|
||||
|
||||
tests = ("ufunc",)
|
||||
test_mods = [test_env.SharedLibrary("%s_mod" % k, "%s_mod.cpp" % k, SHLIBPREFIX="")
|
||||
for k in tests]
|
||||
os.path.abspath(".")
|
||||
test_runs = [test_env.Command(".%s" % name, [mod,"%s.py" % name],
|
||||
"cd %s; python %s.py" % (os.path.abspath("."), name))
|
||||
for name, mod in zip(tests, test_mods)]
|
||||
|
||||
test = Alias("test",[test_runs])
|
||||
|
||||
Return("test")
|
||||
49
libs/python/numpy/test/ufunc.py
Executable file
49
libs/python/numpy/test/ufunc.py
Executable file
@@ -0,0 +1,49 @@
|
||||
import ufunc_mod
|
||||
import unittest
|
||||
import numpy
|
||||
|
||||
class TestUnary(unittest.TestCase):
|
||||
|
||||
def testScalar(self):
|
||||
f = ufunc_mod.UnaryCallable()
|
||||
self.assertEqual(f(1.0), 2.0)
|
||||
self.assertEqual(f(3.0), 6.0)
|
||||
|
||||
def testArray(self):
|
||||
f = ufunc_mod.UnaryCallable()
|
||||
a = numpy.arange(5, dtype=float)
|
||||
b = f(a)
|
||||
self.assert_((b == a*2.0).all())
|
||||
c = numpy.zeros(5, dtype=float)
|
||||
d = f(a,output=c)
|
||||
self.assert_((c == a*2.0).all())
|
||||
self.assert_((d == a*2.0).all())
|
||||
|
||||
def testList(self):
|
||||
f = ufunc_mod.UnaryCallable()
|
||||
a = range(5)
|
||||
b = f(a)
|
||||
self.assert_((b/2.0 == a).all())
|
||||
|
||||
class TestBinary(unittest.TestCase):
|
||||
|
||||
def testScalar(self):
|
||||
f = ufunc_mod.BinaryCallable()
|
||||
self.assertEqual(f(1.0, 3.0), 11.0)
|
||||
self.assertEqual(f(3.0, 2.0), 12.0)
|
||||
|
||||
def testArray(self):
|
||||
f = ufunc_mod.BinaryCallable()
|
||||
a = numpy.random.randn(5)
|
||||
b = numpy.random.randn(5)
|
||||
self.assert_((f(a,b) == (a*2+b*3)).all())
|
||||
c = numpy.zeros(5, dtype=float)
|
||||
d = f(a,b,output=c)
|
||||
self.assert_((c == a*2 + b*3).all())
|
||||
self.assert_((d == a*2 + b*3).all())
|
||||
self.assert_((f(a, 2.0) == a*2 + 6.0).all())
|
||||
self.assert_((f(1.0, b) == 2.0 + b*3).all())
|
||||
|
||||
|
||||
if __name__=="__main__":
|
||||
unittest.main()
|
||||
31
libs/python/numpy/test/ufunc_mod.cpp
Normal file
31
libs/python/numpy/test/ufunc_mod.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <boost/python/numpy.hpp>
|
||||
|
||||
namespace bp = boost::python;
|
||||
|
||||
struct UnaryCallable {
|
||||
|
||||
typedef double argument_type;
|
||||
typedef double result_type;
|
||||
|
||||
double operator()(double r) const { return r * 2; }
|
||||
|
||||
};
|
||||
|
||||
struct BinaryCallable {
|
||||
|
||||
typedef double first_argument_type;
|
||||
typedef double second_argument_type;
|
||||
typedef double result_type;
|
||||
|
||||
double operator()(double a, double b) const { return a * 2 + b * 3; }
|
||||
|
||||
};
|
||||
|
||||
BOOST_PYTHON_MODULE(ufunc_mod) {
|
||||
bp::numpy::initialize();
|
||||
bp::class_< UnaryCallable, boost::shared_ptr<UnaryCallable> >("UnaryCallable")
|
||||
.def("__call__", bp::numpy::unary_ufunc<UnaryCallable>::make());
|
||||
bp::class_< BinaryCallable, boost::shared_ptr<BinaryCallable> >("BinaryCallable")
|
||||
.def("__call__", bp::numpy::binary_ufunc<BinaryCallable>::make());
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user