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