NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
append.hpp
Go to the documentation of this file.
1 
28 #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:
53  template<typename dtype>
54  NdArray<dtype> append(const NdArray<dtype>& inArray, const NdArray<dtype>& inAppendValues, Axis inAxis = Axis::NONE)
55  {
56  switch (inAxis)
57  {
58  case Axis::NONE:
59  {
60  NdArray<dtype> returnArray(1, inArray.size() + inAppendValues.size());
61  stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
62  stl_algorithms::copy(inAppendValues.cbegin(), inAppendValues.cend(), returnArray.begin() + inArray.size());
63 
64  return returnArray;
65  }
66  case Axis::ROW:
67  {
68  const Shape inShape = inArray.shape();
69  const Shape appendShape = inAppendValues.shape();
70  if (inShape.cols != appendShape.cols)
71  {
72  THROW_INVALID_ARGUMENT_ERROR("all the input array dimensions except for the concatenation axis must match exactly");
73  }
74 
75  NdArray<dtype> returnArray(inShape.rows + appendShape.rows, inShape.cols);
76  stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
77  stl_algorithms::copy(inAppendValues.cbegin(), inAppendValues.cend(), returnArray.begin() + inArray.size());
78 
79  return returnArray;
80  }
81  case Axis::COL:
82  {
83  const Shape inShape = inArray.shape();
84  const Shape appendShape = inAppendValues.shape();
85  if (inShape.rows != appendShape.rows)
86  {
87  THROW_INVALID_ARGUMENT_ERROR("all the input array dimensions except for the concatenation axis must match exactly");
88  }
89 
90  NdArray<dtype> returnArray(inShape.rows, inShape.cols + appendShape.cols);
91  for (uint32 row = 0; row < returnArray.shape().rows; ++row)
92  {
93  stl_algorithms::copy(inArray.cbegin(row), inArray.cend(row), returnArray.begin(row));
94  stl_algorithms::copy(inAppendValues.cbegin(row), inAppendValues.cend(row), returnArray.begin(row) + inShape.cols);
95  }
96 
97  return returnArray;
98  }
99  default:
100  {
101  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
102  return {}; // get rid of compiler warning
103  }
104  }
105  }
106 } // 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:4497
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1270
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1614
iterator begin() noexcept
Definition: NdArrayCore.hpp:1214
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:54
std::uint32_t uint32
Definition: Types.hpp:40