NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
arange.hpp
Go to the documentation of this file.
1 #pragma once
29 
32 #include "NumCpp/NdArray.hpp"
33 
34 #include <string>
35 #include <vector>
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
59  template<typename dtype>
60  NdArray<dtype> arange(dtype inStart, dtype inStop, dtype inStep = 1)
61  {
63 
64  if (inStep > 0 && inStop < inStart)
65  {
66  THROW_INVALID_ARGUMENT_ERROR("stop value must be larger than the start value for positive step.");
67  }
68 
69  if (inStep < 0 && inStop > inStart)
70  {
71  THROW_INVALID_ARGUMENT_ERROR("start value must be larger than the stop value for negative step.");
72  }
73 
74  std::vector<dtype> values;
75 
76  dtype theValue = inStart;
77  auto counter = dtype{ 1 };
78 
79  if (inStep > 0)
80  {
81  while (theValue < inStop)
82  {
83  values.push_back(theValue);
84  theValue = inStart + inStep * counter++;
85  }
86  }
87  else
88  {
89  while (theValue > inStop)
90  {
91  values.push_back(theValue);
92  theValue = inStart + inStep * counter++;
93  }
94  }
95 
96  return NdArray<dtype>(values);
97  }
98 
99  //============================================================================
100  // Method Description:
118  template<typename dtype>
119  NdArray<dtype> arange(dtype inStop)
120  {
121  if (inStop <= 0)
122  {
123  THROW_INVALID_ARGUMENT_ERROR("stop value must ge greater than 0.");
124  }
125 
126  return arange<dtype>(0, inStop, 1);
127  }
128 
129  //============================================================================
130  // Method Description:
148  template<typename dtype>
149  NdArray<dtype> arange(const Slice& inSlice)
150  {
151  return arange<dtype>(inSlice.start, inSlice.stop, inSlice.step);
152  }
153 } // namespace nc
StaticAsserts.hpp
nc::Slice::stop
int32 stop
Definition: Slice.hpp:48
Error.hpp
STATIC_ASSERT_ARITHMETIC
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
nc::NdArray< dtype >
NdArray.hpp
nc::Slice::start
int32 start
Definition: Slice.hpp:47
nc::Slice::step
int32 step
Definition: Slice.hpp:49
nc
Definition: Coordinate.hpp:44
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
nc::arange
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition: arange.hpp:60
nc::Slice
A Class for slicing into NdArrays.
Definition: Slice.hpp:43