NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
linspace.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <string>
31
32#include "NumCpp/Core/Enums.hpp"
35#include "NumCpp/NdArray.hpp"
36
37namespace nc
38{
39 //============================================================================
40 // Method Description:
60 template<typename dtype>
61 NdArray<dtype> linspace(dtype inStart, dtype inStop, uint32 inNum = 50, EndPoint endPoint = EndPoint::YES)
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 == EndPoint::YES)
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: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:139
const_reference back() const noexcept
Definition: NdArrayCore.hpp:2287
const_reference front() const noexcept
Definition: NdArrayCore.hpp:2860
Definition: Cartesian.hpp:40
NdArray< dtype > linspace(dtype inStart, dtype inStop, uint32 inNum=50, EndPoint endPoint=EndPoint::YES)
Definition: linspace.hpp:61
std::uint32_t uint32
Definition: Types.hpp:40
EndPoint
End Point boolean.
Definition: Enums.hpp:74