NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
column_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/NdArray.hpp"
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
48  template<typename dtype>
49  NdArray<dtype> column_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().rows != finalShape.rows)
60  {
61  THROW_INVALID_ARGUMENT_ERROR("input arrays must have the same number of rows.");
62  }
63  else
64  {
65  finalShape.cols += ndarray.shape().cols;
66  }
67  }
68 
69  // now that we know the final size, contruct the output array
70  NdArray<dtype> returnArray(finalShape);
71  uint32 colStart = 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(row, colStart + col) = ndarray(row, col);
80  }
81  }
82  colStart += theShape.cols;
83  }
84 
85  return returnArray;
86  }
87 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
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 > column_stack(const std::initializer_list< NdArray< dtype >> &inArrayList)
Definition: column_stack.hpp:49
std::uint32_t uint32
Definition: Types.hpp:40