diff --git a/mlpp/data/data.cpp b/mlpp/data/data.cpp index 681af5a..a9484cd 100644 --- a/mlpp/data/data.cpp +++ b/mlpp/data/data.cpp @@ -938,12 +938,12 @@ std::vector> MLPPData::LSA(std::vector sentence MLPPLinAlg alg; std::vector> docWordData = BOW(sentences, "Binary"); - auto [U, S, Vt] = alg.SVD(docWordData); + MLPPLinAlg::SDVResult svr_res = alg.SVD(docWordData); std::vector> S_trunc = alg.zeromat(dim, dim); std::vector> Vt_trunc; for (int i = 0; i < dim; i++) { - S_trunc[i][i] = S[i][i]; - Vt_trunc.push_back(Vt[i]); + S_trunc[i][i] = svr_res.S[i][i]; + Vt_trunc.push_back(svr_res.Vt[i]); } std::vector> embeddings = alg.matmult(S_trunc, Vt_trunc); diff --git a/mlpp/lin_alg/lin_alg.cpp b/mlpp/lin_alg/lin_alg.cpp index 4ce49b7..6d3a1f7 100644 --- a/mlpp/lin_alg/lin_alg.cpp +++ b/mlpp/lin_alg/lin_alg.cpp @@ -1198,18 +1198,24 @@ MLPPLinAlg::EigenResult MLPPLinAlg::eigen(std::vector> A) { return res; } -std::tuple>, std::vector>, std::vector>> MLPPLinAlg::SVD(std::vector> A) { - auto [left_eigenvecs, eigenvals] = eig(matmult(A, transpose(A))); - auto [right_eigenvecs, right_eigenvals] = eig(matmult(transpose(A), A)); +MLPPLinAlg::SDVResult MLPPLinAlg::SVD(std::vector> A) { + EigenResult left_eigen = eigen(matmult(A, transpose(A))); + EigenResult right_eigen = eigen(matmult(transpose(A), A)); - std::vector> singularvals = sqrt(eigenvals); + std::vector> singularvals = sqrt(left_eigen.eigen_values); std::vector> sigma = zeromat(A.size(), A[0].size()); for (int i = 0; i < singularvals.size(); i++) { for (int j = 0; j < singularvals[i].size(); j++) { sigma[i][j] = singularvals[i][j]; } } - return { left_eigenvecs, sigma, right_eigenvecs }; + + SDVResult res; + res.U = left_eigen.eigen_vectors; + res.S = sigma; + res.Vt = right_eigen.eigen_vectors; + + return res; } MLPPLinAlg::SDVResult MLPPLinAlg::svd(std::vector> A) { diff --git a/mlpp/lin_alg/lin_alg.h b/mlpp/lin_alg/lin_alg.h index 1e6f413..489995a 100644 --- a/mlpp/lin_alg/lin_alg.h +++ b/mlpp/lin_alg/lin_alg.h @@ -121,14 +121,14 @@ public: EigenResult eigen(std::vector> A); - std::tuple>, std::vector>, std::vector>> SVD(std::vector> A); - struct SDVResult { std::vector> U; std::vector> S; std::vector> Vt; }; + SDVResult SVD(std::vector> A); + SDVResult svd(std::vector> A); std::vector vectorProjection(std::vector a, std::vector b); diff --git a/mlpp/pca/pca.cpp b/mlpp/pca/pca.cpp index d13bc92..543ed85 100644 --- a/mlpp/pca/pca.cpp +++ b/mlpp/pca/pca.cpp @@ -21,12 +21,12 @@ std::vector> MLPPPCA::principalComponents() { MLPPLinAlg alg; MLPPData data; - auto [U, S, Vt] = alg.SVD(alg.cov(inputSet)); + MLPPLinAlg::SDVResult svr_res = alg.SVD(alg.cov(inputSet)); X_normalized = data.meanCentering(inputSet); - U_reduce.resize(U.size()); + U_reduce.resize(svr_res.U.size()); for (int i = 0; i < k; i++) { - for (int j = 0; j < U.size(); j++) { - U_reduce[j].push_back(U[j][i]); + for (int j = 0; j < svr_res.U.size(); j++) { + U_reduce[j].push_back(svr_res.U[j][i]); } } Z = alg.matmult(alg.transpose(U_reduce), X_normalized); diff --git a/mlpp/pca/pca_old.cpp b/mlpp/pca/pca_old.cpp index 3b52b03..0997cc0 100644 --- a/mlpp/pca/pca_old.cpp +++ b/mlpp/pca/pca_old.cpp @@ -21,12 +21,12 @@ std::vector> MLPPPCAOld::principalComponents() { MLPPLinAlg alg; MLPPData data; - auto [U, S, Vt] = alg.SVD(alg.cov(inputSet)); + MLPPLinAlg::SDVResult svr_res = alg.SVD(alg.cov(inputSet)); X_normalized = data.meanCentering(inputSet); - U_reduce.resize(U.size()); + U_reduce.resize(svr_res.U.size()); for (int i = 0; i < k; i++) { - for (int j = 0; j < U.size(); j++) { - U_reduce[j].push_back(U[j][i]); + for (int j = 0; j < svr_res.U.size(); j++) { + U_reduce[j].push_back(svr_res.U[j][i]); } } Z = alg.matmult(alg.transpose(U_reduce), X_normalized); diff --git a/test/mlpp_tests.cpp b/test/mlpp_tests.cpp index c606910..add15cc 100644 --- a/test/mlpp_tests.cpp +++ b/test/mlpp_tests.cpp @@ -721,7 +721,7 @@ void MLPPTests::test_pca_svd_eigenvalues_eigenvectors(bool ui) { std::cout << "SVD" << std::endl; - MLPPLinAlg::SDVResult svd = alg.svd(inputSet); + MLPPLinAlg::SDVResult svd = alg.SVD(inputSet); std::cout << "U:" << std::endl; alg.printMatrix(svd.U);