NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
inv.hpp
Go to the documentation of this file.
1 #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:
51  template<typename dtype>
53  {
55 
56  const Shape inShape = inArray.shape();
57  if (inShape.rows != inShape.cols)
58  {
59  THROW_INVALID_ARGUMENT_ERROR("input array must be square.");
60  }
61 
62  const uint32 order = inShape.rows;
63 
64  Shape newShape(inShape);
65  newShape.rows *= 2;
66  newShape.cols *= 2;
67 
68  NdArray<double> tempArray(newShape);
69  for (uint32 row = 0; row < order; ++row)
70  {
71  for (uint32 col = 0; col < order; ++col)
72  {
73  tempArray(row, col) = static_cast<double>(inArray(row, col));
74  }
75  }
76 
77  for (uint32 row = 0; row < order; ++row)
78  {
79  for (uint32 col = order; col < 2 * order; ++col)
80  {
81  if (row == col - order)
82  {
83  tempArray(row, col) = 1.0;
84  }
85  else
86  {
87  tempArray(row, col) = 0.0;
88  }
89  }
90  }
91 
92  for (uint32 row = 0; row < order; ++row)
93  {
94  double t = tempArray(row, row);
95  for (uint32 col = row; col < 2 * order; ++col)
96  {
97  tempArray(row, col) /= t;
98  }
99 
100  for (uint32 col = 0; col < order; ++col)
101  {
102  if (row != col)
103  {
104  t = tempArray(col, row);
105  for (uint32 k = 0; k < 2 * order; ++k)
106  {
107  tempArray(col, k) -= t * tempArray(row, k);
108  }
109  }
110  }
111  }
112 
113  NdArray<double> returnArray(inShape);
114  for (uint32 row = 0; row < order; row++)
115  {
116  uint32 colCounter = 0;
117  for (uint32 col = order; col < 2 * order; ++col)
118  {
119  returnArray(row, colCounter++) = tempArray(row, col);
120  }
121  }
122 
123  return returnArray;
124  }
125  } // namespace linalg
126 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
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:52
Definition: Coordinate.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40