NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
NdArrayCore.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <array>
31#include <cmath>
32#include <deque>
33#include <filesystem>
34#include <fstream>
35#include <initializer_list>
36#include <iostream>
37#include <iterator>
38#include <list>
39#include <memory>
40#include <numeric>
41#include <string>
42#include <type_traits>
43#include <utility>
44#include <vector>
45
47#include "NumCpp/Core/Enums.hpp"
54#include "NumCpp/Core/Shape.hpp"
55#include "NumCpp/Core/Slice.hpp"
56#include "NumCpp/Core/Types.hpp"
63
64namespace nc
65{
66 namespace type_traits
67 {
68 //============================================================================
69 // Class Description:
72 template<typename>
73 struct is_ndarray_int : std::false_type
74 {
75 };
76
77 //============================================================================
78 // Class Description:
81
82 template<typename dtype, typename Allocator>
83 struct is_ndarray_int<NdArray<dtype, Allocator>>
84 {
85 static constexpr bool value = std::is_integral_v<dtype>;
86 };
87
88 //============================================================================
89 // Class Description:
92 template<typename T>
94
95 //============================================================================
96 // Class Description:
99 template<typename>
100 struct is_ndarray_signed_int : std::false_type
101 {
102 };
103
104 //============================================================================
105 // Class Description:
108
109 template<typename dtype, typename Allocator>
110 struct is_ndarray_signed_int<NdArray<dtype, Allocator>>
111 {
112 static constexpr bool value = std::is_signed_v<dtype>;
113 };
114
115 //============================================================================
116 // Class Description:
119 template<typename T>
121
122 //============================================================================
123 // Class Description:
126 template<typename T>
127 using ndarray_int_concept = std::enable_if_t<is_ndarray_int_v<T>, int>;
128 } // namespace type_traits
129
130 //================================================================================
131 // Class Description:
133 template<typename dtype, class Allocator = std::allocator<dtype>>
135 {
136 private:
138 static_assert(std::is_same_v<dtype, typename Allocator::value_type>,
139 "value_type and Allocator::value_type must match");
140
141 using AllocType = typename std::allocator_traits<Allocator>::template rebind_alloc<dtype>;
142 using AllocTraits = std::allocator_traits<AllocType>;
143
144 public:
146 using value_type = dtype;
147 using allocator_type = Allocator;
148 using pointer = typename AllocTraits::pointer;
149 using const_pointer = typename AllocTraits::const_pointer;
150 using reference = dtype&;
151 using const_reference = const dtype&;
154 using difference_type = typename AllocTraits::difference_type;
155
158 using reverse_iterator = std::reverse_iterator<iterator>;
159 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
160
163 using reverse_column_iterator = std::reverse_iterator<column_iterator>;
164 using const_reverse_column_iterator = std::reverse_iterator<const_column_iterator>;
165
166 //============================================================================
167 // Method Description:
170 NdArray() = default;
171
172 //============================================================================
173 // Method Description:
178 explicit NdArray(size_type inSquareSize) :
179 shape_{ inSquareSize, inSquareSize },
180 size_{ inSquareSize * inSquareSize }
181 {
182 newArray();
183 }
184
185 //============================================================================
186 // Method Description:
192 NdArray(size_type inNumRows, size_type inNumCols) :
193 shape_{ inNumRows, inNumCols },
194 size_{ inNumRows * inNumCols }
195 {
196 newArray();
197 }
198
199 //============================================================================
200 // Method Description:
205 explicit NdArray(const Shape& inShape) :
206 shape_{ inShape },
207 size_{ shape_.size() }
208 {
209 newArray();
210 }
211
212 //============================================================================
213 // Method Description:
218 NdArray(std::initializer_list<dtype> inList) :
219 shape_{ 1, static_cast<uint32>(inList.size()) },
220 size_{ shape_.size() }
221 {
222 newArray();
223 if (size_ > 0)
224 {
225 stl_algorithms::copy(inList.begin(), inList.end(), begin());
226 }
227 }
228
229 //============================================================================
230 // Method Description:
235 NdArray(const std::initializer_list<std::initializer_list<dtype>>& inList) :
236 shape_{ static_cast<uint32>(inList.size()), 0 }
237 {
238 for (const auto& list : inList)
239 {
240 if (shape_.cols == 0)
241 {
242 shape_.cols = static_cast<uint32>(list.size());
243 }
244 else if (list.size() != shape_.cols)
245 {
247 "All rows of the initializer list needs to have the same number of elements");
248 }
249 }
250
251 size_ = shape_.size();
252 newArray();
253 uint32 row = 0;
254 for (const auto& list : inList)
255 {
256 const auto ptr = begin() += row * shape_.cols;
257 stl_algorithms::copy(list.begin(), list.end(), ptr);
258 ++row;
259 }
260 }
261
262 //============================================================================
263 // Method Description:
269 template<size_t ArraySize, std::enable_if_t<is_valid_dtype_v<dtype>, int> = 0>
270 NdArray(std::array<dtype, ArraySize>& inArray, PointerPolicy policy = PointerPolicy::COPY) :
271 shape_{ 1, static_cast<uint32>(ArraySize) },
272 size_{ shape_.size() }
273 {
274 switch (policy)
275 {
277 {
278 newArray();
279 if (size_ > 0)
280 {
281 stl_algorithms::copy(inArray.begin(), inArray.end(), begin());
282 }
283 break;
284 }
286 {
287 array_ = inArray.data();
288 ownsPtr_ = false;
289 break;
290 }
291 default:
292 {
293 THROW_RUNTIME_ERROR("Unimplemented PointerPolicy type");
294 }
295 }
296 }
297
298 //============================================================================
299 // Method Description:
305 template<size_t Dim0Size, size_t Dim1Size>
306 NdArray(std::array<std::array<dtype, Dim1Size>, Dim0Size>& in2dArray,
308 shape_{ static_cast<uint32>(Dim0Size), static_cast<uint32>(Dim1Size) },
309 size_{ shape_.size() }
310 {
311 switch (policy)
312 {
314 {
315 newArray();
316 if (size_ > 0)
317 {
318 const auto start = in2dArray.front().begin();
319 stl_algorithms::copy(start, start + size_, begin());
320 }
321 break;
322 }
324 {
325 array_ = in2dArray.front().data();
326 ownsPtr_ = false;
327 break;
328 }
329 default:
330 {
331 THROW_RUNTIME_ERROR("Unimplemented PointerPolicy type");
332 }
333 }
334 }
335
336 //============================================================================
337 // Method Description:
343 template<std::enable_if_t<is_valid_dtype_v<dtype>, int> = 0>
344 NdArray(std::vector<dtype>& inVector, PointerPolicy policy = PointerPolicy::COPY) :
345 shape_{ 1, static_cast<uint32>(inVector.size()) },
346 size_{ shape_.size() }
347 {
348 switch (policy)
349 {
351 {
352 newArray();
353 if (size_ > 0)
354 {
355 stl_algorithms::copy(inVector.begin(), inVector.end(), begin());
356 }
357 break;
358 }
360 {
361 array_ = inVector.data();
362 ownsPtr_ = false;
363 break;
364 }
365 default:
366 {
367 THROW_RUNTIME_ERROR("Unimplemented PointerPolicy type");
368 }
369 }
370 }
371
372 //============================================================================
373 // Method Description:
378 explicit NdArray(const std::vector<std::vector<dtype>>& in2dVector) :
379 shape_{ static_cast<uint32>(in2dVector.size()), 0 }
380 {
381 for (const auto& row : in2dVector)
382 {
383 if (shape_.cols == 0)
384 {
385 shape_.cols = static_cast<uint32>(row.size());
386 }
387 else if (row.size() != shape_.cols)
388 {
389 THROW_INVALID_ARGUMENT_ERROR("All rows of the 2d vector need to have the same number of elements");
390 }
391 }
392
393 size_ = shape_.size();
394
395 newArray();
396 auto currentPosition = begin();
397 for (const auto& row : in2dVector)
398 {
399 stl_algorithms::copy(row.begin(), row.end(), currentPosition);
400 currentPosition += shape_.cols;
401 }
402 }
403
404 //============================================================================
405 // Method Description:
411 template<size_t Dim1Size>
412 NdArray(std::vector<std::array<dtype, Dim1Size>>& in2dArray, PointerPolicy policy = PointerPolicy::COPY) :
413 shape_{ static_cast<uint32>(in2dArray.size()), static_cast<uint32>(Dim1Size) },
414 size_{ shape_.size() }
415 {
416 switch (policy)
417 {
419 {
420 newArray();
421 if (size_ > 0)
422 {
423 const auto start = in2dArray.front().begin();
424 stl_algorithms::copy(start, start + size_, begin());
425 }
426 break;
427 }
429 {
430 array_ = in2dArray.front().data();
431 ownsPtr_ = false;
432 break;
433 }
434 default:
435 {
436 THROW_RUNTIME_ERROR("Unimplemented PointerPolicy type");
437 }
438 }
439 }
440
441 //============================================================================
442 // Method Description:
447 template<std::enable_if_t<is_valid_dtype_v<dtype>, int> = 0>
448 explicit NdArray(const std::deque<dtype>& inDeque) :
449 shape_{ 1, static_cast<uint32>(inDeque.size()) },
450 size_{ shape_.size() }
451 {
452 newArray();
453 if (size_ > 0)
454 {
455 stl_algorithms::copy(inDeque.begin(), inDeque.end(), begin());
456 }
457 }
458
459 //============================================================================
460 // Method Description:
465 explicit NdArray(const std::deque<std::deque<dtype>>& in2dDeque) :
466 shape_{ static_cast<uint32>(in2dDeque.size()), 0 }
467 {
468 for (const auto& row : in2dDeque)
469 {
470 if (shape_.cols == 0)
471 {
472 shape_.cols = static_cast<uint32>(row.size());
473 }
474 else if (row.size() != shape_.cols)
475 {
476 THROW_INVALID_ARGUMENT_ERROR("All rows of the 2d vector need to have the same number of elements");
477 }
478 }
479
480 size_ = shape_.size();
481
482 newArray();
483 auto currentPosition = begin();
484 for (const auto& row : in2dDeque)
485 {
486 stl_algorithms::copy(row.begin(), row.end(), currentPosition);
487 currentPosition += shape_.cols;
488 }
489 }
490
491 //============================================================================
492 // Method Description:
497 explicit NdArray(const std::list<dtype>& inList) :
498 shape_{ 1, static_cast<uint32>(inList.size()) },
499 size_{ shape_.size() }
500 {
501 newArray();
502 if (size_ > 0)
503 {
504 stl_algorithms::copy(inList.begin(), inList.end(), begin());
505 }
506 }
507
508 //============================================================================
509 // Method Description:
515 template<typename Iterator,
516 std::enable_if_t<std::is_same_v<typename std::iterator_traits<Iterator>::value_type, dtype>, int> = 0>
517 NdArray(Iterator inFirst, Iterator inLast) :
518 shape_{ 1, static_cast<uint32>(std::distance(inFirst, inLast)) },
519 size_{ shape_.size() }
520 {
521 newArray();
522 if (size_ > 0)
523 {
524 stl_algorithms::copy(inFirst, inLast, begin());
525 }
526 }
527
528 //============================================================================
529 // Method Description:
535 template<typename UIntType,
536 std::enable_if_t<std::is_integral_v<UIntType> && !std::is_same_v<UIntType, bool>, int> = 0>
537 NdArray(const_pointer inPtr, UIntType size) :
538 NdArray(inPtr, 1, size)
539 {
540 }
541
542 //============================================================================
543 // Method Description:
550 template<typename UIntType1,
551 typename UIntType2,
552 std::enable_if_t<std::is_integral_v<UIntType1> && !std::is_same_v<UIntType1, bool>, int> = 0,
553 std::enable_if_t<std::is_integral_v<UIntType2> && !std::is_same_v<UIntType2, bool>, int> = 0>
554 NdArray(const_pointer inPtr, UIntType1 numRows, UIntType2 numCols) :
555 shape_(numRows, numCols),
556 size_{ shape_.size() }
557 {
558 newArray();
559 if (inPtr != nullptr && size_ > 0)
560 {
561 stl_algorithms::copy(inPtr, inPtr + size_, begin());
562 }
563 }
564
565 //============================================================================
566 // Method Description:
574 template<typename UIntType,
575 std::enable_if_t<std::is_integral_v<UIntType> && !std::is_same_v<UIntType, bool>, int> = 0>
576 NdArray(pointer inPtr, UIntType size, PointerPolicy policy) :
577 NdArray(inPtr, 1, size, policy)
578 {
579 }
580
581 //============================================================================
582 // Method Description:
591 template<typename UIntType1,
592 typename UIntType2,
593 std::enable_if_t<std::is_integral_v<UIntType1> && !std::is_same_v<UIntType1, bool>, int> = 0,
594 std::enable_if_t<std::is_integral_v<UIntType2> && !std::is_same_v<UIntType2, bool>, int> = 0>
595 NdArray(pointer inPtr, UIntType1 numRows, UIntType2 numCols, PointerPolicy policy) :
596 shape_(numRows, numCols),
597 size_{ shape_.size() }
598 {
599 switch (policy)
600 {
602 {
603 newArray();
604 if (inPtr != nullptr && size_ > 0)
605 {
606 stl_algorithms::copy(inPtr, inPtr + size_, begin());
607 }
608 break;
609 }
611 {
612 array_ = inPtr;
613 ownsPtr_ = false;
614 break;
615 }
616 default:
617 {
618 THROW_RUNTIME_ERROR("Unimplemented PointerPolicy type");
619 }
620 }
621 }
622
623 //============================================================================
624 // Method Description:
629 NdArray(const self_type& inOtherArray) :
630 shape_{ inOtherArray.shape_ },
631 size_{ inOtherArray.size_ },
632 endianess_{ inOtherArray.endianess_ }
633 {
634 newArray();
635 if (size_ > 0)
636 {
637 stl_algorithms::copy(inOtherArray.cbegin(), inOtherArray.cend(), begin());
638 }
639 }
640
641 //============================================================================
642 // Method Description:
647 NdArray(self_type&& inOtherArray) noexcept :
648 shape_{ inOtherArray.shape_ },
649 size_{ inOtherArray.size_ },
650 endianess_{ inOtherArray.endianess_ },
651 array_{ inOtherArray.array_ },
652 ownsPtr_{ inOtherArray.ownsPtr_ }
653 {
654 inOtherArray.shape_.rows = inOtherArray.shape_.cols = 0;
655 inOtherArray.size_ = 0;
656 inOtherArray.ownsPtr_ = false;
657 inOtherArray.array_ = nullptr;
658 }
659
660 //============================================================================
661 // Method Description:
664 ~NdArray() noexcept
665 {
666 deleteArray();
667 }
668
669 //============================================================================
670 // Method Description:
674 explicit operator bool() const noexcept
675 {
676 return isempty();
677 }
678
679 //============================================================================
680 // Method Description:
687 {
688 if (&rhs != this)
689 {
690 if (rhs.size_ > 0)
691 {
692 newArray(rhs.shape_);
693 endianess_ = rhs.endianess_;
694
695 stl_algorithms::copy(rhs.cbegin(), rhs.cend(), begin());
696 }
697 }
698
699 return *this;
700 }
701
702 //============================================================================
703 // Method Description:
710 self_type& operator=(value_type inValue) noexcept
711 {
712 if (array_ != nullptr)
713 {
714 stl_algorithms::fill(begin(), end(), inValue);
715 }
716
717 return *this;
718 }
719
720 //============================================================================
721 // Method Description:
728 {
729 if (&rhs != this)
730 {
731 deleteArray();
732 shape_ = rhs.shape_;
733 size_ = rhs.size_;
734 endianess_ = rhs.endianess_;
735 array_ = rhs.array_;
736 ownsPtr_ = rhs.ownsPtr_;
737
738 rhs.shape_.rows = rhs.shape_.cols = rhs.size_ = 0;
739 rhs.array_ = nullptr;
740 rhs.ownsPtr_ = false;
741 }
742
743 return *this;
744 }
745
746 //============================================================================
747 // Method Description:
754 {
755 return const_cast<reference>(const_cast<const self_type*>(this)->operator[](inIndex));
756 }
757
758 //============================================================================
759 // Method Description:
765 [[nodiscard]] const_reference operator[](index_type inIndex) const noexcept
766 {
767 if (inIndex < 0)
768 {
769 inIndex += size_;
770 }
771
772 return array_[inIndex];
773 }
774
775 //============================================================================
776 // Method Description:
783 reference operator()(index_type inRowIndex, index_type inColIndex) noexcept
784 {
785 return const_cast<reference>(const_cast<const self_type*>(this)->operator()(inRowIndex, inColIndex));
786 }
787
788 //============================================================================
789 // Method Description:
796 [[nodiscard]] const_reference operator()(index_type inRowIndex, index_type inColIndex) const noexcept
797 {
798 if (inRowIndex < 0)
799 {
800 inRowIndex += shape_.rows;
801 }
802
803 if (inColIndex < 0)
804 {
805 inColIndex += shape_.cols;
806 }
807
808 return array_[inRowIndex * shape_.cols + inColIndex];
809 }
810
811 //============================================================================
812 // Method Description:
819 [[nodiscard]] self_type operator[](Slice inSlice) const
820 {
821 return operator[](toIndices(inSlice, Axis::NONE));
822 }
823
824 //============================================================================
825 // Method Description:
831 [[nodiscard]] self_type operator[](const NdArray<bool>& inMask) const
832 {
833 return operator[](inMask.flatnonzero());
834 }
835
836 //============================================================================
837 // Method Description:
844 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
845 [[nodiscard]] self_type operator[](const Indices& inIndices) const
846 {
847 auto outArray = self_type(1, static_cast<size_type>(inIndices.size()));
848 size_type i = 0;
849 for (auto& index : inIndices)
850 {
851 outArray[i++] = operator[](static_cast<index_type>(index));
852 }
853
854 return outArray;
855 }
856
857 //============================================================================
858 // Method Description:
866 [[nodiscard]] self_type operator()(Slice inRowSlice, Slice inColSlice) const
867 {
868 return operator()(toIndices(inRowSlice, Axis::ROW), toIndices(inColSlice, Axis::COL));
869 }
870
871 //============================================================================
872 // Method Description:
880 [[nodiscard]] self_type operator()(Slice inRowSlice, index_type inColIndex) const
881 {
882 const NdArray<index_type> colIndices = { inColIndex };
883 return operator()(toIndices(inRowSlice, Axis::ROW), colIndices);
884 }
885
886 //============================================================================
887 // Method Description:
895 [[nodiscard]] self_type operator()(index_type inRowIndex, Slice inColSlice) const
896 {
897 const NdArray<index_type> rowIndices = { inRowIndex };
898 return operator()(rowIndices, toIndices(inColSlice, Axis::COL));
899 }
900
901 //============================================================================
902 // Method Description:
910 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
911 [[nodiscard]] self_type operator()(const Indices& rowIndices, index_type colIndex) const
912 {
913 const NdArray<index_type> colIndices = { colIndex };
914 return operator()(rowIndices, colIndices);
915 }
916
917 //============================================================================
918 // Method Description:
926 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
927 [[nodiscard]] self_type operator()(const Indices& rowIndices, Slice colSlice) const
928 {
929 return operator()(rowIndices, toIndices(colSlice, Axis::COL));
930 }
931
932 //============================================================================
933 // Method Description:
941 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
942 [[nodiscard]] self_type operator()(index_type rowIndex, const Indices& colIndices) const
943 {
944 const NdArray<index_type> rowIndices = { rowIndex };
945 return operator()(rowIndices, colIndices);
946 }
947
948 //============================================================================
949 // Method Description:
957 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
958 [[nodiscard]] self_type operator()(Slice rowSlice, const Indices& colIndices) const
959 {
960 return operator()(toIndices(rowSlice, Axis::ROW), colIndices);
961 }
962
963 //============================================================================
964 // Method Description:
972 template<typename RowIndices,
973 typename ColIndices,
976 [[nodiscard]] self_type operator()(const RowIndices& rowIndices, const ColIndices& colIndices) const
977 {
978 self_type returnArray(rowIndices.size(), colIndices.size());
979
980 size_type rowCounter = 0;
981 for (auto rowIter = rowIndices.begin(); rowIter != rowIndices.end(); ++rowIter)
982 {
983 size_type colCounter = 0;
984 for (auto colIter = colIndices.begin(); colIter != colIndices.end(); ++colIter)
985 {
986 returnArray(rowCounter, colCounter++) = operator()(*rowIter, *colIter);
987 }
988
989 ++rowCounter;
990 }
991
992 return returnArray;
993 }
994
995 //============================================================================
996 // Method Description:
1004 [[nodiscard]] Slice cSlice(index_type inStartIdx = 0, size_type inStepSize = 1) const
1005 {
1006 return Slice(inStartIdx, shape_.cols, inStepSize); // NOLINT(modernize-return-braced-init-list)
1007 }
1008
1009 //============================================================================
1010 // Method Description:
1018 [[nodiscard]] Slice rSlice(index_type inStartIdx = 0, size_type inStepSize = 1) const
1019 {
1020 return Slice(inStartIdx, shape_.rows, inStepSize); // NOLINT(modernize-return-braced-init-list)
1021 }
1022
1023 //============================================================================
1024 // Method Description:
1031 {
1032 return const_cast<reference>(const_cast<const self_type*>(this)->at(inIndex));
1033 }
1034
1035 //============================================================================
1036 // Method Description:
1042 [[nodiscard]] const_reference at(index_type inIndex) const
1043 {
1044 // this doesn't allow for calling the first element as -size_...
1045 // but why would you really want to do that anyway?
1046 if (std::abs(inIndex) > static_cast<int64>(size_ - 1))
1047 {
1048 std::string errStr = "Input index " + utils::num2str(inIndex);
1049 errStr += " is out of bounds for array of size " + utils::num2str(size_) + ".";
1051 }
1052
1053 return operator[](inIndex); // cppcheck-suppress returnTempReference
1054 }
1055
1056 //============================================================================
1057 // Method Description:
1064 reference at(index_type inRowIndex, index_type inColIndex)
1065 {
1066 return const_cast<reference>(const_cast<const self_type*>(this)->at(inRowIndex, inColIndex));
1067 }
1068
1069 //============================================================================
1070 // Method Description:
1077 [[nodiscard]] const_reference at(index_type inRowIndex, index_type inColIndex) const
1078 {
1079 // this doesn't allow for calling the first element as -size_...
1080 // but why would you really want to do that anyway?
1081 if (std::abs(inRowIndex) > static_cast<index_type>(shape_.rows - 1))
1082 {
1083 std::string errStr = "Row index " + utils::num2str(inRowIndex);
1084 errStr += " is out of bounds for array of size " + utils::num2str(shape_.rows) + ".";
1086 }
1087
1088 // this doesn't allow for calling the first element as -size_...
1089 // but why would you really want to do that anyway?
1090 if (std::abs(inColIndex) > static_cast<index_type>(shape_.cols - 1))
1091 {
1092 std::string errStr = "Column index " + utils::num2str(inColIndex);
1093 errStr += " is out of bounds for array of size " + utils::num2str(shape_.cols) + ".";
1095 }
1096
1097 return operator()(inRowIndex, inColIndex); // cppcheck-suppress returnTempReference
1098 }
1099
1100 //============================================================================
1101 // Method Description:
1107 [[nodiscard]] self_type at(const Slice& inSlice) const
1108 {
1109 return at(toIndices(inSlice, Axis::NONE));
1110 }
1111
1112 //============================================================================
1113 // Method Description:
1119 [[nodiscard]] self_type at(const NdArray<bool>& inMask) const
1120 {
1121 if (inMask.shape() != shape_)
1122 {
1123 THROW_INVALID_ARGUMENT_ERROR("Input mask must have the same dimensions as array.");
1124 }
1125
1126 return operator[](inMask);
1127 }
1128
1129 //============================================================================
1130 // Method Description:
1136 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
1137 [[nodiscard]] self_type at(const Indices& inIndices) const
1138 {
1139 stl_algorithms::for_each(inIndices.begin(),
1140 inIndices.end(),
1141 [this](auto index)
1142 {
1143 auto indexSigned = static_cast<index_type>(index);
1144 if (indexSigned < 0)
1145 {
1146 indexSigned += size_;
1147 }
1148
1149 if (indexSigned < 0 || indexSigned > static_cast<index_type>(size_ - 1))
1150 {
1151 THROW_INVALID_ARGUMENT_ERROR("Index exceeds matrix dimensions");
1152 }
1153 });
1154
1155 return operator[](inIndices);
1156 }
1157
1158 //============================================================================
1159 // Method Description:
1166 [[nodiscard]] self_type at(const Slice& inRowSlice, const Slice& inColSlice) const
1167 {
1168 return at(toIndices(inRowSlice, Axis::ROW), toIndices(inColSlice, Axis::COL));
1169 }
1170
1171 //============================================================================
1172 // Method Description:
1179 [[nodiscard]] self_type at(const Slice& inRowSlice, index_type inColIndex) const
1180 {
1181 const NdArray<index_type> colIndices = { inColIndex };
1182 return at(toIndices(inRowSlice, Axis::ROW), colIndices);
1183 }
1184
1185 //============================================================================
1186 // Method Description:
1193 [[nodiscard]] self_type at(index_type inRowIndex, const Slice& inColSlice) const
1194 {
1195 const NdArray<index_type> rowIndices = { inRowIndex };
1196 return at(rowIndices, toIndices(inColSlice, Axis::COL));
1197 }
1198
1199 //============================================================================
1200 // Method Description:
1207 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
1208 [[nodiscard]] self_type at(const Indices& rowIndices, index_type colIndex) const
1209 {
1210 const NdArray<index_type> colIndices = { colIndex };
1211 return at(rowIndices, colIndices);
1212 }
1213
1214 //============================================================================
1215 // Method Description:
1222 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
1223 [[nodiscard]] self_type at(const Indices& rowIndices, Slice colSlice) const
1224 {
1225 return at(rowIndices, toIndices(colSlice, Axis::COL));
1226 }
1227
1228 //============================================================================
1229 // Method Description:
1236 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
1237 [[nodiscard]] self_type at(index_type rowIndex, const Indices& colIndices) const
1238 {
1239 const NdArray<index_type> rowIndices = { rowIndex };
1240 return at(rowIndices, colIndices);
1241 }
1242
1243 //============================================================================
1244 // Method Description:
1251 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
1252 [[nodiscard]] self_type at(Slice rowSlice, const Indices& colIndices) const
1253 {
1254 return at(toIndices(rowSlice, Axis::ROW), colIndices);
1255 }
1256
1257 //============================================================================
1258 // Method Description:
1265 template<typename RowIndices,
1266 typename ColIndices,
1269 [[nodiscard]] self_type at(const RowIndices& rowIndices, const ColIndices& colIndices) const
1270 {
1271 stl_algorithms::for_each(rowIndices.begin(),
1272 rowIndices.end(),
1273 [this](auto row)
1274 {
1275 auto rowSigned = static_cast<index_type>(row);
1276 if (rowSigned < 0)
1277 {
1278 rowSigned += shape_.rows;
1279 }
1280
1281 if (rowSigned < 0 || rowSigned > static_cast<index_type>(shape_.rows - 1))
1282 {
1283 THROW_INVALID_ARGUMENT_ERROR("Row index exceeds matrix dimensions");
1284 }
1285 });
1286
1287 stl_algorithms::for_each(colIndices.begin(),
1288 colIndices.end(),
1289 [this](auto col)
1290 {
1291 auto colSigned = static_cast<index_type>(col);
1292 if (colSigned < 0)
1293 {
1294 colSigned += shape_.cols;
1295 }
1296
1297 if (colSigned < 0 || colSigned > static_cast<index_type>(shape_.cols - 1))
1298 {
1299 THROW_INVALID_ARGUMENT_ERROR("Column index exceeds matrix dimensions");
1300 }
1301 });
1302
1303 return operator()(rowIndices, colIndices);
1304 }
1305
1306 //============================================================================
1307 // Method Description:
1311 [[nodiscard]] iterator begin() noexcept
1312 {
1313 return iterator(array_);
1314 }
1315
1316 //============================================================================
1317 // Method Description:
1323 [[nodiscard]] iterator begin(size_type inRow)
1324 {
1325 if (inRow >= shape_.rows)
1326 {
1327 THROW_INVALID_ARGUMENT_ERROR("input row is greater than the number of rows in the array.");
1328 }
1329
1330 return begin() += (inRow * shape_.cols);
1331 }
1332
1333 //============================================================================
1334 // Method Description:
1338 [[nodiscard]] const_iterator begin() const noexcept
1339 {
1340 return cbegin();
1341 }
1342
1343 //============================================================================
1344 // Method Description:
1350 [[nodiscard]] const_iterator begin(size_type inRow) const
1351 {
1352 return cbegin(inRow);
1353 }
1354
1355 //============================================================================
1356 // Method Description:
1361 [[nodiscard]] const_iterator cbegin() const noexcept
1362 {
1363 return const_iterator(array_);
1364 }
1365
1366 //============================================================================
1367 // Method Description:
1373 [[nodiscard]] const_iterator cbegin(size_type inRow) const
1374 {
1375 if (inRow >= shape_.rows)
1376 {
1377 THROW_INVALID_ARGUMENT_ERROR("input row is greater than the number of rows in the array.");
1378 }
1379
1380 return cbegin() += (inRow * shape_.cols);
1381 }
1382
1383 //============================================================================
1384 // Method Description:
1388 [[nodiscard]] column_iterator colbegin() noexcept
1389 {
1390 return column_iterator(array_, shape_.rows, shape_.cols);
1391 }
1392
1393 //============================================================================
1394 // Method Description:
1401 {
1402 if (inCol >= shape_.cols)
1403 {
1404 THROW_INVALID_ARGUMENT_ERROR("input col is greater than the number of cols in the array.");
1405 }
1406
1407 return colbegin() += (inCol * shape_.rows);
1408 }
1409
1410 //============================================================================
1411 // Method Description:
1415 [[nodiscard]] const_column_iterator colbegin() const noexcept
1416 {
1417 return ccolbegin();
1418 }
1419
1420 //============================================================================
1421 // Method Description:
1427 [[nodiscard]] const_column_iterator colbegin(size_type inCol) const
1428 {
1429 return ccolbegin(inCol);
1430 }
1431
1432 //============================================================================
1433 // Method Description:
1438 [[nodiscard]] const_column_iterator ccolbegin() const noexcept
1439 {
1440 return const_column_iterator(array_, shape_.rows, shape_.cols);
1441 }
1442
1443 //============================================================================
1444 // Method Description:
1450 [[nodiscard]] const_column_iterator ccolbegin(size_type inCol) const
1451 {
1452 if (inCol >= shape_.cols)
1453 {
1454 THROW_INVALID_ARGUMENT_ERROR("input col is greater than the number of cols in the array.");
1455 }
1456
1457 return ccolbegin() += (inCol * shape_.rows);
1458 }
1459
1460 //============================================================================
1461 // Method Description:
1465 [[nodiscard]] reverse_iterator rbegin() noexcept
1466 {
1467 return reverse_iterator(end());
1468 }
1469
1470 //============================================================================
1471 // Method Description:
1477 [[nodiscard]] reverse_iterator rbegin(size_type inRow)
1478 {
1479 if (inRow >= shape_.rows)
1480 {
1481 THROW_INVALID_ARGUMENT_ERROR("input row is greater than the number of rows in the array.");
1482 }
1483
1484 return rbegin() += (shape_.rows - inRow - 1) * shape_.cols;
1485 }
1486
1487 //============================================================================
1488 // Method Description:
1492 [[nodiscard]] const_reverse_iterator rbegin() const noexcept
1493 {
1494 return crbegin();
1495 }
1496
1497 //============================================================================
1498 // Method Description:
1504 [[nodiscard]] const_reverse_iterator rbegin(size_type inRow) const
1505 {
1506 return crbegin(inRow);
1507 }
1508
1509 //============================================================================
1510 // Method Description:
1515 [[nodiscard]] const_reverse_iterator crbegin() const noexcept
1516 {
1517 return const_reverse_iterator(cend());
1518 }
1519
1520 //============================================================================
1521 // Method Description:
1527 [[nodiscard]] const_reverse_iterator crbegin(size_type inRow) const
1528 {
1529 if (inRow >= shape_.rows)
1530 {
1531 THROW_INVALID_ARGUMENT_ERROR("input row is greater than the number of rows in the array.");
1532 }
1533
1534 return crbegin() += (shape_.rows - inRow - 1) * shape_.cols;
1535 }
1536
1537 //============================================================================
1538 // Method Description:
1542 [[nodiscard]] reverse_column_iterator rcolbegin() noexcept
1543 {
1545 }
1546
1547 //============================================================================
1548 // Method Description:
1555 {
1556 if (inCol >= shape_.cols)
1557 {
1558 THROW_INVALID_ARGUMENT_ERROR("input col is greater than the number of cols in the array.");
1559 }
1560
1561 return rcolbegin() += (shape_.cols - inCol - 1) * shape_.rows;
1562 }
1563
1564 //============================================================================
1565 // Method Description:
1569 [[nodiscard]] const_reverse_column_iterator rcolbegin() const noexcept
1570 {
1571 return crcolbegin();
1572 }
1573
1574 //============================================================================
1575 // Method Description:
1582 {
1583 return crcolbegin(inCol);
1584 }
1585
1586 //============================================================================
1587 // Method Description:
1592 [[nodiscard]] const_reverse_column_iterator crcolbegin() const noexcept
1593 {
1595 }
1596
1597 //============================================================================
1598 // Method Description:
1605 {
1606 if (inCol >= shape_.cols)
1607 {
1608 THROW_INVALID_ARGUMENT_ERROR("input col is greater than the number of cols in the array.");
1609 }
1610
1611 return crcolbegin() += (shape_.cols - inCol - 1) * shape_.rows;
1612 }
1613
1614 //============================================================================
1615 // Method Description:
1619 [[nodiscard]] iterator end() noexcept
1620 {
1621 return begin() += size_;
1622 }
1623
1624 //============================================================================
1625 // Method Description:
1631 [[nodiscard]] iterator end(size_type inRow)
1632 {
1633 if (inRow >= shape_.rows)
1634 {
1635 THROW_INVALID_ARGUMENT_ERROR("input row is greater than the number of rows in the array.");
1636 }
1637
1638 return begin(inRow) += shape_.cols;
1639 }
1640
1641 //============================================================================
1642 // Method Description:
1646 [[nodiscard]] const_iterator end() const noexcept
1647 {
1648 return cend();
1649 }
1650
1651 //============================================================================
1652 // Method Description:
1658 [[nodiscard]] const_iterator end(size_type inRow) const
1659 {
1660 return cend(inRow);
1661 }
1662
1663 //============================================================================
1664 // Method Description:
1669 [[nodiscard]] const_iterator cend() const noexcept
1670 {
1671 return cbegin() += size_;
1672 }
1673
1674 //============================================================================
1675 // Method Description:
1681 [[nodiscard]] const_iterator cend(size_type inRow) const
1682 {
1683 if (inRow >= shape_.rows)
1684 {
1685 THROW_INVALID_ARGUMENT_ERROR("input row is greater than the number of rows in the array.");
1686 }
1687
1688 return cbegin(inRow) += shape_.cols;
1689 }
1690
1691 //============================================================================
1692 // Method Description:
1696 [[nodiscard]] reverse_iterator rend() noexcept
1697 {
1698 return rbegin() += size_;
1699 }
1700
1701 //============================================================================
1702 // Method Description:
1708 [[nodiscard]] reverse_iterator rend(size_type inRow)
1709 {
1710 if (inRow >= shape_.rows)
1711 {
1712 THROW_INVALID_ARGUMENT_ERROR("input row is greater than the number of rows in the array.");
1713 }
1714
1715 return rbegin(inRow) += shape_.cols;
1716 }
1717
1718 //============================================================================
1719 // Method Description:
1723 [[nodiscard]] const_reverse_iterator rend() const noexcept
1724 {
1725 return crend();
1726 }
1727
1728 //============================================================================
1729 // Method Description:
1735 [[nodiscard]] const_reverse_iterator rend(size_type inRow) const
1736 {
1737 return crend(inRow);
1738 }
1739
1740 //============================================================================
1741 // Method Description:
1746 [[nodiscard]] const_reverse_iterator crend() const noexcept
1747 {
1748 return crbegin() += size_;
1749 }
1750
1751 //============================================================================
1752 // Method Description:
1758 [[nodiscard]] const_reverse_iterator crend(size_type inRow) const
1759 {
1760 if (inRow >= shape_.rows)
1761 {
1762 THROW_INVALID_ARGUMENT_ERROR("input row is greater than the number of rows in the array.");
1763 }
1764
1765 return crbegin(inRow) += shape_.cols;
1766 }
1767
1768 //============================================================================
1769 // Method Description:
1773 [[nodiscard]] column_iterator colend() noexcept
1774 {
1775 return colbegin() += size_;
1776 }
1777
1778 //============================================================================
1779 // Method Description:
1785 [[nodiscard]] column_iterator colend(size_type inCol)
1786 {
1787 if (inCol >= shape_.cols)
1788 {
1789 THROW_INVALID_ARGUMENT_ERROR("input col is greater than the number of cols in the array.");
1790 }
1791
1792 return colbegin(inCol) += shape_.rows;
1793 }
1794
1795 //============================================================================
1796 // Method Description:
1800 [[nodiscard]] const_column_iterator colend() const noexcept
1801 {
1802 return ccolend();
1803 }
1804
1805 //============================================================================
1806 // Method Description:
1812 [[nodiscard]] const_column_iterator colend(size_type inCol) const
1813 {
1814 return ccolend(inCol);
1815 }
1816
1817 //============================================================================
1818 // Method Description:
1823 [[nodiscard]] const_column_iterator ccolend() const noexcept
1824 {
1825 return ccolbegin() += size_;
1826 }
1827
1828 //============================================================================
1829 // Method Description:
1835 [[nodiscard]] const_column_iterator ccolend(size_type inCol) const
1836 {
1837 if (inCol >= shape_.cols)
1838 {
1839 THROW_INVALID_ARGUMENT_ERROR("input col is greater than the number of cols in the array.");
1840 }
1841
1842 return ccolbegin(inCol) += shape_.rows;
1843 }
1844
1845 //============================================================================
1846 // Method Description:
1850 [[nodiscard]] reverse_column_iterator rcolend() noexcept
1851 {
1852 return rcolbegin() += size_;
1853 }
1854
1855 //============================================================================
1856 // Method Description:
1863 {
1864 if (inCol >= shape_.cols)
1865 {
1866 THROW_INVALID_ARGUMENT_ERROR("input col is greater than the number of cols in the array.");
1867 }
1868
1869 return rcolbegin(inCol) += shape_.rows;
1870 }
1871
1872 //============================================================================
1873 // Method Description:
1877 [[nodiscard]] const_reverse_column_iterator rcolend() const noexcept
1878 {
1879 return crcolend();
1880 }
1881
1882 //============================================================================
1883 // Method Description:
1890 {
1891 return crcolend(inCol);
1892 }
1893
1894 //============================================================================
1895 // Method Description:
1900 [[nodiscard]] const_reverse_column_iterator crcolend() const noexcept
1901 {
1902 return crcolbegin() += size_;
1903 }
1904
1905 //============================================================================
1906 // Method Description:
1913 {
1914 if (inCol >= shape_.cols)
1915 {
1916 THROW_INVALID_ARGUMENT_ERROR("input col is greater than the number of cols in the array.");
1917 }
1918
1919 return crcolbegin(inCol) += shape_.rows;
1920 }
1921
1922 //============================================================================
1923 // Method Description:
1931 [[nodiscard]] NdArray<bool> all(Axis inAxis = Axis::NONE) const
1932 {
1934
1935 const auto function = [](dtype i) -> bool { return !utils::essentiallyEqual(i, dtype{ 0 }); };
1936
1937 switch (inAxis)
1938 {
1939 case Axis::NONE:
1940 {
1941 NdArray<bool> returnArray = { stl_algorithms::all_of(cbegin(), cend(), function) };
1942 return returnArray;
1943 }
1944 case Axis::COL:
1945 {
1946 NdArray<bool> returnArray(1, shape_.rows);
1947 for (uint32 row = 0; row < shape_.rows; ++row)
1948 {
1949 returnArray(0, row) = stl_algorithms::all_of(cbegin(row), cend(row), function);
1950 }
1951
1952 return returnArray;
1953 }
1954 case Axis::ROW:
1955 {
1956 return transpose().all(Axis::COL);
1957 }
1958 default:
1959 {
1960 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
1961 return {}; // get rid of compiler warning
1962 }
1963 }
1964 }
1965
1966 //============================================================================
1967 // Method Description:
1975 [[nodiscard]] NdArray<bool> any(Axis inAxis = Axis::NONE) const
1976 {
1978
1979 const auto function = [](dtype i) -> bool { return !utils::essentiallyEqual(i, dtype{ 0 }); };
1980
1981 switch (inAxis)
1982 {
1983 case Axis::NONE:
1984 {
1985 NdArray<bool> returnArray = { stl_algorithms::any_of(cbegin(), cend(), function) };
1986 return returnArray;
1987 }
1988 case Axis::COL:
1989 {
1990 NdArray<bool> returnArray(1, shape_.rows);
1991 for (uint32 row = 0; row < shape_.rows; ++row)
1992 {
1993 returnArray(0, row) = stl_algorithms::any_of(cbegin(row), cend(row), function);
1994 }
1995
1996 return returnArray;
1997 }
1998 case Axis::ROW:
1999 {
2000 return transpose().any(Axis::COL);
2001 }
2002 default:
2003 {
2004 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
2005 return {}; // get rid of compiler warning
2006 }
2007 }
2008 }
2009
2010 //============================================================================
2011 // Method Description:
2020 [[nodiscard]] NdArray<size_type> argmax(Axis inAxis = Axis::NONE) const
2021 {
2023
2024 const auto comparitor = [](dtype lhs, dtype rhs) noexcept -> bool { return lhs < rhs; };
2025
2026 switch (inAxis)
2027 {
2028 case Axis::NONE:
2029 {
2030 NdArray<size_type> returnArray = { static_cast<size_type>(
2031 stl_algorithms::max_element(cbegin(), cend(), comparitor) - cbegin()) };
2032 return returnArray;
2033 }
2034 case Axis::COL:
2035 {
2036 NdArray<size_type> returnArray(1, shape_.rows);
2037 for (size_type row = 0; row < shape_.rows; ++row)
2038 {
2039 returnArray(0, row) = static_cast<size_type>(
2041 }
2042
2043 return returnArray;
2044 }
2045 case Axis::ROW:
2046 {
2047 return transpose().argmax(Axis::COL);
2048 }
2049 default:
2050 {
2051 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
2052 return {}; // get rid of compiler warning
2053 }
2054 }
2055 }
2056
2057 //============================================================================
2058 // Method Description:
2067 [[nodiscard]] NdArray<size_type> argmin(Axis inAxis = Axis::NONE) const
2068 {
2070
2071 const auto comparitor = [](dtype lhs, dtype rhs) noexcept -> bool { return lhs < rhs; };
2072
2073 switch (inAxis)
2074 {
2075 case Axis::NONE:
2076 {
2077 NdArray<size_type> returnArray = { static_cast<size_type>(
2078 stl_algorithms::min_element(cbegin(), cend(), comparitor) - cbegin()) };
2079 return returnArray;
2080 }
2081 case Axis::COL:
2082 {
2083 NdArray<size_type> returnArray(1, shape_.rows);
2084 for (size_type row = 0; row < shape_.rows; ++row)
2085 {
2086 returnArray(0, row) = static_cast<size_type>(
2088 }
2089
2090 return returnArray;
2091 }
2092 case Axis::ROW:
2093 {
2094 return transpose().argmin(Axis::COL);
2095 }
2096 default:
2097 {
2098 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
2099 return {}; // get rid of compiler warning
2100 }
2101 }
2102 }
2103
2104 //============================================================================
2105 // Method Description:
2116 [[nodiscard]] NdArray<size_type> argpartition(size_type inKth, Axis inAxis = Axis::NONE) const
2117 {
2119
2120 switch (inAxis)
2121 {
2122 case Axis::NONE:
2123 {
2124 if (inKth >= size_)
2125 {
2126 std::string errStr = "kth(=" + utils::num2str(inKth);
2127 errStr += ") out of bounds (" + utils::num2str(size_) + ")";
2129 }
2130
2131 std::vector<size_type> idx(size_);
2132 std::iota(idx.begin(), idx.end(), 0);
2133
2134 const auto comparitor = [this](size_type i1, size_type i2) noexcept -> bool
2135 { return (*this)[i1] < (*this)[i2]; };
2136
2137 stl_algorithms::nth_element(idx.begin(), idx.begin() + inKth, idx.end(), comparitor);
2138 return NdArray<size_type>(idx); // NOLINT(modernize-return-braced-init-list)
2139 }
2140 case Axis::COL:
2141 {
2142 if (inKth >= shape_.cols)
2143 {
2144 std::string errStr = "kth(=" + utils::num2str(inKth);
2145 errStr += ") out of bounds (" + utils::num2str(shape_.cols) + ")";
2147 }
2148
2149 NdArray<size_type> returnArray(shape_);
2150 std::vector<size_type> idx(shape_.cols);
2151
2152 for (uint32 row = 0; row < shape_.rows; ++row)
2153 {
2154 std::iota(idx.begin(), idx.end(), 0);
2155
2156 const auto comparitor = [this, row](size_type i1, size_type i2) noexcept -> bool
2157 { return operator()(row, i1) < operator()(row, i2); };
2158
2159 stl_algorithms::nth_element(idx.begin(), idx.begin() + inKth, idx.end(), comparitor);
2160
2161 for (index_type col = 0; col < static_cast<index_type>(shape_.cols); ++col)
2162 {
2163 returnArray(row, col) = idx[static_cast<size_type>(col)];
2164 }
2165 }
2166 return returnArray;
2167 }
2168 case Axis::ROW:
2169 {
2170 return transpose().argpartition(inKth, Axis::COL).transpose();
2171 }
2172 default:
2173 {
2174 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
2175 return {}; // get rid of compiler warning
2176 }
2177 }
2178 }
2179
2180 //============================================================================
2181 // Method Description:
2189 [[nodiscard]] NdArray<size_type> argsort(Axis inAxis = Axis::NONE) const
2190 {
2192
2193 switch (inAxis)
2194 {
2195 case Axis::NONE:
2196 {
2197 std::vector<size_type> idx(size_);
2198 std::iota(idx.begin(), idx.end(), 0);
2199
2200 const auto comparitor = [this](size_type i1, size_type i2) noexcept -> bool
2201 { return (*this)[i1] < (*this)[i2]; };
2202
2203 stl_algorithms::stable_sort(idx.begin(), idx.end(), comparitor);
2204 return NdArray<size_type>(idx); // NOLINT(modernize-return-braced-init-list)
2205 }
2206 case Axis::COL:
2207 {
2208 NdArray<size_type> returnArray(shape_);
2209 std::vector<size_type> idx(shape_.cols);
2210
2211 for (index_type row = 0; row < static_cast<index_type>(shape_.rows); ++row)
2212 {
2213 std::iota(idx.begin(), idx.end(), 0);
2214
2215 const auto comparitor = [this, row](size_type i1, size_type i2) noexcept -> bool
2216 { return operator()(row, i1) < operator()(row, i2); };
2217
2218 stl_algorithms::stable_sort(idx.begin(), idx.end(), comparitor);
2219
2220 for (index_type col = 0; col < static_cast<index_type>(shape_.cols); ++col)
2221 {
2222 returnArray(row, col) = idx[static_cast<size_type>(col)];
2223 }
2224 }
2225 return returnArray;
2226 }
2227 case Axis::ROW:
2228 {
2229 return transpose().argsort(Axis::COL).transpose();
2230 }
2231 default:
2232 {
2233 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
2234 return {}; // get rid of compiler warning
2235 }
2236 }
2237 }
2238
2239 //============================================================================
2240 // Method Description:
2248 template<typename dtypeOut,
2249 typename dtype_ = dtype,
2250 std::enable_if_t<std::is_same_v<dtype_, dtype>, int> = 0,
2251 std::enable_if_t<std::is_arithmetic_v<dtype_>, int> = 0,
2252 std::enable_if_t<std::is_arithmetic_v<dtypeOut>, int> = 0>
2253 [[nodiscard]] NdArray<dtypeOut> astype() const
2254 {
2255 if constexpr (std::is_same_v<dtypeOut, dtype>)
2256 {
2257 return *this;
2258 }
2259 else
2260 {
2261 NdArray<dtypeOut> outArray(shape_);
2263 cend(),
2264 outArray.begin(),
2265 [](dtype value) -> dtypeOut { return static_cast<dtypeOut>(value); });
2266
2267 return outArray;
2268 }
2269 }
2270
2271 //============================================================================
2272 // Method Description:
2280 template<typename dtypeOut,
2281 typename dtype_ = dtype,
2282 std::enable_if_t<std::is_same_v<dtype_, dtype>, int> = 0,
2283 std::enable_if_t<std::is_arithmetic_v<dtype_>, int> = 0,
2284 std::enable_if_t<is_complex_v<dtypeOut>, int> = 0>
2285 [[nodiscard]] NdArray<dtypeOut> astype() const
2286 {
2287 NdArray<dtypeOut> outArray(shape_);
2288
2289 const auto function = [](const_reference value) -> dtypeOut
2290 { return std::complex<typename dtypeOut::value_type>(value); };
2291
2292 stl_algorithms::transform(cbegin(), cend(), outArray.begin(), function);
2293
2294 return outArray;
2295 }
2296
2297 //============================================================================
2298 // Method Description:
2306 template<typename dtypeOut,
2307 typename dtype_ = dtype,
2308 std::enable_if_t<std::is_same_v<dtype_, dtype>, int> = 0,
2309 std::enable_if_t<is_complex_v<dtype_>, int> = 0,
2310 std::enable_if_t<is_complex_v<dtypeOut>, int> = 0>
2311 [[nodiscard]] NdArray<dtypeOut> astype() const
2312 {
2313 if constexpr (std::is_same_v<dtypeOut, dtype>)
2314 {
2315 return *this;
2316 }
2317 else
2318 {
2319 const auto function = [](const_reference value) noexcept -> dtypeOut
2321
2322 NdArray<dtypeOut> outArray(shape_);
2323 stl_algorithms::transform(cbegin(), cend(), outArray.begin(), function);
2324 return outArray;
2325 }
2326 }
2327
2328 //============================================================================
2329 // Method Description:
2337 template<typename dtypeOut,
2338 typename dtype_ = dtype,
2339 std::enable_if_t<std::is_same_v<dtype_, dtype>, int> = 0,
2340 std::enable_if_t<is_complex_v<dtype_>, int> = 0,
2341 std::enable_if_t<std::is_arithmetic_v<dtypeOut>, int> = 0>
2342 [[nodiscard]] NdArray<dtypeOut> astype() const
2343 {
2344 NdArray<dtypeOut> outArray(shape_);
2345
2346 const auto function = [](const_reference value) -> dtypeOut { return static_cast<dtypeOut>(value.real()); };
2347
2348 stl_algorithms::transform(cbegin(), cend(), outArray.begin(), function);
2349
2350 return outArray;
2351 }
2352
2353 //============================================================================
2354 // Method Description:
2359 [[nodiscard]] const_reference back() const noexcept
2360 {
2361 return *(cend() - 1);
2362 }
2363
2364 //============================================================================
2365 // Method Description:
2370 reference back() noexcept
2371 {
2372 return *(end() - 1);
2373 }
2374
2375 //============================================================================
2376 // Method Description:
2381 [[nodiscard]] const_reference back(size_type row) const
2382 {
2383 return *(cend(row) - 1);
2384 }
2385
2386 //============================================================================
2387 // Method Description:
2393 {
2394 return *(end(row) - 1);
2395 }
2396
2397 //============================================================================
2398 // Method Description:
2406 {
2407 STATIC_ASSERT_INTEGER(dtype);
2408
2410 end(),
2411 [](dtype& value) noexcept -> void { value = endian::byteSwap(value); });
2412
2413 switch (endianess_)
2414 {
2415 case Endian::NATIVE:
2416 {
2418 break;
2419 }
2420 case Endian::LITTLE:
2421 {
2422 endianess_ = Endian::BIG;
2423 break;
2424 }
2425 case Endian::BIG:
2426 {
2427 endianess_ = Endian::LITTLE;
2428 break;
2429 }
2430 }
2431
2432 return *this;
2433 }
2434
2435 //============================================================================
2436 // Method Description:
2445 [[nodiscard]] self_type clip(value_type inMin, value_type inMax) const
2446 {
2448
2449 self_type outArray(shape_);
2451 cend(),
2452 outArray.begin(),
2453 [inMin, inMax](dtype value) noexcept -> dtype
2454 {
2455#ifdef __cpp_lib_clamp
2456 const auto comparitor = [](dtype lhs, dtype rhs) noexcept -> bool
2457 { return lhs < rhs; };
2458
2459 return std::clamp(value, inMin, inMax, comparitor);
2460#else
2461 if (value < inMin)
2462 {
2463 return inMin;
2464 }
2465 else if (value > inMax)
2466 {
2467 return inMax;
2468 }
2469
2470 return value;
2471#endif
2472 });
2473
2474 return outArray;
2475 }
2476
2477 //============================================================================
2478 // Method Description:
2484 [[nodiscard]] self_type column(size_type inColumn) const
2485 {
2486 return operator()(rSlice(), inColumn);
2487 }
2488
2489 //============================================================================
2490 // Method Description:
2496 [[nodiscard]] self_type columns(const NdArray<size_type>& inCols) const
2497 {
2498 auto returnArray = self_type(shape_.rows, inCols.size());
2499 const auto rSlice = returnArray.rSlice();
2500
2501 for (size_type i = 0; i < inCols.size(); ++i)
2502 {
2503 returnArray.put(rSlice, i, column(inCols[i]));
2504 }
2505
2506 return returnArray;
2507 }
2508
2509 //============================================================================
2510 // Method Description:
2517 [[nodiscard]] NdArray<bool> contains(value_type inValue, Axis inAxis = Axis::NONE) const
2518 {
2520
2521 switch (inAxis)
2522 {
2523 case Axis::NONE:
2524 {
2525 NdArray<bool> returnArray = { stl_algorithms::find(cbegin(), cend(), inValue) != cend() };
2526 return returnArray;
2527 }
2528 case Axis::COL:
2529 {
2530 NdArray<bool> returnArray(1, shape_.rows);
2531 for (size_type row = 0; row < shape_.rows; ++row)
2532 {
2533 returnArray(0, row) = stl_algorithms::find(cbegin(row), cend(row), inValue) != cend(row);
2534 }
2535
2536 return returnArray;
2537 }
2538 case Axis::ROW:
2539 {
2540 return transpose().contains(inValue, Axis::COL);
2541 }
2542 default:
2543 {
2544 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
2545 return {}; // get rid of compiler warning
2546 }
2547 }
2548 }
2549
2550 //============================================================================
2551 // Method Description:
2558 [[nodiscard]] self_type copy() const
2559 {
2560 return self_type(*this);
2561 }
2562
2563 //============================================================================
2564 // Method Description:
2572 [[nodiscard]] self_type cumprod(Axis inAxis = Axis::NONE) const
2573 {
2575
2576 switch (inAxis)
2577 {
2578 case Axis::NONE:
2579 {
2580 self_type returnArray(1, size_);
2581 returnArray[0] = front();
2582 for (size_type i = 1; i < size_; ++i)
2583 {
2584 returnArray[i] = returnArray[i - 1] * array_[i];
2585 }
2586
2587 return returnArray;
2588 }
2589 case Axis::COL:
2590 {
2591 self_type returnArray(shape_);
2592 for (uint32 row = 0; row < shape_.rows; ++row)
2593 {
2594 returnArray(row, 0) = operator()(row, 0);
2595 for (uint32 col = 1; col < shape_.cols; ++col)
2596 {
2597 returnArray(row, col) = returnArray(row, col - 1) * operator()(row, col);
2598 }
2599 }
2600
2601 return returnArray;
2602 }
2603 case Axis::ROW:
2604 {
2605 return transpose().cumprod(Axis::COL).transpose();
2606 }
2607 default:
2608 {
2609 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
2610 return {}; // get rid of compiler warning
2611 }
2612 }
2613 }
2614
2615 //============================================================================
2616 // Method Description:
2624 [[nodiscard]] self_type cumsum(Axis inAxis = Axis::NONE) const
2625 {
2627
2628 switch (inAxis)
2629 {
2630 case Axis::NONE:
2631 {
2632 self_type returnArray(1, size_);
2633 returnArray[0] = front();
2634 for (size_type i = 1; i < size_; ++i)
2635 {
2636 returnArray[i] = returnArray[i - 1] + array_[i];
2637 }
2638
2639 return returnArray;
2640 }
2641 case Axis::COL:
2642 {
2643 self_type returnArray(shape_);
2644 for (uint32 row = 0; row < shape_.rows; ++row)
2645 {
2646 returnArray(row, 0) = operator()(row, 0);
2647 for (uint32 col = 1; col < shape_.cols; ++col)
2648 {
2649 returnArray(row, col) = returnArray(row, col - 1) + operator()(row, col);
2650 }
2651 }
2652
2653 return returnArray;
2654 }
2655 case Axis::ROW:
2656 {
2657 return transpose().cumsum(Axis::COL).transpose();
2658 }
2659 default:
2660 {
2661 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
2662 return {}; // get rid of compiler warning
2663 }
2664 }
2665 }
2666
2667 //============================================================================
2668 // Method Description:
2672 [[nodiscard]] pointer data() noexcept
2673 {
2674 return array_;
2675 }
2676
2677 //============================================================================
2678 // Method Description:
2682 [[nodiscard]] const_pointer data() const noexcept
2683 {
2684 return array_;
2685 }
2686
2687 //============================================================================
2688 // Method Description:
2694 [[nodiscard]] pointer dataRelease() noexcept
2695 {
2696 ownsPtr_ = false;
2697 return data();
2698 }
2699
2700 //============================================================================
2701 // Method Description:
2711 [[nodiscard]] self_type diagonal(index_type inOffset = 0, Axis inAxis = Axis::ROW) const
2712 {
2713 switch (inAxis)
2714 {
2715 case Axis::COL:
2716 {
2717 std::vector<dtype> diagnolValues;
2718 size_type col = 0;
2719 for (index_type row = inOffset; row < static_cast<index_type>(shape_.rows); ++row)
2720 {
2721 if (row < 0)
2722 {
2723 ++col;
2724 continue;
2725 }
2726 if (col >= shape_.cols)
2727 {
2728 break;
2729 }
2730
2731 diagnolValues.push_back(operator()(static_cast<size_type>(row), col));
2732 ++col;
2733 }
2734
2735 return self_type(diagnolValues);
2736 }
2737 case Axis::ROW:
2738 {
2739 return transpose().diagonal(inOffset, Axis::COL);
2740 }
2741 default:
2742 {
2743 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
2744 return {}; // get rid of compiler warning
2745 }
2746 }
2747 }
2748
2749 //============================================================================
2750 // Method Description:
2756 [[nodiscard]] size_type dimSize(Axis inAxis) const noexcept
2757 {
2758 switch (inAxis)
2759 {
2760 case Axis::NONE:
2761 {
2762 return size();
2763 }
2764 case Axis::ROW:
2765 {
2766 return numRows();
2767 }
2768 case Axis::COL:
2769 {
2770 return numCols();
2771 }
2772 default:
2773 {
2774 return {}; // get rid of compiler warning
2775 }
2776 }
2777 }
2778
2779 //============================================================================
2780 // Method Description:
2791 [[nodiscard]] self_type dot(const self_type& inOtherArray) const
2792 {
2794
2795 if (shape_ == inOtherArray.shape_ && (shape_.rows == 1 || shape_.cols == 1))
2796 {
2797 dtype dotProduct =
2798 stl_algorithms::transform_reduce(cbegin(), cend(), inOtherArray.cbegin(), dtype{ 0 });
2799 self_type returnArray = { dotProduct };
2800 return returnArray;
2801 }
2802
2803 if (shape_.cols == inOtherArray.shape_.rows)
2804 {
2805 // 2D array, use matrix multiplication
2806 self_type returnArray(shape_.rows, inOtherArray.shape_.cols);
2807 auto otherArrayT = inOtherArray.transpose();
2808
2809 for (uint32 i = 0; i < shape_.rows; ++i)
2810 {
2811 for (uint32 j = 0; j < otherArrayT.shape_.rows; ++j)
2812 {
2813 returnArray(i, j) = stl_algorithms::transform_reduce(otherArrayT.cbegin(j),
2814 otherArrayT.cend(j),
2815 cbegin(i),
2816 dtype{ 0 });
2817 }
2818 }
2819
2820 return returnArray;
2821 }
2822
2823 std::string errStr = "shapes of [" + utils::num2str(shape_.rows) + ", " + utils::num2str(shape_.cols) + "]";
2824 errStr += " and [" + utils::num2str(inOtherArray.shape_.rows) + ", " +
2825 utils::num2str(inOtherArray.shape_.cols) + "]";
2826 errStr += " are not consistent.";
2828
2829 return self_type(); // get rid of compiler warning
2830 }
2831
2832 //============================================================================
2833 // Method Description:
2841 void dump(const std::string& inFilename) const
2842 {
2843 std::filesystem::path f(inFilename);
2844 if (!f.has_extension())
2845 {
2846 f.replace_extension("bin");
2847 }
2848
2849 std::ofstream ofile(f.c_str(), std::ios::binary);
2850 if (!ofile.good())
2851 {
2852 THROW_RUNTIME_ERROR("Unable to open the input file:\n\t" + inFilename);
2853 }
2854
2855 if (array_ != nullptr)
2856 {
2857 ofile.write(reinterpret_cast<const char*>(array_), size_ * sizeof(dtype));
2858 }
2859 ofile.close();
2860 }
2861
2862 //============================================================================
2863 // Method Description:
2868 [[nodiscard]] Endian endianess() const noexcept
2869 {
2871
2872 return endianess_;
2873 }
2874
2875 //============================================================================
2876 // Method Description:
2884 self_type& fill(value_type inFillValue) noexcept
2885 {
2886 stl_algorithms::fill(begin(), end(), inFillValue);
2887 return *this;
2888 }
2889
2890 //============================================================================
2891 // Method Description:
2897 [[nodiscard]] NdArray<size_type> flatnonzero() const
2898 {
2900
2901 std::vector<size_type> indices;
2902 size_type idx = 0;
2903 for (auto value : *this)
2904 {
2905 if (!utils::essentiallyEqual(value, dtype{ 0 }))
2906 {
2907 indices.push_back(idx);
2908 }
2909 ++idx;
2910 }
2911
2912 return NdArray<size_type>(indices); // NOLINT(modernize-return-braced-init-list)
2913 }
2914
2915 //============================================================================
2916 // Method Description:
2923 [[nodiscard]] self_type flatten() const
2924 {
2925 self_type outArray(1, size_);
2926 stl_algorithms::copy(cbegin(), cend(), outArray.begin());
2927 return outArray;
2928 }
2929
2930 //============================================================================
2931 // Method Description:
2936 [[nodiscard]] const_reference front() const noexcept
2937 {
2938 return *cbegin();
2939 }
2940
2941 //============================================================================
2942 // Method Description:
2947 reference front() noexcept
2948 {
2949 return *begin();
2950 }
2951
2952 //============================================================================
2953 // Method Description:
2958 [[nodiscard]] const_reference front(size_type row) const
2959 {
2960 return *cbegin(row);
2961 }
2962
2963 //============================================================================
2964 // Method Description:
2970 {
2971 return *begin(row);
2972 }
2973
2974 //============================================================================
2975 // Method Description:
2981 [[nodiscard]] self_type getByIndices(const NdArray<size_type>& inIndices) const
2982 {
2983 return operator[](inIndices);
2984 }
2985
2986 //============================================================================
2987 // Method Description:
2995 [[nodiscard]] self_type getByMask(const NdArray<bool>& inMask) const
2996 {
2997 return operator[](inMask);
2998 }
2999
3000 //============================================================================
3001 // Method Description:
3007 // NOLINTNEXTLINE(modernize-use-nodiscard)
3008 bool isempty() const noexcept
3009 {
3010 return size_ == 0;
3011 }
3012
3013 //============================================================================
3014 // Method Description:
3020 // NOLINTNEXTLINE(modernize-use-nodiscard)
3021 bool isflat() const noexcept
3022 {
3023 return !isscalar() && (shape_.rows == 1 || shape_.cols == 1);
3024 }
3025
3026 //============================================================================
3027 // Method Description:
3031 // NOLINTNEXTLINE(modernize-use-nodiscard)
3032 bool isscalar() const noexcept
3033 {
3034 return size_ == 1;
3035 }
3036
3037 //============================================================================
3038 // Method Description:
3044 [[nodiscard]] NdArray<bool> issorted(Axis inAxis = Axis::NONE) const
3045 {
3047
3048 const auto comparitor = [](dtype lhs, dtype rhs) noexcept -> bool { return lhs < rhs; };
3049
3050 switch (inAxis)
3051 {
3052 case Axis::NONE:
3053 {
3054 return { stl_algorithms::is_sorted(cbegin(), cend(), comparitor) };
3055 }
3056 case Axis::COL:
3057 {
3058 NdArray<bool> returnArray(1, shape_.rows);
3059 for (uint32 row = 0; row < shape_.rows; ++row)
3060 {
3061 returnArray(0, row) = stl_algorithms::is_sorted(cbegin(row), cend(row), comparitor);
3062 }
3063
3064 return returnArray;
3065 }
3066 case Axis::ROW:
3067 {
3068 return transpose().issorted(Axis::COL);
3069 }
3070 default:
3071 {
3072 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
3073 return {}; // get rid of compiler warning
3074 }
3075 }
3076 }
3077
3078 //============================================================================
3079 // Method Description:
3084 // NOLINTNEXTLINE(modernize-use-nodiscard)
3085 bool issquare() const noexcept
3086 {
3087 return shape_.issquare();
3088 }
3089
3090 //============================================================================
3091 // Method Description:
3098 [[nodiscard]] value_type item() const
3099 {
3100 if (!isscalar())
3101 {
3102 THROW_INVALID_ARGUMENT_ERROR("Can only convert an array of size 1 to a C++ scalar");
3103 }
3104
3105 return front();
3106 }
3107
3108 //============================================================================
3109 // Method Description:
3117 [[nodiscard]] self_type max(Axis inAxis = Axis::NONE) const
3118 {
3120
3121 const auto comparitor = [](dtype lhs, dtype rhs) noexcept -> bool { return lhs < rhs; };
3122
3123 switch (inAxis)
3124 {
3125 case Axis::NONE:
3126 {
3127 self_type returnArray = { *stl_algorithms::max_element(cbegin(), cend(), comparitor) };
3128 return returnArray;
3129 }
3130 case Axis::COL:
3131 {
3132 self_type returnArray(1, shape_.rows);
3133 for (uint32 row = 0; row < shape_.rows; ++row)
3134 {
3135 returnArray(0, row) = *stl_algorithms::max_element(cbegin(row), cend(row), comparitor);
3136 }
3137
3138 return returnArray;
3139 }
3140 case Axis::ROW:
3141 {
3142 return transpose().max(Axis::COL);
3143 }
3144 default:
3145 {
3146 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
3147 return {}; // get rid of compiler warning
3148 }
3149 }
3150 }
3151
3152 //============================================================================
3153 // Method Description:
3161 [[nodiscard]] self_type min(Axis inAxis = Axis::NONE) const
3162 {
3164
3165 const auto comparitor = [](dtype lhs, dtype rhs) noexcept -> bool { return lhs < rhs; };
3166
3167 switch (inAxis)
3168 {
3169 case Axis::NONE:
3170 {
3171 self_type returnArray = { *stl_algorithms::min_element(cbegin(), cend(), comparitor) };
3172 return returnArray;
3173 }
3174 case Axis::COL:
3175 {
3176 self_type returnArray(1, shape_.rows);
3177 for (uint32 row = 0; row < shape_.rows; ++row)
3178 {
3179 returnArray(0, row) = *stl_algorithms::min_element(cbegin(row), cend(row), comparitor);
3180 }
3181
3182 return returnArray;
3183 }
3184 case Axis::ROW:
3185 {
3186 return transpose().min(Axis::COL);
3187 }
3188 default:
3189 {
3190 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
3191 return {}; // get rid of compiler warning
3192 }
3193 }
3194 }
3195
3196 //============================================================================
3197 // Method Description:
3207 [[nodiscard]] self_type median(Axis inAxis = Axis::NONE) const
3208 {
3210
3211 const auto comparitor = [](dtype lhs, dtype rhs) noexcept -> bool { return lhs < rhs; };
3212
3213 if (size_ == 0)
3214 {
3215 THROW_RUNTIME_ERROR("Median is undefined for an array of size = 0.");
3216 }
3217
3218 switch (inAxis)
3219 {
3220 case Axis::NONE:
3221 {
3222 self_type copyArray(*this);
3223
3224 const size_type middleIdx = size_ / 2; // integer division
3226 copyArray.begin() + middleIdx,
3227 copyArray.end(),
3228 comparitor);
3229
3230 dtype medianValue = copyArray.array_[middleIdx];
3231 if (size_ % 2 == 0)
3232 {
3233 const size_type lhsIndex = middleIdx - 1;
3235 copyArray.begin() + lhsIndex,
3236 copyArray.end(),
3237 comparitor);
3238 medianValue =
3239 (medianValue + copyArray.array_[lhsIndex]) / dtype{ 2 }; // potentially integer division, ok
3240 }
3241
3242 return { medianValue };
3243 }
3244 case Axis::COL:
3245 {
3246 self_type copyArray(*this);
3247 self_type returnArray(1, shape_.rows);
3248
3249 const bool isEven = shape_.cols % 2 == 0;
3250 for (uint32 row = 0; row < shape_.rows; ++row)
3251 {
3252 const uint32 middleIdx = shape_.cols / 2; // integer division
3254 copyArray.begin(row) + middleIdx,
3255 copyArray.end(row),
3256 comparitor);
3257
3258 dtype medianValue = copyArray(row, middleIdx);
3259 if (isEven)
3260 {
3261 const size_type lhsIndex = middleIdx - 1;
3263 copyArray.begin(row) + lhsIndex,
3264 copyArray.end(row),
3265 comparitor);
3266 medianValue = (medianValue + copyArray(row, lhsIndex)) /
3267 dtype{ 2 }; // potentially integer division, ok
3268 }
3269
3270 returnArray(0, row) = medianValue;
3271 }
3272
3273 return returnArray;
3274 }
3275 case Axis::ROW:
3276 {
3277 return transpose().median(Axis::COL);
3278 }
3279 default:
3280 {
3281 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
3282 return {}; // get rid of compiler warning
3283 }
3284 }
3285 }
3286
3287 //============================================================================
3288 // Method Description:
3292 self_type& nans() noexcept
3293
3294 {
3295 STATIC_ASSERT_FLOAT(dtype);
3296
3298 return *this;
3299 }
3300
3301 //============================================================================
3302 // Method Description:
3309 [[nodiscard]] uint64 nbytes() const noexcept
3310 {
3311 return static_cast<uint64>(sizeof(dtype) * size_);
3312 }
3313
3314 //============================================================================
3315 // Method Description:
3324 [[nodiscard]] self_type newbyteorder(Endian inEndianess) const
3325 {
3326 STATIC_ASSERT_INTEGER(dtype);
3327
3328 const bool nativeIsLittle = endian::isLittleEndian();
3329
3330 switch (endianess_)
3331 {
3332 case Endian::NATIVE:
3333 {
3334 switch (inEndianess)
3335 {
3336 case Endian::NATIVE:
3337 {
3338 return NdArray(*this);
3339 }
3340 case Endian::BIG:
3341 {
3342 if (nativeIsLittle)
3343 {
3344 self_type outArray(shape_);
3345
3347
3348 outArray.endianess_ = Endian::BIG;
3349 return outArray;
3350 }
3351 else
3352 {
3353 auto outArray = NdArray(*this);
3354 outArray.endianess_ = Endian::BIG;
3355 return outArray;
3356 }
3357 }
3358 case Endian::LITTLE:
3359 {
3360 if (nativeIsLittle)
3361 {
3362 auto outArray = NdArray(*this);
3363 outArray.endianess_ = Endian::LITTLE;
3364 return outArray;
3365 }
3366 else
3367 {
3368 self_type outArray(shape_);
3369
3371
3372 outArray.endianess_ = Endian::LITTLE;
3373 return outArray;
3374 }
3375 }
3376 default:
3377 {
3378 THROW_INVALID_ARGUMENT_ERROR("Unimplemented endian type.");
3379 return {}; // get rid of compiler warning
3380 }
3381 }
3382 break;
3383 }
3384 case Endian::BIG:
3385 {
3386 switch (inEndianess)
3387 {
3388 case Endian::NATIVE:
3389 {
3390 if (nativeIsLittle)
3391 {
3392 self_type outArray(shape_);
3393
3395
3396 outArray.endianess_ = Endian::NATIVE;
3397 return outArray;
3398 }
3399 else
3400 {
3401 auto outArray = NdArray(*this);
3402 outArray.endianess_ = Endian::NATIVE;
3403 return outArray;
3404 }
3405 }
3406 case Endian::BIG:
3407 {
3408 return NdArray(*this);
3409 }
3410 case Endian::LITTLE:
3411 {
3412 self_type outArray(shape_);
3413
3415
3416 outArray.endianess_ = Endian::LITTLE;
3417 return outArray;
3418 }
3419 default:
3420 {
3421 THROW_INVALID_ARGUMENT_ERROR("Unimplemented endian type.");
3422 return {}; // get rid of compiler warning
3423 }
3424 }
3425 break;
3426 }
3427 case Endian::LITTLE:
3428 {
3429 switch (inEndianess)
3430 {
3431 case Endian::NATIVE:
3432 {
3433 if (nativeIsLittle)
3434 {
3435 auto outArray = NdArray(*this);
3436 outArray.endianess_ = Endian::NATIVE;
3437 return outArray;
3438 }
3439 else
3440 {
3441 self_type outArray(shape_);
3442
3444
3445 outArray.endianess_ = Endian::NATIVE;
3446 return outArray;
3447 }
3448 }
3449 case Endian::BIG:
3450 {
3451 self_type outArray(shape_);
3452
3454
3455 outArray.endianess_ = Endian::BIG;
3456 return outArray;
3457 }
3458 case Endian::LITTLE:
3459 {
3460 return NdArray(*this);
3461 }
3462 default:
3463 {
3464 THROW_INVALID_ARGUMENT_ERROR("Unimplemented endian type.");
3465 return {}; // get rid of compiler warning
3466 }
3467 }
3468 break;
3469 }
3470 default:
3471 {
3472 THROW_INVALID_ARGUMENT_ERROR("Unimplemented endian type.");
3473 return {}; // get rid of compiler warning
3474 }
3475 }
3476 }
3477
3478 //============================================================================
3479 // Method Description:
3487 [[nodiscard]] NdArray<bool> none(Axis inAxis = Axis::NONE) const
3488 {
3490
3491 const auto function = [](dtype i) -> bool { return !utils::essentiallyEqual(i, dtype{ 0 }); };
3492
3493 switch (inAxis)
3494 {
3495 case Axis::NONE:
3496 {
3497 NdArray<bool> returnArray = { stl_algorithms::none_of(cbegin(), cend(), function) };
3498 return returnArray;
3499 }
3500 case Axis::COL:
3501 {
3502 NdArray<bool> returnArray(1, shape_.rows);
3503 for (uint32 row = 0; row < shape_.rows; ++row)
3504 {
3505 returnArray(0, row) = stl_algorithms::none_of(cbegin(row), cend(row), function);
3506 }
3507
3508 return returnArray;
3509 }
3510 case Axis::ROW:
3511 {
3512 return transpose().none(Axis::COL);
3513 }
3514 default:
3515 {
3516 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
3517 return {}; // get rid of compiler warning
3518 }
3519 }
3520 }
3521
3522 //============================================================================
3523 // Method Description:
3532 [[nodiscard]] std::pair<NdArray<size_type>, NdArray<size_type>> nonzero() const;
3533
3534 //============================================================================
3535 // Method Description:
3541 [[nodiscard]] size_type numCols() const noexcept
3542 {
3543 return shape_.cols;
3544 }
3545
3546 //============================================================================
3547 // Method Description:
3553 [[nodiscard]] size_type numRows() const noexcept
3554 {
3555 return shape_.rows;
3556 }
3557
3558 //============================================================================
3559 // Method Description:
3563 self_type& ones() noexcept
3564 {
3566
3567 fill(dtype{ 1 });
3568 return *this;
3569 }
3570
3571 //============================================================================
3572 // Method Description:
3577 bool ownsInternalData() noexcept
3578 {
3579 return ownsPtr_;
3580 }
3581
3582 //============================================================================
3583 // Method Description:
3598 {
3600
3601 const auto comparitor = [](dtype lhs, dtype rhs) noexcept -> bool
3602 { return lhs < rhs; }; // cppcheck-suppress returnTempReference
3603
3604 switch (inAxis)
3605 {
3606 case Axis::NONE:
3607 {
3608 if (inKth >= size_)
3609 {
3610 std::string errStr = "kth(=" + utils::num2str(inKth);
3611 errStr += ") out of bounds (" + utils::num2str(size_) + ")";
3613 }
3614
3615 stl_algorithms::nth_element(begin(), begin() + inKth, end(), comparitor);
3616 break;
3617 }
3618 case Axis::COL:
3619 {
3620 if (inKth >= shape_.cols)
3621 {
3622 std::string errStr = "kth(=" + utils::num2str(inKth);
3623 errStr += ") out of bounds (" + utils::num2str(shape_.cols) + ")";
3625 }
3626
3627 for (uint32 row = 0; row < shape_.rows; ++row)
3628 {
3629 stl_algorithms::nth_element(begin(row), begin(row) + inKth, end(row), comparitor);
3630 }
3631 break;
3632 }
3633 case Axis::ROW:
3634 {
3635 if (inKth >= shape_.rows)
3636 {
3637 std::string errStr = "kth(=" + utils::num2str(inKth);
3638 errStr += ") out of bounds (" + utils::num2str(shape_.rows) + ")";
3640 }
3641
3642 self_type transposedArray = transpose();
3643 for (uint32 row = 0; row < transposedArray.shape_.rows; ++row)
3644 {
3645 stl_algorithms::nth_element(transposedArray.begin(row),
3646 transposedArray.begin(row) + inKth,
3647 transposedArray.end(row),
3648 comparitor);
3649 }
3650 *this = transposedArray.transpose();
3651 break;
3652 }
3653 }
3654
3655 return *this;
3656 }
3657
3658 //============================================================================
3659 // Method Description:
3663 void print() const
3664 {
3666
3667 std::cout << *this;
3668 }
3669
3670 //============================================================================
3671 // Method Description:
3679 [[nodiscard]] self_type prod(Axis inAxis = Axis::NONE) const
3680 {
3682
3683 switch (inAxis)
3684 {
3685 case Axis::NONE:
3686 {
3687 dtype product = std::accumulate(cbegin(), cend(), dtype{ 1 }, std::multiplies<dtype>());
3688 self_type returnArray = { product };
3689 return returnArray;
3690 }
3691 case Axis::COL:
3692 {
3693 self_type returnArray(1, shape_.rows);
3694 for (uint32 row = 0; row < shape_.rows; ++row)
3695 {
3696 returnArray(0, row) =
3697 std::accumulate(cbegin(row), cend(row), dtype{ 1 }, std::multiplies<dtype>());
3698 }
3699
3700 return returnArray;
3701 }
3702 case Axis::ROW:
3703 {
3704 return transpose().prod(Axis::COL);
3705 }
3706 default:
3707 {
3708 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
3709 return {}; // get rid of compiler warning
3710 }
3711 }
3712 }
3713
3714 //============================================================================
3715 // Method Description:
3723 [[nodiscard]] self_type ptp(Axis inAxis = Axis::NONE) const
3724 {
3726
3727 const auto comparitor = [](dtype lhs, dtype rhs) noexcept -> bool { return lhs < rhs; };
3728
3729 switch (inAxis)
3730 {
3731 case Axis::NONE:
3732 {
3733 const auto result = stl_algorithms::minmax_element(cbegin(), cend(), comparitor);
3734 self_type returnArray = { *result.second - *result.first };
3735 return returnArray;
3736 }
3737 case Axis::COL:
3738 {
3739 self_type returnArray(1, shape_.rows);
3740 for (uint32 row = 0; row < shape_.rows; ++row)
3741 {
3742 const auto result = stl_algorithms::minmax_element(cbegin(row), cend(row), comparitor);
3743 returnArray(0, row) = *result.second - *result.first;
3744 }
3745
3746 return returnArray;
3747 }
3748 case Axis::ROW:
3749 {
3750 return transpose().ptp(Axis::COL);
3751 }
3752 default:
3753 {
3754 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
3755 return {}; // get rid of compiler warning
3756 }
3757 }
3758 }
3759
3760 //============================================================================
3761 // Method Description:
3769 self_type& put(index_type inIndex, const value_type& inValue)
3770 {
3771 at(inIndex) = inValue;
3772
3773 return *this;
3774 }
3775
3776 //============================================================================
3777 // Method Description:
3786 self_type& put(index_type inRow, index_type inCol, const value_type& inValue)
3787 {
3788 at(inRow, inCol) = inValue;
3789
3790 return *this;
3791 }
3792
3793 //============================================================================
3794 // Method Description:
3803 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
3804 self_type& put(const Indices& inIndices, const value_type& inValue)
3805 {
3806 for (auto index : inIndices)
3807 {
3808 put(index, inValue);
3809 }
3810
3811 return *this;
3812 }
3813
3814 //============================================================================
3815 // Method Description:
3824 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
3825 self_type& put(const Indices& inIndices, const self_type& inValues)
3826 {
3827 if (inValues.isscalar())
3828 {
3829 return put(inIndices, inValues.item());
3830 }
3831 else if (inIndices.size() != inValues.size())
3832 {
3833 THROW_INVALID_ARGUMENT_ERROR("Input indices do not match values dimensions.");
3834 }
3835
3836 size_type counter = 0;
3837 for (auto index : inIndices)
3838 {
3839 put(index, inValues[counter++]);
3840 }
3841
3842 return *this;
3843 }
3844
3845 //============================================================================
3846 // Method Description:
3855 self_type& put(const Slice& inSlice, const value_type& inValue)
3856 {
3857 return put(toIndices(inSlice, Axis::NONE), inValue);
3858 }
3859
3860 //============================================================================
3861 // Method Description:
3870 self_type& put(const Slice& inSlice, const self_type& inValues)
3871 {
3872 return put(toIndices(inSlice, Axis::NONE), inValues);
3873 }
3874
3875 //============================================================================
3876 // Method Description:
3886 template<typename RowIndices,
3887 typename ColIndices,
3890 self_type& put(const RowIndices& inRowIndices, const ColIndices& inColIndices, const value_type& inValue)
3891 {
3892 stl_algorithms::for_each(inRowIndices.begin(),
3893 inRowIndices.end(),
3894 [this, &inColIndices, &inValue](const auto row)
3895 {
3896 stl_algorithms::for_each(inColIndices.begin(),
3897 inColIndices.end(),
3898 [this, row, &inValue](const auto col)
3899 { this->put(row, col, inValue); });
3900 });
3901
3902 return *this;
3903 }
3904
3905 //============================================================================
3906 // Method Description:
3916 template<typename RowIndices, type_traits::ndarray_int_concept<RowIndices> = 0>
3917 self_type& put(const RowIndices& inRowIndices, const Slice& inColSlice, const value_type& inValue)
3918 {
3919 return put(inRowIndices, toIndices(inColSlice, Axis::COL), inValue);
3920 }
3921
3922 //============================================================================
3923 // Method Description:
3933 template<typename ColIndices, type_traits::ndarray_int_concept<ColIndices> = 0>
3934 self_type& put(const Slice& inRowSlice, const ColIndices& inColIndices, const value_type& inValue)
3935 {
3936 return put(toIndices(inRowSlice, Axis::ROW), inColIndices, inValue);
3937 }
3938
3939 //============================================================================
3940 // Method Description:
3950 self_type& put(const Slice& inRowSlice, const Slice& inColSlice, const value_type& inValue)
3951 {
3952 return put(toIndices(inRowSlice, Axis::ROW), toIndices(inColSlice, Axis::COL), inValue);
3953 }
3954
3955 //============================================================================
3956 // Method Description:
3966 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
3967 self_type& put(const Indices& inRowIndices, index_type inColIndex, const value_type& inValue)
3968 {
3969 const NdArray<index_type> colIndices = { inColIndex };
3970 return put(inRowIndices, colIndices, inValue);
3971 }
3972
3973 //============================================================================
3974 // Method Description:
3984 self_type& put(const Slice& inRowSlice, index_type inColIndex, const value_type& inValue)
3985 {
3986 const NdArray<index_type> colIndices = { inColIndex };
3987 return put(toIndices(inRowSlice, Axis::ROW), colIndices, inValue);
3988 }
3989
3990 //============================================================================
3991 // Method Description:
4001 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
4002 self_type& put(index_type inRowIndex, const Indices& inColIndices, const value_type& inValue)
4003 {
4004 const NdArray<index_type> rowIndices = { inRowIndex };
4005 return put(rowIndices, inColIndices, inValue);
4006 }
4007
4008 //============================================================================
4009 // Method Description:
4019 self_type& put(index_type inRowIndex, const Slice& inColSlice, const value_type& inValue)
4020 {
4021 const NdArray<index_type> rowIndices = { inRowIndex };
4022 return put(rowIndices, toIndices(inColSlice, Axis::COL), inValue);
4023 }
4024
4025 //============================================================================
4026 // Method Description:
4036 template<typename RowIndices,
4037 typename ColIndices,
4040 self_type& put(const RowIndices& inRowIndices, const ColIndices& inColIndices, const self_type& inValues)
4041 {
4042 std::vector<size_type> indices;
4043 indices.reserve(inRowIndices.size() * inColIndices.size());
4044 std::for_each(inRowIndices.begin(),
4045 inRowIndices.end(),
4046 [this, &inColIndices, &indices](auto row)
4047 {
4048 if constexpr (std::is_signed_v<decltype(row)>)
4049 {
4050 if (row < 0)
4051 {
4052 row += shape_.rows;
4053 }
4054 // still
4055 if (row < 0)
4056 {
4057 THROW_INVALID_ARGUMENT_ERROR("row index exceeds matrix dimensions");
4058 }
4059 }
4060 std::for_each(inColIndices.begin(),
4061 inColIndices.end(),
4062 [this, row, &indices](auto col)
4063 {
4064 if constexpr (std::is_signed_v<decltype(col)>)
4065 {
4066 if (col < 0)
4067 {
4068 col += shape_.cols;
4069 }
4070 // still
4071 if (col < 0)
4072 {
4073 THROW_INVALID_ARGUMENT_ERROR(
4074 "col index exceeds matrix dimensions");
4075 }
4076 }
4077 indices.push_back(row * shape_.cols + col);
4078 });
4079 });
4080
4081 return put(NdArray<size_type>(indices.data(), indices.size(), PointerPolicy::SHELL), inValues);
4082 }
4083
4084 //============================================================================
4085 // Method Description:
4095 template<typename RowIndices, type_traits::ndarray_int_concept<RowIndices> = 0>
4096 self_type& put(const RowIndices& inRowIndices, Slice inColSlice, const self_type& inValues)
4097 {
4098 return put(inRowIndices, toIndices(inColSlice, Axis::COL), inValues);
4099 }
4100
4101 //============================================================================
4102 // Method Description:
4112 template<typename ColIndices, type_traits::ndarray_int_concept<ColIndices> = 0>
4113 self_type& put(Slice inRowSlice, const ColIndices& inColIndices, const self_type& inValues)
4114 {
4115 return put(toIndices(inRowSlice, Axis::ROW), inColIndices, inValues);
4116 }
4117
4118 //============================================================================
4119 // Method Description:
4129 self_type& put(Slice inRowSlice, Slice inColSlice, const self_type& inValues)
4130 {
4131 return put(toIndices(inRowSlice, Axis::ROW), toIndices(inColSlice, Axis::COL), inValues);
4132 }
4133
4134 //============================================================================
4135 // Method Description:
4145 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
4146 self_type& put(const Indices& inRowIndices, index_type inColIndex, const self_type& inValues)
4147 {
4148 const NdArray<index_type> colIndices = { inColIndex };
4149 return put(inRowIndices, colIndices, inValues);
4150 }
4151
4152 //============================================================================
4153 // Method Description:
4163 self_type& put(const Slice& inRowSlice, index_type inColIndex, const self_type& inValues)
4164 {
4165 const NdArray<index_type> colIndices = { inColIndex };
4166 return put(toIndices(inRowSlice, Axis::ROW), colIndices, inValues);
4167 }
4168
4169 //============================================================================
4170 // Method Description:
4180 template<typename Indices, type_traits::ndarray_int_concept<Indices> = 0>
4181 self_type& put(index_type inRowIndex, const Indices& inColIndices, const self_type& inValues)
4182 {
4183 const NdArray<index_type> rowIndices = { inRowIndex };
4184 return put(rowIndices, inColIndices, inValues);
4185 }
4186
4187 //============================================================================
4188 // Method Description:
4198 self_type& put(index_type inRowIndex, const Slice& inColSlice, const self_type& inValues)
4199 {
4200 const NdArray<index_type> rowIndices = { inRowIndex };
4201 return put(rowIndices, toIndices(inColSlice, Axis::COL), inValues);
4202 }
4203
4204 //============================================================================
4205 // Method Description:
4211 self_type& putMask(const NdArray<bool>& inMask, const value_type& inValue)
4212 {
4213 if (inMask.shape() != shape_)
4214 {
4215 THROW_INVALID_ARGUMENT_ERROR("input inMask must be the same shape as the array it is masking.");
4216 }
4217
4218 return put(inMask.flatnonzero(), inValue);
4219 }
4220
4221 //============================================================================
4222 // Method Description:
4228 self_type& putMask(const NdArray<bool>& inMask, const self_type& inValues)
4229 {
4230 if (inMask.shape() != shape_)
4231 {
4232 THROW_INVALID_ARGUMENT_ERROR("input inMask must be the same shape as the array it is masking.");
4233 }
4234
4235 if (inValues.isscalar())
4236 {
4237 put(inMask.flatnonzero(), inValues.item());
4238 }
4239 else
4240 {
4241 put(inMask.flatnonzero(), inValues);
4242 }
4243
4244 return *this;
4245 }
4246
4247 //============================================================================
4248 // Method Description:
4256 {
4257 reshape(size_);
4258 return *this;
4259 }
4260
4261 //============================================================================
4262 // Method Description:
4271 [[nodiscard]] self_type repeat(size_type inNumRows, size_type inNumCols) const
4272 {
4273 self_type returnArray(shape_.rows * inNumRows, shape_.cols * inNumCols);
4274
4275 for (size_type row = 0; row < inNumRows; ++row)
4276 {
4277 for (size_type col = 0; col < inNumCols; ++col)
4278 {
4279 std::vector<size_type> indices(shape_.size());
4280
4281 const size_type rowStart = row * shape_.rows;
4282 const size_type colStart = col * shape_.cols;
4283
4284 const size_type rowEnd = (row + 1) * shape_.rows;
4285 const size_type colEnd = (col + 1) * shape_.cols;
4286
4287 size_type counter = 0;
4288 for (size_type rowIdx = rowStart; rowIdx < rowEnd; ++rowIdx)
4289 {
4290 for (size_type colIdx = colStart; colIdx < colEnd; ++colIdx)
4291 {
4292 indices[counter++] = rowIdx * returnArray.shape_.cols + colIdx;
4293 }
4294 }
4295
4296 returnArray.put(NdArray<size_type>(indices), *this);
4297 }
4298 }
4299
4300 return returnArray;
4301 }
4302
4303 //============================================================================
4304 // Method Description:
4312 [[nodiscard]] self_type repeat(const Shape& inRepeatShape) const
4313 {
4314 return repeat(inRepeatShape.rows, inRepeatShape.cols);
4315 }
4316
4317 //============================================================================
4318 // Method Description:
4325 {
4327
4328 stl_algorithms::replace(begin(), end(), oldValue, newValue);
4329 return *this;
4330 }
4331
4332 //============================================================================
4333 // Method Description:
4348 {
4349 if (inSize != size_)
4350 {
4351 std::string errStr = "Cannot reshape array of size " + utils::num2str(size_) + " into shape ";
4352 errStr += "[" + utils::num2str(1) + ", " + utils::num2str(inSize) + "]";
4353 THROW_RUNTIME_ERROR(errStr);
4354 }
4355
4356 shape_.rows = 1;
4357 shape_.cols = inSize;
4358
4359 return *this;
4360 }
4361
4362 //============================================================================
4363 // Method Description:
4379 {
4380 if (inNumRows < 0)
4381 {
4382 if (size_ % inNumCols == 0)
4383 {
4384 return reshape(size_ / inNumCols, inNumCols);
4385 }
4386
4387 std::string errStr = "Cannot reshape array of size " + utils::num2str(size_) + " into a shape ";
4388 errStr += "with " + utils::num2str(inNumCols) + " columns";
4390 }
4391
4392 if (inNumCols < 0)
4393 {
4394 if (size_ % inNumRows == 0)
4395 {
4396 return reshape(inNumRows, size_ / inNumRows);
4397 }
4398
4399 std::string errStr = "Cannot reshape array of size " + utils::num2str(size_) + " into a shape ";
4400 errStr += "with " + utils::num2str(inNumRows) + " rows";
4402 }
4403
4404 if (static_cast<size_type>(inNumRows * inNumCols) != size_)
4405 {
4406 std::string errStr = "Cannot reshape array of size " + utils::num2str(size_) + " into shape ";
4407 errStr += "[" + utils::num2str(inNumRows) + ", " + utils::num2str(inNumCols) + "]";
4409 }
4410
4411 shape_.rows = static_cast<size_type>(inNumRows);
4412 shape_.cols = static_cast<size_type>(inNumCols);
4413
4414 return *this;
4415 }
4416
4417 //============================================================================
4418 // Method Description:
4432 self_type& reshape(const Shape& inShape)
4433 {
4434 return reshape(inShape.rows, inShape.cols);
4435 }
4436
4437 //============================================================================
4438 // Method Description:
4448 {
4449 newArray(Shape(inNumRows, inNumCols));
4450 return *this;
4451 }
4452
4453 //============================================================================
4454 // Method Description:
4462 self_type& resizeFast(const Shape& inShape)
4463 {
4464 return resizeFast(inShape.rows, inShape.cols);
4465 }
4466
4467 //============================================================================
4468 // Method Description:
4480 {
4481 std::vector<dtype> oldData(size_);
4482 stl_algorithms::copy(begin(), end(), oldData.begin());
4483
4484 const Shape inShape(inNumRows, inNumCols);
4485 const Shape oldShape = shape_;
4486
4487 newArray(inShape);
4488
4489 for (uint32 row = 0; row < inShape.rows; ++row)
4490 {
4491 for (uint32 col = 0; col < inShape.cols; ++col)
4492 {
4493 if (row >= oldShape.rows || col >= oldShape.cols)
4494 {
4495 operator()(row, col) = dtype{ 0 }; // zero fill
4496 }
4497 else
4498 {
4499 operator()(row, col) = oldData[row * oldShape.cols + col];
4500 }
4501 }
4502 }
4503
4504 return *this;
4505 }
4506
4507 //============================================================================
4508 // Method Description:
4518 self_type& resizeSlow(const Shape& inShape)
4519 {
4520 return resizeSlow(inShape.rows, inShape.cols);
4521 }
4522
4523 //============================================================================
4524 // Method Description:
4533 [[nodiscard]] self_type round(uint8 inNumDecimals = 0) const
4534 {
4535 STATIC_ASSERT_FLOAT(dtype);
4536
4537 self_type returnArray(shape_);
4538 const double multFactor = utils::power(10., inNumDecimals);
4539 const auto function = [multFactor](dtype value) noexcept -> dtype
4540 { return static_cast<dtype>(std::nearbyint(static_cast<double>(value) * multFactor) / multFactor); };
4541
4542 stl_algorithms::transform(cbegin(), cend(), returnArray.begin(), function);
4543
4544 return returnArray;
4545 }
4546
4547 //============================================================================
4548 // Method Description:
4554 [[nodiscard]] self_type row(size_type inRow) const
4555 {
4556 return self_type(cbegin(inRow), cend(inRow));
4557 }
4558
4559 //============================================================================
4560 // Method Description:
4566 [[nodiscard]] self_type rows(const NdArray<size_type>& inRows) const
4567 {
4568 auto returnArray = self_type(inRows.size(), shape_.cols);
4569 const auto cSlice = returnArray.cSlice();
4570
4571 for (size_type i = 0; i < inRows.size(); ++i)
4572 {
4573 returnArray.put(i, cSlice, row(inRows[i]));
4574 }
4575
4576 return returnArray;
4577 }
4578
4579 //============================================================================
4580 // Method Description:
4587 [[nodiscard]] const Shape& shape() const noexcept
4588 {
4589 return shape_;
4590 }
4591
4592 //============================================================================
4593 // Method Description:
4600 [[nodiscard]] size_type size() const noexcept
4601 {
4602 return size_;
4603 }
4604
4605 //============================================================================
4606 // Method Description:
4615 {
4617
4618 const auto comparitor = [](dtype lhs, dtype rhs) noexcept -> bool
4619 { return lhs < rhs; }; // cppcheck-suppress returnTempReference
4620
4621 switch (inAxis)
4622 {
4623 case Axis::NONE:
4624 {
4625 stl_algorithms::sort(begin(), end(), comparitor);
4626 break;
4627 }
4628 case Axis::COL:
4629 {
4630 for (uint32 row = 0; row < shape_.rows; ++row)
4631 {
4632 stl_algorithms::sort(begin(row), end(row), comparitor);
4633 }
4634 break;
4635 }
4636 case Axis::ROW:
4637 {
4638 self_type transposedArray = transpose();
4639 for (uint32 row = 0; row < transposedArray.shape_.rows; ++row)
4640 {
4641 stl_algorithms::sort(transposedArray.begin(row), transposedArray.end(row), comparitor);
4642 }
4643
4644 *this = transposedArray.transpose();
4645 break;
4646 }
4647 }
4648
4649 return *this;
4650 }
4651
4652 //============================================================================
4653 // Method Description:
4658 [[nodiscard]] std::string str() const
4659 {
4661
4662 std::string out;
4663 out += "[";
4664 for (uint32 row = 0; row < shape_.rows; ++row)
4665 {
4666 out += "[";
4667 for (uint32 col = 0; col < shape_.cols; ++col)
4668 {
4669 out += utils::value2str(operator()(row, col)) + ", ";
4670 }
4671
4672 if (row == shape_.rows - 1)
4673 {
4674 out += "]";
4675 }
4676 else
4677 {
4678 out += "]\n";
4679 }
4680 }
4681 out += "]\n";
4682 return out;
4683 }
4684
4685 //============================================================================
4686 // Method Description:
4694 [[nodiscard]] self_type sum(Axis inAxis = Axis::NONE) const
4695 {
4697
4698 switch (inAxis)
4699 {
4700 case Axis::NONE:
4701 {
4702 self_type returnArray = { std::accumulate(cbegin(), cend(), dtype{ 0 }) };
4703 return returnArray;
4704 }
4705 case Axis::COL:
4706 {
4707 self_type returnArray(1, shape_.rows);
4708 for (uint32 row = 0; row < shape_.rows; ++row)
4709 {
4710 returnArray(0, row) = std::accumulate(cbegin(row), cend(row), dtype{ 0 });
4711 }
4712
4713 return returnArray;
4714 }
4715 case Axis::ROW:
4716 {
4717 return transpose().sum(Axis::COL);
4718 }
4719 default:
4720 {
4721 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
4722 return {}; // get rid of compiler warning
4723 }
4724 }
4725 }
4726
4727 //============================================================================
4728 // Method Description:
4735 [[nodiscard]] self_type swapaxes() const
4736 {
4737 return transpose();
4738 }
4739
4740 //============================================================================
4741 // Method Description:
4748 self_type& swapCols(index_type colIdx1, index_type colIdx2) noexcept
4749 {
4750 for (index_type row = 0; row < static_cast<index_type>(shape_.rows); ++row)
4751 {
4752 std::swap(operator()(row, colIdx1), operator()(row, colIdx2));
4753 }
4754
4755 return *this;
4756 }
4757
4758 //============================================================================
4759 // Method Description:
4766 self_type& swapRows(index_type rowIdx1, index_type rowIdx2) noexcept
4767 {
4768 for (index_type col = 0; col < static_cast<index_type>(shape_.cols); ++col)
4769 {
4770 std::swap(operator()(rowIdx1, col), operator()(rowIdx2, col));
4771 }
4772
4773 return *this;
4774 }
4775
4776 //============================================================================
4777 // Method Description:
4786 void tofile(const std::string& inFilename) const
4787 {
4788 dump(inFilename);
4789 }
4790
4791 //============================================================================
4792 // Method Description:
4802 void tofile(const std::string& inFilename, const char inSep) const
4803 {
4805
4806 std::filesystem::path f(inFilename);
4807 if (!f.has_extension())
4808 {
4809 f.replace_extension("txt");
4810 }
4811
4812 std::ofstream ofile(f.c_str());
4813 if (!ofile.good())
4814 {
4815 THROW_RUNTIME_ERROR("Input file could not be opened:\n\t" + inFilename);
4816 }
4817
4818 size_type counter = 0;
4819 for (auto value : *this)
4820 {
4821 ofile << value;
4822 if (counter++ != size_ - 1)
4823 {
4824 ofile << inSep;
4825 }
4826 }
4827 ofile << '\n';
4828 ofile.close();
4829 }
4830
4831 //============================================================================
4832 // Method Description:
4840 [[nodiscard]] NdArray<size_type> toIndices(Slice inSlice, Axis inAxis = Axis::NONE) const
4841 {
4842 size_type numElements = 0;
4843 switch (inAxis)
4844 {
4845 case Axis::NONE:
4846 {
4847 numElements = inSlice.numElements(size_);
4848 break;
4849 }
4850 case Axis::ROW:
4851 {
4852 numElements = inSlice.numElements(shape_.rows);
4853 break;
4854 }
4855 case Axis::COL:
4856 {
4857 numElements = inSlice.numElements(shape_.cols);
4858 break;
4859 }
4860 default:
4861 {
4862 // not actually possible, getting rid of compiler warning
4863 THROW_INVALID_ARGUMENT_ERROR("Invalid 'inAxis' option");
4864 }
4865 }
4866
4867 if (numElements == 0)
4868 {
4869 return {};
4870 }
4871
4872 NdArray<size_type> indices(1, numElements);
4873 indices[0] = static_cast<size_type>(inSlice.start);
4874 for (size_type i = 1; i < indices.size(); ++i)
4875 {
4876 indices[static_cast<index_type>(i)] = static_cast<size_type>(
4877 indices[static_cast<index_type>(i - size_type{ 1 })] + static_cast<size_type>(inSlice.step));
4878 }
4879
4880 return indices;
4881 }
4882
4883 //============================================================================
4884 // Method Description:
4889 [[nodiscard]] std::vector<dtype> toStlVector() const
4890 {
4891 return std::vector<dtype>(cbegin(), cend());
4892 }
4893
4894 //============================================================================
4895 // Method Description:
4906 [[nodiscard]] value_type trace(size_type inOffset = 0, Axis inAxis = Axis::ROW) const noexcept
4907 {
4909
4910 size_type rowStart = 0;
4911 size_type colStart = 0;
4912 switch (inAxis)
4913 {
4914 case Axis::ROW:
4915 {
4916 rowStart += inOffset;
4917 break;
4918 }
4919 case Axis::COL:
4920 {
4921 colStart += inOffset;
4922 break;
4923 }
4924 default:
4925 {
4926 // if the user input NONE, override back to ROW
4927 inAxis = Axis::ROW;
4928 break;
4929 }
4930 }
4931
4932 if (rowStart >= shape_.rows || colStart >= shape_.cols)
4933 {
4934 return dtype{ 0 };
4935 }
4936
4937 size_type col = colStart;
4938 dtype sum = 0;
4939 for (size_type row = rowStart; row < shape_.rows; ++row)
4940 {
4941 if (col >= shape_.cols)
4942 {
4943 break;
4944 }
4945 sum += operator()(row, col++);
4946 }
4947
4948 return sum;
4949 }
4950
4951 //============================================================================
4952 // Method Description:
4959 [[nodiscard]] self_type transpose() const
4960 {
4961 self_type transArray(shape_.cols, shape_.rows);
4962 for (uint32 row = 0; row < shape_.rows; ++row)
4963 {
4964 for (uint32 col = 0; col < shape_.cols; ++col)
4965 {
4966 transArray(col, row) = operator()(row, col);
4967 }
4968 }
4969 return transArray;
4970 }
4971
4972 //============================================================================
4973 // Method Description:
4977 self_type& zeros() noexcept
4978 {
4980
4981 fill(dtype{ 0 });
4982 return *this;
4983 }
4984
4985 private:
4986 //====================================Attributes==============================
4987 allocator_type allocator_{};
4988 Shape shape_{ 0, 0 };
4989 size_type size_{ 0 };
4990 Endian endianess_{ Endian::NATIVE };
4991 pointer array_{ nullptr };
4992 bool ownsPtr_{ false };
4993
4994 //============================================================================
4995 // Method Description:
4998 void deleteArray() noexcept
4999 {
5000 if (ownsPtr_ && array_ != nullptr)
5001 {
5002 allocator_.deallocate(array_, size_);
5003 }
5004
5005 array_ = nullptr;
5006 shape_.rows = shape_.cols = 0;
5007 size_ = 0;
5008 ownsPtr_ = false;
5009 endianess_ = Endian::NATIVE;
5010 }
5011
5012 //============================================================================
5013 // Method Description:
5016 void newArray()
5017 {
5018 if (size_ > 0)
5019 {
5020 array_ = allocator_.allocate(size_);
5021 ownsPtr_ = true;
5022 }
5023 }
5024
5025 //============================================================================
5026 // Method Description:
5031 void newArray(const Shape& inShape)
5032 {
5033 deleteArray();
5034
5035 shape_ = inShape;
5036 size_ = inShape.size();
5037 newArray();
5038 }
5039 };
5040
5041 // NOTE: this needs to be defined outside of the class to get rid of a compiler
5042 // error in Visual Studio
5043 template<typename dtype, class Alloc_>
5044 [[nodiscard]] std::pair<NdArray<uint32>, NdArray<uint32>> NdArray<dtype, Alloc_>::nonzero() const
5045 {
5047
5048 std::vector<size_type> rowIndices;
5049 std::vector<size_type> colIndices;
5050
5051 for (uint32 row = 0; row < shape_.rows; ++row)
5052 {
5053 for (uint32 col = 0; col < shape_.cols; ++col)
5054 {
5055 if (!utils::essentiallyEqual(operator()(row, col), dtype{ 0 }))
5056 {
5057 rowIndices.push_back(row);
5058 colIndices.push_back(col);
5059 }
5060 }
5061 }
5062
5063 return std::make_pair(NdArray<size_type>(rowIndices), NdArray<size_type>(colIndices));
5064 }
5065} // namespace nc
#define THROW_RUNTIME_ERROR(msg)
Definition Error.hpp:40
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_VALID_DTYPE(dtype)
Definition StaticAsserts.hpp:35
#define STATIC_ASSERT_FLOAT(dtype)
Definition StaticAsserts.hpp:50
#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
Custom column iterator for NdArray.
Definition NdArrayIterators.hpp:813
Custom column const_iterator for NdArray.
Definition NdArrayIterators.hpp:487
Custom const_iterator for NdArray.
Definition NdArrayIterators.hpp:41
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
NdArray(pointer inPtr, UIntType size, PointerPolicy policy)
Definition NdArrayCore.hpp:576
const_reverse_column_iterator rcolbegin() const noexcept
Definition NdArrayCore.hpp:1569
size_type dimSize(Axis inAxis) const noexcept
Definition NdArrayCore.hpp:2756
NdArray< size_type > toIndices(Slice inSlice, Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:4840
self_type operator[](Slice inSlice) const
Definition NdArrayCore.hpp:819
self_type max(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3117
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
reverse_iterator rbegin() noexcept
Definition NdArrayCore.hpp:1465
self_type at(index_type inRowIndex, const Slice &inColSlice) const
Definition NdArrayCore.hpp:1193
self_type columns(const NdArray< size_type > &inCols) const
Definition NdArrayCore.hpp:2496
self_type & put(const RowIndices &inRowIndices, const ColIndices &inColIndices, const self_type &inValues)
Definition NdArrayCore.hpp:4040
self_type & resizeSlow(size_type inNumRows, size_type inNumCols)
Definition NdArrayCore.hpp:4479
self_type & put(const Slice &inRowSlice, const ColIndices &inColIndices, const value_type &inValue)
Definition NdArrayCore.hpp:3934
self_type & zeros() noexcept
Definition NdArrayCore.hpp:4977
self_type & ones() noexcept
Definition NdArrayCore.hpp:3563
const_iterator cbegin() const noexcept
Definition NdArrayCore.hpp:1361
self_type at(const RowIndices &rowIndices, const ColIndices &colIndices) const
Definition NdArrayCore.hpp:1269
const_column_iterator ccolbegin(size_type inCol) const
Definition NdArrayCore.hpp:1450
NdArrayConstColumnIterator< dtype, size_type, const_pointer, difference_type > const_column_iterator
Definition NdArrayCore.hpp:162
NdArray< bool > contains(value_type inValue, Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:2517
const_pointer data() const noexcept
Definition NdArrayCore.hpp:2682
iterator end() noexcept
Definition NdArrayCore.hpp:1619
self_type & swapCols(index_type colIdx1, index_type colIdx2) noexcept
Definition NdArrayCore.hpp:4748
NdArray(const std::initializer_list< std::initializer_list< dtype > > &inList)
Definition NdArrayCore.hpp:235
reference at(index_type inRowIndex, index_type inColIndex)
Definition NdArrayCore.hpp:1064
self_type & put(const RowIndices &inRowIndices, const ColIndices &inColIndices, const value_type &inValue)
Definition NdArrayCore.hpp:3890
self_type repeat(const Shape &inRepeatShape) const
Definition NdArrayCore.hpp:4312
self_type at(Slice rowSlice, const Indices &colIndices) const
Definition NdArrayCore.hpp:1252
reference back(size_type row)
Definition NdArrayCore.hpp:2392
iterator end(size_type inRow)
Definition NdArrayCore.hpp:1631
self_type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) const
Definition NdArrayCore.hpp:976
void tofile(const std::string &inFilename, const char inSep) const
Definition NdArrayCore.hpp:4802
const_column_iterator ccolbegin() const noexcept
Definition NdArrayCore.hpp:1438
NdArray(std::array< dtype, ArraySize > &inArray, PointerPolicy policy=PointerPolicy::COPY)
Definition NdArrayCore.hpp:270
NdArray(const std::deque< std::deque< dtype > > &in2dDeque)
Definition NdArrayCore.hpp:465
self_type & reshape(size_type inSize)
Definition NdArrayCore.hpp:4347
typename AllocTraits::pointer pointer
Definition NdArrayCore.hpp:148
self_type transpose() const
Definition NdArrayCore.hpp:4959
reverse_iterator rbegin(size_type inRow)
Definition NdArrayCore.hpp:1477
NdArray(std::vector< dtype > &inVector, PointerPolicy policy=PointerPolicy::COPY)
Definition NdArrayCore.hpp:344
NdArray< size_type > argsort(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:2189
const_reverse_column_iterator rcolend() const noexcept
Definition NdArrayCore.hpp:1877
self_type rows(const NdArray< size_type > &inRows) const
Definition NdArrayCore.hpp:4566
const dtype & const_reference
Definition NdArrayCore.hpp:151
bool issquare() const noexcept
Definition NdArrayCore.hpp:3085
NdArrayIterator< dtype, pointer, difference_type > iterator
Definition NdArrayCore.hpp:156
bool isflat() const noexcept
Definition NdArrayCore.hpp:3021
Endian endianess() const noexcept
Definition NdArrayCore.hpp:2868
self_type dot(const self_type &inOtherArray) const
Definition NdArrayCore.hpp:2791
void tofile(const std::string &inFilename) const
Definition NdArrayCore.hpp:4786
const_reverse_column_iterator crcolbegin() const noexcept
Definition NdArrayCore.hpp:1592
self_type & swapRows(index_type rowIdx1, index_type rowIdx2) noexcept
Definition NdArrayCore.hpp:4766
const_reverse_column_iterator crcolend(size_type inCol) const
Definition NdArrayCore.hpp:1912
size_type numCols() const noexcept
Definition NdArrayCore.hpp:3541
column_iterator colbegin(size_type inCol)
Definition NdArrayCore.hpp:1400
NdArrayColumnIterator< dtype, size_type, pointer, difference_type > column_iterator
Definition NdArrayCore.hpp:161
self_type median(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3207
const_reference at(index_type inRowIndex, index_type inColIndex) const
Definition NdArrayCore.hpp:1077
self_type at(const Slice &inSlice) const
Definition NdArrayCore.hpp:1107
NdArray< bool > none(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3487
pointer data() noexcept
Definition NdArrayCore.hpp:2672
bool isempty() const noexcept
Definition NdArrayCore.hpp:3008
column_iterator colbegin() noexcept
Definition NdArrayCore.hpp:1388
NdArray(std::vector< std::array< dtype, Dim1Size > > &in2dArray, PointerPolicy policy=PointerPolicy::COPY)
Definition NdArrayCore.hpp:412
const_reference front(size_type row) const
Definition NdArrayCore.hpp:2958
reverse_column_iterator rcolend(size_type inCol)
Definition NdArrayCore.hpp:1862
self_type column(size_type inColumn) const
Definition NdArrayCore.hpp:2484
self_type prod(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3679
NdArray(Iterator inFirst, Iterator inLast)
Definition NdArrayCore.hpp:517
reference at(index_type inIndex)
Definition NdArrayCore.hpp:1030
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
reverse_column_iterator rcolbegin() noexcept
Definition NdArrayCore.hpp:1542
self_type at(const Indices &inIndices) const
Definition NdArrayCore.hpp:1137
NdArrayConstIterator< dtype, const_pointer, difference_type > const_iterator
Definition NdArrayCore.hpp:157
const_iterator cbegin(size_type inRow) const
Definition NdArrayCore.hpp:1373
const_column_iterator ccolend(size_type inCol) const
Definition NdArrayCore.hpp:1835
self_type at(index_type rowIndex, const Indices &colIndices) const
Definition NdArrayCore.hpp:1237
self_type & resizeFast(size_type inNumRows, size_type inNumCols)
Definition NdArrayCore.hpp:4447
const_iterator cend(size_type inRow) const
Definition NdArrayCore.hpp:1681
NdArray< size_type > flatnonzero() const
Definition NdArrayCore.hpp:2897
self_type & byteswap() noexcept
Definition NdArrayCore.hpp:2405
self_type & ravel()
Definition NdArrayCore.hpp:4255
self_type getByIndices(const NdArray< size_type > &inIndices) const
Definition NdArrayCore.hpp:2981
const_reverse_column_iterator rcolend(size_type inCol) const
Definition NdArrayCore.hpp:1889
NdArray(const std::vector< std::vector< dtype > > &in2dVector)
Definition NdArrayCore.hpp:378
self_type & partition(size_type inKth, Axis inAxis=Axis::NONE)
Definition NdArrayCore.hpp:3597
self_type & put(const Indices &inRowIndices, index_type inColIndex, const value_type &inValue)
Definition NdArrayCore.hpp:3967
const_iterator end(size_type inRow) const
Definition NdArrayCore.hpp:1658
self_type flatten() const
Definition NdArrayCore.hpp:2923
self_type & replace(value_type oldValue, value_type newValue)
Definition NdArrayCore.hpp:4324
reference back() noexcept
Definition NdArrayCore.hpp:2370
const_reverse_column_iterator crcolend() const noexcept
Definition NdArrayCore.hpp:1900
const_reference back(size_type row) const
Definition NdArrayCore.hpp:2381
reverse_column_iterator rcolbegin(size_type inCol)
Definition NdArrayCore.hpp:1554
self_type & put(Slice inRowSlice, Slice inColSlice, const self_type &inValues)
Definition NdArrayCore.hpp:4129
iterator begin(size_type inRow)
Definition NdArrayCore.hpp:1323
const_reverse_iterator rend() const noexcept
Definition NdArrayCore.hpp:1723
self_type copy() const
Definition NdArrayCore.hpp:2558
self_type round(uint8 inNumDecimals=0) const
Definition NdArrayCore.hpp:4533
std::vector< dtype > toStlVector() const
Definition NdArrayCore.hpp:4889
self_type swapaxes() const
Definition NdArrayCore.hpp:4735
const_reverse_column_iterator rcolbegin(size_type inCol) const
Definition NdArrayCore.hpp:1581
typename AllocTraits::difference_type difference_type
Definition NdArrayCore.hpp:154
const_iterator end() const noexcept
Definition NdArrayCore.hpp:1646
NdArray< dtypeOut > astype() const
Definition NdArrayCore.hpp:2253
self_type & put(index_type inRowIndex, const Slice &inColSlice, const self_type &inValues)
Definition NdArrayCore.hpp:4198
bool ownsInternalData() noexcept
Definition NdArrayCore.hpp:3577
self_type operator[](const NdArray< bool > &inMask) const
Definition NdArrayCore.hpp:831
self_type & fill(value_type inFillValue) noexcept
Definition NdArrayCore.hpp:2884
column_iterator colend() noexcept
Definition NdArrayCore.hpp:1773
NdArray(std::initializer_list< dtype > inList)
Definition NdArrayCore.hpp:218
self_type at(const Indices &rowIndices, Slice colSlice) const
Definition NdArrayCore.hpp:1223
const_reference back() const noexcept
Definition NdArrayCore.hpp:2359
std::pair< NdArray< size_type >, NdArray< size_type > > nonzero() const
Definition NdArrayCore.hpp:5044
self_type diagonal(index_type inOffset=0, Axis inAxis=Axis::ROW) const
Definition NdArrayCore.hpp:2711
self_type & put(index_type inRowIndex, const Indices &inColIndices, const self_type &inValues)
Definition NdArrayCore.hpp:4181
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition NdArrayCore.hpp:159
self_type at(const Slice &inRowSlice, index_type inColIndex) const
Definition NdArrayCore.hpp:1179
NdArray(const_pointer inPtr, UIntType1 numRows, UIntType2 numCols)
Definition NdArrayCore.hpp:554
NdArray< bool > any(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:1975
NdArray(const self_type &inOtherArray)
Definition NdArrayCore.hpp:629
NdArray(self_type &&inOtherArray) noexcept
Definition NdArrayCore.hpp:647
uint64 nbytes() const noexcept
Definition NdArrayCore.hpp:3309
NdArray< size_type > argmax(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:2020
self_type at(const NdArray< bool > &inMask) const
Definition NdArrayCore.hpp:1119
NdArray(const std::list< dtype > &inList)
Definition NdArrayCore.hpp:497
NdArray()=default
self_type & put(const Indices &inRowIndices, index_type inColIndex, const self_type &inValues)
Definition NdArrayCore.hpp:4146
const_reference front() const noexcept
Definition NdArrayCore.hpp:2936
~NdArray() noexcept
Definition NdArrayCore.hpp:664
Slice rSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition NdArrayCore.hpp:1018
reference front() noexcept
Definition NdArrayCore.hpp:2947
self_type & put(const Slice &inRowSlice, index_type inColIndex, const self_type &inValues)
Definition NdArrayCore.hpp:4163
self_type & put(Slice inRowSlice, const ColIndices &inColIndices, const self_type &inValues)
Definition NdArrayCore.hpp:4113
self_type & put(const Slice &inRowSlice, index_type inColIndex, const value_type &inValue)
Definition NdArrayCore.hpp:3984
NdArray(size_type inNumRows, size_type inNumCols)
Definition NdArrayCore.hpp:192
self_type & putMask(const NdArray< bool > &inMask, const value_type &inValue)
Definition NdArrayCore.hpp:4211
Allocator allocator_type
Definition NdArrayCore.hpp:147
self_type & nans() noexcept
Definition NdArrayCore.hpp:3292
NdArray(pointer inPtr, UIntType1 numRows, UIntType2 numCols, PointerPolicy policy)
Definition NdArrayCore.hpp:595
void print() const
Definition NdArrayCore.hpp:3663
const_reverse_column_iterator crcolbegin(size_type inCol) const
Definition NdArrayCore.hpp:1604
self_type & reshape(const Shape &inShape)
Definition NdArrayCore.hpp:4432
size_type numRows() const noexcept
Definition NdArrayCore.hpp:3553
reverse_iterator rend(size_type inRow)
Definition NdArrayCore.hpp:1708
NdArray(size_type inSquareSize)
Definition NdArrayCore.hpp:178
self_type at(const Indices &rowIndices, index_type colIndex) const
Definition NdArrayCore.hpp:1208
reverse_iterator rend() noexcept
Definition NdArrayCore.hpp:1696
const_reverse_iterator rend(size_type inRow) const
Definition NdArrayCore.hpp:1735
typename AllocTraits::const_pointer const_pointer
Definition NdArrayCore.hpp:149
self_type & put(const Indices &inIndices, const self_type &inValues)
Definition NdArrayCore.hpp:3825
self_type & operator=(value_type inValue) noexcept
Definition NdArrayCore.hpp:710
const_reverse_iterator crbegin() const noexcept
Definition NdArrayCore.hpp:1515
NdArray(const_pointer inPtr, UIntType size)
Definition NdArrayCore.hpp:537
const_column_iterator colend(size_type inCol) const
Definition NdArrayCore.hpp:1812
std::reverse_iterator< iterator > reverse_iterator
Definition NdArrayCore.hpp:158
const_reverse_iterator rbegin(size_type inRow) const
Definition NdArrayCore.hpp:1504
self_type operator[](const Indices &inIndices) const
Definition NdArrayCore.hpp:845
const_iterator cend() const noexcept
Definition NdArrayCore.hpp:1669
self_type & resizeSlow(const Shape &inShape)
Definition NdArrayCore.hpp:4518
self_type & operator=(self_type &&rhs) noexcept
Definition NdArrayCore.hpp:727
std::string str() const
Definition NdArrayCore.hpp:4658
std::reverse_iterator< const_column_iterator > const_reverse_column_iterator
Definition NdArrayCore.hpp:164
self_type cumsum(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:2624
NdArray< bool > all(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:1931
self_type & resizeFast(const Shape &inShape)
Definition NdArrayCore.hpp:4462
self_type & put(index_type inRowIndex, const Indices &inColIndices, const value_type &inValue)
Definition NdArrayCore.hpp:4002
reference front(size_type row)
Definition NdArrayCore.hpp:2969
self_type & put(const RowIndices &inRowIndices, Slice inColSlice, const self_type &inValues)
Definition NdArrayCore.hpp:4096
NdArray(std::array< std::array< dtype, Dim1Size >, Dim0Size > &in2dArray, PointerPolicy policy=PointerPolicy::COPY)
Definition NdArrayCore.hpp:306
self_type repeat(size_type inNumRows, size_type inNumCols) const
Definition NdArrayCore.hpp:4271
const_iterator begin(size_type inRow) const
Definition NdArrayCore.hpp:1350
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
const_column_iterator colbegin() const noexcept
Definition NdArrayCore.hpp:1415
bool isscalar() const noexcept
Definition NdArrayCore.hpp:3032
std::reverse_iterator< column_iterator > reverse_column_iterator
Definition NdArrayCore.hpp:163
self_type operator()(Slice inRowSlice, Slice inColSlice) const
Definition NdArrayCore.hpp:866
self_type & put(const Slice &inSlice, const value_type &inValue)
Definition NdArrayCore.hpp:3855
self_type newbyteorder(Endian inEndianess) const
Definition NdArrayCore.hpp:3324
value_type item() const
Definition NdArrayCore.hpp:3098
const_reference at(index_type inIndex) const
Definition NdArrayCore.hpp:1042
self_type & reshape(index_type inNumRows, index_type inNumCols)
Definition NdArrayCore.hpp:4378
const_column_iterator colend() const noexcept
Definition NdArrayCore.hpp:1800
self_type row(size_type inRow) const
Definition NdArrayCore.hpp:4554
self_type operator()(const Indices &rowIndices, index_type colIndex) const
Definition NdArrayCore.hpp:911
self_type at(const Slice &inRowSlice, const Slice &inColSlice) const
Definition NdArrayCore.hpp:1166
self_type operator()(Slice inRowSlice, index_type inColIndex) const
Definition NdArrayCore.hpp:880
const_reverse_iterator crend() const noexcept
Definition NdArrayCore.hpp:1746
self_type operator()(index_type rowIndex, const Indices &colIndices) const
Definition NdArrayCore.hpp:942
NdArray< dtype, Allocator > self_type
Definition NdArrayCore.hpp:145
const_column_iterator colbegin(size_type inCol) const
Definition NdArrayCore.hpp:1427
self_type & put(const Slice &inSlice, const self_type &inValues)
Definition NdArrayCore.hpp:3870
self_type & put(const Indices &inIndices, const value_type &inValue)
Definition NdArrayCore.hpp:3804
self_type cumprod(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:2572
const_column_iterator ccolend() const noexcept
Definition NdArrayCore.hpp:1823
reference operator()(index_type inRowIndex, index_type inColIndex) noexcept
Definition NdArrayCore.hpp:783
self_type operator()(index_type inRowIndex, Slice inColSlice) const
Definition NdArrayCore.hpp:895
reverse_column_iterator rcolend() noexcept
Definition NdArrayCore.hpp:1850
const_reverse_iterator rbegin() const noexcept
Definition NdArrayCore.hpp:1492
NdArray(const std::deque< dtype > &inDeque)
Definition NdArrayCore.hpp:448
void dump(const std::string &inFilename) const
Definition NdArrayCore.hpp:2841
dtype & reference
Definition NdArrayCore.hpp:150
self_type & put(const RowIndices &inRowIndices, const Slice &inColSlice, const value_type &inValue)
Definition NdArrayCore.hpp:3917
self_type & put(index_type inRowIndex, const Slice &inColSlice, const value_type &inValue)
Definition NdArrayCore.hpp:4019
value_type trace(size_type inOffset=0, Axis inAxis=Axis::ROW) const noexcept
Definition NdArrayCore.hpp:4906
pointer dataRelease() noexcept
Definition NdArrayCore.hpp:2694
self_type ptp(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3723
self_type clip(value_type inMin, value_type inMax) const
Definition NdArrayCore.hpp:2445
self_type getByMask(const NdArray< bool > &inMask) const
Definition NdArrayCore.hpp:2995
uint32 size_type
Definition NdArrayCore.hpp:152
NdArray< bool > issorted(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3044
self_type & operator=(const self_type &rhs)
Definition NdArrayCore.hpp:686
const_iterator begin() const noexcept
Definition NdArrayCore.hpp:1338
NdArray< size_type > argpartition(size_type inKth, Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:2116
column_iterator colend(size_type inCol)
Definition NdArrayCore.hpp:1785
NdArray< size_type > argmin(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:2067
const_reference operator[](index_type inIndex) const noexcept
Definition NdArrayCore.hpp:765
self_type operator()(Slice rowSlice, const Indices &colIndices) const
Definition NdArrayCore.hpp:958
const_reference operator()(index_type inRowIndex, index_type inColIndex) const noexcept
Definition NdArrayCore.hpp:796
self_type min(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3161
self_type & putMask(const NdArray< bool > &inMask, const self_type &inValues)
Definition NdArrayCore.hpp:4228
dtype value_type
Definition NdArrayCore.hpp:146
reference operator[](index_type inIndex) noexcept
Definition NdArrayCore.hpp:753
Slice cSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition NdArrayCore.hpp:1004
self_type & sort(Axis inAxis=Axis::NONE)
Definition NdArrayCore.hpp:4614
self_type & put(const Slice &inRowSlice, const Slice &inColSlice, const value_type &inValue)
Definition NdArrayCore.hpp:3950
const_reverse_iterator crend(size_type inRow) const
Definition NdArrayCore.hpp:1758
const_reverse_iterator crbegin(size_type inRow) const
Definition NdArrayCore.hpp:1527
int32 index_type
Definition NdArrayCore.hpp:153
NdArray(const Shape &inShape)
Definition NdArrayCore.hpp:205
self_type sum(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:4694
self_type operator()(const Indices &rowIndices, Slice colSlice) const
Definition NdArrayCore.hpp:927
self_type & put(index_type inRow, index_type inCol, const value_type &inValue)
Definition NdArrayCore.hpp:3786
self_type & put(index_type inIndex, const value_type &inValue)
Definition NdArrayCore.hpp:3769
Custom iterator for NdArray.
Definition NdArrayIterators.hpp:313
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
uint32 rows
Definition Core/shape.hpp:44
uint32 cols
Definition Core/shape.hpp:45
A Class for slicing into NdArrays.
Definition Slice.hpp:45
int32 step
Definition Slice.hpp:50
int32 start
Definition Slice.hpp:48
uint32 numElements(uint32 inArraySize)
Definition Slice.hpp:195
const double nan
NaN.
Definition Core/Constants.hpp:41
bool isLittleEndian() noexcept
Definition Endian.hpp:44
dtype byteSwap(dtype value) noexcept
Definition Endian.hpp:64
bool any_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition StlAlgorithms.hpp:77
T transform_reduce(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, T init)
Definition StlAlgorithms.hpp:825
void sort(RandomIt first, RandomIt last) noexcept
Definition StlAlgorithms.hpp:697
bool none_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition StlAlgorithms.hpp:406
ForwardIt max_element(ForwardIt first, ForwardIt last) noexcept
Definition StlAlgorithms.hpp:286
void stable_sort(RandomIt first, RandomIt last) noexcept
Definition StlAlgorithms.hpp:735
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition StlAlgorithms.hpp:776
bool all_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition StlAlgorithms.hpp:56
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition StlAlgorithms.hpp:226
InputIt find(InputIt first, InputIt last, const T &value) noexcept
Definition StlAlgorithms.hpp:206
void replace(ForwardIt first, ForwardIt last, const T &oldValue, const T &newValue) noexcept
Definition StlAlgorithms.hpp:469
std::pair< ForwardIt, ForwardIt > minmax_element(ForwardIt first, ForwardIt last) noexcept
Definition StlAlgorithms.hpp:365
bool is_sorted(ForwardIt first, ForwardIt last) noexcept
Definition StlAlgorithms.hpp:246
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition StlAlgorithms.hpp:98
void nth_element(RandomIt first, RandomIt nth, RandomIt last) noexcept
Definition StlAlgorithms.hpp:426
ForwardIt min_element(ForwardIt first, ForwardIt last) noexcept
Definition StlAlgorithms.hpp:325
void fill(ForwardIt first, ForwardIt last, const T &value) noexcept
Definition StlAlgorithms.hpp:184
Definition NdArrayCore.hpp:67
constexpr bool is_ndarray_signed_int_v
Definition NdArrayCore.hpp:120
constexpr bool is_ndarray_int_v
Definition NdArrayCore.hpp:93
std::enable_if_t< is_ndarray_int_v< T >, int > ndarray_int_concept
Definition NdArrayCore.hpp:127
std::string num2str(dtype inNumber)
Definition num2str.hpp:44
dtype power(dtype inValue, uint8 inPower) noexcept
Definition Utils/power.hpp:44
std::string value2str(dtype inValue)
Definition value2str.hpp:45
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition essentiallyEqual.hpp:47
Definition Cartesian.hpp:39
std::uint64_t uint64
Definition Types.hpp:39
std::uint8_t uint8
Definition Types.hpp:42
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
std::complex< Out > complex_cast(const std::complex< In > &value) noexcept
Definition StdComplexOperators.hpp:111
std::int32_t int32
Definition Types.hpp:36
Endian
Enum for endianess.
Definition Enums.hpp:46
@ LITTLE
Definition Enums.hpp:49
@ BIG
Definition Enums.hpp:48
@ NATIVE
Definition Enums.hpp:47
std::int64_t int64
Definition Types.hpp:35
PointerPolicy
Policy for NdArray constructor that takes in a pointer to data.
Definition Enums.hpp:56
@ SHELL
Definition Enums.hpp:58
@ COPY
Definition Enums.hpp:57
std::uint32_t uint32
Definition Types.hpp:40
static constexpr bool value
Definition NdArrayCore.hpp:85
Definition NdArrayCore.hpp:74
static constexpr bool value
Definition NdArrayCore.hpp:112
Definition NdArrayCore.hpp:101