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