NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
column_stack.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <initializer_list>
31#include <string>
32#include <vector>
33
35#include "NumCpp/Core/Shape.hpp"
36#include "NumCpp/NdArray.hpp"
37
38namespace nc
39{
40 namespace detail
41 {
42 //============================================================================
43 // Method Description:
52 template<typename dtype, typename Iterator>
53 NdArray<dtype> column_stack(Iterator begin, Iterator end)
54 {
55 // first loop through to calculate the final size of the array
56 Shape finalShape;
57 auto iter = begin;
58 while (iter != end)
59 {
60 const auto& ndarray = *iter++;
61 if (ndarray.shape().isnull())
62 {
63 continue;
64 }
65
66 if (finalShape.isnull())
67 {
68 finalShape = ndarray.shape();
69 }
70 else if (ndarray.shape().rows != finalShape.rows)
71 {
72 THROW_INVALID_ARGUMENT_ERROR("input arrays must have the same number of rows.");
73 }
74 else
75 {
76 finalShape.cols += ndarray.shape().cols;
77 }
78 }
79
80 // now that we know the final size, contruct the output array
81 NdArray<dtype> returnArray(finalShape);
82 uint32 colStart = 0;
83 iter = begin;
84 while (iter != end)
85 {
86 const auto& ndarray = *iter++;
87 const Shape theShape = ndarray.shape();
88 for (uint32 row = 0; row < theShape.rows; ++row)
89 {
90 for (uint32 col = 0; col < theShape.cols; ++col)
91 {
92 returnArray(row, colStart + col) = ndarray(row, col);
93 }
94 }
95 colStart += theShape.cols;
96 }
97
98 return returnArray;
99 }
100 } // namespace detail
101
102 //============================================================================
103 // Method Description:
111 template<typename dtype>
112 NdArray<dtype> column_stack(const std::initializer_list<NdArray<dtype>>& inArrayList)
113 {
114 return detail::column_stack<dtype>(inArrayList.begin(), inArrayList.end());
115 }
116
117 //============================================================================
118 // Method Description:
126 template<typename dtype>
127 NdArray<dtype> column_stack(const std::vector<NdArray<dtype>>& inArrayList)
128 {
129 return detail::column_stack<dtype>(inArrayList.begin(), inArrayList.end());
130 }
131} // 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
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
NdArray< dtype > column_stack(Iterator begin, Iterator end)
Definition: column_stack.hpp:53
Definition: Cartesian.hpp:40
NdArray< dtype > column_stack(const std::initializer_list< NdArray< dtype > > &inArrayList)
Definition: column_stack.hpp:112
std::uint32_t uint32
Definition: Types.hpp:40