mirror of
https://github.com/boostorg/python.git
synced 2026-01-24 06:02:14 +00:00
ndarray.shape(k),strides(k) act more like their python counterparts (negative indexing, bounds checking) (issue #157)
This commit is contained in:
committed by
Stefan Seefeld
parent
00b7ed03a7
commit
ecf05c4a90
@@ -138,6 +138,30 @@ ndarray from_data_impl(void * data,
|
||||
|
||||
} // namespace detail
|
||||
|
||||
namespace {
|
||||
int normalize_index(int n,int nlim) // wraps [-nlim:nlim) into [0:nlim), throw IndexError otherwise
|
||||
{
|
||||
if (n<0)
|
||||
n += nlim; // negative indices work backwards from end
|
||||
if (n < 0 || n >= nlim)
|
||||
{
|
||||
PyErr_SetObject(PyExc_IndexError, Py_None);
|
||||
throw_error_already_set();
|
||||
}
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
Py_intptr_t ndarray::shape(int n) const
|
||||
{
|
||||
return get_shape()[normalize_index(n,get_nd())];
|
||||
}
|
||||
|
||||
Py_intptr_t ndarray::strides(int n) const
|
||||
{
|
||||
return get_strides()[normalize_index(n,get_nd())];
|
||||
}
|
||||
|
||||
ndarray ndarray::view(dtype const & dt) const
|
||||
{
|
||||
return ndarray(python::detail::new_reference
|
||||
|
||||
Reference in New Issue
Block a user