NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
column_stack.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
31 #include "NumCpp/Core/Shape.hpp"
32 #include "NumCpp/NdArray.hpp"
33 
34 #include <initializer_list>
35 #include <string>
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
50  template<typename dtype>
51  NdArray<dtype> column_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().rows != finalShape.rows)
62  {
63  THROW_INVALID_ARGUMENT_ERROR("input arrays must have the same number of rows.");
64  }
65  else
66  {
67  finalShape.cols += ndarray.shape().cols;
68  }
69  }
70 
71  // now that we know the final size, contruct the output array
72  NdArray<dtype> returnArray(finalShape);
73  uint32 colStart = 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(row, colStart + col) = ndarray(row, col);
82  }
83  }
84  colStart += theShape.cols;
85  }
86 
87  return returnArray;
88  }
89 } // 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:113
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:51
std::uint32_t uint32
Definition: Types.hpp:40