NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
hypot.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <cmath>
31
34#include "NumCpp/NdArray.hpp"
35#include "NumCpp/Utils/sqr.hpp"
36
37namespace nc
38{
39 //============================================================================
40 // Method Description:
53 template<typename dtype>
54 double hypot(dtype inValue1, dtype inValue2) noexcept
55 {
57
58 return std::hypot(static_cast<double>(inValue1), static_cast<double>(inValue2));
59 }
60
61 //============================================================================
62 // Method Description:
76 template<typename dtype>
77 double hypot(dtype inValue1, dtype inValue2, dtype inValue3) noexcept
78 {
80
81#ifdef __cpp_lib_hypot
82 return std::hypot(static_cast<double>(inValue1), static_cast<double>(inValue2), static_cast<double>(inValue3));
83#else
84 return std::sqrt(utils::sqr(static_cast<double>(inValue1)) + utils::sqr(static_cast<double>(inValue2)) +
85 utils::sqr(static_cast<double>(inValue3)));
86#endif
87 }
88
89 //============================================================================
90 // Method Description:
103 template<typename dtype>
104 NdArray<double> hypot(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2)
105 {
106 return broadcast::broadcaster<double>(inArray1,
107 inArray2,
108 [](dtype inValue1, dtype inValue2) noexcept -> double
109 { return hypot(inValue1, inValue2); });
110 }
111
112 //============================================================================
113 // Method Description:
127 template<typename dtype>
128 NdArray<double>
129 hypot(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2, const NdArray<dtype>& inArray3)
130 {
131 if (inArray1.size() != inArray2.size() || inArray1.size() != inArray3.size())
132 {
133 THROW_INVALID_ARGUMENT_ERROR("input array sizes are not consistant.");
134 }
135
136 NdArray<double> returnArray(inArray1.shape());
137 for (typename NdArray<dtype>::size_type i = 0; i < inArray1.size(); ++i)
138 {
139 returnArray[i] = hypot(inArray1[i], inArray2[i], inArray3[i]);
140 }
141
142 return returnArray;
143 }
144} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
uint32 size_type
Definition NdArrayCore.hpp:152
NdArray< dtypeIn1 > & broadcaster(NdArray< dtypeIn1 > &inArray1, const NdArray< dtypeIn2 > &inArray2, const Function &function, const AdditionalFunctionArgs &&... additionalFunctionArgs)
Definition NdArrayBroadcast.hpp:51
constexpr dtype sqr(dtype inValue) noexcept
Definition sqr.hpp:42
Definition Cartesian.hpp:39
double hypot(dtype inValue1, dtype inValue2) noexcept
Definition hypot.hpp:54