numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
approximate_max_eigen_aat.h
Go to the documentation of this file.
1/*
2 * Copyright 2024 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cmath>
23
24#include <Eigen/Core>
25
29
31
39template <typename Matrix>
40 requires(base::concepts::real_scalar_dense_matrix<Matrix> ||
41 base::concepts::real_scalar_sparse_matrix<Matrix>)
42[[nodiscard]] auto approximate_max_eigen_aat(const Matrix& matrix) ->
43 typename Matrix::Scalar {
44 const index_type rows = matrix.rows();
45 using scalar_type = typename Matrix::Scalar;
46 using vector_type = Eigen::VectorX<scalar_type>;
47 vector_type vec = vector_type::Random(rows);
48 vec.normalize();
49
50 vector_type mul_vec = matrix * matrix.transpose() * vec;
51 scalar_type eigen = vec.dot(mul_vec) / vec.squaredNorm();
52 const index_type num_iterations = rows * 10;
53 for (index_type i = 0; i < num_iterations; ++i) {
54 const scalar_type eigen_before = eigen;
55 vec = mul_vec.normalized();
56 mul_vec = matrix * matrix.transpose() * vec;
57 eigen = vec.dot(mul_vec) / vec.squaredNorm();
58 using std::abs;
59 constexpr auto tol_update = static_cast<scalar_type>(1e-4);
60 if (abs(eigen - eigen_before) / abs(eigen) < tol_update) {
61 break;
62 }
63 }
64
65 return eigen;
66}
67
68} // namespace num_collect::regularization::impl
Definition of index_type type.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of internal implementations.
auto approximate_max_eigen_aat(const Matrix &matrix) -> typename Matrix::Scalar
Approximate the maximum eigenvalue of for a matrix .
Definition of real_scalar_dense_matrix concept.
Definition of real_scalar_sparse_matrix concept.