NumCpp  2.11.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
tri.hpp
Go to the documentation of this file.
1 #pragma once
29 
31 #include "NumCpp/Core/Shape.hpp"
32 #include "NumCpp/Core/Types.hpp"
33 #include "NumCpp/NdArray.hpp"
34 
35 namespace nc
36 {
37  //============================================================================
38  // Method Description:
49  template<typename dtype>
50  NdArray<dtype> tril(uint32 inN, int32 inOffset = 0)
51  {
53 
54  uint32 rowStart = 0;
55  uint32 colStart = 0;
56  if (inOffset > 0)
57  {
58  colStart = inOffset;
59  }
60  else
61  {
62  rowStart = inOffset * -1;
63  }
64 
65  NdArray<dtype> returnArray(inN);
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>
97  NdArray<dtype> tril(uint32 inN, uint32 inM, int32 inOffset = 0)
98  {
100 
101  uint32 rowStart = 0;
102  uint32 colStart = 0;
103  if (inOffset > 0)
104  {
105  colStart = inOffset;
106  }
107  else if (inOffset < 0)
108  {
109  rowStart = inOffset * -1;
110  }
111 
112  NdArray<dtype> returnArray(inN, inM);
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>
132  NdArray<dtype> triu(uint32 inN, uint32 inM, int32 inOffset = 0);
133 
134  //============================================================================
135  // Method Description:
150  template<typename dtype>
151  NdArray<dtype> tril(const NdArray<dtype>& inArray, int32 inOffset = 0)
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>
175  NdArray<dtype> triu(uint32 inN, uint32 inM, int32 inOffset)
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  {
186  colStart = inOffset;
187  }
188  else if (inOffset < 0)
189  {
190  rowStart = inOffset * -1;
191  }
192 
193  NdArray<dtype> returnArray(inN, inM);
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>
224  NdArray<dtype> triu(uint32 inN, int32 inOffset = 0)
225  {
227 
228  return tril<dtype>(inN, -inOffset).transpose();
229  }
230 
231  //============================================================================
232  // Method Description:
247  template<typename dtype>
248  NdArray<dtype> triu(const NdArray<dtype>& inArray, int32 inOffset = 0)
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:138
self_type & zeros() noexcept
Definition: NdArrayCore.hpp:4855
self_type & ones() noexcept
Definition: NdArrayCore.hpp:3440
self_type copy() const
Definition: NdArrayCore.hpp:2439
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4464
self_type & putMask(const NdArray< bool > &inMask, const value_type &inValue)
Definition: NdArrayCore.hpp:4088
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
Definition: Cartesian.hpp:40
NdArray< dtype > triu(uint32 inN, uint32 inM, int32 inOffset=0)
Definition: tri.hpp:175
std::int32_t int32
Definition: Types.hpp:36
NdArray< dtype > tril(uint32 inN, int32 inOffset=0)
Definition: tri.hpp:50
std::uint32_t uint32
Definition: Types.hpp:40