NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
arange.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <vector>
31
34#include "NumCpp/NdArray.hpp"
35
36namespace nc
37{
38 //============================================================================
39 // Method Description:
57 template<typename dtype>
58 NdArray<dtype> arange(dtype inStart, dtype inStop, dtype inStep = 1)
59 {
61
62 if (inStep > 0 && inStop < inStart)
63 {
64 THROW_INVALID_ARGUMENT_ERROR("stop value must be larger than the start value for positive step.");
65 }
66
67 if (inStep < 0 && inStop > inStart)
68 {
69 THROW_INVALID_ARGUMENT_ERROR("start value must be larger than the stop value for negative step.");
70 }
71
72 std::vector<dtype> values;
73
74 dtype theValue = inStart;
75 auto counter = dtype{ 1 };
76
77 if (inStep > 0)
78 {
79 while (theValue < inStop)
80 {
81 values.push_back(theValue);
82 theValue = inStart + inStep * counter++;
83 }
84 }
85 else
86 {
87 while (theValue > inStop)
88 {
89 values.push_back(theValue);
90 theValue = inStart + inStep * counter++;
91 }
92 }
93
94 return NdArray<dtype>(values);
95 }
96
97 //============================================================================
98 // Method Description:
114 template<typename dtype>
115 NdArray<dtype> arange(dtype inStop)
116 {
117 if (inStop <= 0)
118 {
119 THROW_INVALID_ARGUMENT_ERROR("stop value must ge greater than 0.");
120 }
121
122 return arange<dtype>(0, inStop, 1);
123 }
124
125 //============================================================================
126 // Method Description:
142 template<typename dtype>
144 {
145 return arange<dtype>(inSlice.start, inSlice.stop, inSlice.step);
146 }
147} // 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
A Class for slicing into NdArrays.
Definition Slice.hpp:45
int32 step
Definition Slice.hpp:50
int32 start
Definition Slice.hpp:48
int32 stop
Definition Slice.hpp:49
Definition Cartesian.hpp:39
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:58