3#include "../../Matrix.h"
14std::vector<Matrix<double>>
svd(
const Matrix<double>& A,
const size_t& k,
const double epsilon = 0.1e-4) {
16 size_t n_orig = A.
rows();
19 if(k == 0) { chosen_k = n_orig < m_orig ? n_orig : m_orig; }
27 }
else if(n_orig < m_orig) {
28 A_copy = A_copy * A_copy.Transpose();
42 for(
size_t i = 0; i < 1000; i++) {
47 auto err = ((Q - Q_prev)).Apply([](
float val) {
return val * val; }).sumElements();
49 if(err < epsilon) {
break; }
51 auto singular_values =
diag_elements(R).Apply([](
double val) {
return sqrt(val); });
56 }
else if(n_orig == m_orig) {
58 right_vecs = left_vecs;
59 singular_values = singular_values.
Apply([](
double val) {
return val * val; });
65 return { left_vecs, singular_values, right_vecs };
constexpr Matrix< T > Transpose() const
Definition: Matrix.h:256
size_t rows() const
Definition: Matrix.h:193
size_t columns() const
Definition: Matrix.h:198
Matrix< T > Apply(const std::function< T(T)> &fun) const
Definition: Matrix.h:375
static Matrix Random(size_t rows, size_t columns, size_t element_size=1, double minValue=0.0, double maxValue=1.0)
Definition: Matrix.h:158
Matrix< double > gaussJordan(const Matrix< double > &A)
Definition: gaussJordan.h:27
Matrix< T > diag_elements(const Matrix< T > &in)
Definition: matrix_utils.h:451
Matrix< T > diag(const Matrix< T > &in)
Definition: utils.h:135
std::pair< Matrix< double >, Matrix< double > > qr(const Matrix< double > &A)
Definition: qr.h:12
std::vector< Matrix< double > > svd(const Matrix< double > &A, const size_t &k, const double epsilon=0.1e-4)
Definition: svd.h:14