]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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) | |
6 | ||
7 | #ifndef boost_python_numpy_matrix_hpp_ | |
8 | #define boost_python_numpy_matrix_hpp_ | |
9 | ||
10 | /** | |
11 | * @brief Object manager for numpy.matrix. | |
12 | */ | |
13 | ||
14 | #include <boost/python.hpp> | |
15 | #include <boost/python/numpy/numpy_object_mgr_traits.hpp> | |
16 | #include <boost/python/numpy/ndarray.hpp> | |
17 | ||
18 | namespace boost { namespace python { namespace numpy { | |
19 | ||
20 | /** | |
21 | * @brief A boost.python "object manager" (subclass of object) for numpy.matrix. | |
22 | * | |
23 | * @internal numpy.matrix is defined in Python, so object_manager_traits<matrix>::get_pytype() | |
24 | * is implemented by importing numpy and getting the "matrix" attribute of the module. | |
25 | * We then just hope that doesn't get destroyed while we need it, because if we put | |
26 | * a dynamic python object in a static-allocated boost::python::object or handle<>, | |
27 | * bad things happen when Python shuts down. I think this solution is safe, but I'd | |
28 | * love to get that confirmed. | |
29 | */ | |
30 | class matrix : public ndarray | |
31 | { | |
32 | static object construct(object_cref obj, dtype const & dt, bool copy); | |
33 | static object construct(object_cref obj, bool copy); | |
34 | public: | |
35 | ||
36 | BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(matrix, ndarray); | |
37 | ||
38 | /// @brief Equivalent to "numpy.matrix(obj,dt,copy)" in Python. | |
39 | explicit matrix(object const & obj, dtype const & dt, bool copy=true) | |
40 | : ndarray(extract<ndarray>(construct(obj, dt, copy))) {} | |
41 | ||
42 | /// @brief Equivalent to "numpy.matrix(obj,copy=copy)" in Python. | |
43 | explicit matrix(object const & obj, bool copy=true) | |
44 | : ndarray(extract<ndarray>(construct(obj, copy))) {} | |
45 | ||
46 | /// \brief Return a view of the matrix with the given dtype. | |
47 | matrix view(dtype const & dt) const; | |
48 | ||
49 | /// \brief Copy the scalar (deep for all non-object fields). | |
50 | matrix copy() const; | |
51 | ||
52 | /// \brief Transpose the matrix. | |
53 | matrix transpose() const; | |
54 | ||
55 | }; | |
56 | ||
57 | /** | |
58 | * @brief CallPolicies that causes a function that returns a numpy.ndarray to | |
59 | * return a numpy.matrix instead. | |
60 | */ | |
61 | template <typename Base = default_call_policies> | |
62 | struct as_matrix : Base | |
63 | { | |
64 | static PyObject * postcall(PyObject *, PyObject * result) | |
65 | { | |
66 | object a = object(handle<>(result)); | |
67 | numpy::matrix m(a, false); | |
68 | Py_INCREF(m.ptr()); | |
69 | return m.ptr(); | |
70 | } | |
71 | }; | |
72 | ||
73 | } // namespace boost::python::numpy | |
74 | ||
75 | namespace converter | |
76 | { | |
77 | ||
78 | NUMPY_OBJECT_MANAGER_TRAITS(numpy::matrix); | |
79 | ||
80 | }}} // namespace boost::python::converter | |
81 | ||
82 | #endif |