NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
divmod.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <cmath>
31#include <type_traits>
32
34#include "NumCpp/NdArray.hpp"
35
36namespace nc
37{
38 //===========================================================================
39 // Method Description:
49 template<typename dtype>
50 std::pair<NdArray<dtype>, NdArray<dtype>> divmod(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2)
51 {
53
54 if (inArray1.size() != inArray2.size())
55 {
56 THROW_INVALID_ARGUMENT_ERROR("Arrays must have the same size.");
57 }
58
59 auto div = NdArray<dtype>(inArray1.shape());
60 auto mod = NdArray<dtype>(inArray1.shape());
61
62 for (auto i = 0u; i < inArray1.size(); ++i)
63 {
64 if constexpr (std::is_floating_point_v<dtype>)
65 {
66 div[i] = std::floor(inArray1[i] / inArray2[i]);
67 mod[i] = std::fmod(inArray1[i], inArray2[i]);
68 }
69 else
70 {
71 div[i] = inArray1[i] / inArray2[i];
72 mod[i] = inArray1[i] % inArray2[i];
73 }
74 }
75
76 return std::make_pair(div, mod);
77 }
78} // 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
Definition Cartesian.hpp:39
std::pair< NdArray< dtype >, NdArray< dtype > > divmod(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition divmod.hpp:50
NdArray< dtype > mod(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition mod.hpp:46