NumCpp  2.14.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tri.hpp
Go to the documentation of this file.
1
28#pragma once
29
31#include "NumCpp/Core/Shape.hpp"
32#include "NumCpp/Core/Types.hpp"
33#include "NumCpp/NdArray.hpp"
34
35namespace nc
36{
37 //============================================================================
38 // Method Description:
49 template<typename dtype>
51 {
53
54 uint32 rowStart = 0;
55 uint32 colStart = 0;
56 if (inOffset > 0)
57 {
59 }
60 else
61 {
62 rowStart = inOffset * -1;
63 }
64
66 returnArray.zeros();
67 for (uint32 row = rowStart; row < inN; ++row)
68 {
69 for (uint32 col = 0; col < row + colStart + 1 - rowStart; ++col)
70 {
71 if (col == inN)
72 {
73 break;
74 }
75
76 returnArray(row, col) = dtype{ 1 };
77 }
78 }
79
80 return returnArray;
81 }
82
83 //============================================================================
84 // Method Description:
96 template<typename dtype>
98 {
100
101 uint32 rowStart = 0;
102 uint32 colStart = 0;
103 if (inOffset > 0)
104 {
106 }
107 else if (inOffset < 0)
108 {
109 rowStart = inOffset * -1;
110 }
111
113 returnArray.zeros();
114 for (uint32 row = rowStart; row < inN; ++row)
115 {
116 for (uint32 col = 0; col < row + colStart + 1 - rowStart; ++col)
117 {
118 if (col == inM)
119 {
120 break;
121 }
122
123 returnArray(row, col) = dtype{ 1 };
124 }
125 }
126
127 return returnArray;
128 }
129
130 // forward declare
131 template<typename dtype>
133
134 //============================================================================
135 // Method Description:
150 template<typename dtype>
152 {
154
155 const Shape inShape = inArray.shape();
156 auto outArray = inArray.copy();
157 outArray.putMask(triu<bool>(inShape.rows, inShape.cols, inOffset + 1), 0);
158 return outArray;
159 }
160
161 //============================================================================
162 // Method Description:
174 template<typename dtype>
176 {
178
179 // because i'm stealing the lines of code from tril and reversing it, this is necessary
180 inOffset -= 1;
181
182 uint32 rowStart = 0;
183 uint32 colStart = 0;
184 if (inOffset > 0)
185 {
187 }
188 else if (inOffset < 0)
189 {
190 rowStart = inOffset * -1;
191 }
192
194 returnArray.ones();
195 for (uint32 row = rowStart; row < inN; ++row)
196 {
197 for (uint32 col = 0; col < row + colStart + 1 - rowStart; ++col)
198 {
199 if (col == inM)
200 {
201 break;
202 }
203
204 returnArray(row, col) = dtype{ 0 };
205 }
206 }
207
208 return returnArray;
209 }
210
211 //============================================================================
212 // Method Description:
223 template<typename dtype>
230
231 //============================================================================
232 // Method Description:
247 template<typename dtype>
249 {
251
252 const Shape inShape = inArray.shape();
253 auto outArray = inArray.copy();
254 outArray.putMask(tril<bool>(inShape.rows, inShape.cols, inOffset - 1), 0);
255 return outArray;
256 }
257} // namespace nc
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition StaticAsserts.hpp:56
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:139
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
Definition Cartesian.hpp:40
NdArray< dtype > triu(uint32 inN, uint32 inM, int32 inOffset=0)
Definition tri.hpp:175
NdArray< dtype > tril(uint32 inN, int32 inOffset=0)
Definition tri.hpp:50
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
std::int32_t int32
Definition Types.hpp:36
std::uint32_t uint32
Definition Types.hpp:40