7 #ifndef MDS_UTILS_PYTHON_UBLAS_NUMPY_ARRAY_HPP_INCLUDED 8 #define MDS_UTILS_PYTHON_UBLAS_NUMPY_ARRAY_HPP_INCLUDED 30 #include <numpy/arrayobject.h> 33 #include <boost/numeric/ublas/traits.hpp> 34 #include <boost/numeric/ublas/detail/vector_assign.hpp> 37 namespace boost {
namespace numeric {
namespace ublas {
61 typedef npy_intp size_type;
62 typedef npy_intp difference_type;
64 typedef const T& const_reference;
67 typedef const T* const_pointer;
68 typedef pointer array_type;
69 typedef const vector_reference<const self_type> const_closure_type;
70 typedef vector_reference<self_type> closure_type;
72 typedef dense_tag storage_category;
78 return reinterpret_cast<PyArrayObject*
>(
getPyObject());
82 operator PyArrayObject*()
const {
94 PyArray_INCREF(reinterpret_cast<PyArrayObject*>(
getPyObject()));
101 PyArray_XDECREF(reinterpret_cast<PyArrayObject*>(
getPyObject()));
116 PyArray_INCREF(reinterpret_cast<PyArrayObject*>(pret));
144 explicit BOOST_UBLAS_INLINE
147 mds_utils::python::numpy::numpy_dtype_traits<T>::typenum)) {
182 explicit BOOST_UBLAS_INLINE
191 if (b != e) std::fill(b,e,init);
204 if (po == NULL)
return 0;
205 return PyArray_SIZE(po);
223 throw std::runtime_error(
"Cannot copy the NumPy array.");
256 vector_assign<scalar_assign>(*
this,ae);
273 if (!PyArray_Check(rhs)) {
274 throw std::invalid_argument(
"Cannot convert to " 275 "boost::numeric::ublas::NumPy1DArrayr: rhs is not a NumPy array.");
284 void check_index(size_type i)
const {
307 return const_cast<pointer
>(
const_cast<const self_type&
>(*this).find_element(i));
322 return const_cast<const pointer
>(
reinterpret_cast<pointer
>(PyArray_GETPTR1(
getPyArrayObject(),i)));
406 if (b != e) std::fill(b,e,static_cast<T>(0));
428 vector_assign<scalar_assign>(*
this, ae);
465 size_type n(rhs.
size());
470 m_po = PyArray_SimpleNew(1,&n,
477 throw std::runtime_error(
"Cannot copy the NumPy array.");
518 throw std::invalid_argument(
"Cannot assign: rhs is not a NumPy array.");
539 template<
class val_T>
544 if (
size() == 0)
return *
this;
546 for_each(
begin(),
end(),[&val](T& x){ x = val; });
567 assert(
size() == rhs.size());
591 self_type temporary(ae);
617 self_type temporary(*
this + ae);
660 vector_assign<scalar_plus_assign> (*
this, ae);
680 self_type temporary (*
this - ae);
724 vector_assign<scalar_minus_assign> (*
this, ae);
746 vector_assign_scalar<scalar_multiplies_assign> (*
this, at);
768 vector_assign_scalar<scalar_divides_assign> (*
this, at);
797 for (;it != e;++it,++it_rhs) {
826 mds_utils::python::numpy::c_storage,NPY_ITER_READWRITE>
832 typedef indexed_iterator<self_type,dense_random_access_iterator_tag> iterator;
833 typedef indexed_const_iterator<self_type, dense_random_access_iterator_tag> const_iterator;
848 const_iterator
find(size_type i)
const {
849 return const_iterator(*
this,i);
862 return iterator(*
this,i);
873 const_iterator
end()
const {
890 typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
891 typedef reverse_iterator_base<iterator> reverse_iterator;
896 return const_reverse_iterator (
end ());
901 const_reverse_iterator
rend ()
const {
902 return const_reverse_iterator (
begin ());
908 return reverse_iterator (
end ());
914 return reverse_iterator (
begin ());
void reset()
Resets the object to the state given by the default constructor.
BOOST_UBLAS_INLINE NumPy1DArray & operator*=(const AT &at)
Assign the product of the vector and a scalar to the vector.
Tag for the C storage ordering.
BOOST_UBLAS_INLINE NumPy1DArray & operator-=(const vector_expression< AE > &ae)
Assign the difference of the vector and a vector_expression to the vector.
Contains the wrapper for the NumPy iterator for ndarray objects.
BOOST_UBLAS_INLINE const_pointer find_element(size_type i) const
Return a const pointer to the element .
Iterator on a NumPy ndarray.
BOOST_UBLAS_INLINE NumPy1DArray(const vector_expression< AE > &ae)
Copy-constructor of a NumPy1DArray from a vector_expression.
BOOST_UBLAS_INLINE NumPy1DArray(FwIt b, FwIt e)
Constructor of a vector by copying from another container.
BOOST_UBLAS_INLINE NumPy1DArray(const NumPy1DArray &rhs)
Copy-constructor.
BOOST_UBLAS_INLINE NumPy1DArray(NumPy1DArray &&rhs)
Move constructor.
BOOST_UBLAS_INLINE NumPy1DArray(size_type n)
Constructor of a vector with a predefined size.
Main namespace of all Michele De Stefano's C++ utilities.
Obj()
Default constructor.
BOOST_UBLAS_INLINE NumPy1DArray & operator=(const NumPy1DArray &rhs)
Assign a full vector (RHS-vector) to the current vector (LHS-vector).
BOOST_UBLAS_INLINE const_iterator find(size_type i) const
Return a const iterator to the element i.
BOOST_UBLAS_INLINE const_iterator end() const
return an iterator after the last element of the vector
PyObject * transfer()
Returns the Python object with transferred ownership.
NumPy1DArray(PyObject *rhs)
Constructor from PyObject.
PyObject * m_po
Underlying pointer to the wrapped Python object.
BOOST_UBLAS_INLINE NumPy1DArray(size_type n, const_reference init)
Constructor of a vector with a unique initial value.
PyArrayObject * getPyArrayObject() const
Returns the underlying PyArrayObject.
Contains a wrapper class for the PyObject* datatype.
BOOST_UBLAS_INLINE NumPy1DArray & plus_assign(const vector_expression< AE > &ae)
Assign the sum of the vector and a vector_expression to the vector.
void get_ownership()
Used in place of incref, when the wrapped PyObject* was increfed already.
Contains utilities for the creation of NumPy extensions.
BOOST_UBLAS_INLINE NumPy1DArray()
Default constructor.
Obj operator()()
Calling operator.
Wrapper class for NumPy 1D arrays.
BOOST_UBLAS_INLINE NumPy1DArray & operator+=(const vector_expression< AE > &ae)
Assign the sum of the vector and a vector_expression to the vector.
BOOST_UBLAS_INLINE NumPy1DArray & assign(const vector_expression< AE > &ae)
Assign the result of a vector_expression to the vector.
Obj & operator=(const Obj &rhs)
Standard assignment.
BOOST_UBLAS_INLINE size_type size() const
Return the size of the vector.
BOOST_UBLAS_INLINE void clear()
Clear the vector, i.e. set all values to the zero value.
BOOST_UBLAS_INLINE friend void swap(NumPy1DArray &v1, NumPy1DArray &v2)
Swap the content of two vectors.
void incref()
Increments the reference count using Py_XINCREF.
BOOST_UBLAS_INLINE void swap(NumPy1DArray &rhs)
Swap the content of the vector with another vector.
BOOST_UBLAS_INLINE iterator find(size_type i)
Return an iterator to the element i.
BOOST_UBLAS_INLINE const_reference operator[](size_type i) const
Return a const reference to the element .
BOOST_UBLAS_INLINE pointer find_element(size_type i)
Return a pointer to the element .
BOOST_UBLAS_INLINE NumPy1DArray & assign_temporary(NumPy1DArray &rhs)
Assign a full vector (RHS-vector) to the current vector (LHS-vector).
PyObject * getPyObject() const
Returns the underlying PyObject.
BOOST_UBLAS_INLINE NumPy1DArray & operator/=(const AT &at)
Assign the division of the vector by a scalar to the vector.
BOOST_UBLAS_INLINE iterator end()
Return an iterator at the end of the vector.
BOOST_UBLAS_INLINE NumPy1DArray & minus_assign(const vector_expression< AE > &ae)
Assign the difference of the vector and a vector_expression to the vector.
BOOST_UBLAS_INLINE reverse_iterator rend()
Return a const reverse iterator on the end of the reverse vector (i.e. first element of the normal ve...
BOOST_UBLAS_INLINE iterator begin()
Return an iterator on the first element of the vector.
BOOST_UBLAS_INLINE const_reverse_iterator rbegin() const
Return a const reverse iterator before the first element of the reversed vector (i.e. end() of normal vector)
BOOST_UBLAS_INLINE const_reverse_iterator rend() const
Return a const reverse iterator on the end of the reverse vector (i.e. first element of the normal ve...
void decref()
Decrements the reference count using Py_XDECREF.
BOOST_UBLAS_INLINE reverse_iterator rbegin()
Return a const reverse iterator before the first element of the reversed vector (i.e. end() of normal vector)
BOOST_UBLAS_INLINE const_iterator begin() const
return an iterator on the first element of the vector
Provides traits for a specific C/C++ datatype.
This is a simple wrapper around the PyObject* datatype.