NumCpp  2.11.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 
30 #include <string>
31 
34 #include "NumCpp/NdArray.hpp"
35 
36 namespace nc
37 {
38  //============================================================================
39  // Method Description:
59  template<typename dtype>
60  NdArray<dtype> linspace(dtype inStart, dtype inStop, uint32 inNum = 50, bool endPoint = true)
61  {
63 
64  if (inNum == 0)
65  {
66  return NdArray<dtype>(0);
67  }
68 
69  if (inNum == 1)
70  {
71  NdArray<dtype> returnArray = { inStart };
72  return returnArray;
73  }
74 
75  if (inStop <= inStart)
76  {
77  THROW_INVALID_ARGUMENT_ERROR("stop value must be greater than the start value.");
78  }
79 
80  if (endPoint)
81  {
82  if (inNum == 2)
83  {
84  NdArray<dtype> returnArray = { inStart, inStop };
85  return returnArray;
86  }
87 
88  NdArray<dtype> returnArray(1, inNum);
89  returnArray.front() = inStart;
90  returnArray.back() = inStop;
91 
92  dtype step = (inStop - inStart) / static_cast<dtype>(inNum - 1);
93 
94  for (uint32 i = 1; i < inNum - 1; ++i)
95  {
96  returnArray[i] = inStart + static_cast<dtype>(i) * step;
97  }
98 
99  return returnArray;
100  }
101 
102  if (inNum == 2)
103  {
104  dtype step = (inStop - inStart) / (inNum);
105  NdArray<dtype> returnArray = { inStart, inStart + step };
106  return returnArray;
107  }
108 
109  NdArray<dtype> returnArray(1, inNum);
110  returnArray.front() = inStart;
111 
112  dtype step = (inStop - inStart) / static_cast<dtype>(inNum);
113 
114  for (uint32 i = 1; i < inNum; ++i)
115  {
116  returnArray[i] = inStart + static_cast<dtype>(i) * step;
117  }
118 
119  return returnArray;
120  }
121 } // 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:138
const_reference back() const noexcept
Definition: NdArrayCore.hpp:2240
const_reference front() const noexcept
Definition: NdArrayCore.hpp:2813
Definition: Cartesian.hpp:40
NdArray< dtype > linspace(dtype inStart, dtype inStop, uint32 inNum=50, bool endPoint=true)
Definition: linspace.hpp:60
std::uint32_t uint32
Definition: Types.hpp:40