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