NumCpp  2.11.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
pinv.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <string>
31 
33 #include "NumCpp/Core/Types.hpp"
35 #include "NumCpp/Linalg/svd.hpp"
36 #include "NumCpp/NdArray.hpp"
37 
38 namespace nc::linalg
39 {
40  //============================================================================
41  // Method Description:
49  template<typename dtype>
51  {
53 
57  svd(inArray, u, d, v);
58 
59  const auto inShape = inArray.shape();
60  auto dPlus = nc::zeros<double>(inShape.cols, inShape.rows); // transpose
61 
62  for (uint32 i = 0; i < d.shape().rows; ++i)
63  {
64  dPlus(i, i) = 1. / d(i, i);
65  }
66 
67  return v.transpose().dot(dPlus).dot(u.transpose());
68  }
69 } // namespace nc::linalg
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:56
self_type transpose() const
Definition: NdArrayCore.hpp:4837
self_type dot(const self_type &inOtherArray) const
Definition: NdArrayCore.hpp:2672
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4464
Definition: cholesky.hpp:41
void svd(const NdArray< dtype > &inArray, NdArray< double > &outU, NdArray< double > &outS, NdArray< double > &outVt)
Definition: svd.hpp:51
NdArray< double > pinv(const NdArray< dtype > &inArray)
Definition: pinv.hpp:50
std::uint32_t uint32
Definition: Types.hpp:40