NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
hypot.hpp
Go to the documentation of this file.
1 
28 #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:
56  template<typename dtype>
57  double hypot(dtype inValue1, dtype inValue2) noexcept
58  {
60 
61  return std::hypot(static_cast<double>(inValue1), static_cast<double>(inValue2));
62  }
63 
64  //============================================================================
65  // Method Description:
80  template<typename dtype>
81  double hypot(dtype inValue1, dtype inValue2, dtype inValue3) noexcept
82  {
84 
85 #ifdef __cpp_lib_hypot
86  return std::hypot(static_cast<double>(inValue1),
87  static_cast<double>(inValue2),
88  static_cast<double>(inValue3));
89 #else
90  return std::sqrt(utils::sqr(static_cast<double>(inValue1)) +
91  utils::sqr(static_cast<double>(inValue2)) +
92  utils::sqr(static_cast<double>(inValue3)));
93 #endif
94  }
95 
96  //============================================================================
97  // Method Description:
111  template<typename dtype>
112  NdArray<double> hypot(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2)
113  {
114  if (inArray1.shape() != inArray2.shape())
115  {
116  THROW_INVALID_ARGUMENT_ERROR("input array shapes are not consistant.");
117  }
118 
119  NdArray<double> returnArray(inArray1.shape());
120 
121  stl_algorithms::transform(inArray1.cbegin(), inArray1.cend(), inArray2.cbegin(), returnArray.begin(),
122  [](dtype inValue1, dtype inValue2) noexcept -> double
123  {
124  return hypot(inValue1, inValue2);
125  });
126 
127  return returnArray;
128  }
129 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1270
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1614
iterator begin() noexcept
Definition: NdArrayCore.hpp:1214
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:57
NdArray< double > hypot(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition: hypot.hpp:112
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:50