| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,3 +29,6 @@ Defined in ``xtensor/xsort.hpp`` | |||
| 29 | 29 | ||
| 30 | 30 | .. doxygenfunction:: xt::argmax(const xexpression<E>&, std::size_t) | |
| 31 | 31 | :project: xtensor | |
| 32 | + | ||
| 33 | + .. doxygenfunction:: xt::unique(const xexpression<E>&) | ||
| 34 | + :project: xtensor | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,6 +34,9 @@ Defined in ``xtensor/xstrided_view.hpp`` | |||
| 34 | 34 | .. doxygenfunction:: xt::flatten | |
| 35 | 35 | :project: xtensor | |
| 36 | 36 | ||
| 37 | + .. doxygenfunction:: xt::trim_zeros | ||
| 38 | + :project: xtensor | ||
| 39 | + | ||
| 37 | 40 | .. doxygenfunction:: xt::squeeze(E&&) | |
| 38 | 41 | :project: xtensor | |
| 39 | 42 | ||
@@ -56,4 +59,4 @@ Defined in ``xtensor/xstrided_view.hpp`` | |||
| 56 | 59 | :project: xtensor | |
| 57 | 60 | ||
| 58 | 61 | .. doxygenfunction:: xt::atleast_3d | |
| 59 | - :project: xtensor | ||
| 62 | + :project: xtensor | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,3 +29,6 @@ Defined in ``xtensor/xslice.hpp`` | |||
| 29 | 29 | ||
| 30 | 30 | .. doxygenfunction:: xt::newaxis | |
| 31 | 31 | :project: xtensor | |
| 32 | + | ||
| 33 | + .. doxygenfunction:: xt::ellipsis | ||
| 34 | + :project: xtensor | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -132,6 +132,7 @@ or temporary variables are created. | |||
| 132 | 132 | | | ``a[:, np.newaxis]`` | | ``xt::view(a, xt::all(), xt::newaxis())`` | | |
| 133 | 133 | | | ``a[:5, 1:]`` | | ``xt::view(a, xt::range(_, 5), xt::range(1, _))`` | | |
| 134 | 134 | | | ``a[5:1:-1, :]`` | | ``xt::view(a, xt::range(5, 1, -1), xt::all())`` | | |
| 135 | + | | ``a[..., 3]`` | | ``xt::dynamic_view(a, {xt::ellipsis, 3})`` | | ||
| 135 | 136 | +-----------------------------------------------------+-----------------------------------------------------+ | |
| 136 | 137 | | ``np.broadcast(a, [4, 5, 7])`` | ``xt::broadcast(a, {4, 5, 7})`` | | |
| 137 | 138 | +-----------------------------------------------------+-----------------------------------------------------+ | |
@@ -283,6 +284,8 @@ Minimum, Maximum, Sorting | |||
| 283 | 284 | +--------------------------------------------+-----------------------------------------------+ | |
| 284 | 285 | | ``np.sort(a, axis=1)`` | ``xt::sort(a, axis=1)`` | | |
| 285 | 286 | +--------------------------------------------+-----------------------------------------------+ | |
| 287 | + | ``np.unique(a)`` | ``xt::unique(a)`` | | ||
| 288 | + +--------------------------------------------+-----------------------------------------------+ | ||
| 286 | 289 | ||
| 287 | 290 | Complex numbers | |
| 288 | 291 | --------------- | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -92,7 +92,7 @@ namespace xt | |||
| 92 | 92 | ||
| 93 | 93 | /** | |
| 94 | 94 | * 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*. | ||
| 96 | 96 | * | |
| 97 | 97 | * @param e the xexpression from which to extract shape, value type and layout. | |
| 98 | 98 | */ | |
@@ -104,10 +104,11 @@ namespace xt | |||
| 104 | 104 | } | |
| 105 | 105 | ||
| 106 | 106 | /** | |
| 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*. | ||
| 109 | 109 | * | |
| 110 | 110 | * @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. | ||
| 111 | 112 | */ | |
| 112 | 113 | template <class E> | |
| 113 | 114 | inline typename E::temporary_type full_like(const xexpression<E>& e, typename E::value_type fill_value) | |
@@ -117,9 +118,11 @@ namespace xt | |||
| 117 | 118 | } | |
| 118 | 119 | ||
| 119 | 120 | /** | |
| 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. | ||
| 123 | 126 | * | |
| 124 | 127 | * @param e the xexpression from which to extract shape, value type and layout. | |
| 125 | 128 | */ | |
@@ -130,9 +133,11 @@ namespace xt | |||
| 130 | 133 | } | |
| 131 | 134 | ||
| 132 | 135 | /** | |
| 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. | ||
| 136 | 141 | * | |
| 137 | 142 | * @param e the xexpression from which to extract shape, value type and layout. | |
| 138 | 143 | */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -152,16 +152,57 @@ namespace xt | |||
| 152 | 152 | /** | |
| 153 | 153 | * Returns a slice representing a full dimension, | |
| 154 | 154 | * to be used as an argument of view function. | |
| 155 | - * @sa view | ||
| 155 | + * @sa view, dynamic_view | ||
| 156 | 156 | */ | |
| 157 | 157 | inline auto all() noexcept | |
| 158 | 158 | { | |
| 159 | 159 | return xall_tag(); | |
| 160 | 160 | } | |
| 161 | 161 | ||
| 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 | + | ||
| 162 | 203 | /************************ | |
| 163 | - * xnewaxis declaration * | ||
| 164 | - ************************/ | ||
| 204 | + * xnewaxis declaration * | ||
| 205 | + ************************/ | ||
| 165 | 206 | ||
| 166 | 207 | template <class T> | |
| 167 | 208 | class xnewaxis : public xslice<xnewaxis<T>> | |
@@ -183,10 +224,10 @@ namespace xt | |||
| 183 | 224 | }; | |
| 184 | 225 | ||
| 185 | 226 | /** | |
| 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 | + */ | ||
| 190 | 231 | inline auto newaxis() noexcept | |
| 191 | 232 | { | |
| 192 | 233 | return xnewaxis_tag(); | |
@@ -285,12 +326,37 @@ namespace xt | |||
| 285 | 326 | C m_step; | |
| 286 | 327 | }; | |
| 287 | 328 | ||
| 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 | + */ | ||
| 288 | 343 | template <class A, class B> | |
| 289 | 344 | inline auto range(A min_val, B max_val) | |
| 290 | 345 | { | |
| 291 | 346 | return xrange_adaptor<A, B, placeholders::xtuph>(min_val, max_val, placeholders::xtuph()); | |
| 292 | 347 | } | |
| 293 | 348 | ||
| 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 | + */ | ||
| 294 | 360 | template <class A, class B, class C> | |
| 295 | 361 | inline auto range(A min_val, B max_val, C step) | |
| 296 | 362 | { | |
@@ -363,6 +429,12 @@ namespace xt | |||
| 363 | 429 | return xall<typename E::size_type>(e.shape()[index]); | |
| 364 | 430 | } | |
| 365 | 431 | ||
| 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 | + | ||
| 366 | 438 | template <class E> | |
| 367 | 439 | inline auto get_slice_implementation(E& /*e*/, xnewaxis_tag, std::size_t /*index*/) | |
| 368 | 440 | { | |
@@ -375,7 +447,6 @@ namespace xt | |||
| 375 | 447 | return adaptor.get(e.shape()[index]); | |
| 376 | 448 | } | |
| 377 | 449 | ||
| 378 | - | ||
| 379 | 450 | /****************************** | |
| 380 | 451 | * homogeneous get_slice_type * | |
| 381 | 452 | ******************************/ | |
@@ -394,6 +465,12 @@ namespace xt | |||
| 394 | 465 | using type = xall<typename E::size_type>; | |
| 395 | 466 | }; | |
| 396 | 467 | ||
| 468 | + template <class E> | ||
| 469 | + struct get_slice_type_impl<E, xellipsis_tag> | ||
| 470 | + { | ||
| 471 | + using type = xellipsis<typename E::size_type>; | ||
| 472 | + }; | ||
| 473 | + | ||
| 397 | 474 | template <class E> | |
| 398 | 475 | struct get_slice_type_impl<E, xnewaxis_tag> | |
| 399 | 476 | { | |
@@ -510,9 +587,31 @@ namespace xt | |||
| 510 | 587 | return 1; | |
| 511 | 588 | } | |
| 512 | 589 | ||
| 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 | + | ||
| 513 | 612 | /*************************** | |
| 514 | - * xnewaxis implementation * | ||
| 515 | - ***************************/ | ||
| 613 | + * xnewaxis implementation * | ||
| 614 | + ***************************/ | ||
| 516 | 615 | ||
| 517 | 616 | template <class T> | |
| 518 | 617 | inline auto xnewaxis<T>::operator()(size_type) const noexcept -> size_type | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -257,7 +257,7 @@ namespace xt | |||
| 257 | 257 | /** | |
| 258 | 258 | * Find position of minimal value in xexpression | |
| 259 | 259 | * | |
| 260 | - * @param a xexpression to compute argmin on | ||
| 260 | + * @param e input xexpression | ||
| 261 | 261 | * @param axis select axis (or none) | |
| 262 | 262 | * | |
| 263 | 263 | * @return returns xarray with positions of minimal value | |
@@ -281,7 +281,7 @@ namespace xt | |||
| 281 | 281 | /** | |
| 282 | 282 | * Find position of maximal value in xexpression | |
| 283 | 283 | * | |
| 284 | - * @param a xexpression to compute argmin on | ||
| 284 | + * @param e input xexpression | ||
| 285 | 285 | * @param axis select axis (or none) | |
| 286 | 286 | * | |
| 287 | 287 | * @return returns xarray with positions of minimal value | |
@@ -293,6 +293,24 @@ namespace xt | |||
| 293 | 293 | auto&& ed = eval(e.derived_cast()); | |
| 294 | 294 | return detail::arg_func_impl(ed, axis, std::greater<value_type>()); | |
| 295 | 295 | } | |
| 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 | + } | ||
| 296 | 314 | } | |
| 297 | 315 | ||
| 298 | 316 | #endif | |
| Back | FazBrowse Home | New Git URL |
0 commit comments