array: implement array resize
diff --git a/tests/test_numpy_array.cpp b/tests/test_numpy_array.cpp
index 269f18b..85c185f 100644
--- a/tests/test_numpy_array.cpp
+++ b/tests/test_numpy_array.cpp
@@ -273,4 +273,25 @@
sm.def("array_initializer_list", []() { return py::array_t<float>({ 1, 2 }); });
sm.def("array_initializer_list", []() { return py::array_t<float>({ 1, 2, 3 }); });
sm.def("array_initializer_list", []() { return py::array_t<float>({ 1, 2, 3, 4 }); });
-});
+
+ // reshape array to 2D without changing size
+ sm.def("array_reshape2", [](py::array_t<double> a) {
+ const size_t dim_sz = (size_t)std::sqrt(a.size());
+ if (dim_sz * dim_sz != a.size())
+ throw std::domain_error("array_reshape2: input array total size is not a squared integer");
+ a.resize({dim_sz, dim_sz});
+ });
+
+ // resize to 3D array with each dimension = N
+ sm.def("array_resize3", [](py::array_t<double> a, size_t N, bool refcheck) {
+ a.resize({N, N, N}, refcheck);
+ });
+
+ // return 2D array with Nrows = Ncols = N
+ sm.def("create_and_resize", [](size_t N) {
+ py::array_t<double> a;
+ a.resize({N, N});
+ std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.);
+ return a;
+ });
+});
\ No newline at end of file
diff --git a/tests/test_numpy_array.py b/tests/test_numpy_array.py
index 6281fa4..10af748 100644
--- a/tests/test_numpy_array.py
+++ b/tests/test_numpy_array.py
@@ -389,3 +389,38 @@
with pytest.raises(ValueError) as excinfo:
array_t_fail_test()
assert str(excinfo.value) == 'cannot create a pybind11::array_t from a nullptr'
+
+
+def test_array_resize(msg):
+ from pybind11_tests.array import (array_reshape2, array_resize3)
+
+ a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='float64')
+ array_reshape2(a)
+ assert(a.size == 9)
+ assert(np.all(a == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
+
+ # total size change should succced with refcheck off
+ array_resize3(a, 4, False)
+ assert(a.size == 64)
+ # ... and fail with refcheck on
+ try:
+ array_resize3(a, 3, True)
+ except ValueError as e:
+ assert(str(e).startswith("cannot resize an array"))
+ # transposed array doesn't own data
+ b = a.transpose()
+ try:
+ array_resize3(b, 3, False)
+ except ValueError as e:
+ assert(str(e).startswith("cannot resize this array: it does not own its data"))
+ # ... but reshape should be fine
+ array_reshape2(b)
+ assert(b.shape == (8, 8))
+
+
+@pytest.unsupported_on_pypy
+def test_array_create_and_resize(msg):
+ from pybind11_tests.array import create_and_resize
+ a = create_and_resize(2)
+ assert(a.size == 4)
+ assert(np.all(a == 42.))