NumCpp  2.10.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
row_stack.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <initializer_list>
31 #include <string>
32 
34 #include "NumCpp/Core/Shape.hpp"
35 #include "NumCpp/Core/Types.hpp"
36 #include "NumCpp/NdArray.hpp"
37 
38 namespace nc
39 {
40  //============================================================================
41  // Method Description:
48  template<typename dtype>
49  NdArray<dtype> row_stack(const std::initializer_list<NdArray<dtype>>& inArrayList)
50  {
51  // first loop through to calculate the final size of the array
52  Shape finalShape;
53  for (auto& ndarray : inArrayList)
54  {
55  if (finalShape.isnull())
56  {
57  finalShape = ndarray.shape();
58  }
59  else if (ndarray.shape().cols != finalShape.cols)
60  {
61  THROW_INVALID_ARGUMENT_ERROR("input arrays must have the same number of columns.");
62  }
63  else
64  {
65  finalShape.rows += ndarray.shape().rows;
66  }
67  }
68 
69  // now that we know the final size, contruct the output array
70  NdArray<dtype> returnArray(finalShape);
71  uint32 rowStart = 0;
72  for (auto& ndarray : inArrayList)
73  {
74  const Shape theShape = ndarray.shape();
75  for (uint32 row = 0; row < theShape.rows; ++row)
76  {
77  for (uint32 col = 0; col < theShape.cols; ++col)
78  {
79  returnArray(rowStart + row, col) = ndarray(row, col);
80  }
81  }
82  rowStart += theShape.rows;
83  }
84 
85  return returnArray;
86  }
87 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:138
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
bool isnull() const noexcept
Definition: Core/Shape.hpp:115
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
Definition: Coordinate.hpp:45
NdArray< dtype > row_stack(const std::initializer_list< NdArray< dtype >> &inArrayList)
Definition: row_stack.hpp:49
std::uint32_t uint32
Definition: Types.hpp:40