From 4b99e6b83f64b4bcc51d7db69235a38e8c6d10d1 Mon Sep 17 00:00:00 2001 From: Aaron Wishnick Date: Fri, 11 Jan 2013 15:16:29 -0500 Subject: [PATCH] Add astype to ndarray. --- boost/numpy/ndarray.hpp | 3 +++ libs/numpy/doc/reference/ndarray.rst | 7 +++++++ libs/numpy/src/ndarray.cpp | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/boost/numpy/ndarray.hpp b/boost/numpy/ndarray.hpp index a9e6b738..ee5800c0 100644 --- a/boost/numpy/ndarray.hpp +++ b/boost/numpy/ndarray.hpp @@ -79,6 +79,9 @@ public: /// @brief Return a view of the scalar with the given dtype. ndarray view(dtype const & dt) const; + + /// @brief Copy the array, cast to a specified type. + ndarray astype(dtype const & dt) const; /// @brief Copy the scalar (deep for all non-object fields). ndarray copy() const; diff --git a/libs/numpy/doc/reference/ndarray.rst b/libs/numpy/doc/reference/ndarray.rst index fd5c76d0..9b470392 100644 --- a/libs/numpy/doc/reference/ndarray.rst +++ b/libs/numpy/doc/reference/ndarray.rst @@ -37,6 +37,7 @@ synopsis }; ndarray view(dtype const & dt) const; + ndarray astype(dtype const & dt) const; ndarray copy() const; int const shape(int n) const; int const strides(int n) const; @@ -92,6 +93,12 @@ constructors :Returns: new ndarray with old ndarray data cast as supplied dtype +:: + + ndarray astype(dtype const & dt) const; + +:Returns: new ndarray with old ndarray data converted to supplied dtype + :: ndarray copy() const; diff --git a/libs/numpy/src/ndarray.cpp b/libs/numpy/src/ndarray.cpp index 286819a2..ce431546 100644 --- a/libs/numpy/src/ndarray.cpp +++ b/libs/numpy/src/ndarray.cpp @@ -146,6 +146,12 @@ ndarray ndarray::view(dtype const & dt) const return ndarray(python::detail::new_reference (PyObject_CallMethod(this->ptr(), const_cast("view"), const_cast("O"), dt.ptr()))); } + +ndarray ndarray::astype(dtype const & dt) const +{ + return ndarray(python::detail::new_reference + (PyObject_CallMethod(this->ptr(), const_cast("astype"), const_cast("O"), dt.ptr()))); +} ndarray ndarray::copy() const {