numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
tgv2_second_derivative_matrix_2d.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 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 <vector>
23
24#include <Eigen/SparseCore>
25
28
30
63template <num_collect::concepts::sparse_matrix Matrix>
66 -> Matrix {
67 using scalar_type = typename Matrix::value_type;
68 using storage_index_type = typename Matrix::StorageIndex;
69
70 const num_collect::index_type rows = (outer_size - 2) * inner_size +
71 outer_size * (inner_size - 2) + (outer_size - 1) * (inner_size - 1);
72 const num_collect::index_type cols =
73 (outer_size - 1) * inner_size + outer_size * (inner_size - 1);
74
75 // Difference for the outer index to the difference of the outer index.
76 std::vector<Eigen::Triplet<scalar_type, storage_index_type>> triplets;
77 for (num_collect::index_type o = 0; o < outer_size - 2; ++o) {
78 for (num_collect::index_type i = 0; i < inner_size; ++i) {
79 const num_collect::index_type row = o * inner_size + i;
80 triplets.emplace_back(
81 row, o * inner_size + i, static_cast<scalar_type>(1));
82 triplets.emplace_back(
83 row, (o + 1) * inner_size + i, static_cast<scalar_type>(-1));
84 }
85 }
86
87 // Difference for the inner index to the difference of the inner index.
88 for (num_collect::index_type o = 0; o < outer_size; ++o) {
89 for (num_collect::index_type i = 0; i < inner_size - 2; ++i) {
90 const num_collect::index_type row =
91 (outer_size - 2) * inner_size + o * (inner_size - 2) + i;
92 triplets.emplace_back(row,
93 (outer_size - 1) * inner_size + o * (inner_size - 1) + i,
94 static_cast<scalar_type>(1));
95 triplets.emplace_back(row,
96 (outer_size - 1) * inner_size + o * (inner_size - 1) + i + 1,
97 static_cast<scalar_type>(-1));
98 }
99 }
100
101 // Mixed differences.
102 for (num_collect::index_type o = 0; o < outer_size - 1; ++o) {
103 for (num_collect::index_type i = 0; i < inner_size - 1; ++i) {
104 const num_collect::index_type row = (outer_size - 2) * inner_size +
105 outer_size * (inner_size - 2) + o * (inner_size - 1) + i;
106 triplets.emplace_back(
107 row, o * inner_size + i, static_cast<scalar_type>(1));
108 triplets.emplace_back(
109 row, o * inner_size + i + 1, static_cast<scalar_type>(-1));
110 triplets.emplace_back(row,
111 (outer_size - 1) * inner_size + o * (inner_size - 1) + i,
112 static_cast<scalar_type>(1));
113 triplets.emplace_back(row,
114 (outer_size - 1) * inner_size + (o + 1) * (inner_size - 1) + i,
115 static_cast<scalar_type>(-1));
116 }
117 }
118
119 Matrix matrix(rows, cols);
120 matrix.setFromTriplets(triplets.begin(), triplets.end());
121 return matrix;
122}
123
124} // 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
auto tgv2_second_derivative_matrix_2d(num_collect::index_type outer_size, num_collect::index_type inner_size) -> Matrix
Create a second derivative matrix for 2nd order TGV regularization in 2D images.
Definition of sparse_matrix concept.