Skip to content

Commit 7b6860b

Browse files
committed
fix resize for 0.15
1 parent d2668d3 commit 7b6860b

File tree

9 files changed

+114
-60
lines changed

9 files changed

+114
-60
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ install:
2323
- conda update -q conda
2424
- conda info -a
2525
- conda install gtest cmake -c conda-forge
26-
- conda install xtensor==0.14.0 pytest numpy pybind11==2.2.1 -c conda-forge
26+
- conda install xtensor==0.15.0 pytest numpy pybind11==2.2.1 -c conda-forge
2727
- "set PYTHONHOME=%MINICONDA%"
2828
- cmake -G "NMake Makefiles" -D CMAKE_INSTALL_PREFIX=%MINICONDA%\\Library -D BUILD_TESTS=ON -D PYTHON_EXECUTABLE=%MINICONDA%\\python.exe .
2929
- nmake test_xtensor_python

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ install:
9595
- conda update -q conda
9696
# Useful for debugging any issues with conda
9797
- conda info -a
98-
- conda install xtensor==0.14.0 pytest numpy pybind11==2.2.1 -c conda-forge
98+
- conda install xtensor==0.15.0 pytest numpy pybind11==2.2.1 -c conda-forge
9999
- conda install cmake gtest -c conda-forge
100100
- cmake -D BUILD_TESTS=ON -D CMAKE_INSTALL_PREFIX=$HOME/miniconda .
101101
- make -j2 test_xtensor_python

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ from the `docs` subdirectory.
187187

188188
| `xtensor-python` | `xtensor` | `pybind11` |
189189
|------------------|-----------|------------------|
190-
| master | ^0.14.0 | ~2.1.0 or ~2.2.1 |
190+
| master | ^0.15.0 | ~2.1.0 or ~2.2.1 |
191191
| 0.16.x | ^0.14.0 | ~2.1.0 or ~2.2.1 |
192192
| 0.15.x | ^0.13.1 | ~2.1.0 or ~2.2.1 |
193193
| 0.14.x | ^0.12.0 | ~2.1.0 or ~2.2.1 |

include/xtensor-python/pyarray.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,47 +486,47 @@ namespace xt
486486
inline pyarray<T>::pyarray(const value_type& t)
487487
: base_type()
488488
{
489-
base_type::reshape(xt::shape<shape_type>(t), layout_type::row_major);
489+
base_type::resize(xt::shape<shape_type>(t), layout_type::row_major);
490490
nested_copy(m_data.begin(), t);
491491
}
492492

493493
template <class T>
494494
inline pyarray<T>::pyarray(nested_initializer_list_t<T, 1> t)
495495
: base_type()
496496
{
497-
base_type::reshape(xt::shape<shape_type>(t), layout_type::row_major);
497+
base_type::resize(xt::shape<shape_type>(t), layout_type::row_major);
498498
nested_copy(m_data.begin(), t);
499499
}
500500

501501
template <class T>
502502
inline pyarray<T>::pyarray(nested_initializer_list_t<T, 2> t)
503503
: base_type()
504504
{
505-
base_type::reshape(xt::shape<shape_type>(t), layout_type::row_major);
505+
base_type::resize(xt::shape<shape_type>(t), layout_type::row_major);
506506
nested_copy(m_data.begin(), t);
507507
}
508508

509509
template <class T>
510510
inline pyarray<T>::pyarray(nested_initializer_list_t<T, 3> t)
511511
: base_type()
512512
{
513-
base_type::reshape(xt::shape<shape_type>(t), layout_type::row_major);
513+
base_type::resize(xt::shape<shape_type>(t), layout_type::row_major);
514514
nested_copy(m_data.begin(), t);
515515
}
516516

517517
template <class T>
518518
inline pyarray<T>::pyarray(nested_initializer_list_t<T, 4> t)
519519
: base_type()
520520
{
521-
base_type::reshape(xt::shape<shape_type>(t), layout_type::row_major);
521+
base_type::resize(xt::shape<shape_type>(t), layout_type::row_major);
522522
nested_copy(m_data.begin(), t);
523523
}
524524

525525
template <class T>
526526
inline pyarray<T>::pyarray(nested_initializer_list_t<T, 5> t)
527527
: base_type()
528528
{
529-
base_type::reshape(xt::shape<shape_type>(t), layout_type::row_major);
529+
base_type::resize(xt::shape<shape_type>(t), layout_type::row_major);
530530
nested_copy(m_data.begin(), t);
531531
}
532532

include/xtensor-python/pycontainer.hpp

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@ namespace xt
8585
static constexpr bool contiguous_layout = false;
8686

8787
template <class S = shape_type>
88-
void reshape(const S& shape);
88+
void resize(const S& shape);
8989
template <class S = shape_type>
90-
void reshape(const S& shape, layout_type l);
90+
void resize(const S& shape, layout_type l);
9191
template <class S = shape_type>
92-
void reshape(const S& shape, const strides_type& strides);
92+
void resize(const S& shape, const strides_type& strides);
93+
94+
template <class S = shape_type>
95+
void reshape(S&& shape, layout_type layout = base_type::static_layout);
9396

9497
layout_type layout() const;
9598

@@ -258,46 +261,79 @@ namespace xt
258261
}
259262

260263
/**
261-
* Reshapes the container.
264+
* resizes the container.
262265
* @param shape the new shape
263266
*/
264267
template <class D>
265268
template <class S>
266-
inline void pycontainer<D>::reshape(const S& shape)
269+
inline void pycontainer<D>::resize(const S& shape)
267270
{
268271
if (shape.size() != this->dimension() || !std::equal(std::begin(shape), std::end(shape), std::begin(this->shape())))
269272
{
270-
reshape(shape, layout_type::row_major);
273+
resize(shape, layout_type::row_major);
271274
}
272275
}
273276

274277
/**
275-
* Reshapes the container.
278+
* resizes the container.
276279
* @param shape the new shape
277280
* @param l the new layout
278281
*/
279282
template <class D>
280283
template <class S>
281-
inline void pycontainer<D>::reshape(const S& shape, layout_type l)
284+
inline void pycontainer<D>::resize(const S& shape, layout_type l)
282285
{
283286
strides_type strides = xtl::make_sequence<strides_type>(shape.size(), size_type(1));
284287
compute_strides(shape, l, strides);
285-
reshape(shape, strides);
288+
resize(shape, strides);
286289
}
287290

288291
/**
289-
* Reshapes the container.
292+
* resizes the container.
290293
* @param shape the new shape
291294
* @param strides the new strides
292295
*/
293296
template <class D>
294297
template <class S>
295-
inline void pycontainer<D>::reshape(const S& shape, const strides_type& strides)
298+
inline void pycontainer<D>::resize(const S& shape, const strides_type& strides)
296299
{
297300
derived_type tmp(xtl::forward_sequence<shape_type>(shape), strides);
298301
*static_cast<derived_type*>(this) = std::move(tmp);
299302
}
300303

304+
template <class D>
305+
template <class S>
306+
inline void pycontainer<D>::reshape(S&& shape, layout_type layout)
307+
{
308+
if (compute_size(shape) != this->size())
309+
{
310+
throw std::runtime_error("Cannot reshape with incorrect number of elements.");
311+
}
312+
313+
if (layout == layout_type::dynamic || layout == layout_type::any)
314+
{
315+
layout = DEFAULT_LAYOUT;
316+
}
317+
318+
NPY_ORDER npy_layout;
319+
if (layout == layout_type::row_major)
320+
{
321+
npy_layout = NPY_CORDER;
322+
}
323+
else if (layout == layout_type::column_major)
324+
{
325+
npy_layout = NPY_FORTRANORDER;
326+
}
327+
else
328+
{
329+
throw std::runtime_error("Cannot reshape with unknown layout_type.");
330+
}
331+
332+
std::vector<npy_intp> int_shape(shape.begin(), shape.end());
333+
PyArray_Dims dims({int_shape.data(), static_cast<int>(shape.size())});
334+
PyArray_Newshape((PyArrayObject*) this->ptr(), &dims, npy_layout);
335+
}
336+
301337
/**
302338
* Return the layout_type of the container
303339
* @return layout_type of the container

include/xtensor-python/pytensor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ namespace xt
237237
inline pytensor<T, N>::pytensor(nested_initializer_list_t<T, N> t)
238238
: base_type()
239239
{
240-
base_type::reshape(xt::shape<shape_type>(t), layout_type::row_major);
240+
base_type::resize(xt::shape<shape_type>(t), layout_type::row_major);
241241
nested_copy(m_data.begin(), t);
242242
}
243243

0 commit comments

Comments
 (0)