[C++-sig] Boost.Python v2: Numeric/numarray support

David Abrahams dave at boost-consulting.com
Thu Sep 26 02:40:27 CEST 2002


I've just checked in changes which give some support for Numeric and
numarray arrays.

Summary:

    #include <boost/python/numeric.hpp>
    using namespace boost::python;

    // a function accepting a numeric array argument
    object f(numeric::array x)
    {
        return x + x[make_tuple(3,3,4)]; // return x + x[3,3,4];
    }

    numeric::array g(object sequence)
    {
        // numeric::array() constructor maps to the 'array'
        // factory function.
        return numeric::array(sequence);
    }

Since numeric::array derives from boost::python::object, all the usual
operators are defined by default.

The default is to use the facilities of the numarray module, if it is
installed. Otherwise, we fall back to the Numeric module. If you have both
installed, and you want Numeric, or one of them is installed elsewhere in
your package path, you can tell Boost.Python which one to use explicitly:

    numeric::set_module_and_type("Numeric", "ArrayType");
    numeric::set_module_and_type("numarray", "NDArray");

I implemented all of the interface of numarray.NumArray, except that the
constructor is replaced by the interface of the array() factory function.
That means the C++ class has lots of interface that will just throw
exceptions if you try to use it with Numeric, e.g:

    x.diagonal(1, 2); // works with numarray, throws with Numeric

It seems as though Numeric users will probably want C++ wrappers for all of
the regular Numeric free functions and ufuncs, but that will probably have
to wait until after release (unless some kindly volunteer shows up who
wants to do it).

Enjoy,
Dave

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com







More information about the Cplusplus-sig mailing list