NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
hypot.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <cmath>
31 #include <string>
32 
36 #include "NumCpp/NdArray.hpp"
37 #include "NumCpp/Utils/sqr.hpp"
38 
39 namespace nc
40 {
41  //============================================================================
42  // Method Description:
55  template<typename dtype>
56  double hypot(dtype inValue1, dtype inValue2) noexcept
57  {
59 
60  return std::hypot(static_cast<double>(inValue1), static_cast<double>(inValue2));
61  }
62 
63  //============================================================================
64  // Method Description:
78  template<typename dtype>
79  double hypot(dtype inValue1, dtype inValue2, dtype inValue3) noexcept
80  {
82 
83 #ifdef __cpp_lib_hypot
84  return std::hypot(static_cast<double>(inValue1), static_cast<double>(inValue2), static_cast<double>(inValue3));
85 #else
86  return std::sqrt(utils::sqr(static_cast<double>(inValue1)) + utils::sqr(static_cast<double>(inValue2)) +
87  utils::sqr(static_cast<double>(inValue3)));
88 #endif
89  }
90 
91  //============================================================================
92  // Method Description:
105  template<typename dtype>
106  NdArray<double> hypot(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2)
107  {
108  if (inArray1.shape() != inArray2.shape())
109  {
110  THROW_INVALID_ARGUMENT_ERROR("input array shapes are not consistant.");
111  }
112 
113  NdArray<double> returnArray(inArray1.shape());
114 
116  inArray1.cend(),
117  inArray2.cbegin(),
118  returnArray.begin(),
119  [](dtype inValue1, dtype inValue2) noexcept -> double
120  { return hypot(inValue1, inValue2); });
121 
122  return returnArray;
123  }
124 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1221
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4276
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1529
iterator begin() noexcept
Definition: NdArrayCore.hpp:1171
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:784
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:44
Definition: Coordinate.hpp:45
double hypot(dtype inValue1, dtype inValue2) noexcept
Definition: hypot.hpp:56
NdArray< double > hypot(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition: hypot.hpp:106
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48