NumCpp  2.1.0
A C++ implementation of the Python Numpy library
append.hpp
Go to the documentation of this file.
1 #pragma once
30 
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 #include <string>
37 
38 namespace nc
39 {
40  //============================================================================
41  // Method Description:
54  template<typename dtype>
55  NdArray<dtype> append(const NdArray<dtype>& inArray, const NdArray<dtype>& inAppendValues, Axis inAxis = Axis::NONE)
56  {
57  switch (inAxis)
58  {
59  case Axis::NONE:
60  {
61  NdArray<dtype> returnArray(1, inArray.size() + inAppendValues.size());
62  stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
63  stl_algorithms::copy(inAppendValues.cbegin(), inAppendValues.cend(), 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  {
73  THROW_INVALID_ARGUMENT_ERROR("all the input array dimensions except for the concatenation axis must match exactly");
74  }
75 
76  NdArray<dtype> returnArray(inShape.rows + appendShape.rows, inShape.cols);
77  stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
78  stl_algorithms::copy(inAppendValues.cbegin(), inAppendValues.cend(), returnArray.begin() + inArray.size());
79 
80  return returnArray;
81  }
82  case Axis::COL:
83  {
84  const Shape inShape = inArray.shape();
85  const Shape appendShape = inAppendValues.shape();
86  if (inShape.rows != appendShape.rows)
87  {
88  THROW_INVALID_ARGUMENT_ERROR("all the input array dimensions except for the concatenation axis must match exactly");
89  }
90 
91  NdArray<dtype> returnArray(inShape.rows, inShape.cols + appendShape.cols);
92  for (uint32 row = 0; row < returnArray.shape().rows; ++row)
93  {
94  stl_algorithms::copy(inArray.cbegin(row), inArray.cend(row), returnArray.begin(row));
95  stl_algorithms::copy(inAppendValues.cbegin(row), inAppendValues.cend(row), returnArray.begin(row) + inShape.cols);
96  }
97 
98  return returnArray;
99  }
100  default:
101  {
102  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
103  return {}; // get rid of compiler warning
104  }
105  }
106  }
107 } // namespace nc
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4312
nc::Axis::NONE
@ NONE
Error.hpp
nc::Axis::ROW
@ ROW
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::stl_algorithms::copy
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:96
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4326
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::append
NdArray< dtype > append(const NdArray< dtype > &inArray, const NdArray< dtype > &inAppendValues, Axis inAxis=Axis::NONE)
Definition: append.hpp:55
nc::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1491
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:47
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::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1147
StlAlgorithms.hpp
Types.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1091
nc::Axis::COL
@ COL