NumCpp  2.10.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
append.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <string>
31 
34 #include "NumCpp/Core/Types.hpp"
35 #include "NumCpp/NdArray.hpp"
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
52  template<typename dtype>
53  NdArray<dtype> append(const NdArray<dtype>& inArray, const NdArray<dtype>& inAppendValues, Axis inAxis = Axis::NONE)
54  {
55  switch (inAxis)
56  {
57  case Axis::NONE:
58  {
59  NdArray<dtype> returnArray(1, inArray.size() + inAppendValues.size());
60  stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
61  stl_algorithms::copy(inAppendValues.cbegin(),
62  inAppendValues.cend(),
63  returnArray.begin() + inArray.size());
64 
65  return returnArray;
66  }
67  case Axis::ROW:
68  {
69  const Shape inShape = inArray.shape();
70  const Shape appendShape = inAppendValues.shape();
71  if (inShape.cols != appendShape.cols)
72  {
74  "all the input array dimensions except for the concatenation axis must match exactly");
75  }
76 
77  NdArray<dtype> returnArray(inShape.rows + appendShape.rows, inShape.cols);
78  stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
79  stl_algorithms::copy(inAppendValues.cbegin(),
80  inAppendValues.cend(),
81  returnArray.begin() + inArray.size());
82 
83  return returnArray;
84  }
85  case Axis::COL:
86  {
87  const Shape inShape = inArray.shape();
88  const Shape appendShape = inAppendValues.shape();
89  if (inShape.rows != appendShape.rows)
90  {
92  "all the input array dimensions except for the concatenation axis must match exactly");
93  }
94 
95  NdArray<dtype> returnArray(inShape.rows, inShape.cols + appendShape.cols);
96  for (uint32 row = 0; row < returnArray.shape().rows; ++row)
97  {
98  stl_algorithms::copy(inArray.cbegin(row), inArray.cend(row), returnArray.begin(row));
99  stl_algorithms::copy(inAppendValues.cbegin(row),
100  inAppendValues.cend(row),
101  returnArray.begin(row) + inShape.cols);
102  }
103 
104  return returnArray;
105  }
106  default:
107  {
108  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
109  return {}; // get rid of compiler warning
110  }
111  }
112  }
113 } // 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
size_type size() const noexcept
Definition: NdArrayCore.hpp:4415
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1308
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4402
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1616
iterator begin() noexcept
Definition: NdArrayCore.hpp:1258
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:97
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:47
NdArray< dtype > append(const NdArray< dtype > &inArray, const NdArray< dtype > &inAppendValues, Axis inAxis=Axis::NONE)
Definition: append.hpp:53
std::uint32_t uint32
Definition: Types.hpp:40