15 View<Scalar**, Kokkos::LayoutRight, MemorySpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
19 View<Scalar*, Kokkos::LayoutRight, MemorySpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
61 SubmatrixView
const& submatrix,
62 MatrixView
const& matrix,
63 RowIds
const& row_ids,
64 ColIds
const& col_ids)
66 assert(submatrix.extent(0) == row_ids.size() && submatrix.extent(1) == col_ids.size());
67 for (std::size_t i = 0; i < row_ids.size(); ++i) {
68 for (std::size_t j = 0; j < col_ids.size(); ++j) {
69 submatrix(i, j) = matrix(row_ids[i], col_ids[j]);
75KOKKOS_FUNCTION
typename MatrixView::non_const_value_type
determinant(MatrixView
const& matrix)
77 assert(matrix.extent(0) == matrix.extent(1) &&
"Matrix should be square.");
78 if (matrix.extent(0) == 0) {
81 typename MatrixView::non_const_value_type det = 1.;
84 for (std::size_t i = 0; i < matrix.extent(0); ++i) {
85 std::size_t pivot = i;
86 auto pivot_abs = Kokkos::abs(matrix(i, i));
87 for (std::size_t row = i + 1; row < matrix.extent(0); ++row) {
88 auto const candidate_abs = Kokkos::abs(matrix(row, i));
89 if (candidate_abs > pivot_abs) {
91 pivot_abs = candidate_abs;
95 if (pivot_abs == 0.) {
101 for (std::size_t col = 0; col < matrix.extent(1); ++col) {
102 Kokkos::kokkos_swap(matrix(i, col), matrix(pivot, col));
106 auto const diagonal = matrix(i, i);
108 for (std::size_t row = i + 1; row < matrix.extent(0); ++row) {
109 auto const factor = matrix(row, i) / diagonal;
110 for (std::size_t col = i + 1; col < matrix.extent(1); ++col) {
111 matrix(row, col) -= factor * matrix(i, col);
121 InverseView
const& inverse,
122 MatrixView
const& matrix,
123 WorkspaceView
const& workspace)
125 assert(inverse.extent(0) == inverse.extent(1) &&
"Inverse target should be square.");
126 assert(matrix.extent(0) == matrix.extent(1) &&
"Input matrix should be square.");
127 assert(inverse.extent(0) == matrix.extent(0) &&
"Input/output matrix sizes should match.");
128 assert(workspace.extent(0) >= matrix.extent(0) * matrix.extent(1) &&
"Workspace is too small.");
131 typename MatrixView::non_const_value_type,
132 typename MatrixView::
133 memory_space>(workspace.data(), matrix.extent(0), matrix.extent(1));
137 for (std::size_t i = 0; i < matrix.extent(0); ++i) {
138 std::size_t pivot = i;
139 auto pivot_abs = Kokkos::abs(matrix_work(i, i));
140 for (std::size_t row = i + 1; row < matrix.extent(0); ++row) {
141 auto const candidate_abs = Kokkos::abs(matrix_work(row, i));
142 if (candidate_abs > pivot_abs) {
144 pivot_abs = candidate_abs;
148 if (pivot_abs == 0.) {
149 for (std::size_t row = 0; row < inverse.extent(0); ++row) {
150 for (std::size_t col = 0; col < inverse.extent(1); ++col) {
151 inverse(row, col) = 0.;
158 for (std::size_t col = 0; col < matrix_work.extent(1); ++col) {
159 Kokkos::kokkos_swap(matrix_work(i, col), matrix_work(pivot, col));
160 Kokkos::kokkos_swap(inverse(i, col), inverse(pivot, col));
164 auto const diagonal = matrix_work(i, i);
165 for (std::size_t col = 0; col < matrix_work.extent(1); ++col) {
166 matrix_work(i, col) /= diagonal;
167 inverse(i, col) /= diagonal;
170 for (std::size_t row = 0; row < matrix_work.extent(0); ++row) {
174 auto const factor = matrix_work(row, i);
175 for (std::size_t col = 0; col < matrix_work.extent(1); ++col) {
176 matrix_work(row, col) -= factor * matrix_work(i, col);
177 inverse(row, col) -= factor * inverse(i, col);
187 MatrixView
const& matrix,
188 RowIds
const& row_ids,
189 ColIds
const& col_ids,
190 std::array<double, N * N>& submatrix_alloc)
192 if constexpr (N == 0) {
197 typename MatrixView::memory_space>(submatrix_alloc.data(), N, N);