NumCpp  2.12.1
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
30#include <cmath>
31#include <string>
32
36#include "NumCpp/NdArray.hpp"
37#include "NumCpp/Utils/sqr.hpp"
38
39namespace 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 return broadcast::broadcaster<double>(inArray1,
109 inArray2,
110 [](dtype inValue1, dtype inValue2) noexcept -> double
111 { return hypot(inValue1, inValue2); });
112 }
113
114 //============================================================================
115 // Method Description:
129 template<typename dtype>
130 NdArray<double>
131 hypot(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2, const NdArray<dtype>& inArray3)
132 {
133 if (inArray1.size() != inArray2.size() || inArray1.size() != inArray3.size())
134 {
135 THROW_INVALID_ARGUMENT_ERROR("input array sizes are not consistant.");
136 }
137
138 NdArray<double> returnArray(inArray1.shape());
139 for (typename NdArray<dtype>::size_type i = 0; i < inArray1.size(); ++i)
140 {
141 returnArray[i] = hypot(inArray1[i], inArray2[i], inArray3[i]);
142 }
143
144 return returnArray;
145 }
146} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
size_type size() const noexcept
Definition: NdArrayCore.hpp:4524
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
uint32 size_type
Definition: NdArrayCore.hpp:156
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:42
Definition: Cartesian.hpp:40
NdArray< double > hypot(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2, const NdArray< dtype > &inArray3)
Definition: hypot.hpp:131
double hypot(dtype inValue1, dtype inValue2) noexcept
Definition: hypot.hpp:56
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48