GitXplorerGitXplorer
m

numpy-boost

public
24 stars
5 forks
0 issues

Commits

List of commits on branch master.
Unverified
31c186bf1a0cc2e1bb6d9c2ee1e562410927bd13

Merge pull request #2 from sigurdstorve/master

mmdboom committed 10 years ago
Unverified
cc65e6892b87ad8f28950c7836ba04af9e8966e0

Changed numpy_type_map from class type to a function template with one specialization for each conversion case. This solves the problem of multiple definition linker errors when compiling with GCC. If conversion from a data type not specified in the list of specializations is requested, a runtime_error will be thrown. The specializations are marked as inline to avoid multiple definition linker errors

ssigurdstorve committed 10 years ago
Unverified
2c6595a687c9dc092dbb45525f965963e905c3e6

Update README in reStructuredText for github

mmdboom committed 11 years ago
Unverified
4a49c0a3eb3a7d2ae06dff6c07cb5c4cf323ea23

As reported by foderud in Issue #3, make it easier to link against a debug Python.

committed 11 years ago
Unverified
b067fd2f72ff732246b75f0dde385fa21ecc9c10

As reported by foderud in Issue #4, fix "throw ()" on constructors whose superclass may throw exceptions.

committed 11 years ago
Unverified
b46afc3ccefbf80037bbcaa0b57233c66175f946

As reported by foderud in Issue #5, fix ambiguous namespace usage.

committed 11 years ago

README

The README file for this repository.

This code provides a very thin wrapper around Numpy <http://numpy.org/>__ arrays to make them accessible in C++ as boost::multi_array <http://www.boost.org/doc/libs/release/libs/multi_array/>__ objects. Wherever possible, the data itself is not copied, even when the data is noncontiguous.

Advantages over using the raw Numpy/C API are:

  • Cleaner syntax without using macros, e.g. indexing, iterators

  • Automatic integration with Python reference counting, for easier memory management

  • Any C++ algorithm written to the boost::multi_array interface can easily be recompiled to work with Numpy arrays.

Known shortcomings:

  • Due to the design of boost::multi_array, the datatype and number of dimensions of the array is fixed at compile time (though this is often not an important limitation in practice).

  • Some features in Numpy arrays are not supported by boost::multi_array, and therefore require an implicit data copy on conversion. These include:

    • Values in non-native endianess

    • Object arrays and recarrays are not supported.

.. note::

This code is currently experimental and lacks adequate documentation and unit tests.

Prerequisites

  • Boost 1.34 or later

  • Numpy 1.1 or later

  • Python 2.3 or later

Compilation

The setup.py script included in the source tree is for testing and demonstration purposes only. You do not need to build or install it to start using this library.

The entirety of this project exists in a two header files include/numpy-boost.hpp, which handles the bridge between Numpy and boost::multi_array, and the optional include/numpy-boost-python.hpp which is helpful if you are wrapping your library with boost::python. These need to be on your C++ include path, as well as Numpy's headers. Since everything is implemented in header files, you do not need to link to any additional libraries.

If using distutils to build your Python extension module, this can be achieved with the following::

from distutils.core import setup, Extension

import numpy try: numpy_include = numpy.get_include() except AttributeError: numpy_include = numpy.get_numpy_include()

setup(name="my_project", version="0.1", description = "My project that uses numpy-boost", packages=['my_project'], ext_modules=[ Extension('my_project.extension_module', ['src/my_project_source.cpp'], include_dirs=[PATH_TO_NUMPY_BOOST_HPP, numpy_include] ) ] )

Beyond that, your extension module should do all of the things required of a Numpy-using extension module. See the Numpy documentation for more information.

Usage

To create a numpy_boost array based on an existing Numpy array::

PyArrayObject* numpy_array; // passed into function

...

numpy_boost<double, 3> array(numpy_array);

where the template arguments to numpy_boost are the data type and the number of dimensions.

To create a new empty numpy_boost array::

int dims[] = { 32, 64 };
numpy_boost<int, 2> array(dims);

To return a Numpy array back to the caller, use the py_ptr() method. Note this returns a borrowed reference::

PyObject* result = array.py_ptr();
Py_INCREF(result);
return result;