NumCpp  2.4.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:
50  template<typename dtype>
51  NdArray<dtype> tril(uint32 inN, int32 inOffset = 0)
52  {
54 
55  uint32 rowStart = 0;
56  uint32 colStart = 0;
57  if (inOffset > 0)
58  {
59  colStart = inOffset;
60  }
61  else
62  {
63  rowStart = inOffset * -1;
64  }
65 
66  NdArray<dtype> returnArray(inN);
67  returnArray.zeros();
68  for (uint32 row = rowStart; row < inN; ++row)
69  {
70  for (uint32 col = 0; col < row + colStart + 1 - rowStart; ++col)
71  {
72  if (col == inN)
73  {
74  break;
75  }
76 
77  returnArray(row, col) = dtype{ 1 };
78  }
79  }
80 
81  return returnArray;
82  }
83 
84  //============================================================================
85  // Method Description:
98  template<typename dtype>
99  NdArray<dtype> tril(uint32 inN, uint32 inM, int32 inOffset = 0)
100  {
102 
103  uint32 rowStart = 0;
104  uint32 colStart = 0;
105  if (inOffset > 0)
106  {
107  colStart = inOffset;
108  }
109  else if (inOffset < 0)
110  {
111  rowStart = inOffset * -1;
112  }
113 
114  NdArray<dtype> returnArray(inN, inM);
115  returnArray.zeros();
116  for (uint32 row = rowStart; row < inN; ++row)
117  {
118  for (uint32 col = 0; col < row + colStart + 1 - rowStart; ++col)
119  {
120  if (col == inM)
121  {
122  break;
123  }
124 
125  returnArray(row, col) = dtype{ 1 };
126  }
127  }
128 
129  return returnArray;
130  }
131 
132  // forward declare
133  template<typename dtype>
134  NdArray<dtype> triu(uint32 inN, uint32 inM, int32 inOffset = 0) ;
135 
136  //============================================================================
137  // Method Description:
153  template<typename dtype>
154  NdArray<dtype> tril(const NdArray<dtype>& inArray, int32 inOffset = 0)
155  {
157 
158  const Shape inShape = inArray.shape();
159  auto outArray = inArray.copy();
160  outArray.putMask(triu<bool>(inShape.rows, inShape.cols, inOffset + 1), 0);
161  return outArray;
162  }
163 
164  //============================================================================
165  // Method Description:
178  template<typename dtype>
179  NdArray<dtype> triu(uint32 inN, uint32 inM, int32 inOffset)
180  {
182 
183  // because i'm stealing the lines of code from tril and reversing it, this is necessary
184  inOffset -= 1;
185 
186  uint32 rowStart = 0;
187  uint32 colStart = 0;
188  if (inOffset > 0)
189  {
190  colStart = inOffset;
191  }
192  else if (inOffset < 0)
193  {
194  rowStart = inOffset * -1;
195  }
196 
197  NdArray<dtype> returnArray(inN, inM);
198  returnArray.ones();
199  for (uint32 row = rowStart; row < inN; ++row)
200  {
201  for (uint32 col = 0; col < row + colStart + 1 - rowStart; ++col)
202  {
203  if (col == inM)
204  {
205  break;
206  }
207 
208  returnArray(row, col) = dtype{ 0 };
209  }
210  }
211 
212  return returnArray;
213  }
214 
215  //============================================================================
216  // Method Description:
228  template<typename dtype>
229  NdArray<dtype> triu(uint32 inN, int32 inOffset = 0)
230  {
232 
233  return tril<dtype>(inN, -inOffset).transpose();
234  }
235 
236  //============================================================================
237  // Method Description:
253  template<typename dtype>
254  NdArray<dtype> triu(const NdArray<dtype>& inArray, int32 inOffset = 0)
255  {
257 
258  const Shape inShape = inArray.shape();
259  auto outArray = inArray.copy();
260  outArray.putMask(tril<bool>(inShape.rows, inShape.cols, inOffset - 1), 0);
261  return outArray;
262  }
263 } // namespace nc
StaticAsserts.hpp
nc::tril
NdArray< dtype > tril(uint32 inN, int32 inOffset=0)
Definition: tri.hpp:51
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4356
nc::int32
std::int32_t int32
Definition: Types.hpp:36
nc::NdArray::copy
NdArray< dtype > copy() const
Definition: NdArrayCore.hpp:2414
nc::triu
NdArray< dtype > triu(uint32 inN, uint32 inM, int32 inOffset=0)
Definition: tri.hpp:179
STATIC_ASSERT_ARITHMETIC_OR_COMPLEX
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:50
nc::NdArray::putMask
NdArray< dtype > & putMask(const NdArray< bool > &inMask, value_type inValue)
Definition: NdArrayCore.hpp:4008
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:45
nc::NdArray::zeros
NdArray< dtype > & zeros() noexcept
Definition: NdArrayCore.hpp:4670
Shape.hpp
nc::NdArray::ones
NdArray< dtype > & ones() noexcept
Definition: NdArrayCore.hpp:3498
nc
Definition: Coordinate.hpp:44
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:44
Types.hpp