NumCpp  2.4.0
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 
31 #include "NumCpp/Core/Shape.hpp"
32 #include "NumCpp/Core/Types.hpp"
33 #include "NumCpp/NdArray.hpp"
34 
35 #include <initializer_list>
36 #include <string>
37 
38 namespace nc
39 {
40  //============================================================================
41  // Method Description:
50  template<typename dtype>
51  NdArray<dtype> row_stack(const std::initializer_list<NdArray<dtype> >& inArrayList)
52  {
53  // first loop through to calculate the final size of the array
54  Shape finalShape;
55  for (auto& ndarray : inArrayList)
56  {
57  if (finalShape.isnull())
58  {
59  finalShape = ndarray.shape();
60  }
61  else if (ndarray.shape().cols != finalShape.cols)
62  {
63  THROW_INVALID_ARGUMENT_ERROR("input arrays must have the same number of columns.");
64  }
65  else
66  {
67  finalShape.rows += ndarray.shape().rows;
68  }
69  }
70 
71  // now that we know the final size, contruct the output array
72  NdArray<dtype> returnArray(finalShape);
73  uint32 rowStart = 0;
74  for (auto& ndarray : inArrayList)
75  {
76  const Shape theShape = ndarray.shape();
77  for (uint32 row = 0; row < theShape.rows; ++row)
78  {
79  for (uint32 col = 0; col < theShape.cols; ++col)
80  {
81  returnArray(rowStart + row, col) = ndarray(row, col);
82  }
83  }
84  rowStart += theShape.rows;
85  }
86 
87  return returnArray;
88  }
89 } // namespace nc
Error.hpp
nc::Shape::isnull
bool isnull() const noexcept
Definition: Core/Shape.hpp:113
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:45
nc::row_stack
NdArray< dtype > row_stack(const std::initializer_list< NdArray< dtype > > &inArrayList)
Definition: row_stack.hpp:51
Shape.hpp
nc
Definition: Coordinate.hpp:44
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:44
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Types.hpp