numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
sparse_div_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
29
31
43template <num_collect::concepts::sparse_matrix Matrix>
44[[nodiscard]] auto sparse_div_matrix_2d(num_collect::index_type outer_size,
45 num_collect::index_type inner_size) -> Matrix {
47 outer_size > 2, "Outer size must be greater than 2.");
49 inner_size > 2, "Inner size must be greater than 2.");
50
51 using scalar_type = typename Matrix::value_type;
52 using storage_index_type = typename Matrix::StorageIndex;
53
54 const num_collect::index_type rows = (outer_size - 2) * (inner_size - 2);
55 const num_collect::index_type cols =
56 (outer_size - 1) * inner_size + outer_size * (inner_size - 1);
57
58 std::vector<Eigen::Triplet<scalar_type, storage_index_type>> triplets;
59 const num_collect::index_type col_offset_for_inner_difference =
60 (outer_size - 1) * inner_size;
61 for (num_collect::index_type o = 0; o < outer_size - 2; ++o) {
62 for (num_collect::index_type i = 0; i < inner_size - 2; ++i) {
63 const num_collect::index_type row = o * (inner_size - 2) + i;
64
65 // Difference for the outer index.
66 triplets.emplace_back(
67 row, o * inner_size + i + 1, static_cast<scalar_type>(1));
68 triplets.emplace_back(row, (o + 1) * inner_size + i + 1,
69 static_cast<scalar_type>(-1));
70
71 // Difference for the inner index.
72 triplets.emplace_back(row,
73 col_offset_for_inner_difference + (o + 1) * (inner_size - 1) +
74 i,
75 static_cast<scalar_type>(1));
76 triplets.emplace_back(row,
77 col_offset_for_inner_difference + (o + 1) * (inner_size - 1) +
78 i + 1,
79 static_cast<scalar_type>(-1));
80 }
81 }
82
83 Matrix matrix(rows, cols);
84 matrix.setFromTriplets(triplets.begin(), triplets.end());
85 return matrix;
86}
87
99template <num_collect::concepts::sparse_matrix Matrix>
102 -> Matrix {
104 outer_size > 1, "Outer size must be greater than 1.");
106 inner_size > 1, "Inner size must be greater than 1.");
107
108 using scalar_type = typename Matrix::value_type;
109 using storage_index_type = typename Matrix::StorageIndex;
110
111 const num_collect::index_type rows = outer_size * inner_size;
112 const num_collect::index_type cols =
113 (outer_size - 1) * inner_size + outer_size * (inner_size - 1);
114
115 // Difference for the outer index.
116 std::vector<Eigen::Triplet<scalar_type, storage_index_type>> triplets;
117 for (num_collect::index_type o = 0; o < outer_size - 2; ++o) {
118 for (num_collect::index_type i = 0; i < inner_size; ++i) {
119 const num_collect::index_type row = (o + 1) * inner_size + i;
120 triplets.emplace_back(
121 row, o * inner_size + i, static_cast<scalar_type>(1));
122 triplets.emplace_back(
123 row, (o + 1) * inner_size + i, static_cast<scalar_type>(-1));
124 }
125 }
126
127 // Difference for the inner index.
128 const num_collect::index_type col_offset_for_inner_difference =
129 (outer_size - 1) * inner_size;
130 for (num_collect::index_type o = 0; o < outer_size; ++o) {
131 for (num_collect::index_type i = 0; i < inner_size - 2; ++i) {
132 const num_collect::index_type row = o * inner_size + (i + 1);
133 triplets.emplace_back(row,
134 col_offset_for_inner_difference + o * (inner_size - 1) + i,
135 static_cast<scalar_type>(1));
136 triplets.emplace_back(row,
137 col_offset_for_inner_difference + o * (inner_size - 1) + i + 1,
138 static_cast<scalar_type>(-1));
139 }
140 }
141
142 Matrix matrix(rows, cols);
143 matrix.setFromTriplets(triplets.begin(), triplets.end());
144 return matrix;
145}
146
147} // 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 sparse_div_matrix_2d_with_boundaries(num_collect::index_type outer_size, num_collect::index_type inner_size) -> Matrix
Create a sparse matrix for 2D divergence with boundaries.
auto sparse_div_matrix_2d(num_collect::index_type outer_size, num_collect::index_type inner_size) -> Matrix
Create a sparse matrix for 2D divergence.
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.
Definition of sparse_matrix concept.