NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
BoostInterface.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#if defined(NUMCPP_INCLUDE_BOOST_PYTHON_INTERFACE) && !defined(NUMCPP_NO_USE_BOOST)
31
32#include <map>
33#include <string>
34
35#include "boost/python.hpp"
36#include "boost/python/numpy.hpp"
37
39#include "NumCpp/Core/Shape.hpp"
40#include "NumCpp/NdArray.hpp"
42
43namespace nc
44{
45 namespace boostPythonInterface
46 {
47 //============================================================================
54 template<typename dtype>
55 inline NdArray<dtype> boost2Nc(const boost::python::numpy::ndarray& inArray)
56 {
57 BoostNdarrayHelper<dtype> helper(inArray);
58 if (helper.numDimensions() > 2)
59 {
60 THROW_RUNTIME_ERROR("Can only convert 1 and 2 dimensional arrays.");
61 }
62
63 Shape arrayShape;
64 if (helper.numDimensions() == 1)
65 {
66 arrayShape.rows = 1;
67 arrayShape.cols = static_cast<uint32>(helper.shape().front());
68
69 NdArray<dtype> returnArray(arrayShape);
70 for (uint32 i = 0; i < arrayShape.size(); ++i)
71 {
72 returnArray[i] = helper(i);
73 }
74
75 return returnArray;
76 }
77
78 arrayShape.rows = static_cast<uint32>(helper.shape().front());
79 arrayShape.cols = static_cast<uint32>(helper.shape()[1]);
80
81 NdArray<dtype> returnArray(arrayShape);
82 for (uint32 row = 0; row < arrayShape.rows; ++row)
83 {
84 for (uint32 col = 0; col < arrayShape.cols; ++col)
85 {
86 returnArray(row, col) = helper(row, col);
87 }
88 }
89
90 return returnArray;
91 }
92
93 //============================================================================
100 template<typename dtype>
101 inline boost::python::numpy::ndarray nc2Boost(const NdArray<dtype>& inArray)
102 {
103 const Shape inShape = inArray.shape();
104 boost::python::tuple shape = boost::python::make_tuple(inShape.rows, inShape.cols);
105 BoostNdarrayHelper<dtype> newNdArrayHelper(shape);
106
107 for (uint32 row = 0; row < inShape.rows; ++row)
108 {
109 for (uint32 col = 0; col < inShape.cols; ++col)
110 {
111 newNdArrayHelper(row, col) = inArray(row, col);
112 }
113 }
114 return newNdArrayHelper.getArray();
115 }
116
117 //============================================================================
124 template<typename T>
125 inline std::vector<T> list2vector(const boost::python::list& inList)
126 {
127 return std::vector<T>(boost::python::stl_input_iterator<T>(inList), boost::python::stl_input_iterator<T>());
128 }
129
130 //============================================================================
137 template<typename T>
138 inline boost::python::list vector2list(std::vector<T>& inVector)
139 {
140 boost::python::list outList;
141 for (auto& value : inVector)
142 {
143 outList.append(value);
144 }
145
146 return outList;
147 }
148
149 //============================================================================
156 template<class Key, class Value>
157 inline boost::python::dict map2dict(const std::map<Key, Value>& inMap)
158 {
159 boost::python::dict dictionary;
160 for (auto& keyValue : inMap)
161 {
162 dictionary[keyValue.first] = keyValue.second;
163 }
164 return dictionary;
165 }
166 } // namespace boostPythonInterface
167} // namespace nc
168
169#endif // #if defined(NUMCPP_INCLUDE_BOOST_PYTHON_INTERFACE) && !defined(NUMCPP_NO_USE_BOOST)
#define THROW_RUNTIME_ERROR(msg)
Definition: Error.hpp:40
Definition: Cartesian.hpp:40
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition: Functions/Shape.hpp:42
std::uint32_t uint32
Definition: Types.hpp:40