NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
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
35namespace nc
36{
37 //============================================================================
38 // Method Description:
50 template<typename dtype>
51 NdArray<dtype> append(const NdArray<dtype>& inArray, const NdArray<dtype>& inAppendValues, Axis inAxis = Axis::NONE)
52 {
53 if (inArray.shape().isnull())
54 {
55 return inAppendValues;
56 }
57 else if (inAppendValues.shape().isnull())
58 {
59 return inArray;
60 }
61
62 switch (inAxis)
63 {
64 case Axis::NONE:
65 {
66 NdArray<dtype> returnArray(1, inArray.size() + inAppendValues.size());
67 stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
68 stl_algorithms::copy(inAppendValues.cbegin(),
69 inAppendValues.cend(),
70 returnArray.begin() + inArray.size());
71
72 return returnArray;
73 }
74 case Axis::ROW:
75 {
76 const Shape inShape = inArray.shape();
77 const Shape appendShape = inAppendValues.shape();
78 if (inShape.cols != appendShape.cols)
79 {
81 "all the input array dimensions except for the concatenation axis must match exactly");
82 }
83
84 NdArray<dtype> returnArray(inShape.rows + appendShape.rows, inShape.cols);
85 stl_algorithms::copy(inArray.cbegin(), inArray.cend(), returnArray.begin());
86 stl_algorithms::copy(inAppendValues.cbegin(),
87 inAppendValues.cend(),
88 returnArray.begin() + inArray.size());
89
90 return returnArray;
91 }
92 case Axis::COL:
93 {
94 const Shape inShape = inArray.shape();
95 const Shape appendShape = inAppendValues.shape();
96 if (inShape.rows != appendShape.rows)
97 {
99 "all the input array dimensions except for the concatenation axis must match exactly");
100 }
101
102 NdArray<dtype> returnArray(inShape.rows, inShape.cols + appendShape.cols);
103 for (uint32 row = 0; row < returnArray.shape().rows; ++row)
104 {
105 stl_algorithms::copy(inArray.cbegin(row), inArray.cend(row), returnArray.begin(row));
106 stl_algorithms::copy(inAppendValues.cbegin(row),
107 inAppendValues.cend(row),
108 returnArray.begin(row) + inShape.cols);
109 }
110
111 return returnArray;
112 }
113 default:
114 {
115 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
116 return {}; // get rid of compiler warning
117 }
118 }
119 }
120} // 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:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
const_iterator cbegin() const noexcept
Definition NdArrayCore.hpp:1361
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
const_iterator cend() const noexcept
Definition NdArrayCore.hpp:1669
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
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:98
Definition Cartesian.hpp:39
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
NdArray< dtype > append(const NdArray< dtype > &inArray, const NdArray< dtype > &inAppendValues, Axis inAxis=Axis::NONE)
Definition append.hpp:51
std::uint32_t uint32
Definition Types.hpp:40