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