NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
append.hpp
Go to the documentation of this file.
1 #pragma once
29 
32 #include "NumCpp/Core/Types.hpp"
33 #include "NumCpp/NdArray.hpp"
34 
35 #include <string>
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(), inAppendValues.cend(), returnArray.begin() + inArray.size());
62 
63  return returnArray;
64  }
65  case Axis::ROW:
66  {
67  const Shape inShape = inArray.shape();
68  const Shape appendShape = inAppendValues.shape();
69  if (inShape.cols != appendShape.cols)
70  {
71  THROW_INVALID_ARGUMENT_ERROR("all the input array dimensions except for the concatenation axis must match exactly");
72  }
73 
74  NdArray<dtype> returnArray(inShape.rows + appendShape.rows, inShape.cols);
75  stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
76  stl_algorithms::copy(inAppendValues.cbegin(), inAppendValues.cend(), returnArray.begin() + inArray.size());
77 
78  return returnArray;
79  }
80  case Axis::COL:
81  {
82  const Shape inShape = inArray.shape();
83  const Shape appendShape = inAppendValues.shape();
84  if (inShape.rows != appendShape.rows)
85  {
86  THROW_INVALID_ARGUMENT_ERROR("all the input array dimensions except for the concatenation axis must match exactly");
87  }
88 
89  NdArray<dtype> returnArray(inShape.rows, inShape.cols + appendShape.cols);
90  for (uint32 row = 0; row < returnArray.shape().rows; ++row)
91  {
92  stl_algorithms::copy(inArray.cbegin(row), inArray.cend(row), returnArray.begin(row));
93  stl_algorithms::copy(inAppendValues.cbegin(row), inAppendValues.cend(row), returnArray.begin(row) + inShape.cols);
94  }
95 
96  return returnArray;
97  }
98  default:
99  {
100  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
101  return {}; // get rid of compiler warning
102  }
103  }
104  }
105 } // 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
size_type size() const noexcept
Definition: NdArrayCore.hpp:4296
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1216
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1524
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
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:95
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:46
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