WaveSimPP 1.0
A C library for solving the wave equation and reconstructing the wave field
All Classes Namespaces Functions Modules Pages
Typedefs | Enumerations | Functions
np Namespace Reference

Custom implementation of numpy in C++. More...

Typedefs

typedef double ndArrayValue
 

Enumerations

enum  indexing { xy , ij }
 

Functions

template<std::size_t ND>
boost::multi_array< ndArrayValue, ND >::index getIndex (const boost::multi_array< ndArrayValue, ND > &m, const ndArrayValue *requestedElement, const unsigned short int direction)
 Gets the index of one element in a multi_array in one axis.
 
template<std::size_t ND>
boost::array< typename boost::multi_array< ndArrayValue, ND >::index, ND > getIndexArray (const boost::multi_array< ndArrayValue, ND > &m, const ndArrayValue *requestedElement)
 Gets the index of one element in a multi_array.
 
template<typename Array , typename Element , typename Functor >
void for_each (const boost::type< Element > &type_dispatch, Array A, Functor &xform)
 
template<typename Element , typename Functor >
void for_each (const boost::type< Element > &, Element &Val, Functor &xform)
 Function to apply a function to all elements of a multi_array.
 
template<typename Element , typename Iterator , typename Functor >
void for_each (const boost::type< Element > &type_dispatch, Iterator begin, Iterator end, Functor &xform)
 Function to apply a function to all elements of a multi_array.
 
template<typename Array , typename Functor >
void for_each (Array &A, Functor xform)
 
template<typename T , long unsigned int ND>
requires std::is_floating_point<T>
::value constexpr std::vector< boost::multi_array< T, ND > > gradient (boost::multi_array< T, ND > inArray, std::initializer_list< T > args)
 
boost::multi_array< double, 1 > linspace (double start, double stop, long unsigned int num)
 Implements the numpy linspace function.
 
template<typename T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr std::vector< boost::multi_array< T, ND > > meshgrid (const boost::multi_array< T, 1 >(&cinput)[ND], bool sparsing=false, indexing indexing_type=xy)
 
template<class T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND > element_wise_apply (const boost::multi_array< T, ND > &input_array, std::function< T(T)> func)
 Creates a new array and fills it with the values of the result of the function called on the input array element-wise.
 
template<class T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND > sqrt (const boost::multi_array< T, ND > &input_array)
 Implements the numpy sqrt function on multi arrays.
 
template<class T >
requires std::is_arithmetic<T>
::value constexpr T sqrt (const T input)
 Implements the numpy sqrt function on scalars.
 
template<class T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND > exp (const boost::multi_array< T, ND > &input_array)
 Implements the numpy exp function on multi arrays.
 
template<class T >
requires std::is_arithmetic<T>
::value constexpr T exp (const T input)
 Implements the numpy exp function on scalars.
 
template<class T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND > log (const boost::multi_array< T, ND > &input_array)
 Implements the numpy log function on multi arrays.
 
template<class T >
requires std::is_arithmetic<T>
::value constexpr T log (const T input)
 Implements the numpy log function on scalars.
 
template<class T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND > pow (const boost::multi_array< T, ND > &input_array, const T exponent)
 Implements the numpy pow function on multi arrays.
 
template<class T >
requires std::is_arithmetic<T>
::value constexpr T pow (const T input, const T exponent)
 Implements the numpy pow function on scalars.
 
template<class T , long unsigned int ND>
constexpr boost::multi_array< T, ND > element_wise_duo_apply (boost::multi_array< T, ND > const &lhs, boost::multi_array< T, ND > const &rhs, std::function< T(T, T)> func)
 
template<typename T , typename inT , long unsigned int ND>
requires std::is_integral<inT>
::value &&std::is_arithmetic< T >::value constexpr boost::multi_array< T, ND > zeros (inT(&dimensions_input)[ND])
 Implements the numpy zeros function for an n-dimensionl multi array.
 
template<typename T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr T max (boost::multi_array< T, ND > const &input_array)
 Implements the numpy max function for an n-dimensionl multi array.
 
template<class T , class... Ts, class = std::enable_if_t<(std::is_same_v<T, Ts> && ...)>>
requires std::is_arithmetic<T>
::value constexpr T max (T input1, Ts... inputs)
 Implements the numpy max function for an variadic number of arguments.
 
template<typename T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr T min (boost::multi_array< T, ND > const &input_array)
 Implements the numpy min function for an n-dimensionl multi array.
 
template<class T , class... Ts, class = std::enable_if_t<(std::is_same_v<T, Ts> && ...)>>
requires std::is_arithmetic<T>
constexpr T min (T input1, Ts... inputs)
 Implements the numpy min function for an variadic number of arguments.
 
template<typename T >
requires std::is_arithmetic<T>
::value constexpr T abs (T input)
 Implements the numpy abs function for a scalar.
 
template<typename T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND - 1 > slice (boost::multi_array< T, ND > const &input_array, std::size_t slice_index)
 Slices the array through one dimension and returns a ND - 1 dimensional array.
 
matplot::vector_2d convert_to_matplot (const boost::multi_array< double, 2 > &arr)
 Convert a 2D boost::multi_array to a matplot::vector_2d.
 

Detailed Description

Custom implementation of numpy in C++.

Typedef Documentation

◆ ndArrayValue

typedef double np::ndArrayValue

Definition at line 22 of file np.hpp.

Enumeration Type Documentation

◆ indexing

enum np::indexing

Definition at line 172 of file np.hpp.

173 {
174 xy,
175 ij
176 };

Function Documentation

◆ abs()

template<typename T >
requires std::is_arithmetic<T>
::value constexpr T np::abs ( input)
inlineconstexpr

Implements the numpy abs function for a scalar.

Definition at line 463 of file np.hpp.

464 {
465 return std::abs(input);
466 }

◆ element_wise_apply()

template<class T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND > np::element_wise_apply ( const boost::multi_array< T, ND > &  input_array,
std::function< T(T)>  func 
)
inlineconstexpr

Creates a new array and fills it with the values of the result of the function called on the input array element-wise.

Definition at line 243 of file np.hpp.

244 {
245
246 // Create output array copying extents
247 using arrayIndex = boost::multi_array<double, ND>::index;
248 using ndIndexArray = boost::array<arrayIndex, ND>;
249 boost::detail::multi_array::extent_gen<ND> output_extents;
250 std::vector<size_t> shape_list;
251 for (std::size_t i = 0; i < ND; i++)
252 {
253 shape_list.push_back(input_array.shape()[i]);
254 }
255 std::copy(shape_list.begin(), shape_list.end(), output_extents.ranges_.begin());
256 boost::multi_array<T, ND> output_array(output_extents);
257
258 // Looping through the elements of the output array
259 const T *p = input_array.data();
260 ndIndexArray index;
261 for (std::size_t i = 0; i < input_array.num_elements(); i++)
262 {
263 index = getIndexArray(input_array, p);
264 output_array(index) = func(input_array(index));
265 ++p;
266 }
267 return output_array;
268 }

◆ element_wise_duo_apply()

template<class T , long unsigned int ND>
constexpr boost::multi_array< T, ND > np::element_wise_duo_apply ( boost::multi_array< T, ND > const &  lhs,
boost::multi_array< T, ND > const &  rhs,
std::function< T(T, T)>  func 
)
inlineconstexpr

Creates a new array in which the value at each index is the the result of the input function applied to an element of the left hand side array and one on the righ hand side array in the same index Outputs a copy of the result

Definition at line 337 of file np.hpp.

338 {
339 // Create output array copying extents
340 using arrayIndex = boost::multi_array<double, ND>::index;
341 using ndIndexArray = boost::array<arrayIndex, ND>;
342 boost::detail::multi_array::extent_gen<ND> output_extents;
343 std::vector<size_t> shape_list;
344 for (std::size_t i = 0; i < ND; i++)
345 {
346 shape_list.push_back(lhs.shape()[i]);
347 }
348 std::copy(shape_list.begin(), shape_list.end(), output_extents.ranges_.begin());
349 boost::multi_array<T, ND> output_array(output_extents);
350
351 // Looping through the elements of the output array
352 const T *p = lhs.data();
353 ndIndexArray index;
354 for (std::size_t i = 0; i < lhs.num_elements(); i++)
355 {
356 index = getIndexArray(lhs, p);
357 output_array(index) = func(lhs(index), rhs(index));
358 ++p;
359 }
360 return output_array;
361 }

◆ exp() [1/2]

template<class T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND > np::exp ( const boost::multi_array< T, ND > &  input_array)
inlineconstexpr

Implements the numpy exp function on multi arrays.

Definition at line 289 of file np.hpp.

290 {
291 std::function<T(T)> func = (T(*)(T))std::exp;
292 return element_wise_apply(input_array, func);
293 }
::value constexpr boost::multi_array< T, ND > element_wise_apply(const boost::multi_array< T, ND > &input_array, std::function< T(T)> func)
Creates a new array and fills it with the values of the result of the function called on the input ar...
Definition: np.hpp:243

◆ exp() [2/2]

template<class T >
requires std::is_arithmetic<T>
::value constexpr T np::exp ( const T  input)
inlineconstexpr

Implements the numpy exp function on scalars.

Definition at line 297 of file np.hpp.

298 {
299 return std::exp(input);
300 }

◆ for_each() [1/4]

template<typename Array , typename Functor >
void np::for_each ( Array &  A,
Functor  xform 
)
inline

Function to apply a function to all elements of a multi_array Simple overload

Definition at line 80 of file np.hpp.

81 {
82 // Dispatch to the proper function
83 for_each(boost::type<typename Array::element>(), A.begin(), A.end(), xform);
84 }
void for_each(const boost::type< Element > &type_dispatch, Array A, Functor &xform)
Definition: np.hpp:51

◆ for_each() [2/4]

template<typename Element , typename Functor >
void np::for_each ( const boost::type< Element > &  ,
Element &  Val,
Functor &  xform 
)
inline

Function to apply a function to all elements of a multi_array.

Definition at line 59 of file np.hpp.

60 {
61 Val = xform(Val);
62 }

◆ for_each() [3/4]

template<typename Array , typename Element , typename Functor >
void np::for_each ( const boost::type< Element > &  type_dispatch,
Array  A,
Functor &  xform 
)
inline

Function to apply a function to all elements of a multi_array Simple overload

Definition at line 51 of file np.hpp.

53 {
54 for_each(type_dispatch, A.begin(), A.end(), xform);
55 }

◆ for_each() [4/4]

template<typename Element , typename Iterator , typename Functor >
void np::for_each ( const boost::type< Element > &  type_dispatch,
Iterator  begin,
Iterator  end,
Functor &  xform 
)
inline

Function to apply a function to all elements of a multi_array.

Definition at line 66 of file np.hpp.

69 {
70 while (begin != end)
71 {
72 for_each(type_dispatch, *begin, xform);
73 ++begin;
74 }
75 }

◆ getIndex()

template<std::size_t ND>
boost::multi_array< ndArrayValue, ND >::index np::getIndex ( const boost::multi_array< ndArrayValue, ND > &  m,
const ndArrayValue *  requestedElement,
const unsigned short int  direction 
)
inline

Gets the index of one element in a multi_array in one axis.

Definition at line 27 of file np.hpp.

28 {
29 int offset = requestedElement - m.origin();
30 return (offset / m.strides()[direction] % m.shape()[direction] + m.index_bases()[direction]);
31 }

◆ getIndexArray()

template<std::size_t ND>
boost::array< typename boost::multi_array< ndArrayValue, ND >::index, ND > np::getIndexArray ( const boost::multi_array< ndArrayValue, ND > &  m,
const ndArrayValue *  requestedElement 
)
inline

Gets the index of one element in a multi_array.

Definition at line 36 of file np.hpp.

37 {
38 using indexType = boost::multi_array<ndArrayValue, ND>::index;
39 boost::array<indexType, ND> _index;
40 for (unsigned int dir = 0; dir < ND; dir++)
41 {
42 _index[dir] = getIndex(m, requestedElement, dir);
43 }
44
45 return _index;
46 }
boost::multi_array< ndArrayValue, ND >::index getIndex(const boost::multi_array< ndArrayValue, ND > &m, const ndArrayValue *requestedElement, const unsigned short int direction)
Gets the index of one element in a multi_array in one axis.
Definition: np.hpp:27

◆ gradient()

template<typename T , long unsigned int ND>
requires std::is_floating_point<T>
::value constexpr std::vector< boost::multi_array< T, ND > > np::gradient ( boost::multi_array< T, ND >  inArray,
std::initializer_list< T >  args 
)
inlineconstexpr

Takes the gradient of a n-dimensional multi_array Uses ij indexing Todo: Implement xy indexing

Definition at line 90 of file np.hpp.

91 {
92 // static_assert(args.size() == ND, "Number of arguments must match the number of dimensions of the array");
93 using arrayIndex = boost::multi_array<T, ND>::index;
94
95 using ndIndexArray = boost::array<arrayIndex, ND>;
96
97 // constexpr std::size_t n = sizeof...(Args);
98 std::size_t n = args.size();
99 // std::tuple<Args...> store(args...);
100 std::vector<T> arg_vector = args;
101 boost::multi_array<T, ND> my_array;
102 std::vector<boost::multi_array<T, ND>> output_arrays;
103 for (std::size_t i = 0; i < n; i++)
104 {
105 boost::multi_array<T, ND> dfdh = inArray;
106 output_arrays.push_back(dfdh);
107 }
108
109 ndArrayValue *p = inArray.data();
110 ndIndexArray index;
111 for (std::size_t i = 0; i < inArray.num_elements(); i++)
112 {
113 index = getIndexArray(inArray, p);
114 /*
115 std::cout << "Index: ";
116 for (std::size_t j = 0; j < n; j++)
117 {
118 std::cout << index[j] << " ";
119 }
120 std::cout << "\n";
121 */
122 // Calculating the gradient now
123 // j is the axis/dimension
124 for (std::size_t j = 0; j < n; j++)
125 {
126 ndIndexArray index_high = index;
127 T dh_high;
128 if ((long unsigned int)index_high[j] < inArray.shape()[j] - 1)
129 {
130 index_high[j] += 1;
131 dh_high = arg_vector[j];
132 }
133 else
134 {
135 dh_high = 0;
136 }
137 ndIndexArray index_low = index;
138 T dh_low;
139 if (index_low[j] > 0)
140 {
141 index_low[j] -= 1;
142 dh_low = arg_vector[j];
143 }
144 else
145 {
146 dh_low = 0;
147 }
148
149 T dh = dh_high + dh_low;
150 T gradient = (inArray(index_high) - inArray(index_low)) / dh;
151 // std::cout << gradient << "\n";
152 output_arrays[j](index) = gradient;
153 }
154 // std::cout << " value = " << inArray(index) << " check = " << *p << std::endl;
155 ++p;
156 }
157 return output_arrays;
158 }
::value constexpr std::vector< boost::multi_array< T, ND > > gradient(boost::multi_array< T, ND > inArray, std::initializer_list< T > args)
Definition: np.hpp:90

◆ linspace()

boost::multi_array< double, 1 > np::linspace ( double  start,
double  stop,
long unsigned int  num 
)
inline

Implements the numpy linspace function.

Definition at line 161 of file np.hpp.

162 {
163 double step = (stop - start) / (num - 1);
164 boost::multi_array<double, 1> output(boost::extents[num]);
165 for (std::size_t i = 0; i < num; i++)
166 {
167 output[i] = start + i * step;
168 }
169 return output;
170 }

◆ log() [1/2]

template<class T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND > np::log ( const boost::multi_array< T, ND > &  input_array)
inlineconstexpr

Implements the numpy log function on multi arrays.

Definition at line 304 of file np.hpp.

305 {
306 std::function<T(T)> func = std::log<T>();
307 return element_wise_apply(input_array, func);
308 }

◆ log() [2/2]

template<class T >
requires std::is_arithmetic<T>
::value constexpr T np::log ( const T  input)
inlineconstexpr

Implements the numpy log function on scalars.

Definition at line 312 of file np.hpp.

313 {
314 return std::log(input);
315 }

◆ max() [1/2]

template<typename T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr T np::max ( boost::multi_array< T, ND > const &  input_array)
inlineconstexpr

Implements the numpy max function for an n-dimensionl multi array.

Definition at line 384 of file np.hpp.

385 {
386 T max = 0;
387 bool max_not_set = true;
388 const T *data_pointer = input_array.data();
389 for (std::size_t i = 0; i < input_array.num_elements(); i++)
390 {
391 T element = *data_pointer;
392 if (max_not_set || element > max)
393 {
394 max = element;
395 max_not_set = false;
396 }
397 ++data_pointer;
398 }
399 return max;
400 }
::value constexpr T max(boost::multi_array< T, ND > const &input_array)
Implements the numpy max function for an n-dimensionl multi array.
Definition: np.hpp:384

◆ max() [2/2]

template<class T , class... Ts, class = std::enable_if_t<(std::is_same_v<T, Ts> && ...)>>
requires std::is_arithmetic<T>
::value constexpr T np::max ( input1,
Ts...  inputs 
)
inlineconstexpr

Implements the numpy max function for an variadic number of arguments.

Definition at line 404 of file np.hpp.

405 {
406 T max = input1;
407 for (T input : {inputs...})
408 {
409 if (input > max)
410 {
411 max = input;
412 }
413 }
414 return max;
415 }

◆ meshgrid()

template<typename T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr std::vector< boost::multi_array< T, ND > > np::meshgrid ( const boost::multi_array< T, 1 >(&)  cinput[ND],
bool  sparsing = false,
indexing  indexing_type = xy 
)
inlineconstexpr

Implementation of meshgrid TODO: Implement sparsing=true If the indexing type is xx, then reverse the order of the first two elements of ci if the number of dimensions is 2 or 3 In accordance with the numpy implementation

Definition at line 184 of file np.hpp.

185 {
186 using arrayIndex = boost::multi_array<T, ND>::index;
187 using oneDArrayIndex = boost::multi_array<T, 1>::index;
188 using ndIndexArray = boost::array<arrayIndex, ND>;
189 std::vector<boost::multi_array<T, ND>> output_arrays;
190 boost::multi_array<T, 1> ci[ND];
191 // Copy elements of cinput to ci, do the proper inversions
192 for (std::size_t i = 0; i < ND; i++)
193 {
194 std::size_t source = i;
195 if (indexing_type == xy && (ND == 3 || ND == 2))
196 {
197 if (i == 0)
198 source = 1;
199 else if (i == 1)
200 source = 0;
201 else
202 source = i;
203 }
204 ci[i] = boost::multi_array<T, 1>();
205 ci[i].resize(boost::extents[cinput[source].num_elements()]);
206 ci[i] = cinput[source];
207 }
208 // Deducing the extents of the N-Dimensional output
209 boost::detail::multi_array::extent_gen<ND> output_extents;
210 std::vector<size_t> shape_list;
211 for (std::size_t i = 0; i < ND; i++)
212 {
213 shape_list.push_back(ci[i].shape()[0]);
214 }
215 std::copy(shape_list.begin(), shape_list.end(), output_extents.ranges_.begin());
216
217 // Creating the output arrays
218 for (std::size_t i = 0; i < ND; i++)
219 {
220 boost::multi_array<T, ND> output_array(output_extents);
221 ndArrayValue *p = output_array.data();
222 ndIndexArray index;
223 // Looping through the elements of the output array
224 for (std::size_t j = 0; j < output_array.num_elements(); j++)
225 {
226 index = getIndexArray(output_array, p);
227 oneDArrayIndex index_1d;
228 index_1d = index[i];
229 output_array(index) = ci[i][index_1d];
230 ++p;
231 }
232 output_arrays.push_back(output_array);
233 }
234 if (indexing_type == xy && (ND == 3 || ND == 2))
235 {
236 std::swap(output_arrays[0], output_arrays[1]);
237 }
238 return output_arrays;
239 }
boost::array< typename boost::multi_array< ndArrayValue, ND >::index, ND > getIndexArray(const boost::multi_array< ndArrayValue, ND > &m, const ndArrayValue *requestedElement)
Gets the index of one element in a multi_array.
Definition: np.hpp:36

◆ min() [1/2]

template<typename T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr T np::min ( boost::multi_array< T, ND > const &  input_array)
inlineconstexpr

Implements the numpy min function for an n-dimensionl multi array.

Definition at line 419 of file np.hpp.

420 {
421 T min = 0;
422 bool min_not_set = true;
423 const T *data_pointer = input_array.data();
424 for (std::size_t i = 0; i < input_array.num_elements(); i++)
425 {
426 T element = *data_pointer;
427 if (min_not_set || element < min)
428 {
429 min = element;
430 min_not_set = false;
431 }
432 ++data_pointer;
433 }
434 return min;
435 }
::value constexpr T min(boost::multi_array< T, ND > const &input_array)
Implements the numpy min function for an n-dimensionl multi array.
Definition: np.hpp:419

◆ min() [2/2]

template<class T , class... Ts, class = std::enable_if_t<(std::is_same_v<T, Ts> && ...)>>
requires std::is_arithmetic<T>
constexpr T np::min ( input1,
Ts...  inputs 
)
inlineconstexpr

Implements the numpy min function for an variadic number of arguments.

Definition at line 439 of file np.hpp.

440 {
441 T min = input1;
442 for (T input : {inputs...})
443 {
444 if (input < min)
445 {
446 min = input;
447 }
448 }
449 return min;
450 }
451
453 template <typename T, long unsigned int ND>
454 requires std::is_arithmetic<T>::value inline constexpr boost::multi_array<T, ND> abs(boost::multi_array<T, ND> const &input_array)
455 {
456 std::function<T(T)> abs_func = [](T input)
457 { return std::abs(input); };
458 return element_wise_apply(input_array, abs_func);
459 }
::value constexpr T abs(T input)
Implements the numpy abs function for a scalar.
Definition: np.hpp:463

◆ pow() [1/2]

template<class T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND > np::pow ( const boost::multi_array< T, ND > &  input_array,
const T  exponent 
)
inlineconstexpr

Implements the numpy pow function on multi arrays.

Definition at line 319 of file np.hpp.

320 {
321 std::function<T(T)> pow_func = [exponent](T input)
322 { return std::pow(input, exponent); };
323 return element_wise_apply(input_array, pow_func);
324 }

◆ pow() [2/2]

template<class T >
requires std::is_arithmetic<T>
::value constexpr T np::pow ( const T  input,
const T  exponent 
)
inlineconstexpr

Implements the numpy pow function on scalars.

Definition at line 328 of file np.hpp.

329 {
330 return std::pow(input, exponent);
331 }

◆ slice()

template<typename T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND - 1 > np::slice ( boost::multi_array< T, ND > const &  input_array,
std::size_t  slice_index 
)
inlineconstexpr

Slices the array through one dimension and returns a ND - 1 dimensional array.

Definition at line 470 of file np.hpp.

471 {
472
473 // Deducing the extents of the N-Dimensional output
474 boost::detail::multi_array::extent_gen<ND - 1> output_extents;
475 std::vector<size_t> shape_list;
476 for (std::size_t i = 1; i < ND; i++)
477 {
478 shape_list.push_back(input_array.shape()[i]);
479 }
480 std::copy(shape_list.begin(), shape_list.end(), output_extents.ranges_.begin());
481
482 boost::multi_array<T, ND - 1> output_array(output_extents);
483
484 const T *p = input_array.data();
485 boost::array<std::size_t, ND> index;
486 for (std::size_t i = 0; i < input_array.num_elements(); i++)
487 {
488 index = getIndexArray(input_array, p);
489 output_array(index) = input_array[slice_index](index);
490 p++;
491 }
492 return output_array;
493 }

◆ sqrt() [1/2]

template<class T , long unsigned int ND>
requires std::is_arithmetic<T>
::value constexpr boost::multi_array< T, ND > np::sqrt ( const boost::multi_array< T, ND > &  input_array)
inlineconstexpr

Implements the numpy sqrt function on multi arrays.

Definition at line 274 of file np.hpp.

275 {
276 std::function<T(T)> func = (T(*)(T))std::sqrt;
277 return element_wise_apply(input_array, func);
278 }

◆ sqrt() [2/2]

template<class T >
requires std::is_arithmetic<T>
::value constexpr T np::sqrt ( const T  input)
inlineconstexpr

Implements the numpy sqrt function on scalars.

Definition at line 282 of file np.hpp.

283 {
284 return std::sqrt(input);
285 }

◆ zeros()

template<typename T , typename inT , long unsigned int ND>
requires std::is_integral<inT>
::value &&std::is_arithmetic< T >::value constexpr boost::multi_array< T, ND > np::zeros ( inT(&)  dimensions_input[ND])
inlineconstexpr

Implements the numpy zeros function for an n-dimensionl multi array.

Definition at line 365 of file np.hpp.

366 {
367 // Deducing the extents of the N-Dimensional output
368 boost::detail::multi_array::extent_gen<ND> output_extents;
369 std::vector<size_t> shape_list;
370 for (std::size_t i = 0; i < ND; i++)
371 {
372 shape_list.push_back(dimensions_input[i]);
373 }
374 std::copy(shape_list.begin(), shape_list.end(), output_extents.ranges_.begin());
375 // Applying a function to return zero always to all of its elements
376 boost::multi_array<T, ND> output_array(output_extents);
377 std::function<T(T)> zero_func = [](T input)
378 { return 0; };
379 return element_wise_apply(output_array, zero_func);
380 }