NumCpp  2.16.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
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
37
38namespace nc
39{
40 //===========================================================================
41 // Method Description:
51 template<typename dtype>
52 std::pair<NdArray<dtype>, NdArray<dtype>> divmod(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2)
53 {
55
56 if (inArray1.size() != inArray2.size())
57 {
58 THROW_INVALID_ARGUMENT_ERROR("Arrays must have the same size.");
59 }
60
61 auto div = NdArray<dtype>(inArray1.shape());
62 auto mod = NdArray<dtype>(inArray1.shape());
63
64 for (auto i = 0u; i < inArray1.size(); ++i)
65 {
66 if constexpr (std::is_floating_point_v<dtype>)
67 {
68 div[i] = std::floor(inArray1[i] / inArray2[i]);
69 mod[i] = std::fmod(inArray1[i], inArray2[i]);
70 }
71 else
72 {
73 div[i] = inArray1[i] / inArray2[i];
74 mod[i] = inArray1[i] % inArray2[i];
75 }
76 }
77
78 return std::make_pair(div, mod);
79 }
80} // 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:139
Definition Cartesian.hpp:40
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
std::pair< NdArray< dtype >, NdArray< dtype > > divmod(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition divmod.hpp:52
NdArray< dtype > mod(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition mod.hpp:46