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