Skip to content

Commit 2e4ee75

Browse files
authored
Update sampler.cpp
1 parent 26a6e27 commit 2e4ee75

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

examples/correlation_matrices/sampler.cpp

+13-27
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,6 @@ void write_to_file(std::string filename, std::vector<PointType> const& randPoint
9090
std::cout.rdbuf(coutbuf);
9191
}
9292

93-
bool is_correlation_matrix(const MT& matrix, const double tol = 1e-8){
94-
//check if all the diagonal elements are ones
95-
for(int i=0 ; i<matrix.rows() ; i++)
96-
{
97-
if(std::abs(matrix(i, i)-1.0) > tol)
98-
{
99-
return false;
100-
}
101-
}
102-
103-
//check if the matrix is positive semidefinite
104-
using NT = double;
105-
using MatrixType = Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic>;
106-
EigenvaluesProblems<NT, MatrixType, Eigen::Matrix<NT, Eigen::Dynamic, 1>> solver;
107-
108-
if(solver.isPositiveSemidefinite(matrix))
109-
{
110-
return true;
111-
}
112-
return false;
113-
}
114-
11593
template<typename WalkType>
11694
void correlation_matrix_uniform_sampling(const unsigned int n, const unsigned int num_points, std::string walkname){
11795

@@ -138,26 +116,34 @@ void correlation_matrix_uniform_sampling_MT(const unsigned int n, const unsigned
138116
std::cout << walkname << " samples uniformly "<< num_points << " correlation matrices of size " << n << " with matrix PointType" << std::endl;
139117
std::chrono::steady_clock::time_point start, end;
140118
double time;
141-
std::vector<PointMT> randPoints;
119+
std::list<MT> randCorMatrices;
142120
unsigned int walkL = 1;
143121

144122
start = std::chrono::steady_clock::now();
145123

146-
uniform_correlation_sampling_MT<WalkType, PointMT, RNGType>(n, randPoints, walkL, num_points, 0);
124+
uniform_correlation_sampling_MT<WalkType, PointMT, RNGType>(n, randCorMatrices, walkL, num_points, 0);
147125

148126
end = std::chrono::steady_clock::now();
149127
time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
150128
std::cout << "Elapsed time : " << time << " (ms)" << std::endl;
151129

152130
int valid_points = 0;
153131
EigenvaluesProblems<NT, MT, Eigen::Matrix<NT, Eigen::Dynamic, 1>> solver;
154-
for(const auto& points : randPoints){
155-
if(solver.is_correlation_matrix(points.mat)){
132+
for(const auto& matrix : randCorMatrices){
133+
if(solver.is_correlation_matrix(matrix)){
156134
valid_points++;
157-
}
135+
}
158136
}
137+
159138
std::cout << "Number of valid points = " << valid_points << std::endl;
160139

140+
std::vector<PointMT> randPoints;
141+
for(const auto &mat : randCorMatrices){
142+
PointMT p;
143+
p.mat = mat;
144+
randPoints.push_back(p);
145+
}
146+
161147
write_to_file<PointMT>(walkname + "_matrices_MT" + std::to_string(n) + ".txt", randPoints);
162148
}
163149

0 commit comments

Comments
 (0)