NumCpp  1.0
A C++ implementation of the Python Numpy library
pivotLU_decomposition.hpp
Go to the documentation of this file.
1 #pragma once
34 
35 #include "NumCpp/NdArray.hpp"
36 #include "NumCpp/Core/Types.hpp"
39 #include "NumCpp/Functions/eye.hpp"
42 
43 #include <cmath>
44 #include <tuple>
45 
46 namespace nc
47 {
48  namespace linalg
49  {
50  //============================================================================
51  // Method Description:
58  template<typename dtype>
59  std::tuple<NdArray<double>, NdArray<double>, NdArray<double> > pivotLU_decomposition(const NdArray<dtype>& inMatrix)
60  {
62 
63  const auto shape = inMatrix.shape();
64 
65  if(!shape.issquare())
66  {
67  THROW_RUNTIME_ERROR("Input matrix should be square.");
68  }
69 
70  NdArray<double> lMatrix = zeros_like<double>(inMatrix);
71  NdArray<double> uMatrix = inMatrix.template astype<double>();
72  NdArray<double> pMatrix = eye<double>(shape.rows);
73 
74  for(uint32 k = 0; k < shape.rows; ++k)
75  {
76  double max = 0.0;
77  uint32 pk = 0;
78  for(uint32 i = k; i < shape.rows; ++i)
79  {
80  double s = 0.0;
81  for(uint32 j = k; j < shape.cols; ++j)
82  {
83  s += std::fabs(uMatrix(i, j));
84  }
85 
86  const double q = std::fabs(uMatrix(i, k)) / s;
87  if(q > max)
88  {
89  max = q;
90  pk = i;
91  }
92  }
93 
94  if (utils::essentiallyEqual(max, double{0.0}))
95  {
96  THROW_RUNTIME_ERROR("Division by 0.");
97  }
98 
99  if(pk != k)
100  {
101  for(uint32 j = 0; j < shape.cols; ++j)
102  {
103  std::swap(pMatrix(k, j), pMatrix(pk, j));
104  std::swap(lMatrix(k, j), lMatrix(pk, j));
105  std::swap(uMatrix(k, j), uMatrix(pk, j));
106  }
107  }
108 
109  for(uint32 i = k+1; i < shape.rows; ++i)
110  {
111  lMatrix(i, k) = uMatrix(i, k) / uMatrix(k, k);
112 
113  for(uint32 j = k; j < shape.cols; ++j)
114  {
115  uMatrix(i, j) = uMatrix(i, j) - lMatrix(i, k) * uMatrix(k, j);
116  }
117  }
118  }
119 
120  for(uint32 k = 0; k < shape.rows; ++k)
121  {
122  lMatrix(k, k) = 1.0;
123  }
124 
125  return std::make_tuple(lMatrix, uMatrix, pMatrix);
126  }
127  }
128 }
StaticAsserts.hpp
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
Error.hpp
STATIC_ASSERT_ARITHMETIC
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:38
nc::shape
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition: Functions/Shape.hpp:45
nc::utils::essentiallyEqual
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:53
nc::NdArray< double >
nc::Shape::issquare
constexpr bool issquare() const noexcept
Definition: Core/Shape.hpp:124
nc::constants::j
constexpr auto j
Definition: Constants.hpp:46
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
zeros_like.hpp
NdArray.hpp
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
THROW_RUNTIME_ERROR
#define THROW_RUNTIME_ERROR(msg)
Definition: Error.hpp:38
nc
Definition: Coordinate.hpp:45
nc::swap
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:43
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
essentiallyEqual.hpp
nc::max
NdArray< dtype > max(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: max.hpp:46
eye.hpp
Types.hpp
nc::linalg::pivotLU_decomposition
std::tuple< NdArray< double >, NdArray< double >, NdArray< double > > pivotLU_decomposition(const NdArray< dtype > &inMatrix)
Definition: pivotLU_decomposition.hpp:59