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