1 // Copyright Jim Bosch 2010-2012.
2 // Copyright Stefan Seefeld 2016.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef boost_python_numpy_matrix_hpp_
8 #define boost_python_numpy_matrix_hpp_
11 * @brief Object manager for numpy.matrix.
14 #include <boost/python.hpp>
15 #include <boost/python/numpy/numpy_object_mgr_traits.hpp>
16 #include <boost/python/numpy/ndarray.hpp>
17 #include <boost/python/numpy/config.hpp>
20 namespace boost { namespace python { namespace numpy {
23 * @brief A boost.python "object manager" (subclass of object) for numpy.matrix.
25 * @internal numpy.matrix is defined in Python, so object_manager_traits<matrix>::get_pytype()
26 * is implemented by importing numpy and getting the "matrix" attribute of the module.
27 * We then just hope that doesn't get destroyed while we need it, because if we put
28 * a dynamic python object in a static-allocated boost::python::object or handle<>,
29 * bad things happen when Python shuts down. I think this solution is safe, but I'd
30 * love to get that confirmed.
32 class BOOST_NUMPY_DECL matrix : public ndarray
34 static object construct(object_cref obj, dtype const & dt, bool copy);
35 static object construct(object_cref obj, bool copy);
38 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(matrix, ndarray);
40 /// @brief Equivalent to "numpy.matrix(obj,dt,copy)" in Python.
41 explicit matrix(object const & obj, dtype const & dt, bool copy=true)
42 : ndarray(extract<ndarray>(construct(obj, dt, copy))) {}
44 /// @brief Equivalent to "numpy.matrix(obj,copy=copy)" in Python.
45 explicit matrix(object const & obj, bool copy=true)
46 : ndarray(extract<ndarray>(construct(obj, copy))) {}
48 /// \brief Return a view of the matrix with the given dtype.
49 matrix view(dtype const & dt) const;
51 /// \brief Copy the scalar (deep for all non-object fields).
54 /// \brief Transpose the matrix.
55 matrix transpose() const;
60 * @brief CallPolicies that causes a function that returns a numpy.ndarray to
61 * return a numpy.matrix instead.
63 template <typename Base = default_call_policies>
64 struct as_matrix : Base
66 static PyObject * postcall(PyObject *, PyObject * result)
68 object a = object(handle<>(result));
69 numpy::matrix m(a, false);
75 } // namespace boost::python::numpy
80 NUMPY_OBJECT_MANAGER_TRAITS(numpy::matrix);
82 }}} // namespace boost::python::converter