xtensor
 
Loading...
Searching...
No Matches
xfunction.hpp
1/***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3 * Copyright (c) QuantStack *
4 * *
5 * Distributed under the terms of the BSD 3-Clause License. *
6 * *
7 * The full license is in the file LICENSE, distributed with this software. *
8 ****************************************************************************/
9
10#ifndef XTENSOR_FUNCTION_HPP
11#define XTENSOR_FUNCTION_HPP
12
13#include <algorithm>
14#include <cstddef>
15#include <iterator>
16#include <tuple>
17#include <type_traits>
18#include <utility>
19
20#include <xtl/xsequence.hpp>
21#include <xtl/xtype_traits.hpp>
22
23#include "../containers/xscalar.hpp"
24#include "../core/xaccessible.hpp"
25#include "../core/xexpression_traits.hpp"
26#include "../core/xiterable.hpp"
27#include "../core/xiterator.hpp"
28#include "../core/xlayout.hpp"
29#include "../core/xshape.hpp"
30#include "../core/xstrides.hpp"
31#include "../utils/xtensor_simd.hpp"
32#include "../utils/xutils.hpp"
33
34namespace xt
35{
36 namespace detail
37 {
38
39 template <bool... B>
40 using conjunction_c = std::conjunction<std::integral_constant<bool, B>...>;
41
42 /************************
43 * xfunction_cache_impl *
44 ************************/
45
46 template <class S, class is_shape_trivial>
47 struct xfunction_cache_impl
48 {
49 S shape;
50 bool is_trivial;
51 bool is_initialized;
52
53 xfunction_cache_impl()
54 : shape(xtl::make_sequence<S>(0, std::size_t(0)))
55 , is_trivial(false)
56 , is_initialized(false)
57 {
58 }
59 };
60
61 template <std::size_t... N, class is_shape_trivial>
62 struct xfunction_cache_impl<fixed_shape<N...>, is_shape_trivial>
63 {
64 XTENSOR_CONSTEXPR_ENHANCED_STATIC fixed_shape<N...> shape = fixed_shape<N...>();
65 XTENSOR_CONSTEXPR_ENHANCED_STATIC bool is_trivial = is_shape_trivial::value;
66 XTENSOR_CONSTEXPR_ENHANCED_STATIC bool is_initialized = true;
67 };
68
69 template <class... CT>
70 struct xfunction_bool_load_type
71 {
72 using type = xtl::promote_type_t<typename std::decay_t<CT>::bool_load_type...>;
73 };
74
75 template <class CT>
76 struct xfunction_bool_load_type<CT>
77 {
78 using type = typename std::decay_t<CT>::bool_load_type;
79 };
80
81 template <class... CT>
82 using xfunction_bool_load_type_t = typename xfunction_bool_load_type<CT...>::type;
83 }
84
85 /************************
86 * xfunction extensions *
87 ************************/
88
89 namespace extension
90 {
91
92 template <class Tag, class F, class... CT>
94
95 template <class F, class... CT>
97 {
98 using type = xtensor_empty_base;
99 };
100
101 template <class F, class... CT>
102 struct xfunction_base : xfunction_base_impl<xexpression_tag_t<CT...>, F, CT...>
103 {
104 };
105
106 template <class F, class... CT>
107 using xfunction_base_t = typename xfunction_base<F, CT...>::type;
108 }
109
110 template <class promote>
111 struct xfunction_cache : detail::xfunction_cache_impl<typename promote::type, promote>
112 {
113 };
114
115 template <class F, class... CT>
116 class xfunction_iterator;
117
118 template <class F, class... CT>
119 class xfunction_stepper;
120
121 template <class F, class... CT>
122 class xfunction;
123
124 template <class F, class... CT>
126 {
127 using inner_shape_type = promote_shape_t<typename std::decay_t<CT>::shape_type...>;
128 using const_stepper = xfunction_stepper<F, CT...>;
129 using stepper = const_stepper;
130 };
131
132 template <class F, class... CT>
134 {
135 // Added indirection for MSVC 2017 bug with the operator value_type()
136 using func_return_type = typename meta_identity<
137 decltype(std::declval<F>()(std::declval<xvalue_type_t<std::decay_t<CT>>>()...))>::type;
138 using value_type = std::decay_t<func_return_type>;
139 using reference = func_return_type;
140 using const_reference = reference;
141 using size_type = common_size_type_t<std::decay_t<CT>...>;
142 };
143
144 template <class T, class F, class... CT>
145 struct has_simd_interface<xfunction<F, CT...>, T> : std::conjunction<
146 has_simd_type<T>,
147 has_simd_apply<F, xt_simd::simd_type<T>>,
148 has_simd_interface<std::decay_t<CT>, T>...>
149 {
150 };
151
152 /*************************************
153 * overlapping_memory_checker_traits *
154 *************************************/
155
156 template <class E>
158 E,
159 std::enable_if_t<!has_memory_address<E>::value && is_specialization_of<xfunction, E>::value>>
160 {
161 template <std::size_t I = 0, class... T, std::enable_if_t<(I == sizeof...(T)), int> = 0>
162 static bool check_tuple(const std::tuple<T...>&, const memory_range&)
163 {
164 return false;
165 }
166
167 template <std::size_t I = 0, class... T, std::enable_if_t<(I < sizeof...(T)), int> = 0>
168 static bool check_tuple(const std::tuple<T...>& t, const memory_range& dst_range)
169 {
170 using ChildE = std::decay_t<decltype(std::get<I>(t))>;
171 return overlapping_memory_checker_traits<ChildE>::check_overlap(std::get<I>(t), dst_range)
172 || check_tuple<I + 1>(t, dst_range);
173 }
174
175 static bool check_overlap(const E& expr, const memory_range& dst_range)
176 {
177 if (expr.size() == 0)
178 {
179 return false;
180 }
181 else
182 {
183 return check_tuple(expr.arguments(), dst_range);
184 }
185 }
186 };
187
188 /*************
189 * xfunction *
190 *************/
191
203 template <class F, class... CT>
204 class xfunction : private xconst_iterable<xfunction<F, CT...>>,
205 public xsharable_expression<xfunction<F, CT...>>,
206 private xconst_accessible<xfunction<F, CT...>>,
207 public extension::xfunction_base_t<F, CT...>
208 {
209 public:
210
211 using self_type = xfunction<F, CT...>;
212 using accessible_base = xconst_accessible<self_type>;
213 using extension_base = extension::xfunction_base_t<F, CT...>;
214 using expression_tag = typename extension_base::expression_tag;
215 using only_scalar = all_xscalar<CT...>;
216 using functor_type = typename std::remove_reference<F>::type;
217 using tuple_type = std::tuple<CT...>;
218
219 using inner_types = xcontainer_inner_types<self_type>;
220 using value_type = typename inner_types::value_type;
221 using reference = typename inner_types::reference;
222 using const_reference = typename inner_types::const_reference;
223 using pointer = value_type*;
224 using const_pointer = const value_type*;
225 using size_type = typename inner_types::size_type;
226 using difference_type = common_difference_type_t<std::decay_t<CT>...>;
227
228 using simd_value_type = xt_simd::simd_type<value_type>;
229
230 // xtl::promote_type_t<typename std::decay_t<CT>::bool_load_type...>;
231 using bool_load_type = detail::xfunction_bool_load_type_t<CT...>;
232
233 template <class requested_type>
234 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
235
236 using iterable_base = xconst_iterable<xfunction<F, CT...>>;
237 using inner_shape_type = typename iterable_base::inner_shape_type;
238 using shape_type = inner_shape_type;
239
240 using stepper = typename iterable_base::stepper;
241 using const_stepper = typename iterable_base::const_stepper;
242
243 static constexpr layout_type static_layout = compute_layout(std::decay_t<CT>::static_layout...);
244 static constexpr bool contiguous_layout = static_layout != layout_type::dynamic;
245
246 template <layout_type L>
247 using layout_iterator = typename iterable_base::template layout_iterator<L>;
248 template <layout_type L>
249 using const_layout_iterator = typename iterable_base::template const_layout_iterator<L>;
250 template <layout_type L>
251 using reverse_layout_iterator = typename iterable_base::template reverse_layout_iterator<L>;
252 template <layout_type L>
253 using const_reverse_layout_iterator = typename iterable_base::template const_reverse_layout_iterator<L>;
254
255 template <class S, layout_type L>
256 using broadcast_iterator = typename iterable_base::template broadcast_iterator<S, L>;
257 template <class S, layout_type L>
258 using const_broadcast_iterator = typename iterable_base::template const_broadcast_iterator<S, L>;
259 template <class S, layout_type L>
260 using reverse_broadcast_iterator = typename iterable_base::template reverse_broadcast_iterator<S, L>;
261 template <class S, layout_type L>
262 using const_reverse_broadcast_iterator = typename iterable_base::template const_reverse_broadcast_iterator<S, L>;
263
264 using const_linear_iterator = xfunction_iterator<F, CT...>;
265 using linear_iterator = const_linear_iterator;
266 using const_reverse_linear_iterator = std::reverse_iterator<const_linear_iterator>;
267 using reverse_linear_iterator = std::reverse_iterator<linear_iterator>;
268
269 using iterator = typename iterable_base::iterator;
270 using const_iterator = typename iterable_base::const_iterator;
271 using reverse_iterator = typename iterable_base::reverse_iterator;
272 using const_reverse_iterator = typename iterable_base::const_reverse_iterator;
273
274 template <class Func, class... CTA, class U = std::enable_if_t<!std::is_base_of<std::decay_t<Func>, self_type>::value>>
275 xfunction(Func&& f, CTA&&... e) noexcept;
276
277 template <class FA, class... CTA>
279
280 ~xfunction() = default;
281
282 xfunction(const xfunction&) = default;
283 xfunction& operator=(const xfunction&) = default;
284
285 xfunction(xfunction&&) = default;
286 xfunction& operator=(xfunction&&) = default;
287
289 size_type dimension() const noexcept;
290 const inner_shape_type& shape() const;
291 layout_type layout() const noexcept;
292 bool is_contiguous() const noexcept;
293 using accessible_base::shape;
294
295 template <class... Args>
296 const_reference operator()(Args... args) const;
297
298 template <class... Args>
299 const_reference unchecked(Args... args) const;
300
301 using accessible_base::at;
302 using accessible_base::operator[];
303 using accessible_base::back;
304 using accessible_base::front;
305 using accessible_base::in_bounds;
306 using accessible_base::periodic;
307
308 template <class It>
309 const_reference element(It first, It last) const;
310
311 template <class S>
312 bool broadcast_shape(S& shape, bool reuse_cache = false) const;
313
314 template <class S>
315 bool has_linear_assign(const S& strides) const noexcept;
316
317 using iterable_base::begin;
318 using iterable_base::cbegin;
319 using iterable_base::cend;
320 using iterable_base::crbegin;
321 using iterable_base::crend;
322 using iterable_base::end;
323 using iterable_base::rbegin;
324 using iterable_base::rend;
325
326 const_linear_iterator linear_begin() const noexcept;
327 const_linear_iterator linear_end() const noexcept;
328 const_linear_iterator linear_cbegin() const noexcept;
329 const_linear_iterator linear_cend() const noexcept;
330
331 const_reverse_linear_iterator linear_rbegin() const noexcept;
332 const_reverse_linear_iterator linear_rend() const noexcept;
333 const_reverse_linear_iterator linear_crbegin() const noexcept;
334 const_reverse_linear_iterator linear_crend() const noexcept;
335
336 template <class S>
337 const_stepper stepper_begin(const S& shape) const noexcept;
338 template <class S>
339 const_stepper stepper_end(const S& shape, layout_type l) const noexcept;
340
341 const_reference data_element(size_type i) const;
342
343 const_reference flat(size_type i) const;
344
345 template <class UT = self_type, class = typename std::enable_if<UT::only_scalar::value>::type>
346 operator value_type() const;
347
348 template <class align, class requested_type = value_type, std::size_t N = xt_simd::simd_traits<requested_type>::size>
349 simd_return_type<requested_type> load_simd(size_type i) const;
350
351 const tuple_type& arguments() const noexcept;
352
353 const functor_type& functor() const noexcept;
354
355 private:
356
357 template <class Func, std::size_t... I>
358 const_stepper build_stepper(Func&& f, std::index_sequence<I...>) const noexcept;
359
360 template <class Func, std::size_t... I>
361 auto build_iterator(Func&& f, std::index_sequence<I...>) const noexcept;
362
363 size_type compute_dimension() const noexcept;
364
365 void compute_cached_shape() const;
366
367 tuple_type m_e;
368 functor_type m_f;
369 mutable xfunction_cache<detail::promote_index<typename std::decay_t<CT>::shape_type...>> m_cache;
370
371 friend class xfunction_iterator<F, CT...>;
372 friend class xfunction_stepper<F, CT...>;
373 friend class xconst_iterable<self_type>;
374 friend class xconst_accessible<self_type>;
375 };
376
377 /**********************
378 * xfunction_iterator *
379 **********************/
380
381 template <class F, class... CT>
382 class xfunction_iterator : public xtl::xrandom_access_iterator_base<
383 xfunction_iterator<F, CT...>,
384 typename xfunction<F, CT...>::value_type,
385 typename xfunction<F, CT...>::difference_type,
386 typename xfunction<F, CT...>::pointer,
387 typename xfunction<F, CT...>::reference>
388 {
389 public:
390
391 using self_type = xfunction_iterator<F, CT...>;
392 using functor_type = typename std::remove_reference<F>::type;
393 using xfunction_type = xfunction<F, CT...>;
394
395 using value_type = typename xfunction_type::value_type;
396 using reference = typename xfunction_type::value_type;
397 using pointer = typename xfunction_type::const_pointer;
398 using difference_type = typename xfunction_type::difference_type;
399 using iterator_category = std::random_access_iterator_tag;
400
401 template <class... It>
402 xfunction_iterator(const xfunction_type* func, It&&... it) noexcept;
403
404 self_type& operator++();
405 self_type& operator--();
406
407 self_type& operator+=(difference_type n);
408 self_type& operator-=(difference_type n);
409
410 difference_type operator-(const self_type& rhs) const;
411
412 reference operator*() const;
413
414 bool equal(const self_type& rhs) const;
415 bool less_than(const self_type& rhs) const;
416
417 private:
418
419 using data_type = std::tuple<decltype(xt::linear_begin(std::declval<const std::decay_t<CT>>()))...>;
420
421 template <std::size_t... I>
422 difference_type
423 tuple_max_diff(std::index_sequence<I...>, const data_type& lhs, const data_type& rhs) const;
424
425 const xfunction_type* p_f;
426 data_type m_it;
427 };
428
429 template <class F, class... CT>
430 bool operator==(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2);
431
432 template <class F, class... CT>
433 bool operator<(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2);
434
435 /*********************
436 * xfunction_stepper *
437 *********************/
438
439 template <class F, class... CT>
440 class xfunction_stepper
441 {
442 public:
443
444 using self_type = xfunction_stepper<F, CT...>;
445 using functor_type = typename std::remove_reference<F>::type;
446 using xfunction_type = xfunction<F, CT...>;
447
448 using value_type = typename xfunction_type::value_type;
449 using reference = typename xfunction_type::reference;
450 using pointer = typename xfunction_type::const_pointer;
451 using size_type = typename xfunction_type::size_type;
452 using difference_type = typename xfunction_type::difference_type;
453
454 using shape_type = typename xfunction_type::shape_type;
455
456 template <class requested_type>
457 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
458
459 template <class... St>
460 xfunction_stepper(const xfunction_type* func, St&&... st) noexcept;
461
462 void step(size_type dim);
463 void step_back(size_type dim);
464 void step(size_type dim, size_type n);
465 void step_back(size_type dim, size_type n);
466 void reset(size_type dim);
467 void reset_back(size_type dim);
468
469 void to_begin();
470 void to_end(layout_type l);
471
472 reference operator*() const;
473
474 template <class T>
475 simd_return_type<T> step_simd();
476
477 void step_leading();
478
479 private:
480
481 const xfunction_type* p_f;
482 std::tuple<typename std::decay_t<CT>::const_stepper...> m_st;
483 };
484
485 /*********************************
486 * xfunction implementation *
487 *********************************/
488
493
499 template <class F, class... CT>
500 template <class Func, class... CTA, class U>
501 inline xfunction<F, CT...>::xfunction(Func&& f, CTA&&... e) noexcept
502 : m_e(std::forward<CTA>(e)...)
503 , m_f(std::forward<Func>(f))
504 {
505 }
506
512 template <class F, class... CT>
513 template <class FA, class... CTA>
515 : m_e(xf.arguments())
516 , m_f(xf.functor())
517 {
518 }
519
521
526
529 template <class F, class... CT>
530 inline auto xfunction<F, CT...>::dimension() const noexcept -> size_type
531 {
532 size_type dimension = m_cache.is_initialized ? m_cache.shape.size() : compute_dimension();
533 return dimension;
534 }
535
536 template <class F, class... CT>
537 inline void xfunction<F, CT...>::compute_cached_shape() const
538 {
539 static_assert(!detail::is_fixed<shape_type>::value, "Calling compute_cached_shape on fixed!");
540
541 m_cache.shape = uninitialized_shape<xindex_type_t<inner_shape_type>>(compute_dimension());
542 m_cache.is_trivial = broadcast_shape(m_cache.shape, false);
543 m_cache.is_initialized = true;
544 }
545
549 template <class F, class... CT>
550 inline auto xfunction<F, CT...>::shape() const -> const inner_shape_type&
551 {
552 if constexpr (!detail::is_fixed<inner_shape_type>::value)
553 {
554 if (!m_cache.is_initialized)
555 {
556 compute_cached_shape();
557 }
558 }
559 return m_cache.shape;
560 }
561
565 template <class F, class... CT>
567 {
568 return std::apply(
569 [&](auto&... e)
570 {
571 return compute_layout(e.layout()...);
572 },
573 m_e
574 );
575 }
576
577 template <class F, class... CT>
578 inline bool xfunction<F, CT...>::is_contiguous() const noexcept
579 {
580 return layout() != layout_type::dynamic
581 && accumulate(
582 [](bool r, const auto& exp)
583 {
584 return r && exp.is_contiguous();
585 },
586 true,
587 m_e
588 );
589 }
590
592
596
603 template <class F, class... CT>
604 template <class... Args>
605 inline auto xfunction<F, CT...>::operator()(Args... args) const -> const_reference
606 {
607 // The static cast prevents the compiler from instantiating the template methods with signed integers,
608 // leading to warning about signed/unsigned conversions in the deeper layers of the access methods
609
610 return std::apply(
611 [&](auto&... e)
612 {
613 XTENSOR_TRY(check_index(shape(), args...));
614 XTENSOR_CHECK_DIMENSION(shape(), args...);
615 return m_f(e(args...)...);
616 },
617 m_e
618 );
619 }
620
624
630 template <class F, class... CT>
631 inline auto xfunction<F, CT...>::flat(size_type index) const -> const_reference
632 {
633 return std::apply(
634 [&](auto&... e)
635 {
636 return m_f(e.data_element(index)...);
637 },
638 m_e
639 );
640 }
641
661 template <class F, class... CT>
662 template <class... Args>
663 inline auto xfunction<F, CT...>::unchecked(Args... args) const -> const_reference
664 {
665 // The static cast prevents the compiler from instantiating the template methods with signed integers,
666 // leading to warning about signed/unsigned conversions in the deeper layers of the access methods
667 return std::apply(
668 [&](const auto&... e)
669 {
670 return m_f(e.unchecked(static_cast<size_type>(args)...)...);
671 },
672 m_e
673 );
674 }
675
683 template <class F, class... CT>
684 template <class It>
685 inline auto xfunction<F, CT...>::element(It first, It last) const -> const_reference
686 {
687 return std::apply(
688 [&](auto&... e)
689 {
690 XTENSOR_TRY(check_element_index(shape(), first, last));
691 return m_f(e.element(first, last)...);
692 },
693 m_e
694 );
695 }
696
698
703
709 template <class F, class... CT>
710 template <class S>
711 inline bool xfunction<F, CT...>::broadcast_shape(S& shape, bool reuse_cache) const
712 {
713 if (m_cache.is_initialized && reuse_cache)
714 {
715 std::copy(m_cache.shape.cbegin(), m_cache.shape.cend(), shape.begin());
716 return m_cache.is_trivial;
717 }
718 else
719 {
720 // e.broadcast_shape must be evaluated even if b is false
721 auto func = [&shape](bool b, auto&& e)
722 {
723 return e.broadcast_shape(shape) && b;
724 };
725 return accumulate(func, true, m_e);
726 }
727 }
728
734 template <class F, class... CT>
735 template <class S>
736 inline bool xfunction<F, CT...>::has_linear_assign(const S& strides) const noexcept
737 {
738 auto func = [&strides](bool b, auto&& e)
739 {
740 return b && e.has_linear_assign(strides);
741 };
742 return accumulate(func, true, m_e);
743 }
744
746
747 template <class F, class... CT>
748 inline auto xfunction<F, CT...>::linear_begin() const noexcept -> const_linear_iterator
749 {
750 return linear_cbegin();
751 }
752
753 template <class F, class... CT>
754 inline auto xfunction<F, CT...>::linear_end() const noexcept -> const_linear_iterator
755 {
756 return linear_cend();
757 }
758
759 template <class F, class... CT>
760 inline auto xfunction<F, CT...>::linear_cbegin() const noexcept -> const_linear_iterator
761 {
762 auto f = [](const auto& e) noexcept
763 {
764 return xt::linear_begin(e);
765 };
766 return build_iterator(f, std::make_index_sequence<sizeof...(CT)>());
767 }
768
769 template <class F, class... CT>
770 inline auto xfunction<F, CT...>::linear_cend() const noexcept -> const_linear_iterator
771 {
772 auto f = [](const auto& e) noexcept
773 {
774 return xt::linear_end(e);
775 };
776 return build_iterator(f, std::make_index_sequence<sizeof...(CT)>());
777 }
778
779 template <class F, class... CT>
780 inline auto xfunction<F, CT...>::linear_rbegin() const noexcept -> const_reverse_linear_iterator
781 {
782 return linear_crbegin();
783 }
784
785 template <class F, class... CT>
786 inline auto xfunction<F, CT...>::linear_rend() const noexcept -> const_reverse_linear_iterator
787 {
788 return linear_crend();
789 }
790
791 template <class F, class... CT>
792 inline auto xfunction<F, CT...>::linear_crbegin() const noexcept -> const_reverse_linear_iterator
793 {
794 return const_reverse_linear_iterator(linear_cend());
795 }
796
797 template <class F, class... CT>
798 inline auto xfunction<F, CT...>::linear_crend() const noexcept -> const_reverse_linear_iterator
799 {
800 return const_reverse_linear_iterator(linear_cbegin());
801 }
802
803 template <class F, class... CT>
804 template <class S>
805 inline auto xfunction<F, CT...>::stepper_begin(const S& shape) const noexcept -> const_stepper
806 {
807 auto f = [&shape](const auto& e) noexcept
808 {
809 return e.stepper_begin(shape);
810 };
811 return build_stepper(f, std::make_index_sequence<sizeof...(CT)>());
812 }
813
814 template <class F, class... CT>
815 template <class S>
816 inline auto xfunction<F, CT...>::stepper_end(const S& shape, layout_type l) const noexcept -> const_stepper
817 {
818 auto f = [&shape, l](const auto& e) noexcept
819 {
820 return e.stepper_end(shape, l);
821 };
822 return build_stepper(f, std::make_index_sequence<sizeof...(CT)>());
823 }
824
825 template <class F, class... CT>
826 inline auto xfunction<F, CT...>::data_element(size_type i) const -> const_reference
827 {
828 return std::apply(
829 [&](auto&... e)
830 {
831 return m_f(e.data_element(i)...);
832 },
833 m_e
834 );
835 }
836
837 template <class F, class... CT>
838 template <class UT, class>
839 inline xfunction<F, CT...>::operator value_type() const
840 {
841 return operator()();
842 }
843
844 template <class F, class... CT>
845 template <class align, class requested_type, std::size_t N>
846 inline auto xfunction<F, CT...>::load_simd(size_type i) const -> simd_return_type<requested_type>
847 {
848 return std::apply(
849 [&](auto&... e)
850 {
851 return m_f.simd_apply((e.template load_simd<align, requested_type>(i))...);
852 },
853 m_e
854 );
855 }
856
857 template <class F, class... CT>
858 inline auto xfunction<F, CT...>::arguments() const noexcept -> const tuple_type&
859 {
860 return m_e;
861 }
862
863 template <class F, class... CT>
864 inline auto xfunction<F, CT...>::functor() const noexcept -> const functor_type&
865 {
866 return m_f;
867 }
868
869 template <class F, class... CT>
870 template <class Func, std::size_t... I>
871 inline auto xfunction<F, CT...>::build_stepper(Func&& f, std::index_sequence<I...>) const noexcept
872 -> const_stepper
873 {
874 return const_stepper(this, f(std::get<I>(m_e))...);
875 }
876
877 template <class F, class... CT>
878 template <class Func, std::size_t... I>
879 inline auto xfunction<F, CT...>::build_iterator(Func&& f, std::index_sequence<I...>) const noexcept
880 {
881 return const_linear_iterator(this, f(std::get<I>(m_e))...);
882 }
883
884 template <class F, class... CT>
885 inline auto xfunction<F, CT...>::compute_dimension() const noexcept -> size_type
886 {
887 auto func = [](size_type d, auto&& e) noexcept
888 {
889 return (std::max)(d, e.dimension());
890 };
891 return accumulate(func, size_type(0), m_e);
892 }
893
894 /*************************************
895 * xfunction_iterator implementation *
896 *************************************/
897
898 template <class F, class... CT>
899 template <class... It>
900 inline xfunction_iterator<F, CT...>::xfunction_iterator(const xfunction_type* func, It&&... it) noexcept
901 : p_f(func)
902 , m_it(std::forward<It>(it)...)
903 {
904 }
905
906 template <class F, class... CT>
907 inline auto xfunction_iterator<F, CT...>::operator++() -> self_type&
908 {
909 auto f = [](auto& it)
910 {
911 ++it;
912 };
913 for_each(f, m_it);
914 return *this;
915 }
916
917 template <class F, class... CT>
918 inline auto xfunction_iterator<F, CT...>::operator--() -> self_type&
919 {
920 auto f = [](auto& it)
921 {
922 return --it;
923 };
924 for_each(f, m_it);
925 return *this;
926 }
927
928 template <class F, class... CT>
929 inline auto xfunction_iterator<F, CT...>::operator+=(difference_type n) -> self_type&
930 {
931 auto f = [n](auto& it)
932 {
933 it += n;
934 };
935 for_each(f, m_it);
936 return *this;
937 }
938
939 template <class F, class... CT>
940 inline auto xfunction_iterator<F, CT...>::operator-=(difference_type n) -> self_type&
941 {
942 auto f = [n](auto& it)
943 {
944 it -= n;
945 };
946 for_each(f, m_it);
947 return *this;
948 }
949
950 template <class F, class... CT>
951 inline auto xfunction_iterator<F, CT...>::operator-(const self_type& rhs) const -> difference_type
952 {
953 return tuple_max_diff(std::make_index_sequence<sizeof...(CT)>(), m_it, rhs.m_it);
954 }
955
956 template <class F, class... CT>
957 inline auto xfunction_iterator<F, CT...>::operator*() const -> reference
958 {
959 return std::apply(
960 [&](auto&... it)
961 {
962 return (p_f->m_f)(*it...);
963 },
964 m_it
965 );
966 }
967
968 template <class F, class... CT>
969 inline bool xfunction_iterator<F, CT...>::equal(const self_type& rhs) const
970 {
971 // Optimization: no need to compare each subiterator since they all
972 // are incremented decremented together.
973 constexpr std::size_t temp = xtl::mpl::find_if<is_not_xdummy_iterator, data_type>::value;
974 constexpr std::size_t index = (temp == std::tuple_size<data_type>::value) ? 0 : temp;
975 return std::get<index>(m_it) == std::get<index>(rhs.m_it);
976 }
977
978 template <class F, class... CT>
979 inline bool xfunction_iterator<F, CT...>::less_than(const self_type& rhs) const
980 {
981 // Optimization: no need to compare each subiterator since they all
982 // are incremented decremented together.
983 constexpr std::size_t temp = xtl::mpl::find_if<is_not_xdummy_iterator, data_type>::value;
984 constexpr std::size_t index = (temp == std::tuple_size<data_type>::value) ? 0 : temp;
985 return std::get<index>(m_it) < std::get<index>(rhs.m_it);
986 }
987
988 template <class F, class... CT>
989 template <std::size_t... I>
990 inline auto xfunction_iterator<F, CT...>::tuple_max_diff(
991 std::index_sequence<I...>,
992 const data_type& lhs,
993 const data_type& rhs
994 ) const -> difference_type
995 {
996 auto diff = std::make_tuple((std::get<I>(lhs) - std::get<I>(rhs))...);
997 auto func = [](difference_type n, auto&& v)
998 {
999 return (std::max)(n, v);
1000 };
1001 return accumulate(func, difference_type(0), diff);
1002 }
1003
1004 template <class F, class... CT>
1005 inline bool operator==(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2)
1006 {
1007 return it1.equal(it2);
1008 }
1009
1010 template <class F, class... CT>
1011 inline bool operator<(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2)
1012 {
1013 return it1.less_than(it2);
1014 }
1015
1016 /************************************
1017 * xfunction_stepper implementation *
1018 ************************************/
1019
1020 template <class F, class... CT>
1021 template <class... St>
1022 inline xfunction_stepper<F, CT...>::xfunction_stepper(const xfunction_type* func, St&&... st) noexcept
1023 : p_f(func)
1024 , m_st(std::forward<St>(st)...)
1025 {
1026 }
1027
1028 template <class F, class... CT>
1029 inline void xfunction_stepper<F, CT...>::step(size_type dim)
1030 {
1031 auto f = [dim](auto& st)
1032 {
1033 st.step(dim);
1034 };
1035 for_each(f, m_st);
1036 }
1037
1038 template <class F, class... CT>
1039 inline void xfunction_stepper<F, CT...>::step_back(size_type dim)
1040 {
1041 auto f = [dim](auto& st)
1042 {
1043 st.step_back(dim);
1044 };
1045 for_each(f, m_st);
1046 }
1047
1048 template <class F, class... CT>
1049 inline void xfunction_stepper<F, CT...>::step(size_type dim, size_type n)
1050 {
1051 auto f = [dim, n](auto& st)
1052 {
1053 st.step(dim, n);
1054 };
1055 for_each(f, m_st);
1056 }
1057
1058 template <class F, class... CT>
1059 inline void xfunction_stepper<F, CT...>::step_back(size_type dim, size_type n)
1060 {
1061 auto f = [dim, n](auto& st)
1062 {
1063 st.step_back(dim, n);
1064 };
1065 for_each(f, m_st);
1066 }
1067
1068 template <class F, class... CT>
1069 inline void xfunction_stepper<F, CT...>::reset(size_type dim)
1070 {
1071 auto f = [dim](auto& st)
1072 {
1073 st.reset(dim);
1074 };
1075 for_each(f, m_st);
1076 }
1077
1078 template <class F, class... CT>
1079 inline void xfunction_stepper<F, CT...>::reset_back(size_type dim)
1080 {
1081 auto f = [dim](auto& st)
1082 {
1083 st.reset_back(dim);
1084 };
1085 for_each(f, m_st);
1086 }
1087
1088 template <class F, class... CT>
1089 inline void xfunction_stepper<F, CT...>::to_begin()
1090 {
1091 auto f = [](auto& st)
1092 {
1093 st.to_begin();
1094 };
1095 for_each(f, m_st);
1096 }
1097
1098 template <class F, class... CT>
1099 inline void xfunction_stepper<F, CT...>::to_end(layout_type l)
1100 {
1101 auto f = [l](auto& st)
1102 {
1103 st.to_end(l);
1104 };
1105 for_each(f, m_st);
1106 }
1107
1108 template <class F, class... CT>
1109 inline auto xfunction_stepper<F, CT...>::operator*() const -> reference
1110 {
1111 return std::apply(
1112 [&](auto&... e)
1113 {
1114 return (p_f->m_f)(*e...);
1115 },
1116 m_st
1117 );
1118 }
1119
1120 template <class F, class... CT>
1121 template <class T>
1122 inline auto xfunction_stepper<F, CT...>::step_simd() -> simd_return_type<T>
1123 {
1124 return std::apply(
1125 [&](auto&... st)
1126 {
1127 return (p_f->m_f.simd_apply)(st.template step_simd<T>()...);
1128 },
1129 m_st
1130 );
1131 }
1132
1133 template <class F, class... CT>
1134 inline void xfunction_stepper<F, CT...>::step_leading()
1135 {
1136 auto step_leading_lambda = [](auto&& st)
1137 {
1138 st.step_leading();
1139 };
1140 for_each(step_leading_lambda, m_st);
1141 }
1142}
1143
1144#endif
size_type size() const noexcept(noexcept(derived_cast().shape()))
Base class for multidimensional iterable constant expressions.
Definition xiterable.hpp:37
Multidimensional function operating on xtensor expressions.
const_reference back() const
xfunction(xfunction< FA, CTA... > xf) noexcept
Constructs an xfunction applying the specified function given by another xfunction with its arguments...
bool in_bounds(Args... args) const
bool broadcast_shape(S &shape, bool reuse_cache=false) const
layout_type layout() const noexcept
const inner_shape_type & shape() const
size_type dimension() const noexcept
Returns the number of dimensions of the function.
xfunction(Func &&f, CTA &&... e) noexcept
Constructs an xfunction applying the specified function to the given arguments.
bool has_linear_assign(const S &strides) const noexcept
const_reference flat(size_type i) const
const_reference front() const
size_type size() const noexcept(noexcept(derived_cast().shape()))
auto operator-(E &&e) noexcept -> detail::xfunction_type_t< detail::negate, E >
Opposite.
auto operator*(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< detail::multiplies, E1, E2 >
Multiplication.
auto equal(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< detail::equal_to, E1, E2 >
Element-wise equality.
auto exp(E &&e) noexcept -> detail::xfunction_type_t< math::exp_fun, E >
Natural exponential function.
Definition xmath.hpp:900
auto diff(const xexpression< T > &a, std::size_t n=1, std::ptrdiff_t axis=-1)
Calculate the n-th discrete difference along the given axis.
Definition xmath.hpp:2873
auto strides(const E &e, stride_type type=stride_type::normal) noexcept
Get strides of an object.
Definition xstrides.hpp:250
standard mathematical functions for xexpressions
constexpr layout_type compute_layout(Args... args) noexcept
Implementation of the following logical table:
Definition xlayout.hpp:88
layout_type
Definition xlayout.hpp:24
auto accumulate(F &&f, E &&e, EVS evaluation_strategy=EVS())
Accumulate and flatten array NOTE This function is not lazy!