RealTimeTransport 1.0.0
Real-time simulation of quantum transport processes
Loading...
Searching...
No Matches
BlockHelper.h
1//
2// This Source Code Form is subject to the terms of the Mozilla Public
3// License, v. 2.0. If a copy of the MPL was not distributed with this
4// file, You can obtain one at https://mozilla.org/MPL/2.0/.
5//
6
7#ifndef REAL_TIME_TRANSPORT_BLOCK_MATRICES_BLOCK_HELPER_H
8#define REAL_TIME_TRANSPORT_BLOCK_MATRICES_BLOCK_HELPER_H
9
10#include <set>
11#include <vector>
12
13#include <SciCore/Definitions.h>
14
15namespace RealTimeTransport
16{
17
18template <typename FuncT>
19void foreachCommonElement(FuncT&& f, const std::set<int>& x, const std::set<int>& y)
20{
21 auto it_x = x.begin();
22 auto it_y = y.begin();
23
24 while (it_x != x.end() && it_y != y.end())
25 {
26 if (*it_x < *it_y)
27 {
28 ++it_x;
29 }
30 else if (*it_y < *it_x)
31 {
32 ++it_y;
33 }
34 else
35 {
36 // Found a common element
37 f(*it_x);
38 ++it_x;
39 ++it_y;
40 }
41 }
42}
43
44// Helper function to extract the block indexed by i, j from a dense matrix
45template <typename MatrixT>
46MatrixT extractDenseBlock(const MatrixT& A, int i, int j, const std::vector<int>& blockDims)
47{
48 // Calculate the start row and column
49 int startRow = 0;
50 for (int k = 0; k < i; ++k)
51 {
52 startRow += blockDims[k];
53 }
54
55 int startCol = 0;
56 for (int k = 0; k < j; ++k)
57 {
58 startCol += blockDims[k];
59 }
60
61 // Determine the size of the block
62 int numRows = blockDims[i];
63 int numCols = blockDims[j];
64
65 // Extract and return the block
66 return A.block(startRow, startCol, numRows, numCols);
67}
68
69template <typename MatrixT>
70bool isBlockDiagonal(const MatrixT& matrix, const std::vector<int>& blockDimensions)
71{
72 int currentRow = 0;
73 int numBlocks = static_cast<int>(blockDimensions.size());
74 for (int i = 0; i < numBlocks; ++i)
75 {
76 int currentCol = 0;
77 for (int j = 0; j < numBlocks; ++j)
78 {
79 // If not the same block (diagonal block), check if the block is zero
80 if (i != j)
81 {
82 auto block = matrix.block(currentRow, currentCol, blockDimensions[i], blockDimensions[j]);
83
84 // If any element in the off-diagonal block is non-zero, return false
85 if (block.isZero() == false)
86 {
87 return false;
88 }
89 }
90 currentCol += blockDimensions[j];
91 }
92 currentRow += blockDimensions[i];
93 }
94 return true;
95}
96
97} // namespace RealTimeTransport
98
99#endif // REAL_TIME_TRANSPORT_BLOCK_MATRICES_BLOCK_HELPER_H