Loading...
Searching...
No Matches
random_orthogonal_matrix.h
1/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2 * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3 * Author(s): Siargey Kachanovich
4 *
5 * Copyright (C) 2019 Inria
6 *
7 * Modification(s):
8 * - 2026/04 Vincent Rouvreau: Replace c++ random call with CGAL::get_default_random().get_double()
9 * - YYYY/MM Author: Description of the modification
10 */
11
12#ifndef FUNCTIONS_RANDOM_ORTHOGONAL_MATRIX_H_
13#define FUNCTIONS_RANDOM_ORTHOGONAL_MATRIX_H_
14
15#include <cstdlib> // for std::size_t
16#include <cmath> // for std::cos, std::sin
17
18#include <Eigen/Dense>
19#include <Eigen/Sparse>
20#include <Eigen/SVD>
21
22#include <CGAL/Epick_d.h>
23#include <CGAL/point_generators_d.h>
24#include <CGAL/Random.h>
25
26#include <boost/math/constants/constants.hpp> // for PI value
27
28namespace Gudhi {
29
30namespace coxeter_triangulation {
31
40// Note: the householderQR operation at the end seems to take a lot of time at compilation.
41// The CGAL headers are another source of long compilation time.
42Eigen::MatrixXd random_orthogonal_matrix(std::size_t d) {
43 typedef CGAL::Epick_d<CGAL::Dynamic_dimension_tag> Kernel;
44 typedef typename Kernel::Point_d Point_d;
45 if (d == 1) return Eigen::VectorXd::Constant(1, 1.0);
46 if (d == 2) {
47 // 0. < alpha < 2 Pi
48 CGAL::Random& rng = CGAL::get_default_random();
49 double alpha = rng.get_double(0., 2 * boost::math::constants::pi<double>());
50
51 Eigen::Matrix2d rot;
52 rot << std::cos(alpha), -std::sin(alpha), std::sin(alpha), cos(alpha);
53 return rot;
54 }
55 Eigen::MatrixXd low_dim_rot = random_orthogonal_matrix(d - 1);
56 Eigen::MatrixXd rot(d, d);
57 Point_d v = *CGAL::Random_points_on_sphere_d<Point_d>(d, 1);
58 for (std::size_t i = 0; i < d; ++i) rot(i, 0) = v[i];
59 for (std::size_t i = 0; i < d - 1; ++i)
60 for (std::size_t j = 1; j < d - 1; ++j) rot(i, j) = low_dim_rot(i, j - 1);
61 for (std::size_t j = 1; j < d; ++j) rot(d - 1, j) = 0;
62 rot = rot.householderQr()
63 .householderQ(); // a way to do Gram-Schmidt, see https://forum.kde.org/viewtopic.php?f=74&t=118568#p297246
64 return rot;
65}
66
67} // namespace coxeter_triangulation
68
69} // namespace Gudhi
70
71#endif
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14