forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxoperation.hpp
More file actions
851 lines (773 loc) · 28.4 KB
/
Copy pathxoperation.hpp
File metadata and controls
851 lines (773 loc) · 28.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XTENSOR_OPERATION_HPP
#define XTENSOR_OPERATION_HPP
#include <algorithm>
#include <functional>
#include <type_traits>
#include <xtl/xsequence.hpp>
#include "xconcepts.hpp"
#include "xfunction.hpp"
#include "xscalar.hpp"
#include "xstrides.hpp"
#include "xstrided_view.hpp"
#include "xmanipulation.hpp"
namespace xt
{
template <class F, class... CT>
class xoptional_function;
/***********
* helpers *
***********/
#define UNARY_OPERATOR_FUNCTOR_IMPL(NAME, OP, R) \
struct NAME \
{ \
template <class A1> \
constexpr auto operator()(const A1& arg) const \
{ \
return OP arg; \
} \
template <class B> \
constexpr auto simd_apply(const B& arg) const \
{ \
return OP arg; \
} \
}
#define UNARY_OPERATOR_FUNCTOR(NAME, OP) UNARY_OPERATOR_FUNCTOR_IMPL(NAME, OP, T)
#define UNARY_BOOL_OPERATOR_FUNCTOR(NAME, OP) UNARY_OPERATOR_FUNCTOR_IMPL(NAME, OP, bool)
/* In this macro, T is assumed to be the promote_type of all arguments.
Nonetheless, operator() is implemented as a function template,
because automatic conversion of the actual argument types to T may
cause 'possible loss of data' warnings, e.g. when T is double and an
argument is uint64_t.
*/
#define BINARY_OPERATOR_FUNCTOR_IMPL(NAME, OP, R) \
struct NAME \
{ \
template <class T1, class T2> \
constexpr auto operator()(const T1& arg1, const T2& arg2) const \
{ \
return (arg1 OP arg2); \
} \
template <class B> \
constexpr auto simd_apply(const B& arg1, const B& arg2) const \
{ \
return (arg1 OP arg2); \
} \
}
#define BINARY_OPERATOR_FUNCTOR(NAME, OP) BINARY_OPERATOR_FUNCTOR_IMPL(NAME, OP, T)
#define BINARY_BOOL_OPERATOR_FUNCTOR(NAME, OP) BINARY_OPERATOR_FUNCTOR_IMPL(NAME, OP, bool)
namespace detail
{
UNARY_OPERATOR_FUNCTOR(identity, +);
UNARY_OPERATOR_FUNCTOR(negate, -);
BINARY_OPERATOR_FUNCTOR(plus, +);
BINARY_OPERATOR_FUNCTOR(minus, -);
BINARY_OPERATOR_FUNCTOR(multiplies, *);
BINARY_OPERATOR_FUNCTOR(divides, /);
BINARY_OPERATOR_FUNCTOR(modulus, %);
BINARY_BOOL_OPERATOR_FUNCTOR(logical_or, ||);
BINARY_BOOL_OPERATOR_FUNCTOR(logical_and, &&);
UNARY_BOOL_OPERATOR_FUNCTOR(logical_not, !);
BINARY_OPERATOR_FUNCTOR(bitwise_or, |);
BINARY_OPERATOR_FUNCTOR(bitwise_and, &);
BINARY_OPERATOR_FUNCTOR(bitwise_xor, ^);
UNARY_OPERATOR_FUNCTOR(bitwise_not, ~);
BINARY_OPERATOR_FUNCTOR(left_shift, <<);
BINARY_OPERATOR_FUNCTOR(right_shift, >>);
BINARY_BOOL_OPERATOR_FUNCTOR(less, <);
BINARY_BOOL_OPERATOR_FUNCTOR(less_equal, <=);
BINARY_BOOL_OPERATOR_FUNCTOR(greater, >);
BINARY_BOOL_OPERATOR_FUNCTOR(greater_equal, >=);
BINARY_BOOL_OPERATOR_FUNCTOR(equal_to, ==);
BINARY_BOOL_OPERATOR_FUNCTOR(not_equal_to, !=);
struct conditional_ternary
{
template <class B>
using get_batch_bool = typename xsimd::simd_traits<typename xsimd::revert_simd_traits<B>::type>::bool_type;
template <class A1, class A2>
constexpr auto operator()(bool t1, const A1& t2, const A2& t3) const noexcept
{
return t1 ? t2 : t3;
}
template <class B>
constexpr B simd_apply(const get_batch_bool<B>& t1,
const B& t2,
const B& t3) const noexcept
{
return xsimd::select(t1, t2, t3);
}
};
template <class R>
struct cast
{
struct functor
{
using result_type = R;
template <class A1>
constexpr result_type operator()(const A1& arg) const
{
return static_cast<R>(arg);
}
// SIMD conversion disabled for now since it does not make sense
// in most of the cases
/*constexpr simd_result_type simd_apply(const simd_value_type& arg) const
{
return static_cast<R>(arg);
}*/
};
};
template <class Tag, class F, class... E>
struct select_xfunction_expression;
template <class F, class... E>
struct select_xfunction_expression<xscalar_expression_tag, F, E...>
{
using type = typename select_xfunction_expression<xtensor_expression_tag, F, E...>::type;
};
template <class F, class... E>
struct select_xfunction_expression<xtensor_expression_tag, F, E...>
{
using type = xfunction<F, E...>;
};
template <class F, class... E>
struct select_xfunction_expression<xoptional_expression_tag, F, E...>
{
using type = xoptional_function<F, E...>;
};
template <class Tag, class F, class... E>
using select_xfunction_expression_t = typename select_xfunction_expression<Tag, F, E...>::type;
template <class Tag, class F, class... E>
struct build_functor_type;
template <class F, class... E>
struct build_functor_type<xscalar_expression_tag, F, E...>
{
using type = typename build_functor_type<xtensor_expression_tag, F, E...>::type;
};
template <class F, class... E>
struct build_functor_type<xtensor_expression_tag, F, E...>
{
using xtype = decltype(std::declval<F>()(std::declval<xvalue_type_t<std::decay_t<E>>>()...));
using type = F;
};
template <class F, class... E>
struct build_functor_type<xoptional_expression_tag, F, E...>
{
using type = F;
};
template <class Tag, class F, class... E>
using build_functor_type_t = typename build_functor_type<Tag, F, E...>::type;
template <class F, class... E>
struct xfunction_type
{
using expression_tag = xexpression_tag_t<E...>;
using functor_type = build_functor_type_t<expression_tag, F, E...>;
using type = select_xfunction_expression_t<expression_tag,
functor_type,
const_xclosure_t<E>...>;
};
template <class F, class... E>
inline auto make_xfunction(E&&... e) noexcept
{
using function_type = xfunction_type<F, E...>;
using functor_type = typename function_type::functor_type;
using type = typename function_type::type;
return type(functor_type(), std::forward<E>(e)...);
}
// On MSVC, the second argument of enable_if_t is always evaluated, even if the condition is false.
// Wrapping the xfunction type in the xfunction_type metafunction avoids this evaluation when
// the condition is false, since it leads to a tricky bug preventing from using operator+ and operator-
// on vector and arrays iterators.
template <class F, class... E>
using xfunction_type_t = typename std::enable_if_t<has_xexpression<std::decay_t<E>...>::value,
xfunction_type<F, E...>>::type;
}
#undef UNARY_OPERATOR_FUNCTOR
#undef UNARY_BOOL_OPERATOR_FUNCTOR
#undef UNARY_OPERATOR_FUNCTOR_IMPL
#undef BINARY_OPERATOR_FUNCTOR
#undef BINARY_BOOL_OPERATOR_FUNCTOR
#undef BINARY_OPERATOR_FUNCTOR_IMPL
/*************
* operators *
*************/
/**
* @defgroup arithmetic_operators Arithmetic operators
*/
/**
* @ingroup arithmetic_operators
* @brief Identity
*
* Returns an \ref xfunction for the element-wise identity
* of \a e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto operator+(E&& e) noexcept
-> detail::xfunction_type_t<detail::identity, E>
{
return detail::make_xfunction<detail::identity>(std::forward<E>(e));
}
/**
* @ingroup arithmetic_operators
* @brief Opposite
*
* Returns an \ref xfunction for the element-wise opposite
* of \a e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto operator-(E&& e) noexcept
-> detail::xfunction_type_t<detail::negate, E>
{
return detail::make_xfunction<detail::negate>(std::forward<E>(e));
}
/**
* @ingroup arithmetic_operators
* @brief Addition
*
* Returns an \ref xfunction for the element-wise addition
* of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator+(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::plus, E1, E2>
{
return detail::make_xfunction<detail::plus>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup arithmetic_operators
* @brief Substraction
*
* Returns an \ref xfunction for the element-wise substraction
* of \a e2 to \a e1.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator-(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::minus, E1, E2>
{
return detail::make_xfunction<detail::minus>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup arithmetic_operators
* @brief Multiplication
*
* Returns an \ref xfunction for the element-wise multiplication
* of \a e1 by \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator*(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::multiplies, E1, E2>
{
return detail::make_xfunction<detail::multiplies>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup arithmetic_operators
* @brief Division
*
* Returns an \ref xfunction for the element-wise division
* of \a e1 by \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator/(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::divides, E1, E2>
{
return detail::make_xfunction<detail::divides>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup arithmetic_operators
* @brief Modulus
*
* Returns an \ref xfunction for the element-wise modulus
* of \a e1 by \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator%(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::modulus, E1, E2>
{
return detail::make_xfunction<detail::modulus>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @defgroup logical_operators Logical operators
*/
/**
* @ingroup logical_operators
* @brief Or
*
* Returns an \ref xfunction for the element-wise or
* of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator||(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::logical_or, E1, E2>
{
return detail::make_xfunction<detail::logical_or>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup logical_operators
* @brief And
*
* Returns an \ref xfunction for the element-wise and
* of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator&&(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::logical_and, E1, E2>
{
return detail::make_xfunction<detail::logical_and>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup logical_operators
* @brief Not
*
* Returns an \ref xfunction for the element-wise not
* of \a e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto operator!(E&& e) noexcept
-> detail::xfunction_type_t<detail::logical_not, E>
{
return detail::make_xfunction<detail::logical_not>(std::forward<E>(e));
}
/**
* @defgroup bitwise_operators Bitwise operators
*/
/**
* @ingroup bitwise_operators
* @brief Bitwise and
*
* Returns an \ref xfunction for the element-wise bitwise and
* of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator&(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::bitwise_and, E1, E2>
{
return detail::make_xfunction<detail::bitwise_and>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup bitwise_operators
* @brief Bitwise or
*
* Returns an \ref xfunction for the element-wise bitwise or
* of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator|(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::bitwise_or, E1, E2>
{
return detail::make_xfunction<detail::bitwise_or>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup bitwise_operators
* @brief Bitwise xor
*
* Returns an \ref xfunction for the element-wise bitwise xor
* of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator^(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::bitwise_xor, E1, E2>
{
return detail::make_xfunction<detail::bitwise_xor>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup bitwise_operators
* @brief Bitwise not
*
* Returns an \ref xfunction for the element-wise bitwise not
* of \a e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto operator~(E&& e) noexcept
-> detail::xfunction_type_t<detail::bitwise_not, E>
{
return detail::make_xfunction<detail::bitwise_not>(std::forward<E>(e));
}
/**
* @ingroup bitwise_operators
* @brief Bitwise left shift
*
* Returns an \ref xfunction for the element-wise bitwise left shift of e1
* by e2.
* @param e1 an \ref xexpression
* @param e2 an \ref xexpression
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto left_shift(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::left_shift, E1, E2>
{
return detail::make_xfunction<detail::left_shift>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup bitwise_operators
* @brief Bitwise left shift
*
* Returns an \ref xfunction for the element-wise bitwise left shift of e1
* by e2.
* @param e1 an \ref xexpression
* @param e2 an \ref xexpression
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto right_shift(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::right_shift, E1, E2>
{
return detail::make_xfunction<detail::right_shift>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @defgroup comparison_operators Comparison operators
*/
/**
* @ingroup comparison_operators
* @brief Lesser than
*
* Returns an \ref xfunction for the element-wise
* lesser than comparison of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator<(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::less, E1, E2>
{
return detail::make_xfunction<detail::less>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup comparison_operators
* @brief Lesser or equal
*
* Returns an \ref xfunction for the element-wise
* lesser or equal comparison of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator<=(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::less_equal, E1, E2>
{
return detail::make_xfunction<detail::less_equal>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup comparison_operators
* @brief Greater than
*
* Returns an \ref xfunction for the element-wise
* greater than comparison of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator>(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::greater, E1, E2>
{
return detail::make_xfunction<detail::greater>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup comparison_operators
* @brief Greater or equal
*
* Returns an \ref xfunction for the element-wise
* greater or equal comparison of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto operator>=(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::greater_equal, E1, E2>
{
return detail::make_xfunction<detail::greater_equal>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup comparison_operators
* @brief Equality
*
* Returns true if \a e1 and \a e2 have the same shape
* and hold the same values. Unlike other comparison
* operators, this does not return an \ref xfunction.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return a boolean
*/
template <class E1, class E2>
inline std::enable_if_t<xoptional_comparable<E1, E2>::value, bool>
operator==(const xexpression<E1>& e1, const xexpression<E2>& e2)
{
const E1& de1 = e1.derived_cast();
const E2& de2 = e2.derived_cast();
bool res = de1.dimension() == de2.dimension() && std::equal(de1.shape().begin(), de1.shape().end(), de2.shape().begin());
auto iter1 = de1.begin();
auto iter2 = de2.begin();
auto iter_end = de1.end();
while (res && iter1 != iter_end)
{
res = (*iter1++ == *iter2++);
}
return res;
}
/**
* @ingroup comparison_operators
* @brief Inequality
*
* Returns true if \a e1 and \a e2 have different shapes
* or hold the different values. Unlike other comparison
* operators, this does not return an \ref xfunction.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return a boolean
*/
template <class E1, class E2>
inline bool operator!=(const xexpression<E1>& e1, const xexpression<E2>& e2)
{
return !(e1 == e2);
}
/**
* @ingroup comparison_operators
* @brief Element-wise equality
*
* Returns an \ref xfunction for the element-wise
* equality of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto equal(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::equal_to, E1, E2>
{
return detail::make_xfunction<detail::equal_to>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup comparison_operators
* @brief Element-wise inequality
*
* Returns an \ref xfunction for the element-wise
* inequality of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto not_equal(E1&& e1, E2&& e2) noexcept
-> detail::xfunction_type_t<detail::not_equal_to, E1, E2>
{
return detail::make_xfunction<detail::not_equal_to>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup logical_operators
* @brief Ternary selection
*
* Returns an \ref xfunction for the element-wise
* ternary selection (i.e. operator ? :) of \a e1,
* \a e2 and \a e3.
* @param e1 a boolean \ref xexpression
* @param e2 an \ref xexpression or a scalar
* @param e3 an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class E1, class E2, class E3>
inline auto where(E1&& e1, E2&& e2, E3&& e3) noexcept
-> detail::xfunction_type_t<detail::conditional_ternary, E1, E2, E3>
{
return detail::make_xfunction<detail::conditional_ternary>(std::forward<E1>(e1), std::forward<E2>(e2), std::forward<E3>(e3));
}
namespace detail
{
template <class S, class I>
inline auto next_idx(const S& shape, I& idx)
{
for (std::size_t j = shape.size(); j > 0; --j)
{
std::size_t i = j - 1;
if (idx[i] >= shape[i] - 1)
{
idx[i] = 0;
}
else
{
idx[i]++;
return idx;
}
}
// return empty index, happens at last iteration step, but remains unused
return I();
}
}
/**
* @ingroup logical_operators
* @brief return vector of indices where T is not zero
*
* @param arr input array
* @return vector of vectors, one for each dimension of arr, containing
* the indices of the non-zero elements in that dimension
*/
template <class T>
inline auto nonzero(const T& arr)
{
auto shape = arr.shape();
using index_type = xindex_type_t<typename T::shape_type>;
using size_type = typename T::size_type;
auto idx = xtl::make_sequence<index_type>(arr.dimension(), 0);
std::vector<std::vector<size_type>> indices(arr.dimension());
size_type total_size = compute_size(shape);
for (size_type i = 0; i < total_size; i++, detail::next_idx(shape, idx))
{
if (arr.element(std::begin(idx), std::end(idx)))
{
for (std::size_t n = 0; n < indices.size(); ++n)
{
indices.at(n).push_back(idx[n]);
}
}
}
return indices;
}
/**
* @ingroup logical_operators
* @brief return indices that are non-zero in the flattened version of arr,
* equivalent to nonzero(ravel<layout_type>(arr))[0];
*
* @param arr input array
* @return indices that are non-zero in the flattened version of arr
*/
template <layout_type L, class T>
inline auto flatnonzero(const T& arr)
{
return nonzero(ravel<L>(arr))[0];
}
/**
* @ingroup logical_operators
* @brief return vector of indices where condition is true
* (equivalent to \a nonzero(condition))
*
* @param condition input array
* @return vector of \a index_types where condition is not equal to zero
*/
template <class T>
inline auto where(const T& condition)
{
return nonzero(condition);
}
/**
* @ingroup logical_operators
* @brief return vector of indices where arr is not zero
*
* @param arr input array
* @return vector of index_types where arr is not equal to zero
*/
template <class T>
inline auto argwhere(const T& arr)
{
auto shape = arr.shape();
using index_type = xindex_type_t<typename T::shape_type>;
using size_type = typename T::size_type;
auto idx = xtl::make_sequence<index_type>(arr.dimension(), 0);
std::vector<index_type> indices;
size_type total_size = compute_size(shape);
for (size_type i = 0; i < total_size; i++, detail::next_idx(shape, idx))
{
if (arr.element(std::begin(idx), std::end(idx)))
{
indices.push_back(idx);
}
}
return indices;
}
/**
* @ingroup logical_operators
* @brief Any
*
* Returns true if any of the values of \a e is truthy,
* false otherwise.
* @param e an \ref xexpression
* @return a boolean
*/
template <class E>
inline bool any(E&& e)
{
using xtype = std::decay_t<E>;
using value_type = typename xtype::value_type;
return std::any_of(e.cbegin(), e.cend(), [](const value_type& el) { return el; });
}
/**
* @ingroup logical_operators
* @brief Any
*
* Returns true if all of the values of \a e are truthy,
* false otherwise.
* @param e an \ref xexpression
* @return a boolean
*/
template <class E>
inline bool all(E&& e)
{
using xtype = std::decay_t<E>;
using value_type = typename xtype::value_type;
return std::all_of(e.cbegin(), e.cend(), [](const value_type& el) { return el; });
}
/**
* @defgroup casting_operators Casting operators
*/
/**
* @ingroup casting_operators
* @brief Element-wise ``static_cast``.
*
* Returns an \ref xfunction for the element-wise
* static_cast of \a e to type R.
*
* @param e an \ref xexpression or a scalar
* @return an \ref xfunction
*/
template <class R, class E>
inline auto cast(E&& e) noexcept
-> detail::xfunction_type_t<typename detail::cast<R>::functor, E>
{
return detail::make_xfunction<typename detail::cast<R>::functor>(std::forward<E>(e));
}
}
#endif