NumCpp  2.7.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 
33 #include "NumCpp/NdArray.hpp"
34 #include "NumCpp/Utils/sqr.hpp"
35 
36 #include <cmath>
37 #include <string>
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),
85  static_cast<double>(inValue2),
86  static_cast<double>(inValue3));
87 #else
88  return std::sqrt(utils::sqr(static_cast<double>(inValue1)) +
89  utils::sqr(static_cast<double>(inValue2)) +
90  utils::sqr(static_cast<double>(inValue3)));
91 #endif
92  }
93 
94  //============================================================================
95  // Method Description:
108  template<typename dtype>
109  NdArray<double> hypot(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2)
110  {
111  if (inArray1.shape() != inArray2.shape())
112  {
113  THROW_INVALID_ARGUMENT_ERROR("input array shapes are not consistant.");
114  }
115 
116  NdArray<double> returnArray(inArray1.shape());
117 
118  stl_algorithms::transform(inArray1.cbegin(), inArray1.cend(), inArray2.cbegin(), returnArray.begin(),
119  [](dtype inValue1, dtype inValue2) noexcept -> double
120  {
121  return hypot(inValue1, inValue2);
122  });
123 
124  return returnArray;
125  }
126 } // 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:1216
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1524
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:702
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:109
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48