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 <std::size_t... I>
359 layout_type layout_impl(std::index_sequence<I...>) const noexcept;
360
361 template <std::size_t... I, class... Args>
362 const_reference access_impl(std::index_sequence<I...>, Args... args) const;
363
364 template <std::size_t... I, class... Args>
365 const_reference unchecked_impl(std::index_sequence<I...>, Args... args) const;
366
367 template <std::size_t... I, class It>
368 const_reference element_access_impl(std::index_sequence<I...>, It first, It last) const;
369
370 template <std::size_t... I>
371 const_reference data_element_impl(std::index_sequence<I...>, size_type i) const;
372
373 template <class align, class requested_type, std::size_t N, std::size_t... I>
374 auto load_simd_impl(std::index_sequence<I...>, size_type i) const;
375
376 template <class Func, std::size_t... I>
377 const_stepper build_stepper(Func&& f, std::index_sequence<I...>) const noexcept;
378
379 template <class Func, std::size_t... I>
380 auto build_iterator(Func&& f, std::index_sequence<I...>) const noexcept;
381
382 size_type compute_dimension() const noexcept;
383
384 void compute_cached_shape() const;
385
386 tuple_type m_e;
387 functor_type m_f;
388 mutable xfunction_cache<detail::promote_index<typename std::decay_t<CT>::shape_type...>> m_cache;
389
390 friend class xfunction_iterator<F, CT...>;
391 friend class xfunction_stepper<F, CT...>;
392 friend class xconst_iterable<self_type>;
393 friend class xconst_accessible<self_type>;
394 };
395
396 /**********************
397 * xfunction_iterator *
398 **********************/
399
400 template <class F, class... CT>
401 class xfunction_iterator : public xtl::xrandom_access_iterator_base<
402 xfunction_iterator<F, CT...>,
403 typename xfunction<F, CT...>::value_type,
404 typename xfunction<F, CT...>::difference_type,
405 typename xfunction<F, CT...>::pointer,
406 typename xfunction<F, CT...>::reference>
407 {
408 public:
409
410 using self_type = xfunction_iterator<F, CT...>;
411 using functor_type = typename std::remove_reference<F>::type;
412 using xfunction_type = xfunction<F, CT...>;
413
414 using value_type = typename xfunction_type::value_type;
415 using reference = typename xfunction_type::value_type;
416 using pointer = typename xfunction_type::const_pointer;
417 using difference_type = typename xfunction_type::difference_type;
418 using iterator_category = std::random_access_iterator_tag;
419
420 template <class... It>
421 xfunction_iterator(const xfunction_type* func, It&&... it) noexcept;
422
423 self_type& operator++();
424 self_type& operator--();
425
426 self_type& operator+=(difference_type n);
427 self_type& operator-=(difference_type n);
428
429 difference_type operator-(const self_type& rhs) const;
430
431 reference operator*() const;
432
433 bool equal(const self_type& rhs) const;
434 bool less_than(const self_type& rhs) const;
435
436 private:
437
438 using data_type = std::tuple<decltype(xt::linear_begin(std::declval<const std::decay_t<CT>>()))...>;
439
440 template <std::size_t... I>
441 reference deref_impl(std::index_sequence<I...>) const;
442
443 template <std::size_t... I>
444 difference_type
445 tuple_max_diff(std::index_sequence<I...>, const data_type& lhs, const data_type& rhs) const;
446
447 const xfunction_type* p_f;
448 data_type m_it;
449 };
450
451 template <class F, class... CT>
452 bool operator==(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2);
453
454 template <class F, class... CT>
455 bool operator<(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2);
456
457 /*********************
458 * xfunction_stepper *
459 *********************/
460
461 template <class F, class... CT>
462 class xfunction_stepper
463 {
464 public:
465
466 using self_type = xfunction_stepper<F, CT...>;
467 using functor_type = typename std::remove_reference<F>::type;
468 using xfunction_type = xfunction<F, CT...>;
469
470 using value_type = typename xfunction_type::value_type;
471 using reference = typename xfunction_type::reference;
472 using pointer = typename xfunction_type::const_pointer;
473 using size_type = typename xfunction_type::size_type;
474 using difference_type = typename xfunction_type::difference_type;
475
476 using shape_type = typename xfunction_type::shape_type;
477
478 template <class requested_type>
479 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
480
481 template <class... St>
482 xfunction_stepper(const xfunction_type* func, St&&... st) noexcept;
483
484 void step(size_type dim);
485 void step_back(size_type dim);
486 void step(size_type dim, size_type n);
487 void step_back(size_type dim, size_type n);
488 void reset(size_type dim);
489 void reset_back(size_type dim);
490
491 void to_begin();
492 void to_end(layout_type l);
493
494 reference operator*() const;
495
496 template <class T>
497 simd_return_type<T> step_simd();
498
499 void step_leading();
500
501 private:
502
503 template <std::size_t... I>
504 reference deref_impl(std::index_sequence<I...>) const;
505
506 template <class T, std::size_t... I>
507 simd_return_type<T> step_simd_impl(std::index_sequence<I...>);
508
509 const xfunction_type* p_f;
510 std::tuple<typename std::decay_t<CT>::const_stepper...> m_st;
511 };
512
513 /*********************************
514 * xfunction implementation *
515 *********************************/
516
521
527 template <class F, class... CT>
528 template <class Func, class... CTA, class U>
529 inline xfunction<F, CT...>::xfunction(Func&& f, CTA&&... e) noexcept
530 : m_e(std::forward<CTA>(e)...)
531 , m_f(std::forward<Func>(f))
532 {
533 }
534
540 template <class F, class... CT>
541 template <class FA, class... CTA>
543 : m_e(xf.arguments())
544 , m_f(xf.functor())
545 {
546 }
547
549
554
557 template <class F, class... CT>
558 inline auto xfunction<F, CT...>::dimension() const noexcept -> size_type
559 {
560 size_type dimension = m_cache.is_initialized ? m_cache.shape.size() : compute_dimension();
561 return dimension;
562 }
563
564 template <class F, class... CT>
565 inline void xfunction<F, CT...>::compute_cached_shape() const
566 {
567 static_assert(!detail::is_fixed<shape_type>::value, "Calling compute_cached_shape on fixed!");
568
569 m_cache.shape = uninitialized_shape<xindex_type_t<inner_shape_type>>(compute_dimension());
570 m_cache.is_trivial = broadcast_shape(m_cache.shape, false);
571 m_cache.is_initialized = true;
572 }
573
577 template <class F, class... CT>
578 inline auto xfunction<F, CT...>::shape() const -> const inner_shape_type&
579 {
580 if constexpr (!detail::is_fixed<inner_shape_type>::value)
581 {
582 if (!m_cache.is_initialized)
583 {
584 compute_cached_shape();
585 }
586 }
587 return m_cache.shape;
588 }
589
593 template <class F, class... CT>
595 {
596 return layout_impl(std::make_index_sequence<sizeof...(CT)>());
597 }
598
599 template <class F, class... CT>
600 inline bool xfunction<F, CT...>::is_contiguous() const noexcept
601 {
602 return layout() != layout_type::dynamic
603 && accumulate(
604 [](bool r, const auto& exp)
605 {
606 return r && exp.is_contiguous();
607 },
608 true,
609 m_e
610 );
611 }
612
614
618
625 template <class F, class... CT>
626 template <class... Args>
627 inline auto xfunction<F, CT...>::operator()(Args... args) const -> const_reference
628 {
629 // The static cast prevents the compiler from instantiating the template methods with signed integers,
630 // leading to warning about signed/unsigned conversions in the deeper layers of the access methods
631 return access_impl(std::make_index_sequence<sizeof...(CT)>(), static_cast<size_type>(args)...);
632 }
633
637
643 template <class F, class... CT>
644 inline auto xfunction<F, CT...>::flat(size_type index) const -> const_reference
645 {
646 return data_element_impl(std::make_index_sequence<sizeof...(CT)>(), index);
647 }
648
668 template <class F, class... CT>
669 template <class... Args>
670 inline auto xfunction<F, CT...>::unchecked(Args... args) const -> const_reference
671 {
672 // The static cast prevents the compiler from instantiating the template methods with signed integers,
673 // leading to warning about signed/unsigned conversions in the deeper layers of the access methods
674 return unchecked_impl(std::make_index_sequence<sizeof...(CT)>(), static_cast<size_type>(args)...);
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 element_access_impl(std::make_index_sequence<sizeof...(CT)>(), first, last);
689 }
690
692
697
703 template <class F, class... CT>
704 template <class S>
705 inline bool xfunction<F, CT...>::broadcast_shape(S& shape, bool reuse_cache) const
706 {
707 if (m_cache.is_initialized && reuse_cache)
708 {
709 std::copy(m_cache.shape.cbegin(), m_cache.shape.cend(), shape.begin());
710 return m_cache.is_trivial;
711 }
712 else
713 {
714 // e.broadcast_shape must be evaluated even if b is false
715 auto func = [&shape](bool b, auto&& e)
716 {
717 return e.broadcast_shape(shape) && b;
718 };
719 return accumulate(func, true, m_e);
720 }
721 }
722
728 template <class F, class... CT>
729 template <class S>
730 inline bool xfunction<F, CT...>::has_linear_assign(const S& strides) const noexcept
731 {
732 auto func = [&strides](bool b, auto&& e)
733 {
734 return b && e.has_linear_assign(strides);
735 };
736 return accumulate(func, true, m_e);
737 }
738
740
741 template <class F, class... CT>
742 inline auto xfunction<F, CT...>::linear_begin() const noexcept -> const_linear_iterator
743 {
744 return linear_cbegin();
745 }
746
747 template <class F, class... CT>
748 inline auto xfunction<F, CT...>::linear_end() const noexcept -> const_linear_iterator
749 {
750 return linear_cend();
751 }
752
753 template <class F, class... CT>
754 inline auto xfunction<F, CT...>::linear_cbegin() const noexcept -> const_linear_iterator
755 {
756 auto f = [](const auto& e) noexcept
757 {
758 return xt::linear_begin(e);
759 };
760 return build_iterator(f, std::make_index_sequence<sizeof...(CT)>());
761 }
762
763 template <class F, class... CT>
764 inline auto xfunction<F, CT...>::linear_cend() const noexcept -> const_linear_iterator
765 {
766 auto f = [](const auto& e) noexcept
767 {
768 return xt::linear_end(e);
769 };
770 return build_iterator(f, std::make_index_sequence<sizeof...(CT)>());
771 }
772
773 template <class F, class... CT>
774 inline auto xfunction<F, CT...>::linear_rbegin() const noexcept -> const_reverse_linear_iterator
775 {
776 return linear_crbegin();
777 }
778
779 template <class F, class... CT>
780 inline auto xfunction<F, CT...>::linear_rend() const noexcept -> const_reverse_linear_iterator
781 {
782 return linear_crend();
783 }
784
785 template <class F, class... CT>
786 inline auto xfunction<F, CT...>::linear_crbegin() const noexcept -> const_reverse_linear_iterator
787 {
788 return const_reverse_linear_iterator(linear_cend());
789 }
790
791 template <class F, class... CT>
792 inline auto xfunction<F, CT...>::linear_crend() const noexcept -> const_reverse_linear_iterator
793 {
794 return const_reverse_linear_iterator(linear_cbegin());
795 }
796
797 template <class F, class... CT>
798 template <class S>
799 inline auto xfunction<F, CT...>::stepper_begin(const S& shape) const noexcept -> const_stepper
800 {
801 auto f = [&shape](const auto& e) noexcept
802 {
803 return e.stepper_begin(shape);
804 };
805 return build_stepper(f, std::make_index_sequence<sizeof...(CT)>());
806 }
807
808 template <class F, class... CT>
809 template <class S>
810 inline auto xfunction<F, CT...>::stepper_end(const S& shape, layout_type l) const noexcept -> const_stepper
811 {
812 auto f = [&shape, l](const auto& e) noexcept
813 {
814 return e.stepper_end(shape, l);
815 };
816 return build_stepper(f, std::make_index_sequence<sizeof...(CT)>());
817 }
818
819 template <class F, class... CT>
820 inline auto xfunction<F, CT...>::data_element(size_type i) const -> const_reference
821 {
822 return data_element_impl(std::make_index_sequence<sizeof...(CT)>(), i);
823 }
824
825 template <class F, class... CT>
826 template <class UT, class>
827 inline xfunction<F, CT...>::operator value_type() const
828 {
829 return operator()();
830 }
831
832 template <class F, class... CT>
833 template <class align, class requested_type, std::size_t N>
834 inline auto xfunction<F, CT...>::load_simd(size_type i) const -> simd_return_type<requested_type>
835 {
836 return load_simd_impl<align, requested_type, N>(std::make_index_sequence<sizeof...(CT)>(), i);
837 }
838
839 template <class F, class... CT>
840 inline auto xfunction<F, CT...>::arguments() const noexcept -> const tuple_type&
841 {
842 return m_e;
843 }
844
845 template <class F, class... CT>
846 inline auto xfunction<F, CT...>::functor() const noexcept -> const functor_type&
847 {
848 return m_f;
849 }
850
851 template <class F, class... CT>
852 template <std::size_t... I>
853 inline layout_type xfunction<F, CT...>::layout_impl(std::index_sequence<I...>) const noexcept
854 {
855 return compute_layout(std::get<I>(m_e).layout()...);
856 }
857
858 template <class F, class... CT>
859 template <std::size_t... I, class... Args>
860 inline auto xfunction<F, CT...>::access_impl(std::index_sequence<I...>, Args... args) const
861 -> const_reference
862 {
863 XTENSOR_TRY(check_index(shape(), args...));
864 XTENSOR_CHECK_DIMENSION(shape(), args...);
865 return m_f(std::get<I>(m_e)(args...)...);
866 }
867
868 template <class F, class... CT>
869 template <std::size_t... I, class... Args>
870 inline auto xfunction<F, CT...>::unchecked_impl(std::index_sequence<I...>, Args... args) const
871 -> const_reference
872 {
873 return m_f(std::get<I>(m_e).unchecked(args...)...);
874 }
875
876 template <class F, class... CT>
877 template <std::size_t... I, class It>
878 inline auto xfunction<F, CT...>::element_access_impl(std::index_sequence<I...>, It first, It last) const
879 -> const_reference
880 {
881 XTENSOR_TRY(check_element_index(shape(), first, last));
882 return m_f((std::get<I>(m_e).element(first, last))...);
883 }
884
885 template <class F, class... CT>
886 template <std::size_t... I>
887 inline auto xfunction<F, CT...>::data_element_impl(std::index_sequence<I...>, size_type i) const
888 -> const_reference
889 {
890 return m_f((std::get<I>(m_e).data_element(i))...);
891 }
892
893 template <class F, class... CT>
894 template <class align, class requested_type, std::size_t N, std::size_t... I>
895 inline auto xfunction<F, CT...>::load_simd_impl(std::index_sequence<I...>, size_type i) const
896 {
897 return m_f.simd_apply((std::get<I>(m_e).template load_simd<align, requested_type>(i))...);
898 }
899
900 template <class F, class... CT>
901 template <class Func, std::size_t... I>
902 inline auto xfunction<F, CT...>::build_stepper(Func&& f, std::index_sequence<I...>) const noexcept
903 -> const_stepper
904 {
905 return const_stepper(this, f(std::get<I>(m_e))...);
906 }
907
908 template <class F, class... CT>
909 template <class Func, std::size_t... I>
910 inline auto xfunction<F, CT...>::build_iterator(Func&& f, std::index_sequence<I...>) const noexcept
911 {
912 return const_linear_iterator(this, f(std::get<I>(m_e))...);
913 }
914
915 template <class F, class... CT>
916 inline auto xfunction<F, CT...>::compute_dimension() const noexcept -> size_type
917 {
918 auto func = [](size_type d, auto&& e) noexcept
919 {
920 return (std::max)(d, e.dimension());
921 };
922 return accumulate(func, size_type(0), m_e);
923 }
924
925 /*************************************
926 * xfunction_iterator implementation *
927 *************************************/
928
929 template <class F, class... CT>
930 template <class... It>
931 inline xfunction_iterator<F, CT...>::xfunction_iterator(const xfunction_type* func, It&&... it) noexcept
932 : p_f(func)
933 , m_it(std::forward<It>(it)...)
934 {
935 }
936
937 template <class F, class... CT>
938 inline auto xfunction_iterator<F, CT...>::operator++() -> self_type&
939 {
940 auto f = [](auto& it)
941 {
942 ++it;
943 };
944 for_each(f, m_it);
945 return *this;
946 }
947
948 template <class F, class... CT>
949 inline auto xfunction_iterator<F, CT...>::operator--() -> self_type&
950 {
951 auto f = [](auto& it)
952 {
953 return --it;
954 };
955 for_each(f, m_it);
956 return *this;
957 }
958
959 template <class F, class... CT>
960 inline auto xfunction_iterator<F, CT...>::operator+=(difference_type n) -> self_type&
961 {
962 auto f = [n](auto& it)
963 {
964 it += n;
965 };
966 for_each(f, m_it);
967 return *this;
968 }
969
970 template <class F, class... CT>
971 inline auto xfunction_iterator<F, CT...>::operator-=(difference_type n) -> self_type&
972 {
973 auto f = [n](auto& it)
974 {
975 it -= n;
976 };
977 for_each(f, m_it);
978 return *this;
979 }
980
981 template <class F, class... CT>
982 inline auto xfunction_iterator<F, CT...>::operator-(const self_type& rhs) const -> difference_type
983 {
984 return tuple_max_diff(std::make_index_sequence<sizeof...(CT)>(), m_it, rhs.m_it);
985 }
986
987 template <class F, class... CT>
988 inline auto xfunction_iterator<F, CT...>::operator*() const -> reference
989 {
990 return deref_impl(std::make_index_sequence<sizeof...(CT)>());
991 }
992
993 template <class F, class... CT>
994 inline bool xfunction_iterator<F, CT...>::equal(const self_type& rhs) const
995 {
996 // Optimization: no need to compare each subiterator since they all
997 // are incremented decremented together.
998 constexpr std::size_t temp = xtl::mpl::find_if<is_not_xdummy_iterator, data_type>::value;
999 constexpr std::size_t index = (temp == std::tuple_size<data_type>::value) ? 0 : temp;
1000 return std::get<index>(m_it) == std::get<index>(rhs.m_it);
1001 }
1002
1003 template <class F, class... CT>
1004 inline bool xfunction_iterator<F, CT...>::less_than(const self_type& rhs) const
1005 {
1006 // Optimization: no need to compare each subiterator since they all
1007 // are incremented decremented together.
1008 constexpr std::size_t temp = xtl::mpl::find_if<is_not_xdummy_iterator, data_type>::value;
1009 constexpr std::size_t index = (temp == std::tuple_size<data_type>::value) ? 0 : temp;
1010 return std::get<index>(m_it) < std::get<index>(rhs.m_it);
1011 }
1012
1013 template <class F, class... CT>
1014 template <std::size_t... I>
1015 inline auto xfunction_iterator<F, CT...>::deref_impl(std::index_sequence<I...>) const -> reference
1016 {
1017 return (p_f->m_f)(*std::get<I>(m_it)...);
1018 }
1019
1020 template <class F, class... CT>
1021 template <std::size_t... I>
1022 inline auto xfunction_iterator<F, CT...>::tuple_max_diff(
1023 std::index_sequence<I...>,
1024 const data_type& lhs,
1025 const data_type& rhs
1026 ) const -> difference_type
1027 {
1028 auto diff = std::make_tuple((std::get<I>(lhs) - std::get<I>(rhs))...);
1029 auto func = [](difference_type n, auto&& v)
1030 {
1031 return (std::max)(n, v);
1032 };
1033 return accumulate(func, difference_type(0), diff);
1034 }
1035
1036 template <class F, class... CT>
1037 inline bool operator==(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2)
1038 {
1039 return it1.equal(it2);
1040 }
1041
1042 template <class F, class... CT>
1043 inline bool operator<(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2)
1044 {
1045 return it1.less_than(it2);
1046 }
1047
1048 /************************************
1049 * xfunction_stepper implementation *
1050 ************************************/
1051
1052 template <class F, class... CT>
1053 template <class... St>
1054 inline xfunction_stepper<F, CT...>::xfunction_stepper(const xfunction_type* func, St&&... st) noexcept
1055 : p_f(func)
1056 , m_st(std::forward<St>(st)...)
1057 {
1058 }
1059
1060 template <class F, class... CT>
1061 inline void xfunction_stepper<F, CT...>::step(size_type dim)
1062 {
1063 auto f = [dim](auto& st)
1064 {
1065 st.step(dim);
1066 };
1067 for_each(f, m_st);
1068 }
1069
1070 template <class F, class... CT>
1071 inline void xfunction_stepper<F, CT...>::step_back(size_type dim)
1072 {
1073 auto f = [dim](auto& st)
1074 {
1075 st.step_back(dim);
1076 };
1077 for_each(f, m_st);
1078 }
1079
1080 template <class F, class... CT>
1081 inline void xfunction_stepper<F, CT...>::step(size_type dim, size_type n)
1082 {
1083 auto f = [dim, n](auto& st)
1084 {
1085 st.step(dim, n);
1086 };
1087 for_each(f, m_st);
1088 }
1089
1090 template <class F, class... CT>
1091 inline void xfunction_stepper<F, CT...>::step_back(size_type dim, size_type n)
1092 {
1093 auto f = [dim, n](auto& st)
1094 {
1095 st.step_back(dim, n);
1096 };
1097 for_each(f, m_st);
1098 }
1099
1100 template <class F, class... CT>
1101 inline void xfunction_stepper<F, CT...>::reset(size_type dim)
1102 {
1103 auto f = [dim](auto& st)
1104 {
1105 st.reset(dim);
1106 };
1107 for_each(f, m_st);
1108 }
1109
1110 template <class F, class... CT>
1111 inline void xfunction_stepper<F, CT...>::reset_back(size_type dim)
1112 {
1113 auto f = [dim](auto& st)
1114 {
1115 st.reset_back(dim);
1116 };
1117 for_each(f, m_st);
1118 }
1119
1120 template <class F, class... CT>
1121 inline void xfunction_stepper<F, CT...>::to_begin()
1122 {
1123 auto f = [](auto& st)
1124 {
1125 st.to_begin();
1126 };
1127 for_each(f, m_st);
1128 }
1129
1130 template <class F, class... CT>
1131 inline void xfunction_stepper<F, CT...>::to_end(layout_type l)
1132 {
1133 auto f = [l](auto& st)
1134 {
1135 st.to_end(l);
1136 };
1137 for_each(f, m_st);
1138 }
1139
1140 template <class F, class... CT>
1141 inline auto xfunction_stepper<F, CT...>::operator*() const -> reference
1142 {
1143 return deref_impl(std::make_index_sequence<sizeof...(CT)>());
1144 }
1145
1146 template <class F, class... CT>
1147 template <std::size_t... I>
1148 inline auto xfunction_stepper<F, CT...>::deref_impl(std::index_sequence<I...>) const -> reference
1149 {
1150 return (p_f->m_f)(*std::get<I>(m_st)...);
1151 }
1152
1153 template <class F, class... CT>
1154 template <class T, std::size_t... I>
1155 inline auto xfunction_stepper<F, CT...>::step_simd_impl(std::index_sequence<I...>) -> simd_return_type<T>
1156 {
1157 return (p_f->m_f.simd_apply)(std::get<I>(m_st).template step_simd<T>()...);
1158 }
1159
1160 template <class F, class... CT>
1161 template <class T>
1162 inline auto xfunction_stepper<F, CT...>::step_simd() -> simd_return_type<T>
1163 {
1164 return step_simd_impl<T>(std::make_index_sequence<sizeof...(CT)>());
1165 }
1166
1167 template <class F, class... CT>
1168 inline void xfunction_stepper<F, CT...>::step_leading()
1169 {
1170 auto step_leading_lambda = [](auto&& st)
1171 {
1172 st.step_leading();
1173 };
1174 for_each(step_leading_lambda, m_st);
1175 }
1176}
1177
1178#endif
size_type size() const noexcept
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
size_type size() const noexcept
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
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!