NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
inv.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 #include <string>
37 
38 namespace nc
39 {
40  namespace linalg
41  {
42  //============================================================================
43  // Method Description:
53  template<typename dtype>
55  {
57 
58  const Shape inShape = inArray.shape();
59  if (inShape.rows != inShape.cols)
60  {
61  THROW_INVALID_ARGUMENT_ERROR("input array must be square.");
62  }
63 
64  const uint32 order = inShape.rows;
65 
66  Shape newShape(inShape);
67  newShape.rows *= 2;
68  newShape.cols *= 2;
69 
70  NdArray<double> tempArray(newShape);
71  for (uint32 row = 0; row < order; ++row)
72  {
73  for (uint32 col = 0; col < order; ++col)
74  {
75  tempArray(row, col) = static_cast<double>(inArray(row, col));
76  }
77  }
78 
79  for (uint32 row = 0; row < order; ++row)
80  {
81  for (uint32 col = order; col < 2 * order; ++col)
82  {
83  if (row == col - order)
84  {
85  tempArray(row, col) = 1.0;
86  }
87  else
88  {
89  tempArray(row, col) = 0.0;
90  }
91  }
92  }
93 
94  for (uint32 row = 0; row < order; ++row)
95  {
96  double t = tempArray(row, row);
97  for (uint32 col = row; col < 2 * order; ++col)
98  {
99  tempArray(row, col) /= t;
100  }
101 
102  for (uint32 col = 0; col < order; ++col)
103  {
104  if (row != col)
105  {
106  t = tempArray(col, row);
107  for (uint32 k = 0; k < 2 * order; ++k)
108  {
109  tempArray(col, k) -= t * tempArray(row, k);
110  }
111  }
112  }
113  }
114 
115  NdArray<double> returnArray(inShape);
116  for (uint32 row = 0; row < order; row++)
117  {
118  uint32 colCounter = 0;
119  for (uint32 col = order; col < 2 * order; ++col)
120  {
121  returnArray(row, colCounter++) = tempArray(row, col);
122  }
123  }
124 
125  return returnArray;
126  }
127  } // namespace linalg
128 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition: inv.hpp:54
Definition: Coordinate.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40