NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
NdArrayOperators.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <cmath>
31#include <complex>
32#include <functional>
33
41
42namespace nc
43{
44 //============================================================================
45 // Method Description:
52 template<typename dtype>
54 {
56
57 return broadcast::broadcaster(lhs, rhs, std::plus<dtype>());
58 }
59
60 //============================================================================
61 // Method Description:
68 template<typename dtype>
69 NdArray<std::complex<dtype>>& operator+=(NdArray<std::complex<dtype>>& lhs, const NdArray<dtype>& rhs)
70 {
72
73 const auto function = [](const std::complex<dtype>& val1, dtype val2) -> std::complex<dtype>
74 { return val1 + val2; };
75 return broadcast::broadcaster(lhs, rhs, function);
76 }
77
78 //============================================================================
79 // Method Description:
86 template<typename dtype>
88 {
90
91 const auto function = [rhs](dtype& value) -> dtype { return value += rhs; };
92
93 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
94
95 return lhs;
96 }
97
98 //============================================================================
99 // Method Description:
106 template<typename dtype>
107 NdArray<std::complex<dtype>>& operator+=(NdArray<std::complex<dtype>>& lhs, dtype rhs)
108 {
110
111 const auto function = [rhs](std::complex<dtype>& value) -> std::complex<dtype> { return value += rhs; };
112
113 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
114
115 return lhs;
116 }
117
118 //============================================================================
119 // Method Description:
126 template<typename dtype>
128 {
130
131 return broadcast::broadcaster<dtype>(lhs, rhs, std::plus<dtype>());
132 }
133
134 //============================================================================
135 // Method Description:
142 template<typename dtype>
143 NdArray<std::complex<dtype>> operator+(const NdArray<dtype>& lhs, const NdArray<std::complex<dtype>>& rhs)
144 {
146
147 const auto function = [](const auto& val1, const auto& val2) -> std::complex<dtype> { return val1 + val2; };
148 return broadcast::broadcaster<std::complex<dtype>>(lhs, rhs, function);
149 }
150
151 //============================================================================
152 // Method Description:
159 template<typename dtype>
160 NdArray<std::complex<dtype>> operator+(const NdArray<std::complex<dtype>>& lhs, const NdArray<dtype>& rhs)
161 {
162 return rhs + lhs;
163 }
164
165 //============================================================================
166 // Method Description:
173 template<typename dtype>
175 {
176 lhs += rhs;
177 return lhs;
178 }
179
180 //============================================================================
181 // Method Description:
188 template<typename dtype>
190 {
191 return rhs + lhs;
192 }
193
194 //============================================================================
195 // Method Description:
202 template<typename dtype>
203 NdArray<std::complex<dtype>> operator+(const NdArray<dtype>& lhs, const std::complex<dtype>& rhs)
204 {
206
207 const auto function = [rhs](dtype value) -> std::complex<dtype> { return value + rhs; };
208
209 NdArray<std::complex<dtype>> returnArray(lhs.shape());
210
211 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), function);
212
213 return returnArray;
214 }
215
216 //============================================================================
217 // Method Description:
224 template<typename dtype>
225 NdArray<std::complex<dtype>> operator+(const std::complex<dtype>& lhs, const NdArray<dtype>& rhs)
226 {
227 return rhs + lhs;
228 }
229
230 //============================================================================
231 // Method Description:
238 template<typename dtype>
239 NdArray<std::complex<dtype>> operator+(NdArray<std::complex<dtype>> lhs, dtype rhs)
240 {
241 lhs += rhs;
242 return lhs;
243 }
244
245 //============================================================================
246 // Method Description:
253 template<typename dtype>
254 NdArray<std::complex<dtype>> operator+(dtype lhs, const NdArray<std::complex<dtype>>& rhs)
255 {
256 return rhs + lhs;
257 }
258
259 //============================================================================
260 // Method Description:
267 template<typename dtype>
269 {
271
272 return broadcast::broadcaster(lhs, rhs, std::minus<dtype>());
273 }
274
275 //============================================================================
276 // Method Description:
283 template<typename dtype>
284 NdArray<std::complex<dtype>>& operator-=(NdArray<std::complex<dtype>>& lhs, const NdArray<dtype>& rhs)
285 {
287
288 const auto function = [](const std::complex<dtype>& val1, dtype val2) -> std::complex<dtype>
289 { return val1 - val2; };
290 return broadcast::broadcaster(lhs, rhs, function);
291 }
292
293 //============================================================================
294 // Method Description:
301 template<typename dtype>
303 {
305
306 const auto function = [rhs](dtype& value) -> dtype { return value -= rhs; };
307
308 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
309
310 return lhs;
311 }
312
313 //============================================================================
314 // Method Description:
321 template<typename dtype>
322 NdArray<std::complex<dtype>>& operator-=(NdArray<std::complex<dtype>>& lhs, dtype rhs)
323 {
325
326 const auto function = [rhs](std::complex<dtype>& value) -> std::complex<dtype> { return value -= rhs; };
327
328 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
329
330 return lhs;
331 }
332
333 //============================================================================
334 // Method Description:
341 template<typename dtype>
343 {
345
346 return broadcast::broadcaster<dtype>(lhs, rhs, std::minus<dtype>());
347 }
348
349 //============================================================================
350 // Method Description:
357 template<typename dtype>
358 NdArray<std::complex<dtype>> operator-(const NdArray<dtype>& lhs, const NdArray<std::complex<dtype>>& rhs)
359 {
361
362 const auto function = [](const auto& val1, const auto& val2) -> std::complex<dtype> { return val1 - val2; };
363 return broadcast::broadcaster<std::complex<dtype>>(lhs, rhs, function);
364 }
365
366 //============================================================================
367 // Method Description:
374 template<typename dtype>
375 NdArray<std::complex<dtype>> operator-(const NdArray<std::complex<dtype>>& lhs, const NdArray<dtype>& rhs)
376 {
378
379 const auto function = [](const auto& val1, const auto& val2) -> std::complex<dtype> { return val1 - val2; };
380 return broadcast::broadcaster<std::complex<dtype>>(lhs, rhs, function);
381 }
382
383 //============================================================================
384 // Method Description:
391 template<typename dtype>
393 {
394 lhs -= rhs;
395 return lhs;
396 }
397
398 //============================================================================
399 // Method Description:
406 template<typename dtype>
408 {
410
411 const auto function = [lhs](dtype value) -> dtype { return lhs - value; };
412
413 NdArray<dtype> returnArray(rhs.shape());
414
415 stl_algorithms::transform(rhs.cbegin(), rhs.cend(), returnArray.begin(), function);
416
417 return returnArray;
418 }
419
420 //============================================================================
421 // Method Description:
428 template<typename dtype>
429 NdArray<std::complex<dtype>> operator-(const NdArray<dtype>& lhs, const std::complex<dtype>& rhs)
430 {
432
433 const auto function = [rhs](dtype value) -> std::complex<dtype> { return value - rhs; };
434
435 NdArray<std::complex<dtype>> returnArray(lhs.shape());
436
437 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), function);
438
439 return returnArray;
440 }
441
442 //============================================================================
443 // Method Description:
450 template<typename dtype>
451 NdArray<std::complex<dtype>> operator-(const std::complex<dtype>& lhs, const NdArray<dtype>& rhs)
452 {
454
455 const auto function = [lhs](dtype value) -> std::complex<dtype> { return lhs - value; };
456
457 NdArray<std::complex<dtype>> returnArray(rhs.shape());
458
459 stl_algorithms::transform(rhs.cbegin(), rhs.cend(), returnArray.begin(), function);
460
461 return returnArray;
462 }
463
464 //============================================================================
465 // Method Description:
472 template<typename dtype>
473 NdArray<std::complex<dtype>> operator-(NdArray<std::complex<dtype>> lhs, dtype rhs)
474 {
475 lhs -= rhs;
476 return lhs;
477 }
478
479 //============================================================================
480 // Method Description:
487 template<typename dtype>
488 NdArray<std::complex<dtype>> operator-(dtype lhs, const NdArray<std::complex<dtype>>& rhs)
489 {
491
492 const auto function = [lhs](std::complex<dtype> value) -> std::complex<dtype> { return lhs - value; };
493
494 NdArray<std::complex<dtype>> returnArray(rhs.shape());
495
496 stl_algorithms::transform(rhs.cbegin(), rhs.cend(), returnArray.begin(), function);
497
498 return returnArray;
499 }
500
501 //============================================================================
502 // Method Description:
507 template<typename dtype>
509 {
510 const auto function = [](dtype value) -> dtype { return -value; };
511
512 auto returnArray = NdArray<dtype>(inArray.shape());
513 stl_algorithms::transform(inArray.cbegin(), inArray.cend(), returnArray.begin(), function);
514 return returnArray;
515 }
516
517 //============================================================================
518 // Method Description:
525 template<typename dtype>
527 {
529
530 return broadcast::broadcaster(lhs, rhs, std::multiplies<dtype>());
531 }
532
533 //============================================================================
534 // Method Description:
541 template<typename dtype>
542 NdArray<std::complex<dtype>>& operator*=(NdArray<std::complex<dtype>>& lhs, const NdArray<dtype>& rhs)
543 {
545
546 const auto function = [](const std::complex<dtype>& val1, dtype val2) -> std::complex<dtype>
547 { return val1 * val2; };
548 return broadcast::broadcaster(lhs, rhs, function);
549 }
550
551 //============================================================================
552 // Method Description:
559 template<typename dtype>
561 {
563
564 const auto function = [rhs](dtype& value) -> dtype { return value *= rhs; };
565
566 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
567
568 return lhs;
569 }
570
571 //============================================================================
572 // Method Description:
579 template<typename dtype>
580 NdArray<std::complex<dtype>>& operator*=(NdArray<std::complex<dtype>>& lhs, dtype rhs)
581 {
583
584 const auto function = [rhs](std::complex<dtype>& value) -> std::complex<dtype> { return value *= rhs; };
585
586 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
587
588 return lhs;
589 }
590
591 //============================================================================
592 // Method Description:
599 template<typename dtype>
601 {
603
604 return broadcast::broadcaster<dtype>(lhs, rhs, std::multiplies<dtype>());
605 }
606
607 //============================================================================
608 // Method Description:
615 template<typename dtype>
616 NdArray<std::complex<dtype>> operator*(const NdArray<dtype>& lhs, const NdArray<std::complex<dtype>>& rhs)
617 {
619
620 const auto function = [](const auto& val1, const auto& val2) -> std::complex<dtype> { return val1 * val2; };
621 return broadcast::broadcaster<std::complex<dtype>>(lhs, rhs, function);
622 }
623
624 //============================================================================
625 // Method Description:
632 template<typename dtype>
633 NdArray<std::complex<dtype>> operator*(const NdArray<std::complex<dtype>>& lhs, const NdArray<dtype>& rhs)
634 {
635 return rhs * lhs;
636 }
637
638 //============================================================================
639 // Method Description:
646 template<typename dtype>
648 {
649 lhs *= rhs;
650 return lhs;
651 }
652
653 //============================================================================
654 // Method Description:
661 template<typename dtype>
663 {
664 return rhs * lhs;
665 }
666
667 //============================================================================
668 // Method Description:
675 template<typename dtype>
676 NdArray<std::complex<dtype>> operator*(const NdArray<dtype>& lhs, const std::complex<dtype>& rhs)
677 {
679
680 const auto function = [rhs](dtype value) -> std::complex<dtype> { return value * rhs; };
681
682 NdArray<std::complex<dtype>> returnArray(lhs.shape());
683
684 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), function);
685
686 return returnArray;
687 }
688
689 //============================================================================
690 // Method Description:
697 template<typename dtype>
698 NdArray<std::complex<dtype>> operator*(const std::complex<dtype>& lhs, const NdArray<dtype>& rhs)
699 {
700 return rhs * lhs;
701 }
702
703 //============================================================================
704 // Method Description:
711 template<typename dtype>
712 NdArray<std::complex<dtype>> operator*(NdArray<std::complex<dtype>> lhs, dtype rhs)
713 {
714 lhs *= rhs;
715 return lhs;
716 }
717
718 //============================================================================
719 // Method Description:
726 template<typename dtype>
727 NdArray<std::complex<dtype>> operator*(dtype lhs, const NdArray<std::complex<dtype>>& rhs)
728 {
729 return rhs * lhs;
730 }
731
732 //============================================================================
733 // Method Description:
740 template<typename dtype>
742 {
744
745 return broadcast::broadcaster(lhs, rhs, std::divides<dtype>());
746 }
747
748 //============================================================================
749 // Method Description:
756 template<typename dtype>
757 NdArray<std::complex<dtype>>& operator/=(NdArray<std::complex<dtype>>& lhs, const NdArray<dtype>& rhs)
758 {
760
761 const auto function = [](const std::complex<dtype>& val1, dtype val2) -> std::complex<dtype>
762 { return val1 / val2; };
763 return broadcast::broadcaster(lhs, rhs, function);
764 }
765
766 //============================================================================
767 // Method Description:
774 template<typename dtype>
776 {
778
779 const auto function = [rhs](dtype& value) -> dtype { return value /= rhs; };
780
781 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
782
783 return lhs;
784 }
785
786 //============================================================================
787 // Method Description:
794 template<typename dtype>
795 NdArray<std::complex<dtype>>& operator/=(NdArray<std::complex<dtype>>& lhs, dtype rhs)
796 {
798
799 const auto function = [rhs](std::complex<dtype>& value) -> std::complex<dtype> { return value /= rhs; };
800
801 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
802
803 return lhs;
804 }
805
806 //============================================================================
807 // Method Description:
814 template<typename dtype>
816 {
818
819 return broadcast::broadcaster<dtype>(lhs, rhs, std::divides<dtype>());
820 }
821
822 //============================================================================
823 // Method Description:
830 template<typename dtype>
831 NdArray<std::complex<dtype>> operator/(const NdArray<dtype>& lhs, const NdArray<std::complex<dtype>>& rhs)
832 {
834
835 const auto function = [](const auto& val1, const auto& val2) -> std::complex<dtype> { return val1 / val2; };
836 return broadcast::broadcaster<std::complex<dtype>>(lhs, rhs, function);
837 }
838
839 //============================================================================
840 // Method Description:
847 template<typename dtype>
848 NdArray<std::complex<dtype>> operator/(const NdArray<std::complex<dtype>>& lhs, const NdArray<dtype>& rhs)
849 {
851
852 const auto function = [](const auto& val1, const auto& val2) -> std::complex<dtype> { return val1 / val2; };
853 return broadcast::broadcaster<std::complex<dtype>>(lhs, rhs, function);
854 }
855
856 //============================================================================
857 // Method Description:
864 template<typename dtype>
866 {
867 lhs /= rhs;
868 return lhs;
869 }
870
871 //============================================================================
872 // Method Description:
879 template<typename dtype>
881 {
883
884 const auto function = [lhs](dtype value) -> dtype { return lhs / value; };
885
886 NdArray<dtype> returnArray(rhs.shape());
887
888 stl_algorithms::transform(rhs.cbegin(), rhs.cend(), returnArray.begin(), function);
889
890 return returnArray;
891 }
892
893 //============================================================================
894 // Method Description:
901 template<typename dtype>
902 NdArray<std::complex<dtype>> operator/(const NdArray<dtype>& lhs, const std::complex<dtype>& rhs)
903 {
905
906 const auto function = [rhs](dtype value) -> std::complex<dtype> { return value / rhs; };
907
908 NdArray<std::complex<dtype>> returnArray(lhs.shape());
909
910 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), function);
911
912 return returnArray;
913 }
914
915 //============================================================================
916 // Method Description:
923 template<typename dtype>
924 NdArray<std::complex<dtype>> operator/(const std::complex<dtype>& lhs, const NdArray<dtype>& rhs)
925 {
927
928 const auto function = [lhs](dtype value) -> std::complex<dtype> { return lhs / value; };
929
930 NdArray<std::complex<dtype>> returnArray(rhs.shape());
931
932 stl_algorithms::transform(rhs.cbegin(), rhs.cend(), returnArray.begin(), function);
933
934 return returnArray;
935 }
936
937 //============================================================================
938 // Method Description:
945 template<typename dtype>
946 NdArray<std::complex<dtype>> operator/(NdArray<std::complex<dtype>> lhs, dtype rhs)
947 {
948 lhs /= rhs;
949 return lhs;
950 }
951
952 //============================================================================
953 // Method Description:
960 template<typename dtype>
961 NdArray<std::complex<dtype>> operator/(dtype lhs, const NdArray<std::complex<dtype>>& rhs)
962 {
964
965 const auto function = [lhs](const std::complex<dtype>& value) -> std::complex<dtype> { return lhs / value; };
966
967 NdArray<std::complex<dtype>> returnArray(rhs.shape());
968
969 stl_algorithms::transform(rhs.cbegin(), rhs.cend(), returnArray.begin(), function);
970
971 return returnArray;
972 }
973
974 //============================================================================
975 // Method Description:
982 template<typename dtype, std::enable_if_t<std::is_integral_v<dtype> || std::is_floating_point_v<dtype>, int> = 0>
984 {
985 if constexpr (std::is_integral_v<dtype>)
986 {
987 return broadcast::broadcaster(lhs, rhs, std::modulus<dtype>());
988 }
989 else
990 {
991 const auto function = [](const dtype value1, const dtype value2) -> dtype
992 { return std::fmod(value1, value2); };
993 return broadcast::broadcaster(lhs, rhs, function);
994 }
995 }
996
997 //============================================================================
998 // Method Description:
1005 template<typename dtype, std::enable_if_t<std::is_integral_v<dtype> || std::is_floating_point_v<dtype>, int> = 0>
1007 {
1008 if constexpr (std::is_integral_v<dtype>)
1009 {
1010 const auto function = [rhs](dtype& value) -> dtype { return value %= rhs; };
1011 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
1012 }
1013 else
1014 {
1015 const auto function = [rhs](dtype& value) -> void { value = std::fmod(value, rhs); };
1016 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
1017 }
1018
1019 return lhs;
1020 }
1021
1022 //============================================================================
1023 // Method Description:
1030 template<typename dtype, std::enable_if_t<std::is_integral_v<dtype> || std::is_floating_point_v<dtype>, int> = 0>
1032 {
1033 if constexpr (std::is_integral_v<dtype>)
1034 {
1035 return broadcast::broadcaster<dtype>(lhs, rhs, std::modulus<dtype>());
1036 }
1037 else
1038 {
1039 const auto function = [](dtype value1, dtype value2) -> dtype { return std::fmod(value1, value2); };
1040 return broadcast::broadcaster<dtype>(lhs, rhs, function);
1041 }
1042 }
1043
1044 //============================================================================
1045 // Method Description:
1052 template<typename dtype>
1054 {
1055 lhs %= rhs;
1056 return lhs;
1057 }
1058
1059 //============================================================================
1060 // Method Description:
1067 template<typename dtype, std::enable_if_t<std::is_integral_v<dtype>, int> = 0>
1069 {
1070 NdArray<dtype> returnArray(rhs.shape());
1072 rhs.end(),
1073 returnArray.begin(),
1074 [lhs](dtype value) -> dtype { return lhs % value; });
1075
1076 return returnArray;
1077 }
1078
1079 //============================================================================
1080 // Method Description:
1087 template<typename dtype, std::enable_if_t<std::is_floating_point_v<dtype>, int> = 0>
1089 {
1090 NdArray<dtype> returnArray(rhs.shape());
1092 rhs.end(),
1093 returnArray.begin(),
1094 [lhs](dtype value) -> dtype { return std::fmod(lhs, value); });
1095
1096 return returnArray;
1097 }
1098
1099 //============================================================================
1100 // Method Description:
1107 template<typename dtype>
1109 {
1110 STATIC_ASSERT_INTEGER(dtype);
1111
1112 return broadcast::broadcaster(lhs, rhs, std::bit_or<dtype>());
1113 }
1114
1115 //============================================================================
1116 // Method Description:
1123 template<typename dtype>
1125 {
1126 STATIC_ASSERT_INTEGER(dtype);
1127
1128 const auto function = [rhs](dtype& value) -> dtype { return value |= rhs; };
1129
1130 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
1131
1132 return lhs;
1133 }
1134
1135 //============================================================================
1136 // Method Description:
1143 template<typename dtype>
1145 {
1146 STATIC_ASSERT_INTEGER(dtype);
1147
1148 return broadcast::broadcaster<dtype>(lhs, rhs, std::bit_or<dtype>());
1149 }
1150
1151 //============================================================================
1152 // Method Description:
1159 template<typename dtype>
1161 {
1162 lhs |= rhs;
1163 return lhs;
1164 }
1165
1166 //============================================================================
1167 // Method Description:
1174 template<typename dtype>
1176 {
1177 return rhs | lhs;
1178 }
1179
1180 //============================================================================
1181 // Method Description:
1188 template<typename dtype>
1190 {
1191 STATIC_ASSERT_INTEGER(dtype);
1192
1193 return broadcast::broadcaster(lhs, rhs, std::bit_and<dtype>());
1194 }
1195
1196 //============================================================================
1197 // Method Description:
1204 template<typename dtype>
1206 {
1207 STATIC_ASSERT_INTEGER(dtype);
1208
1209 const auto function = [rhs](dtype& value) -> dtype { return value &= rhs; };
1210
1211 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
1212
1213 return lhs;
1214 }
1215
1216 //============================================================================
1217 // Method Description:
1224 template<typename dtype>
1226 {
1227 STATIC_ASSERT_INTEGER(dtype);
1228
1229 return broadcast::broadcaster<dtype>(lhs, rhs, std::bit_and<dtype>());
1230 }
1231
1232 //============================================================================
1233 // Method Description:
1240 template<typename dtype>
1242 {
1243 lhs &= rhs;
1244 return lhs;
1245 }
1246
1247 //============================================================================
1248 // Method Description:
1255 template<typename dtype>
1257 {
1258 return rhs & lhs;
1259 }
1260
1261 //============================================================================
1262 // Method Description:
1269 template<typename dtype>
1271 {
1272 STATIC_ASSERT_INTEGER(dtype);
1273
1274 return broadcast::broadcaster(lhs, rhs, std::bit_xor<dtype>());
1275 }
1276
1277 //============================================================================
1278 // Method Description:
1285 template<typename dtype>
1287 {
1288 STATIC_ASSERT_INTEGER(dtype);
1289
1290 const auto function = [rhs](dtype& value) -> dtype { return value ^= rhs; };
1291
1292 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
1293
1294 return lhs;
1295 }
1296
1297 //============================================================================
1298 // Method Description:
1305 template<typename dtype>
1307 {
1308 STATIC_ASSERT_INTEGER(dtype);
1309
1310 return broadcast::broadcaster<dtype>(lhs, rhs, std::bit_xor<dtype>());
1311 }
1312
1313 //============================================================================
1314 // Method Description:
1321 template<typename dtype>
1323 {
1324 lhs ^= rhs;
1325 return lhs;
1326 }
1327
1328 //============================================================================
1329 // Method Description:
1336 template<typename dtype>
1338 {
1339 return rhs ^ lhs;
1340 }
1341
1342 //============================================================================
1343 // Method Description:
1349 template<typename dtype>
1351 {
1352 STATIC_ASSERT_INTEGER(dtype);
1353
1354 const auto function = [](dtype value) -> dtype { return ~value; };
1355
1356 NdArray<dtype> returnArray(inArray.shape());
1357
1358 stl_algorithms::transform(inArray.cbegin(), inArray.cend(), returnArray.begin(), function);
1359
1360 return returnArray;
1361 }
1362
1363 //============================================================================
1364 // Method Description:
1371 template<typename dtype>
1373 {
1375
1376 const auto function = [](dtype value1, dtype value2) -> bool
1377 { return !utils::essentiallyEqual(value1, dtype{ 0 }) && !utils::essentiallyEqual(value2, dtype{ 0 }); };
1378
1379 return broadcast::broadcaster<bool>(lhs, rhs, function);
1380 }
1381
1382 //============================================================================
1383 // Method Description:
1390 template<typename dtype>
1392 {
1394
1395 NdArray<bool> returnArray(lhs.shape());
1396
1397 const auto function = [rhs](dtype value) -> bool { return value && rhs; };
1398
1399 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), function);
1400
1401 return returnArray;
1402 }
1403
1404 //============================================================================
1405 // Method Description:
1412 template<typename dtype>
1414 {
1415 return rhs && lhs;
1416 }
1417
1418 //============================================================================
1419 // Method Description:
1426 template<typename dtype>
1428 {
1430
1431 const auto function = [](dtype value1, dtype value2) -> bool
1432 { return !utils::essentiallyEqual(value1, dtype{ 0 }) || !utils::essentiallyEqual(value2, dtype{ 0 }); };
1433
1434 return broadcast::broadcaster<bool>(lhs, rhs, function);
1435 }
1436
1437 //============================================================================
1438 // Method Description:
1445 template<typename dtype>
1447 {
1449
1450 NdArray<bool> returnArray(lhs.shape());
1451
1452 const auto function = [rhs](dtype value) -> bool { return value || rhs; };
1453
1454 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), function);
1455
1456 return returnArray;
1457 }
1458
1459 //============================================================================
1460 // Method Description:
1467 template<typename dtype>
1469 {
1470 return rhs || lhs;
1471 }
1472
1473 //============================================================================
1474 // Method Description:
1480 template<typename dtype>
1482 {
1484
1485 NdArray<bool> returnArray(inArray.shape());
1486
1487 const auto function = [](dtype value) -> dtype { return !value; };
1488
1489 stl_algorithms::transform(inArray.cbegin(), inArray.cend(), returnArray.begin(), function);
1490
1491 return returnArray;
1492 }
1493
1494 //============================================================================
1495 // Method Description:
1503 template<typename dtype>
1505 {
1506 const auto equalTo = [](dtype lhs_, dtype rhs_) noexcept -> bool
1507 { return utils::essentiallyEqual(lhs_, rhs_); };
1508
1509 return broadcast::broadcaster<bool>(lhs, rhs, equalTo);
1510 }
1511
1512 //============================================================================
1513 // Method Description:
1521 template<typename dtype>
1522 NdArray<bool> operator==(const NdArray<dtype>& lhs, dtype inValue)
1523 {
1524 NdArray<bool> returnArray(lhs.shape());
1525
1526 const auto equalTo = [inValue](dtype value) noexcept -> bool
1527 { return utils::essentiallyEqual(inValue, value); };
1528
1529 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), equalTo);
1530
1531 return returnArray;
1532 }
1533
1534 //============================================================================
1535 // Method Description:
1543 template<typename dtype>
1544 NdArray<bool> operator==(dtype inValue, const NdArray<dtype>& inArray)
1545 {
1546 return inArray == inValue;
1547 }
1548
1549 //============================================================================
1550 // Method Description:
1558 template<typename dtype>
1560 {
1561 const auto notEqualTo = [](dtype lhs_, dtype rhs_) noexcept -> bool
1562 { return !utils::essentiallyEqual(lhs_, rhs_); };
1563
1564 return broadcast::broadcaster<bool>(lhs, rhs, notEqualTo);
1565 }
1566
1567 //============================================================================
1568 // Method Description:
1576 template<typename dtype>
1577 NdArray<bool> operator!=(const NdArray<dtype>& lhs, dtype inValue)
1578 {
1579 NdArray<bool> returnArray(lhs.shape());
1580
1581 const auto notEqualTo = [inValue](dtype value) noexcept -> bool
1582 { return !utils::essentiallyEqual(inValue, value); };
1583
1584 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), notEqualTo);
1585
1586 return returnArray;
1587 }
1588
1589 //============================================================================
1590 // Method Description:
1598 template<typename dtype>
1599 NdArray<bool> operator!=(dtype inValue, const NdArray<dtype>& inArray)
1600 {
1601 return inArray != inValue;
1602 }
1603
1604 //============================================================================
1605 // Method Description:
1613 template<typename dtype>
1614 NdArray<bool> operator<(const NdArray<dtype>& lhs, const NdArray<dtype>& rhs)
1615 {
1617
1618 const auto function = [](dtype lhs_, dtype rhs_) noexcept -> bool { return lhs_ < rhs_; };
1619 return broadcast::broadcaster<bool>(lhs, rhs, function);
1620 }
1621
1622 //============================================================================
1623 // Method Description:
1631 template<typename dtype>
1632 NdArray<bool> operator<(const NdArray<dtype>& lhs, dtype inValue)
1633 {
1635
1636 NdArray<bool> returnArray(lhs.shape());
1637
1638 const auto function = [inValue](dtype value) noexcept -> bool { return value < inValue; };
1639
1640 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), function);
1641
1642 return returnArray;
1643 }
1644
1645 //============================================================================
1646 // Method Description:
1654 template<typename dtype>
1655 NdArray<bool> operator<(dtype inValue, const NdArray<dtype>& inArray)
1656 {
1658
1659 NdArray<bool> returnArray(inArray.shape());
1660
1661 const auto function = [inValue](dtype value) noexcept -> bool { return inValue < value; };
1662
1663 stl_algorithms::transform(inArray.cbegin(), inArray.cend(), returnArray.begin(), function);
1664
1665 return returnArray;
1666 }
1667
1668 //============================================================================
1669 // Method Description:
1677 template<typename dtype>
1679 {
1681
1682 const auto function = [](dtype lhs_, dtype rhs_) noexcept -> bool { return lhs_ > rhs_; };
1683 return broadcast::broadcaster<bool>(lhs, rhs, function);
1684 }
1685
1686 //============================================================================
1687 // Method Description:
1695 template<typename dtype>
1696 NdArray<bool> operator>(const NdArray<dtype>& lhs, dtype inValue)
1697 {
1699
1700 NdArray<bool> returnArray(lhs.shape());
1701
1702 const auto function = [inValue](dtype value) noexcept -> bool { return value > inValue; };
1703
1704 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), function);
1705
1706 return returnArray;
1707 }
1708
1709 //============================================================================
1710 // Method Description:
1718 template<typename dtype>
1719 NdArray<bool> operator>(dtype inValue, const NdArray<dtype>& inArray)
1720 {
1722
1723 NdArray<bool> returnArray(inArray.shape());
1724
1725 const auto function = [inValue](dtype value) noexcept -> bool { return inValue > value; };
1726
1727 stl_algorithms::transform(inArray.cbegin(), inArray.cend(), returnArray.begin(), function);
1728
1729 return returnArray;
1730 }
1731
1732 //============================================================================
1733 // Method Description:
1741 template<typename dtype>
1742 NdArray<bool> operator<=(const NdArray<dtype>& lhs, const NdArray<dtype>& rhs)
1743 {
1745
1746 const auto function = [](dtype lhs_, dtype rhs_) noexcept -> bool { return lhs_ <= rhs_; };
1747 return broadcast::broadcaster<bool>(lhs, rhs, function);
1748 }
1749
1750 //============================================================================
1751 // Method Description:
1759 template<typename dtype>
1760 NdArray<bool> operator<=(const NdArray<dtype>& lhs, dtype inValue)
1761 {
1763
1764 NdArray<bool> returnArray(lhs.shape());
1765
1766 const auto function = [inValue](dtype value) noexcept -> bool { return value <= inValue; };
1767
1768 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), function);
1769
1770 return returnArray;
1771 }
1772
1773 //============================================================================
1774 // Method Description:
1782 template<typename dtype>
1783 NdArray<bool> operator<=(dtype inValue, const NdArray<dtype>& inArray)
1784 {
1786
1787 NdArray<bool> returnArray(inArray.shape());
1788
1789 const auto function = [inValue](dtype value) noexcept -> bool { return inValue <= value; };
1790
1791 stl_algorithms::transform(inArray.cbegin(), inArray.cend(), returnArray.begin(), function);
1792
1793 return returnArray;
1794 }
1795
1796 //============================================================================
1797 // Method Description:
1805 template<typename dtype>
1807 {
1809
1810 const auto function = [](dtype lhs_, dtype rhs_) noexcept -> bool { return lhs_ >= rhs_; };
1811 return broadcast::broadcaster<bool>(lhs, rhs, function);
1812 }
1813
1814 //============================================================================
1815 // Method Description:
1823 template<typename dtype>
1824 NdArray<bool> operator>=(const NdArray<dtype>& lhs, dtype inValue)
1825 {
1827
1828 NdArray<bool> returnArray(lhs.shape());
1829
1830 const auto function = [inValue](dtype value) noexcept -> bool { return value >= inValue; };
1831
1832 stl_algorithms::transform(lhs.cbegin(), lhs.cend(), returnArray.begin(), function);
1833
1834 return returnArray;
1835 }
1836
1837 //============================================================================
1838 // Method Description:
1846 template<typename dtype>
1847 NdArray<bool> operator>=(dtype inValue, const NdArray<dtype>& inArray)
1848 {
1850
1851 NdArray<bool> returnArray(inArray.shape());
1852
1853 const auto function = [inValue](dtype value) noexcept -> bool { return inValue >= value; };
1854
1855 stl_algorithms::transform(inArray.cbegin(), inArray.cend(), returnArray.begin(), function);
1856
1857 return returnArray;
1858 }
1859
1860 //============================================================================
1861 // Method Description:
1868 template<typename dtype>
1869 NdArray<dtype>& operator<<=(NdArray<dtype>& lhs, uint8 inNumBits)
1870 {
1871 STATIC_ASSERT_INTEGER(dtype);
1872
1873 const auto function = [inNumBits](dtype& value) -> void { value <<= inNumBits; };
1874
1875 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
1876
1877 return lhs;
1878 }
1879
1880 //============================================================================
1881 // Method Description:
1888 template<typename dtype>
1889 NdArray<dtype> operator<<(const NdArray<dtype>& lhs, uint8 inNumBits)
1890 {
1891 STATIC_ASSERT_INTEGER(dtype);
1892
1893 NdArray<dtype> returnArray(lhs);
1894 returnArray <<= inNumBits;
1895 return returnArray;
1896 }
1897
1898 //============================================================================
1899 // Method Description:
1906 template<typename dtype>
1908 {
1909 STATIC_ASSERT_INTEGER(dtype);
1910
1911 const auto function = [inNumBits](dtype& value) -> void { value >>= inNumBits; };
1912
1913 stl_algorithms::for_each(lhs.begin(), lhs.end(), function);
1914
1915 return lhs;
1916 }
1917
1918 //============================================================================
1919 // Method Description:
1926 template<typename dtype>
1928 {
1929 STATIC_ASSERT_INTEGER(dtype);
1930
1931 NdArray<dtype> returnArray(lhs);
1932 returnArray >>= inNumBits;
1933 return returnArray;
1934 }
1935
1936 //============================================================================
1937 // Method Description:
1942 template<typename dtype>
1944 {
1946
1947 const auto function = [](dtype& value) -> void { ++value; };
1948
1949 stl_algorithms::for_each(rhs.begin(), rhs.end(), function);
1950
1951 return rhs;
1952 }
1953
1954 //============================================================================
1955 // Method Description:
1961 template<typename dtype>
1963 {
1964 auto copy = NdArray<dtype>(lhs);
1965 ++lhs;
1966 return copy;
1967 }
1968
1969 //============================================================================
1970 // Method Description:
1975 template<typename dtype>
1977 {
1979
1980 const auto function = [](dtype& value) -> void { --value; };
1981
1982 stl_algorithms::for_each(rhs.begin(), rhs.end(), function);
1983
1984 return rhs;
1985 }
1986
1987 //============================================================================
1988 // Method Description:
1994 template<typename dtype>
1996 {
1997 auto copy = NdArray<dtype>(lhs);
1998 --lhs;
1999 return copy;
2000 }
2001
2002 //============================================================================
2003 // Method Description:
2010 template<typename dtype>
2011 std::ostream& operator<<(std::ostream& inOStream, const NdArray<dtype>& inArray)
2012 {
2014
2015 inOStream << inArray.str();
2016 return inOStream;
2017 }
2018} // namespace nc
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition StaticAsserts.hpp:56
#define STATIC_ASSERT_INTEGER(dtype)
Definition StaticAsserts.hpp:43
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
const_iterator cbegin() const noexcept
Definition NdArrayCore.hpp:1361
iterator end() noexcept
Definition NdArrayCore.hpp:1619
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
const_iterator cend() const noexcept
Definition NdArrayCore.hpp:1669
std::string str() const
Definition NdArrayCore.hpp:4658
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
NdArray< dtypeIn1 > & broadcaster(NdArray< dtypeIn1 > &inArray1, const NdArray< dtypeIn2 > &inArray2, const Function &function, const AdditionalFunctionArgs &&... additionalFunctionArgs)
Definition NdArrayBroadcast.hpp:51
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition StlAlgorithms.hpp:776
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition StlAlgorithms.hpp:226
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition essentiallyEqual.hpp:47
Definition Cartesian.hpp:39
NdArray< dtype > & operator^=(NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1270
bool operator==(const DateTime &lhs, const DateTime &rhs) noexcept
Equality operator for DateTime.
Definition DateTime/DateTime.hpp:472
NdArray< dtype > & operator%=(NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:983
bool operator>=(const std::complex< T > &lhs, const std::complex< T > &rhs) noexcept
Definition StdComplexOperators.hpp:98
Duration operator-(const DateTime &lhs, const DateTime &rhs) noexcept
Subtraction operator.
Definition DateTime/DateTime.hpp:550
bool operator>(const std::complex< T > &lhs, const std::complex< T > &rhs) noexcept
Definition StdComplexOperators.hpp:84
std::uint8_t uint8
Definition Types.hpp:42
NdArray< dtype > operator%(const NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1031
NdArray< dtype > operator/(const NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:815
NdArray< dtype > & operator*=(NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:526
NdArray< dtype > & operator++(NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1943
NdArray< dtype > & operator/=(NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:741
NdArray< dtype > & operator|=(NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1108
NdArray< dtype > & operator--(NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1976
bool operator!=(const DateTime &lhs, const DateTime &rhs) noexcept
Non Equality operator for DateTime.
Definition DateTime/DateTime.hpp:485
NdArray< bool > operator||(const NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1427
bool operator<(const std::complex< T > &lhs, const std::complex< T > &rhs) noexcept
Definition StdComplexOperators.hpp:46
NdArray< dtype > copy(const NdArray< dtype > &inArray)
Definition copy.hpp:44
std::ostream & operator<<(std::ostream &os, Duration duration)
Output stream operator for the Duration type.
Definition Clock.hpp:30
NdArrayConstIterator< dtype, PointerType, DifferenceType > operator+(typename NdArrayConstIterator< dtype, PointerType, DifferenceType >::difference_type offset, NdArrayConstIterator< dtype, PointerType, DifferenceType > next) noexcept
Definition NdArrayIterators.hpp:302
NdArray< dtype > & operator>>=(NdArray< dtype > &lhs, uint8 inNumBits)
Definition NdArrayOperators.hpp:1907
NdArray< dtype > operator^(const NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1306
NdArray< dtype > & operator<<=(NdArray< dtype > &lhs, uint8 inNumBits)
Definition NdArrayOperators.hpp:1869
NdArray< dtype > & operator+=(NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:53
NdArray< dtype > operator&(const NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1225
bool operator<=(const std::complex< T > &lhs, const std::complex< T > &rhs) noexcept
Definition StdComplexOperators.hpp:65
NdArray< dtype > operator|(const NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1144
NdArray< bool > operator!(const NdArray< dtype > &inArray)
Definition NdArrayOperators.hpp:1481
NdArray< dtype > operator>>(const NdArray< dtype > &lhs, uint8 inNumBits)
Definition NdArrayOperators.hpp:1927
NdArray< dtype > & operator&=(NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1189
NdArray< dtype > & operator-=(NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:268
NdArray< bool > operator&&(const NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:1372
NdArray< dtype > operator~(const NdArray< dtype > &inArray)
Definition NdArrayOperators.hpp:1350
NdArray< dtype > operator*(const NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition NdArrayOperators.hpp:600