matrix.hpp 2.6 KB

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