NumCpp  2.1.0
A C++ implementation of the Python Numpy library
tri.hpp
Go to the documentation of this file.
1 #pragma once
30 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 namespace nc
37 {
38  //============================================================================
39  // Method Description:
51  template<typename dtype>
52  NdArray<dtype> tril(uint32 inN, int32 inOffset = 0)
53  {
55 
56  uint32 rowStart = 0;
57  uint32 colStart = 0;
58  if (inOffset > 0)
59  {
60  colStart = inOffset;
61  }
62  else
63  {
64  rowStart = inOffset * -1;
65  }
66 
67  NdArray<dtype> returnArray(inN);
68  returnArray.zeros();
69  for (uint32 row = rowStart; row < inN; ++row)
70  {
71  for (uint32 col = 0; col < row + colStart + 1 - rowStart; ++col)
72  {
73  if (col == inN)
74  {
75  break;
76  }
77 
78  returnArray(row, col) = dtype{ 1 };
79  }
80  }
81 
82  return returnArray;
83  }
84 
85  //============================================================================
86  // Method Description:
99  template<typename dtype>
100  NdArray<dtype> tril(uint32 inN, uint32 inM, int32 inOffset = 0)
101  {
103 
104  uint32 rowStart = 0;
105  uint32 colStart = 0;
106  if (inOffset > 0)
107  {
108  colStart = inOffset;
109  }
110  else if (inOffset < 0)
111  {
112  rowStart = inOffset * -1;
113  }
114 
115  NdArray<dtype> returnArray(inN, inM);
116  returnArray.zeros();
117  for (uint32 row = rowStart; row < inN; ++row)
118  {
119  for (uint32 col = 0; col < row + colStart + 1 - rowStart; ++col)
120  {
121  if (col == inM)
122  {
123  break;
124  }
125 
126  returnArray(row, col) = dtype{ 1 };
127  }
128  }
129 
130  return returnArray;
131  }
132 
133  // forward declare
134  template<typename dtype>
135  NdArray<dtype> triu(uint32 inN, uint32 inM, int32 inOffset = 0) ;
136 
137  //============================================================================
138  // Method Description:
154  template<typename dtype>
155  NdArray<dtype> tril(const NdArray<dtype>& inArray, int32 inOffset = 0)
156  {
158 
159  const Shape inShape = inArray.shape();
160  auto outArray = inArray.copy();
161  outArray.putMask(triu<bool>(inShape.rows, inShape.cols, inOffset + 1), 0);
162  return outArray;
163  }
164 
165  //============================================================================
166  // Method Description:
179  template<typename dtype>
180  NdArray<dtype> triu(uint32 inN, uint32 inM, int32 inOffset)
181  {
183 
184  // because i'm stealing the lines of code from tril and reversing it, this is necessary
185  inOffset -= 1;
186 
187  uint32 rowStart = 0;
188  uint32 colStart = 0;
189  if (inOffset > 0)
190  {
191  colStart = inOffset;
192  }
193  else if (inOffset < 0)
194  {
195  rowStart = inOffset * -1;
196  }
197 
198  NdArray<dtype> returnArray(inN, inM);
199  returnArray.ones();
200  for (uint32 row = rowStart; row < inN; ++row)
201  {
202  for (uint32 col = 0; col < row + colStart + 1 - rowStart; ++col)
203  {
204  if (col == inM)
205  {
206  break;
207  }
208 
209  returnArray(row, col) = dtype{ 0 };
210  }
211  }
212 
213  return returnArray;
214  }
215 
216  //============================================================================
217  // Method Description:
229  template<typename dtype>
230  NdArray<dtype> triu(uint32 inN, int32 inOffset = 0)
231  {
233 
234  return tril<dtype>(inN, -inOffset).transpose();
235  }
236 
237  //============================================================================
238  // Method Description:
254  template<typename dtype>
255  NdArray<dtype> triu(const NdArray<dtype>& inArray, int32 inOffset = 0)
256  {
258 
259  const Shape inShape = inArray.shape();
260  auto outArray = inArray.copy();
261  outArray.putMask(tril<bool>(inShape.rows, inShape.cols, inOffset - 1), 0);
262  return outArray;
263  }
264 } // namespace nc
StaticAsserts.hpp
nc::tril
NdArray< dtype > tril(uint32 inN, int32 inOffset=0)
Definition: tri.hpp:52
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4312
nc::int32
std::int32_t int32
Definition: Types.hpp:37
nc::NdArray::copy
NdArray< dtype > copy() const
Definition: NdArrayCore.hpp:2397
nc::triu
NdArray< dtype > triu(uint32 inN, uint32 inM, int32 inOffset=0)
Definition: tri.hpp:180
STATIC_ASSERT_ARITHMETIC_OR_COMPLEX
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:51
nc::NdArray::putMask
NdArray< dtype > & putMask(const NdArray< bool > &inMask, value_type inValue)
Definition: NdArrayCore.hpp:3964
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::NdArray::zeros
NdArray< dtype > & zeros() noexcept
Definition: NdArrayCore.hpp:4626
Shape.hpp
nc::NdArray::ones
NdArray< dtype > & ones() noexcept
Definition: NdArrayCore.hpp:3454
nc
Definition: Coordinate.hpp:45
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
Types.hpp