numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
sparse_blur_matrix.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 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 <algorithm>
23#include <cmath> // IWYU pragma: keep
24#include <vector>
25
26#include <Eigen/SparseCore>
27
30
32
41template <num_collect::base::concepts::sparse_matrix Matrix>
43 Matrix& mat, num_collect::index_type rows, num_collect::index_type cols) {
44 using scalar_type = typename Matrix::value_type;
45 using storage_index_type = typename Matrix::StorageIndex;
46
47 std::vector<Eigen::Triplet<scalar_type, storage_index_type>> triplets;
48 for (num_collect::index_type j = 0; j < cols; ++j) {
49 for (num_collect::index_type i = 0; i < rows; ++i) {
50 static constexpr auto factor = static_cast<scalar_type>(100.0);
51 static constexpr auto thresh = static_cast<scalar_type>(0.1);
52 const scalar_type sol_rate =
53 static_cast<scalar_type>(j) / static_cast<scalar_type>(cols);
54 const scalar_type data_rate =
55 static_cast<scalar_type>(i) / static_cast<scalar_type>(rows);
56 const scalar_type diff = sol_rate - data_rate;
57 const scalar_type coeff =
58 std::max(std::exp(-factor * diff * diff) - thresh,
59 static_cast<scalar_type>(0));
60 if (coeff > static_cast<scalar_type>(0)) {
61 triplets.emplace_back(static_cast<storage_index_type>(i),
62 static_cast<storage_index_type>(j), coeff);
63 }
64 }
65 }
66
67 mat.resize(rows, cols);
68 mat.setFromTriplets(triplets.begin(), triplets.end());
69}
70
71} // namespace num_prob_collect::regularization
Definition of index_type type.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of regularization.
Definition namespaces.h:34
void sparse_blur_matrix(Matrix &mat, num_collect::index_type rows, num_collect::index_type cols)
Create a sparse blur matrix.
Definition of sparse_matrix concept.