numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
laplacian_2d_grid.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 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 <cstddef>
24#include <vector>
25
26#include <Eigen/SparseCore>
27
31
33
41template <num_collect::base::concepts::sparse_matrix Matrix>
43public:
45 using matrix_type = Matrix;
46
48 using scalar_type = typename Matrix::Scalar;
49
71
77 [[nodiscard]] auto grid_rows() const noexcept -> num_collect::index_type {
78 return grid_rows_;
79 }
80
86 [[nodiscard]] auto grid_cols() const noexcept -> num_collect::index_type {
87 return grid_cols_;
88 }
89
95 [[nodiscard]] auto mat_size() const noexcept -> num_collect::index_type {
96 return mat_size_;
97 }
98
104 [[nodiscard]] auto diag_coeff() const noexcept -> scalar_type {
105 return diag_coeff_;
106 }
107
113 [[nodiscard]] auto off_diag_coeff() const noexcept -> scalar_type {
114 return off_diag_coeff_;
115 }
116
122 [[nodiscard]] auto mat() const noexcept -> const Matrix& { return mat_; }
123
131 [[nodiscard]] auto index(num_collect::index_type x,
133 return x + grid_cols_ * y;
134 }
135
136private:
140 void calc() {
141 const scalar_type inv_area =
142 static_cast<scalar_type>(1) / (grid_width_ * grid_width_);
143 // NOLINTNEXTLINE
144 diag_coeff_ = static_cast<scalar_type>(8) /
145 static_cast<scalar_type>(3) * inv_area;
146 // NOLINTNEXTLINE
147 off_diag_coeff_ = -inv_area / static_cast<scalar_type>(3);
148
149 std::vector<Eigen::Triplet<scalar_type>> triplets;
150 const std::size_t approx_elements =
151 9U * static_cast<std::size_t>(mat_size_);
152 triplets.reserve(approx_elements);
153 for (num_collect::index_type xi = 0; xi < grid_cols_; ++xi) {
154 for (num_collect::index_type yi = 0; yi < grid_rows_; ++yi) {
155 const num_collect::index_type i = index(xi, yi);
157 xj = std::max<num_collect::index_type>(xi - 1, 0),
158 xj_end = std::min<num_collect::index_type>(
159 xi + 2, grid_cols_);
160 xj < xj_end; ++xj) {
162 yj = std::max<num_collect::index_type>(yi - 1, 0),
163 yj_end = std::min<num_collect::index_type>(
164 yi + 2, grid_rows_);
165 yj < yj_end; ++yj) {
166 const num_collect::index_type j = index(xj, yj);
167 const scalar_type coeff =
168 (i == j) ? diag_coeff_ : off_diag_coeff_;
169 triplets.emplace_back(
170 static_cast<int>(i), static_cast<int>(j), coeff);
171 }
172 }
173 }
174 }
175
176 mat_.setFromTriplets(triplets.begin(), triplets.end());
177 }
178
181
184
187
190
193
196
198 Matrix mat_;
199};
200
201} // namespace num_prob_collect::linear
Definition of assertion macros.
#define NUM_COLLECT_ASSERT(CONDITION)
Macro to check whether a condition is satisfied.
Definition assert.h:66
Class to create matrices of Laplacian on a 2-dimensional grid.
auto grid_cols() const noexcept -> num_collect::index_type
Get the number of columns in the grid.
typename Matrix::Scalar scalar_type
Type of the scalars.
auto diag_coeff() const noexcept -> scalar_type
Get the coefficient of diagonal elements.
num_collect::index_type mat_size_
Number of rows in the Laplacian matrix.
auto off_diag_coeff() const noexcept -> scalar_type
Get the coefficient of off-diagonal elements.
auto grid_rows() const noexcept -> num_collect::index_type
Get the number of rows in the grid.
num_collect::index_type grid_rows_
Number of rows in the grid.
auto index(num_collect::index_type x, num_collect::index_type y) const noexcept -> num_collect::index_type
Calculate an index in the matrix.
scalar_type grid_width_
Width of squares in the grid.
scalar_type diag_coeff_
Coefficient of diagonal elements.
num_collect::index_type grid_cols_
Number of columns in the grid.
auto mat() const noexcept -> const Matrix &
Get the Laplacian matrix.
scalar_type off_diag_coeff_
Coefficient of off-diagonal elements.
laplacian_2d_grid(num_collect::index_type grid_rows, num_collect::index_type grid_cols, typename Matrix::value_type grid_width)
Constructor.
auto mat_size() const noexcept -> num_collect::index_type
Get the size of the matrix.
Definition of index_type type.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of num_collect source codes.
Namespace of linear equations.
Definition namespaces.h:42
Definition of sparse_matrix concept.