NumCpp  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  else 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  else
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  else
106  {
107  if (inNum == 2)
108  {
109  dtype step = (inStop - inStart) / (inNum);
110  NdArray<dtype> returnArray = { inStart, inStart + step };
111  return returnArray;
112  }
113  else
114  {
115  NdArray<dtype> returnArray(1, inNum);
116  returnArray.front() = inStart;
117 
118  dtype step = (inStop - inStart) / static_cast<dtype>(inNum);
119 
120  for (uint32 i = 1; i < inNum; ++i)
121  {
122  returnArray[i] = inStart + static_cast<dtype>(i) * step;
123  }
124 
125  return returnArray;
126  }
127  }
128  }
129 }
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:2782
nc::NdArray::back
value_type back() const noexcept
Definition: NdArrayCore.hpp:2216
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