Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61972 - in sandbox/numpy: . libs/python/numpy/src libs/python/numpy/test
From: talljimbo_at_[hidden]
Date: 2010-05-14 18:47:15


Author: jbosch
Date: 2010-05-14 18:47:14 EDT (Fri, 14 May 2010)
New Revision: 61972
URL: http://svn.boost.org/trac/boost/changeset/61972

Log:
numpy python extension - added basic SCons build system, started on unit tests
Added:
   sandbox/numpy/SConstruct (contents, props changed)
      - copied, changed from r61851, /sandbox/python_extensions/SConstruct
   sandbox/numpy/libs/python/numpy/src/SConscript (contents, props changed)
   sandbox/numpy/libs/python/numpy/test/ (props changed)
   sandbox/numpy/libs/python/numpy/test/SConscript (contents, props changed)
   sandbox/numpy/libs/python/numpy/test/ufunc.py (contents, props changed)
   sandbox/numpy/libs/python/numpy/test/ufunc_mod.cpp (contents, props changed)
Properties modified:
   sandbox/numpy/libs/python/numpy/src/ (props changed)
Text files modified:
   sandbox/numpy/SConstruct | 16 +++++++++++++++-
   1 files changed, 15 insertions(+), 1 deletions(-)

Copied: sandbox/numpy/SConstruct (from r61851, /sandbox/python_extensions/SConstruct)
==============================================================================
--- /sandbox/python_extensions/SConstruct (original)
+++ sandbox/numpy/SConstruct 2010-05-14 18:47:14 EDT (Fri, 14 May 2010)
@@ -1,4 +1,5 @@
 import distutils.sysconfig
+import numpy.distutils.misc_util
 import re
 import os
 
@@ -28,10 +29,23 @@
     cflags.append("-I%s" % distutils.sysconfig.get_python_inc())
     ApplyFlags(env, cflags + libs)
 
+def ConfigureNumpy(env):
+ folders = numpy.distutils.misc_util.get_numpy_include_dirs()
+ env.Append(CPPPATH=folders)
+
 env = Environment()
 ConfigurePython(env)
+ConfigureNumpy(env)
 env.Append(LIBS = "boost_python")
 env.Append(CPPPATH = "#")
 
 Export("env")
-SConscript("libs/python/test/SConscript")
+lib = SConscript("libs/python/numpy/src/SConscript")
+libpath = os.path.abspath("libs/python/numpy/src")
+if os.environ.has_key("LD_LIBRARY_PATH"):
+ env["ENV"]["LD_LIBRARY_PATH"] = "%s:%s" % (libpath, os.environ["LD_LIBRARY_PATH"])
+else:
+ env["ENV"]["LD_LIBRARY_PATH"] = libpath
+env.Append(LIBPATH=libpath)
+Export("lib")
+SConscript("libs/python/numpy/test/SConscript")

Added: sandbox/numpy/libs/python/numpy/src/SConscript
==============================================================================
--- (empty file)
+++ sandbox/numpy/libs/python/numpy/src/SConscript 2010-05-14 18:47:14 EDT (Fri, 14 May 2010)
@@ -0,0 +1,5 @@
+Import("env")
+
+lib = env.SharedLibrary("boost_python_numpy", Glob("*.cpp"))
+
+Return("lib")

Added: sandbox/numpy/libs/python/numpy/test/SConscript
==============================================================================
--- (empty file)
+++ sandbox/numpy/libs/python/numpy/test/SConscript 2010-05-14 18:47:14 EDT (Fri, 14 May 2010)
@@ -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")

Added: sandbox/numpy/libs/python/numpy/test/ufunc.py
==============================================================================
--- (empty file)
+++ sandbox/numpy/libs/python/numpy/test/ufunc.py 2010-05-14 18:47:14 EDT (Fri, 14 May 2010)
@@ -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()

Added: sandbox/numpy/libs/python/numpy/test/ufunc_mod.cpp
==============================================================================
--- (empty file)
+++ sandbox/numpy/libs/python/numpy/test/ufunc_mod.cpp 2010-05-14 18:47:14 EDT (Fri, 14 May 2010)
@@ -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());
+
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk