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