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