NumCpp  2.5.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
eye.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:
51  template<typename dtype>
52  NdArray<dtype> eye(uint32 inN, uint32 inM, int32 inK = 0)
53  {
55 
56  NdArray<dtype> returnArray(inN, inM);
57  returnArray.zeros();
58 
59  if (inK < 0)
60  {
61  uint32 col = 0;
62  for (uint32 row = inK; row < inN; ++row)
63  {
64  if (col >= inM)
65  {
66  break;
67  }
68 
69  returnArray(row, col++) = dtype{ 1 };
70  }
71  }
72  else
73  {
74  uint32 row = 0;
75  for (uint32 col = inK; col < inM; ++col)
76  {
77  if (row >= inN)
78  {
79  break;
80  }
81 
82  returnArray(row++, col) = dtype{ 1 };
83  }
84  }
85 
86  return returnArray;
87  }
88 
89  //============================================================================
90  // Method Description:
102  template<typename dtype>
104  {
105  return eye<dtype>(inN, inN, inK);
106  }
107 
108  //============================================================================
109  // Method Description:
121  template<typename dtype>
122  NdArray<dtype> eye(const Shape& inShape, int32 inK = 0)
123  {
124  return eye<dtype>(inShape.rows, inShape.cols, inK);
125  }
126 } // namespace nc
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:50
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
NdArray< dtype > & zeros() noexcept
Definition: NdArrayCore.hpp:4848
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: Coordinate.hpp:45
std::int32_t int32
Definition: Types.hpp:36
NdArray< dtype > eye(uint32 inN, uint32 inM, int32 inK=0)
Definition: eye.hpp:52
std::uint32_t uint32
Definition: Types.hpp:40