IgANet
IGAnets - Isogeometric Analysis Networks
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <core/core.hpp>
18#include <utils/tensorarray.hpp>
19
20namespace iganet::utils {
21
30 inline torch::Tensor to_sparseCsrTensor(const torch::Tensor& col_indices,
31 const torch::Tensor& values,
32 const torch::IntArrayRef& size) {
33
34 // Compute cumulated row indices
35 auto crow_indices = torch::arange(0, col_indices.size(0)+1, 1, torch::kInt64) * col_indices.size(1);
36
37 // Append last entry if required
38 if (crow_indices.size(0) < size[0]) {
39 auto last = crow_indices[-1];
40 auto pad = last.repeat({size[0] - crow_indices.size(0)});
41 crow_indices = torch::cat({crow_indices, pad});
42 }
43
44 return torch::sparse_csr_tensor(crow_indices.flatten(),
45 col_indices.flatten(),
46 values.flatten(),
47 size, values.options().layout(torch::Layout::SparseCsr));
48 }
49
60 template<std::size_t N>
61 inline torch::Tensor to_sparseCsrTensor(const utils::TensorArray<N>& col_indices,
62 const std::array<int64_t, N>& nbasfuncs,
63 const torch::Tensor& values,
64 const torch::IntArrayRef& size) {
65
66 // Compute absolut column indices
67 torch::Tensor col_indices_;
68 if constexpr (N == 1)
69 col_indices_ = col_indices[0];
70 else if constexpr (N == 2)
71 col_indices_ = (col_indices[0].unsqueeze(2) +
72 nbasfuncs[0]*col_indices[1].unsqueeze(1))
73 .permute({0, 2, 1}).reshape({col_indices[0].size(0), -1});
74 else if constexpr (N == 3)
75 col_indices_ = (col_indices[0].unsqueeze(2).unsqueeze(3) +
76 nbasfuncs[0]*col_indices[1].unsqueeze(1).unsqueeze(3) +
77 nbasfuncs[0]*nbasfuncs[1]*col_indices[2].unsqueeze(1).unsqueeze(2))
78 .permute({0, 3, 2, 1}).reshape({col_indices[0].size(0), -1});
79 else
80 throw std::runtime_error("Invalid dimension");
81
82 return to_sparseCsrTensor(col_indices_, values, size);
83 }
84
99 template<std::size_t N>
100 inline torch::Tensor to_sparseCsrTensor(const utils::TensorArray<N>& knot_indices,
101 const std::array<short, N>& degrees,
102 const std::array<int64_t, N>& nbasfuncs,
103 const torch::Tensor& values,
104 const torch::IntArrayRef& size) {
105
106 // Apply offsets to indices
107 std::array<torch::Tensor, N> col_indices;
108 for (std::size_t i = 0; i < N; ++i) {
109 col_indices[i] =
110 (knot_indices[i].unsqueeze(0) +
111 torch::arange(-degrees[i],
112 1,
113 knot_indices[i].options()
114 ).unsqueeze(1)).t();
115 }
116
117 return to_sparseCsrTensor(col_indices, nbasfuncs, values, size);
118 }
119} // namespace iganet::utils
Core components.
Definition blocktensor.hpp:24
std::array< torch::Tensor, N > TensorArray
Definition tensorarray.hpp:26
torch::Tensor to_sparseCsrTensor(const torch::Tensor &col_indices, const torch::Tensor &values, const torch::IntArrayRef &size)
Constructs a sparse-CSR matrix from the column indices, matrix values and the matrix size.
Definition matrix.hpp:30
TensorArray utility functions.