NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
linspace.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
32 #include "NumCpp/NdArray.hpp"
33 
34 
35 #include <string>
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
61  template<typename dtype>
62  NdArray<dtype> linspace(dtype inStart, dtype inStop, uint32 inNum = 50, bool endPoint = true)
63  {
65 
66  if (inNum == 0)
67  {
68  return NdArray<dtype>(0);
69  }
70 
71  if (inNum == 1)
72  {
73  NdArray<dtype> returnArray = { inStart };
74  return returnArray;
75  }
76 
77  if (inStop <= inStart)
78  {
79  THROW_INVALID_ARGUMENT_ERROR("stop value must be greater than the start value.");
80  }
81 
82  if (endPoint)
83  {
84  if (inNum == 2)
85  {
86  NdArray<dtype> returnArray = { inStart, inStop };
87  return returnArray;
88  }
89 
90  NdArray<dtype> returnArray(1, inNum);
91  returnArray.front() = inStart;
92  returnArray.back() = inStop;
93 
94  dtype step = (inStop - inStart) / static_cast<dtype>(inNum - 1);
95 
96  for (uint32 i = 1; i < inNum - 1; ++i)
97  {
98  returnArray[i] = inStart + static_cast<dtype>(i) * step;
99  }
100 
101  return returnArray;
102  }
103 
104  if (inNum == 2)
105  {
106  dtype step = (inStop - inStart) / (inNum);
107  NdArray<dtype> returnArray = { inStart, inStart + step };
108  return returnArray;
109  }
110 
111  NdArray<dtype> returnArray(1, inNum);
112  returnArray.front() = inStart;
113 
114  dtype step = (inStop - inStart) / static_cast<dtype>(inNum);
115 
116  for (uint32 i = 1; i < inNum; ++i)
117  {
118  returnArray[i] = inStart + static_cast<dtype>(i) * step;
119  }
120 
121  return returnArray;
122  }
123 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
const_reference back() const noexcept
Definition: NdArrayCore.hpp:2344
const_reference front() const noexcept
Definition: NdArrayCore.hpp:2933
Definition: Coordinate.hpp:45
NdArray< dtype > linspace(dtype inStart, dtype inStop, uint32 inNum=50, bool endPoint=true)
Definition: linspace.hpp:62
std::uint32_t uint32
Definition: Types.hpp:40