]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/python/numpy/matrix.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / python / numpy / matrix.hpp
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 #include <boost/python/numpy/config.hpp>
18
19
20 namespace boost { namespace python { namespace numpy {
21
22 /**
23 * @brief A boost.python "object manager" (subclass of object) for numpy.matrix.
24 *
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.
31 */
32 class BOOST_NUMPY_DECL matrix : public ndarray
33 {
34 static object construct(object_cref obj, dtype const & dt, bool copy);
35 static object construct(object_cref obj, bool copy);
36 public:
37
38 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(matrix, ndarray);
39
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))) {}
43
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))) {}
47
48 /// \brief Return a view of the matrix with the given dtype.
49 matrix view(dtype const & dt) const;
50
51 /// \brief Copy the scalar (deep for all non-object fields).
52 matrix copy() const;
53
54 /// \brief Transpose the matrix.
55 matrix transpose() const;
56
57 };
58
59 /**
60 * @brief CallPolicies that causes a function that returns a numpy.ndarray to
61 * return a numpy.matrix instead.
62 */
63 template <typename Base = default_call_policies>
64 struct as_matrix : Base
65 {
66 static PyObject * postcall(PyObject *, PyObject * result)
67 {
68 object a = object(handle<>(result));
69 numpy::matrix m(a, false);
70 Py_INCREF(m.ptr());
71 return m.ptr();
72 }
73 };
74
75 } // namespace boost::python::numpy
76
77 namespace converter
78 {
79
80 NUMPY_OBJECT_MANAGER_TRAITS(numpy::matrix);
81
82 }}} // namespace boost::python::converter
83
84 #endif