FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

initial ellipsis implementation (#689) · xtensor-stack/xtensor@3ece513 · GitHub

Repository navigation

Commit 3ece513

Browse files
authored andcommitted
initial ellipsis implementation (#689)
* initial ellipsis implementation * fix up docs
1 parent 5395385 commit 3ece513

11 files changed

Lines changed: 348 additions & 50 deletions

File tree

‎docs/source/api/xsort.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ Defined in ``xtensor/xsort.hpp``
2929

3030
.. doxygenfunction:: xt::argmax(const xexpression<E>&, std::size_t)
3131
:project: xtensor
32+
33+
.. doxygenfunction:: xt::unique(const xexpression<E>&)
34+
:project: xtensor

‎docs/source/api/xstrided_view.rst‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Defined in ``xtensor/xstrided_view.hpp``
3434
.. doxygenfunction:: xt::flatten
3535
:project: xtensor
3636

37+
.. doxygenfunction:: xt::trim_zeros
38+
:project: xtensor
39+
3740
.. doxygenfunction:: xt::squeeze(E&&)
3841
:project: xtensor
3942

@@ -56,4 +59,4 @@ Defined in ``xtensor/xstrided_view.hpp``
5659
:project: xtensor
5760

5861
.. doxygenfunction:: xt::atleast_3d
59-
:project: xtensor
62+
:project: xtensor

‎docs/source/api/xview.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ Defined in ``xtensor/xslice.hpp``
2929

3030
.. doxygenfunction:: xt::newaxis
3131
:project: xtensor
32+
33+
.. doxygenfunction:: xt::ellipsis
34+
:project: xtensor

‎docs/source/numpy.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ or temporary variables are created.
132132
| | ``a[:, np.newaxis]`` | | ``xt::view(a, xt::all(), xt::newaxis())`` |
133133
| | ``a[:5, 1:]`` | | ``xt::view(a, xt::range(_, 5), xt::range(1, _))`` |
134134
| | ``a[5:1:-1, :]`` | | ``xt::view(a, xt::range(5, 1, -1), xt::all())`` |
135+
| | ``a[..., 3]`` | | ``xt::dynamic_view(a, {xt::ellipsis, 3})`` |
135136
+-----------------------------------------------------+-----------------------------------------------------+
136137
| ``np.broadcast(a, [4, 5, 7])`` | ``xt::broadcast(a, {4, 5, 7})`` |
137138
+-----------------------------------------------------+-----------------------------------------------------+
@@ -283,6 +284,8 @@ Minimum, Maximum, Sorting
283284
+--------------------------------------------+-----------------------------------------------+
284285
| ``np.sort(a, axis=1)`` | ``xt::sort(a, axis=1)`` |
285286
+--------------------------------------------+-----------------------------------------------+
287+
| ``np.unique(a)`` | ``xt::unique(a)`` |
288+
+--------------------------------------------+-----------------------------------------------+
286289

287290
Complex numbers
288291
---------------

‎include/xtensor/xbuilder.hpp‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace xt
9292

9393
/**
9494
* Create a xcontainer (xarray, xtensor or xtensorf) with uninitialized values of
95-
* the same shape, value type and layout as @emph e.
95+
* the same shape, value type and layout as the input xexpression *e*.
9696
*
9797
* @param e the xexpression from which to extract shape, value type and layout.
9898
*/
@@ -104,10 +104,11 @@ namespace xt
104104
}
105105

106106
/**
107-
* Create a xcontainer (xarray, xtensor or xtensorf), filled with @emph fill_value and of
108-
* the same shape, value type and layout as @emph e.
107+
* Create a xcontainer (xarray, xtensor or xtensorf), filled with *fill_value* and of
108+
* the same shape, value type and layout as the input xexpression *e*.
109109
*
110110
* @param e the xexpression from which to extract shape, value type and layout.
111+
* @param fill_value the value used to set each element of the returned xcontainer.
111112
*/
112113
template <class E>
113114
inline typename E::temporary_type full_like(const xexpression<E>& e, typename E::value_type fill_value)
@@ -117,9 +118,11 @@ namespace xt
117118
}
118119

119120
/**
120-
* Create a xcontainer (xarray, xtensor or xtensorf), filled with @emph zeros and of
121-
* the same shape, value type and layout as @emph e. Note: contrary to zeros(shape), this function
122-
* returns a non-lazy, evaluated container! Use ``xt::zeros<double>(e.shape());` for a lazy version.
121+
* Create a xcontainer (xarray, xtensor or xtensorf), filled with zeros and of
122+
* the same shape, value type and layout as the input xexpression *e*.
123+
*
124+
* Note: contrary to zeros(shape), this function returns a non-lazy, allocated container!
125+
* Use ``xt::zeros<double>(e.shape());` for a lazy version.
123126
*
124127
* @param e the xexpression from which to extract shape, value type and layout.
125128
*/
@@ -130,9 +133,11 @@ namespace xt
130133
}
131134

132135
/**
133-
* Create a xcontainer (xarray, xtensor or xtensorf), filled with @emph ones and of
134-
* the same shape, value type and layout as @emph e. Note: contrary to ones(shape), this function
135-
* returns a non-lazy, evaluated container! Use ``xt::ones<double>(e.shape());` for a lazy version.
136+
* Create a xcontainer (xarray, xtensor or xtensorf), filled with ones and of
137+
* the same shape, value type and layout as the input xexpression *e*.
138+
*
139+
* Note: contrary to ones(shape), this function returns a non-lazy, evaluated container!
140+
* Use ``xt::ones<double>(e.shape());`` for a lazy version.
136141
*
137142
* @param e the xexpression from which to extract shape, value type and layout.
138143
*/

‎include/xtensor/xslice.hpp‎

Lines changed: 109 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,57 @@ namespace xt
152152
/**
153153
* Returns a slice representing a full dimension,
154154
* to be used as an argument of view function.
155-
* @sa view
155+
* @sa view, dynamic_view
156156
*/
157157
inline auto all() noexcept
158158
{
159159
return xall_tag();
160160
}
161161

162+
template <class T>
163+
class xellipsis : public xslice<xellipsis<T>>
164+
{
165+
public:
166+
167+
using size_type = T;
168+
169+
xellipsis() = default;
170+
171+
size_type operator()(size_type i) const noexcept;
172+
173+
size_type size() const noexcept;
174+
size_type step_size() const noexcept;
175+
176+
};
177+
178+
struct xellipsis_tag
179+
{
180+
};
181+
182+
/**
183+
* Returns a slice representing all remaining dimensions,
184+
* and selecting all in these dimensions. Ellipsis will expand
185+
* to a series of `all()` slices, until the number of slices is
186+
* equal to the number of dimensions of the source array.
187+
*
188+
* Note: ellipsis can only be used in dynamic_view!
189+
*
190+
* \code{.cpp}
191+
* xarray<double> a = xarray<double>::from_shape({5, 5, 1, 1, 5});
192+
* auto v = xt::dynamic_view(a, {2, xt::ellipsis(), 2});
193+
* // equivalent to using {2, xt::all(), xt::all(), xt::all(), 2};
194+
* \endcode
195+
*
196+
* @sa dynamic_view
197+
*/
198+
inline auto ellipsis() noexcept
199+
{
200+
return xellipsis_tag();
201+
}
202+
162203
/************************
163-
* xnewaxis declaration *
164-
************************/
204+
* xnewaxis declaration *
205+
************************/
165206

166207
template <class T>
167208
class xnewaxis : public xslice<xnewaxis<T>>
@@ -183,10 +224,10 @@ namespace xt
183224
};
184225

185226
/**
186-
* Returns a slice representing a new axis of length one,
187-
* to be used as an argument of view function.
188-
* @sa view
189-
*/
227+
* Returns a slice representing a new axis of length one,
228+
* to be used as an argument of view function.
229+
* @sa view, dynamic_view
230+
*/
190231
inline auto newaxis() noexcept
191232
{
192233
return xnewaxis_tag();
@@ -285,12 +326,37 @@ namespace xt
285326
C m_step;
286327
};
287328

329+
/**
330+
* Select a range from min_val to max_val.
331+
* You can use the shorthand `_` syntax to select from the start or until the end.
332+
*
333+
* \code{.cpp}
334+
* using namespace xt::placeholders; // to enable _ syntax
335+
*
336+
* range(3, _) // select from index 3 to the end
337+
* range(_, 5) // select from index o to 5
338+
* range(_, _) // equivalent to `all()`
339+
* \endcode
340+
*
341+
* @sa view, dynamic_view
342+
*/
288343
template <class A, class B>
289344
inline auto range(A min_val, B max_val)
290345
{
291346
return xrange_adaptor<A, B, placeholders::xtuph>(min_val, max_val, placeholders::xtuph());
292347
}
293348

349+
/**
350+
* Select a range from min_val to max_val with step
351+
* You can use the shorthand `_` syntax to select from the start or until the end.
352+
*
353+
* \code{.cpp}
354+
* using namespace xt::placeholders; // to enable _ syntax
355+
* range(3, _, 5) // select from index 3 to the end with stepsize 5
356+
* \endcode
357+
*
358+
* @sa view, dynamic_view
359+
*/
294360
template <class A, class B, class C>
295361
inline auto range(A min_val, B max_val, C step)
296362
{
@@ -363,6 +429,12 @@ namespace xt
363429
return xall<typename E::size_type>(e.shape()[index]);
364430
}
365431

432+
template <class E>
433+
inline auto get_slice_implementation(E& /*e*/, xellipsis_tag, std::size_t /*index*/)
434+
{
435+
return xellipsis<typename E::size_type>();
436+
}
437+
366438
template <class E>
367439
inline auto get_slice_implementation(E& /*e*/, xnewaxis_tag, std::size_t /*index*/)
368440
{
@@ -375,7 +447,6 @@ namespace xt
375447
return adaptor.get(e.shape()[index]);
376448
}
377449

378-
379450
/******************************
380451
* homogeneous get_slice_type *
381452
******************************/
@@ -394,6 +465,12 @@ namespace xt
394465
using type = xall<typename E::size_type>;
395466
};
396467

468+
template <class E>
469+
struct get_slice_type_impl<E, xellipsis_tag>
470+
{
471+
using type = xellipsis<typename E::size_type>;
472+
};
473+
397474
template <class E>
398475
struct get_slice_type_impl<E, xnewaxis_tag>
399476
{
@@ -510,9 +587,31 @@ namespace xt
510587
return 1;
511588
}
512589

590+
/****************************
591+
* xellipsis implementation *
592+
****************************/
593+
594+
template <class T>
595+
inline auto xellipsis<T>::operator()(size_type /*i*/) const noexcept -> size_type
596+
{
597+
return 0;
598+
}
599+
600+
template <class T>
601+
inline auto xellipsis<T>::size() const noexcept -> size_type
602+
{
603+
return 0;
604+
}
605+
606+
template <class T>
607+
inline auto xellipsis<T>::step_size() const noexcept -> size_type
608+
{
609+
return 1;
610+
}
611+
513612
/***************************
514-
* xnewaxis implementation *
515-
***************************/
613+
* xnewaxis implementation *
614+
***************************/
516615

517616
template <class T>
518617
inline auto xnewaxis<T>::operator()(size_type) const noexcept -> size_type

‎include/xtensor/xsort.hpp‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ namespace xt
257257
/**
258258
* Find position of minimal value in xexpression
259259
*
260-
* @param a xexpression to compute argmin on
260+
* @param e input xexpression
261261
* @param axis select axis (or none)
262262
*
263263
* @return returns xarray with positions of minimal value
@@ -281,7 +281,7 @@ namespace xt
281281
/**
282282
* Find position of maximal value in xexpression
283283
*
284-
* @param a xexpression to compute argmin on
284+
* @param e input xexpression
285285
* @param axis select axis (or none)
286286
*
287287
* @return returns xarray with positions of minimal value
@@ -293,6 +293,24 @@ namespace xt
293293
auto&& ed = eval(e.derived_cast());
294294
return detail::arg_func_impl(ed, axis, std::greater<value_type>());
295295
}
296+
297+
/**
298+
* Find unique elements of a xexpression. This returns a flattened xtensor with
299+
* sorted, unique elements from the original expression.
300+
*
301+
* @param e input xexpression (will be flattened)
302+
*/
303+
template <class E>
304+
auto unique(const xexpression<E>& e)
305+
{
306+
auto sorted = sort(e, xnone());
307+
auto end = std::unique(sorted.begin(), sorted.end());
308+
std::size_t sz = static_cast<std::size_t>(std::distance(sorted.begin(), end));
309+
// TODO check if we can shrink the vector without reallocation
310+
auto result = xtensor<typename E::value_type, 1>::from_shape({ sz });
311+
std::copy(sorted.begin(), end, result.begin());
312+
return result;
313+
}
296314
}
297315

298316
#endif

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL