46 template<
typename dtype>
61 m_{ inMatrix.
shape().rows },
62 n_{ inMatrix.
shape().cols },
113 for (
auto i = 0
u; i < std::min(m_, n_); ++i)
117 sInverse(i, i) = 1. / s_[i];
121 pinv_ =
dot(v_,
dot(sInverse, u_.transpose()));
137 if (inInput.
size() != m_)
163 const auto AtA =
dot(At, A);
164 const auto AAt =
dot(A, At);
166 const auto& [sigmaSquaredU, U] =
eig(AAt);
167 const auto& [sigmaSquaredV, V] =
eig(AtA);
170 for (
auto i = 0
u; i < std::min(m_, n_); ++i)
174 s_[i] = std::sqrt(sigmaSquaredV[i]);
186 auto Av = NdArray<double>(m_, 1);
187 for (
auto i = 0
u; i < rank; ++i)
189 for (
auto j = 0
u; j < m_; ++j)
192 for (
auto k = 0
u; k < n_; ++k)
194 sum += A(j, k) * v_(k, i);
199 const auto normalization =
norm(Av).
item();
203 for (
auto j = 0
u;
j < m_; ++
j)
205 u_(j, i) = Av[
j] / normalization;
215 NdArray<double> u_{};
216 NdArray<double> v_{};
217 NdArray<double> s_{};
218 NdArray<double> pinv_{};
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
self_type & reshape(size_type inSize)
Definition NdArrayCore.hpp:4347
self_type transpose() const
Definition NdArrayCore.hpp:4959
size_type numCols() const noexcept
Definition NdArrayCore.hpp:3541
self_type copy() const
Definition NdArrayCore.hpp:2558
value_type item() const
Definition NdArrayCore.hpp:3098
const NdArray< double > & u() const noexcept
Definition svd/svd.hpp:74
const NdArray< double > & s() const noexcept
Definition svd/svd.hpp:96
STATIC_ASSERT_ARITHMETIC(dtype)
static constexpr auto TOLERANCE
Definition svd/svd.hpp:52
NdArray< double > pinv()
Definition svd/svd.hpp:107
SVD(const NdArray< dtype > &inMatrix)
Definition svd/svd.hpp:60
NdArray< double > lstsq(const NdArray< double > &inInput)
Definition svd/svd.hpp:135
const NdArray< double > & v() const noexcept
Definition svd/svd.hpp:85
constexpr auto j
Definition Core/Constants.hpp:42
Definition cholesky.hpp:41
std::pair< NdArray< double >, NdArray< double > > eig(const NdArray< dtype > &inA, double inTolerance=1e-12)
Definition eig.hpp:53
NdArray< double > norm(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition norm.hpp:51
NdArray< dtype > dot(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition dot.hpp:48
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition sum.hpp:45
NdArray< dtype > zeros(uint32 inSquareSize)
Definition zeros.hpp:48
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition Functions/shape.hpp:42
NdArray< dtypeOut > astype(const NdArray< dtype > inArray)
Definition astype.hpp:43
std::uint32_t uint32
Definition Types.hpp:40