NumCpp  2.1.0
A C++ implementation of the Python Numpy library
Functions/power.hpp
Go to the documentation of this file.
1 #pragma once
30 
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/Core/Types.hpp"
35 #include "NumCpp/NdArray.hpp"
36 #include "NumCpp/Utils/power.hpp"
37 
38 #include <string>
39 
40 namespace nc
41 {
42  //============================================================================
43  // Method Description:
53  template<typename dtype>
54  constexpr dtype power(dtype inValue, uint8 inExponent) noexcept
55  {
56  return utils::power(inValue, inExponent);
57  }
58 
59  //============================================================================
60  // Method Description:
70  template<typename dtype>
71  NdArray<dtype> power(const NdArray<dtype>& inArray, uint8 inExponent)
72  {
73  NdArray<dtype> returnArray(inArray.shape());
74  stl_algorithms::transform(inArray.cbegin(), inArray.cend(), returnArray.begin(),
75  [inExponent](dtype inValue) noexcept -> dtype
76  {
77  return nc::power(inValue, inExponent);
78  });
79 
80  return returnArray;
81  }
82 
83  //============================================================================
84  // Method Description:
94  template<typename dtype>
95  NdArray<dtype> power(const NdArray<dtype>& inArray, const NdArray<uint8>& inExponents)
96  {
97  if (inArray.shape() != inExponents.shape())
98  {
99  THROW_INVALID_ARGUMENT_ERROR("input array shapes are not consistant.");
100  }
101 
102  NdArray<dtype> returnArray(inArray.shape());
103  stl_algorithms::transform(inArray.cbegin(), inArray.cend(), inExponents.cbegin(), returnArray.begin(),
104  [](dtype inValue, uint8 inExponent) -> dtype
105  {
106  return nc::power(inValue, inExponent);
107  });
108 
109  return returnArray;
110  }
111 } // namespace nc
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4312
Error.hpp
nc::uint8
std::uint8_t uint8
Definition: Types.hpp:43
nc::NdArray< dtype >
nc::stl_algorithms::transform
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:703
NdArray.hpp
nc::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1491
power.hpp
Shape.hpp
nc
Definition: Coordinate.hpp:45
nc::power
constexpr dtype power(dtype inValue, uint8 inExponent) noexcept
Definition: Functions/power.hpp:54
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
nc::utils::power
dtype power(dtype inValue, uint8 inPower) noexcept
Definition: Utils/power.hpp:49
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1147
StlAlgorithms.hpp
Types.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1091