-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from SpM-lab/33-porting-svdjl-to-c++
Implement svd.hpp
- Loading branch information
Showing
7 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <cmath> | ||
#include <algorithm> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#pragma once | ||
// C++ Standard Library headers | ||
#include <algorithm> | ||
#include <cassert> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#pragma once | ||
|
||
// C++ Standard Library headers | ||
#include <stdexcept> | ||
#include <iostream> | ||
|
||
// Eigen headers | ||
#include <Eigen/Dense> | ||
|
||
namespace sparseir | ||
{ | ||
|
||
using namespace Eigen; | ||
|
||
template <typename T> | ||
std::tuple<MatrixX<T>, VectorX<T>, MatrixX<T>> compute_svd(const MatrixX<T> &A, int n_sv_hint = 0, std::string strategy = "default") | ||
{ | ||
if (n_sv_hint != 0) | ||
{ | ||
std::cout << "n_sv_hint is set but will not be used in the current implementation!" << std::endl; | ||
} | ||
|
||
if (strategy != "default") | ||
{ | ||
std::cout << "strategy is set but will not be used in the current implementation!" << std::endl; | ||
} | ||
|
||
MatrixX<T> A_copy = A; // create a copy of A | ||
return tsvd<T>(A_copy); | ||
// auto svd_result = tsvd<T>(A_copy); | ||
// MatrixX<T> u = std::get<0>(svd_result); | ||
// VectorX<T> s = std::get<1>(svd_result); | ||
// MatrixX<T> v = std::get<2>(svd_result); | ||
|
||
// return std::make_tuple(u, s, v); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <Eigen/Dense> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <random> | ||
#include <cmath> | ||
#include <stdexcept> | ||
#include <iostream> | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <xprec/ddouble-header-only.hpp> | ||
#include <sparseir/sparseir-header-only.hpp> | ||
|
||
// test_piecewise_legendre_poly.cpp | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
#include <Eigen/Dense> | ||
#include <iostream> | ||
#include <vector> | ||
#include <cmath> | ||
#include <random> | ||
#include <functional> | ||
|
||
TEST_CASE("svd.cpp") | ||
{ | ||
using namespace sparseir; | ||
using namespace Eigen; | ||
using namespace xprec; | ||
|
||
// Create a matrix of Float64x2 equivalent (here just Eigen::MatrixXd for simplicity) | ||
/** | ||
Eigen::MatrixXd mat64x2 = Eigen::MatrixXd::Random(4, 6); | ||
REQUIRE_NOTHROW(sparseir::compute_svd(mat64x2, "accurate", 2)); | ||
std::cout << "n_sv_hint is set but will not be used in the current implementation!" << std::endl; | ||
REQUIRE_NOTHROW({ | ||
compute_svd(mat64x2, "accurate"); | ||
std::cout << "strategy is set but will not be used in the current implementation!" << std::endl; | ||
}); | ||
*/ | ||
|
||
// Create a standard matrix | ||
MatrixX<DDouble> mat = MatrixX<DDouble>::Random(5, 6); | ||
auto svd_result = compute_svd(mat, 0, "default"); | ||
auto U = std::get<0>(svd_result); | ||
auto S = std::get<1>(svd_result); | ||
auto V = std::get<2>(svd_result); | ||
auto diff = (mat - U * S.asDiagonal() * V.transpose()).norm() / mat.norm(); | ||
REQUIRE(diff < 1e-28); | ||
|
||
/* | ||
REQUIRE_THROWS_AS(compute_svd(mat, "fast"), std::domain_error); | ||
*/ | ||
} |