NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
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"
35#include "NumCpp/NdArray.hpp"
37
38namespace nc::linalg
39{
40 //============================================================================
41 // Method Description:
49 template<typename dtype>
51 {
53
54 const Shape inShape = inArray.shape();
55 if (inShape.rows != inShape.cols)
56 {
57 THROW_INVALID_ARGUMENT_ERROR("input array must be square.");
58 }
59
60 NdArray<double> inArrayDouble = inArray.template astype<double>();
61 NdArray<int> incidence = nc::zeros<int>(inShape);
62
63 for (uint32 k = 0; k < inShape.rows - 1; ++k)
64 {
65 if (utils::essentiallyEqual(inArrayDouble(k, k), 0.))
66 {
67 uint32 l = k;
68 while (l < inShape.cols && utils::essentiallyEqual(inArrayDouble(k, l), 0.))
69 {
70 ++l;
71 }
72
73 inArrayDouble.swapRows(k, l);
74 incidence(k, k) = 1;
75 incidence(k, l) = 1;
76 }
77 }
78
79 NdArray<double> result(inShape);
80
81 for (uint32 k = 0; k < inShape.rows; ++k)
82 {
83 result(k, k) = -1. / inArrayDouble(k, k);
84 for (uint32 i = 0; i < inShape.rows; ++i)
85 {
86 for (uint32 j = 0; j < inShape.cols; ++j)
87 {
88 if ((i - k) && (j - k))
89 {
90 result(i, j) = inArrayDouble(i, j) + inArrayDouble(k, j) * inArrayDouble(i, k) * result(k, k);
91 }
92 else if ((i - k) && !(j - k))
93 {
94 result(i, k) = inArrayDouble(i, k) * result(k, k);
95 }
96 else if (!(i - k) && (j - k))
97 {
98 result(k, j) = inArrayDouble(k, j) * result(k, k);
99 }
100 }
101 }
102
103 inArrayDouble = result;
104 }
105
106 result *= -1.;
107
108 for (int i = static_cast<int>(inShape.rows) - 1; i >= 0; --i)
109 {
110 if (incidence(i, i) != 1)
111 {
112 continue;
113 }
114
115 int k = 0;
116 for (; k < static_cast<int>(inShape.cols); ++k)
117 {
118 if ((k - i) && incidence(i, k) != 0)
119 {
120 result.swapCols(i, k);
121 break;
122 }
123 }
124 }
125
126 return result;
127 }
128} // namespace nc::linalg
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition StaticAsserts.hpp:56
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
self_type & swapCols(index_type colIdx1, index_type colIdx2) noexcept
Definition NdArrayCore.hpp:4748
self_type & swapRows(index_type rowIdx1, index_type rowIdx2) noexcept
Definition NdArrayCore.hpp:4766
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
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 cholesky.hpp:41
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition inv.hpp:50
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition essentiallyEqual.hpp:47
NdArray< dtype > zeros(uint32 inSquareSize)
Definition zeros.hpp:48
NdArray< dtypeOut > astype(const NdArray< dtype > inArray)
Definition astype.hpp:43
std::uint32_t uint32
Definition Types.hpp:40