NumCpp  2.12.1
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
30#include <string>
31
34#include "NumCpp/Core/Types.hpp"
35#include "NumCpp/NdArray.hpp"
36
37namespace 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 if (inArray.shape().isnull())
56 {
57 return inAppendValues;
58 }
59 else if (inAppendValues.shape().isnull())
60 {
61 return inArray;
62 }
63
64 switch (inAxis)
65 {
66 case Axis::NONE:
67 {
68 NdArray<dtype> returnArray(1, inArray.size() + inAppendValues.size());
69 stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
70 stl_algorithms::copy(inAppendValues.cbegin(),
71 inAppendValues.cend(),
72 returnArray.begin() + inArray.size());
73
74 return returnArray;
75 }
76 case Axis::ROW:
77 {
78 const Shape inShape = inArray.shape();
79 const Shape appendShape = inAppendValues.shape();
80 if (inShape.cols != appendShape.cols)
81 {
83 "all the input array dimensions except for the concatenation axis must match exactly");
84 }
85
86 NdArray<dtype> returnArray(inShape.rows + appendShape.rows, inShape.cols);
87 stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
88 stl_algorithms::copy(inAppendValues.cbegin(),
89 inAppendValues.cend(),
90 returnArray.begin() + inArray.size());
91
92 return returnArray;
93 }
94 case Axis::COL:
95 {
96 const Shape inShape = inArray.shape();
97 const Shape appendShape = inAppendValues.shape();
98 if (inShape.rows != appendShape.rows)
99 {
101 "all the input array dimensions except for the concatenation axis must match exactly");
102 }
103
104 NdArray<dtype> returnArray(inShape.rows, inShape.cols + appendShape.cols);
105 for (uint32 row = 0; row < returnArray.shape().rows; ++row)
106 {
107 stl_algorithms::copy(inArray.cbegin(row), inArray.cend(row), returnArray.begin(row));
108 stl_algorithms::copy(inAppendValues.cbegin(row),
109 inAppendValues.cend(row),
110 returnArray.begin(row) + inShape.cols);
111 }
112
113 return returnArray;
114 }
115 default:
116 {
117 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
118 return {}; // get rid of compiler warning
119 }
120 }
121 }
122} // 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:139
size_type size() const noexcept
Definition: NdArrayCore.hpp:4524
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1365
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1673
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
bool isnull() const noexcept
Definition: Core/Shape.hpp:115
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: Cartesian.hpp:40
Axis
Enum To describe an axis.
Definition: Enums.hpp:36
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