Note: this is documentation for an old release. View the latest documentation at docs.fenicsproject.org/basix/v0.9.0/cpp/precompute_8h_source.html
Home Installation C++ docs Python docs
precompute.h
1 // Copyright (c) 2020 Matthew Scroggs
2 // FEniCS Project
3 // SPDX-License-Identifier: MIT
4 
5 #pragma once
6 
7 #include <vector>
8 #include <xtensor/xtensor.hpp>
9 #include <xtensor/xview.hpp>
10 #include <xtl/xspan.hpp>
11 
16 {
17 
66 std::vector<std::size_t>
67 prepare_permutation(const std::vector<std::size_t>& perm);
68 
117 template <typename E>
118 void apply_permutation(const std::vector<std::size_t>& perm,
119  const xtl::span<E>& data, std::size_t offset = 0,
120  std::size_t block_size = 1)
121 {
122  for (std::size_t b = 0; b < block_size; ++b)
123  {
124  for (std::size_t i = 0; i < perm.size(); ++i)
125  {
126  std::swap(data[block_size * (offset + i) + b],
127  data[block_size * (offset + perm[i]) + b]);
128  }
129  }
130 }
131 
135 template <typename E>
136 void apply_permutation_to_transpose(const std::vector<std::size_t>& perm,
137  const xtl::span<E>& data,
138  std::size_t offset = 0,
139  std::size_t block_size = 1)
140 {
141  const std::size_t dim = perm.size();
142  const std::size_t data_size = data.size() / block_size;
143  for (std::size_t b = 0; b < block_size; ++b)
144  {
145  for (std::size_t i = 0; i < dim; ++i)
146  {
147  std::swap(data[data_size * b + offset + i],
148  data[data_size * b + offset + perm[i]]);
149  }
150  }
151 }
152 
246 std::tuple<std::vector<std::size_t>, std::vector<double>,
247  xt::xtensor<double, 2>>
248 prepare_matrix(const xt::xtensor<double, 2>& matrix);
249 
310 template <typename T, typename E>
311 void apply_matrix(const std::tuple<std::vector<std::size_t>, std::vector<T>,
312  xt::xtensor<T, 2>>& matrix,
313  const xtl::span<E>& data, std::size_t offset = 0,
314  std::size_t block_size = 1)
315 {
316  const std::vector<std::size_t>& v_size_t = std::get<0>(matrix);
317  const std::vector<T>& v_t = std::get<1>(matrix);
318  const xt::xtensor<T, 2>& M = std::get<2>(matrix);
319 
320  const std::size_t dim = v_size_t.size();
321  apply_permutation(v_size_t, data, offset, block_size);
322  for (std::size_t b = 0; b < block_size; ++b)
323  {
324  for (std::size_t i = 0; i < dim; ++i)
325  {
326  data[block_size * (offset + i) + b] *= v_t[i];
327  for (std::size_t j = 0; j < dim; ++j)
328  {
329  data[block_size * (offset + i) + b]
330  += M(i, j) * data[block_size * (offset + j) + b];
331  }
332  }
333  }
334 }
335 
339 template <typename T, typename E>
341  const std::tuple<std::vector<std::size_t>, std::vector<T>,
342  xt::xtensor<T, 2>>& matrix,
343  const xtl::span<E>& data, std::size_t offset = 0,
344  std::size_t block_size = 1)
345 {
346  const std::vector<std::size_t>& v_size_t = std::get<0>(matrix);
347  const std::vector<T>& v_t = std::get<1>(matrix);
348  const xt::xtensor<T, 2>& M = std::get<2>(matrix);
349 
350  const std::size_t dim = v_size_t.size();
351  const std::size_t data_size = data.size() / block_size;
352  apply_permutation_to_transpose(v_size_t, data, offset, block_size);
353  for (std::size_t b = 0; b < block_size; ++b)
354  {
355  for (std::size_t i = 0; i < dim; ++i)
356  {
357  data[data_size * b + offset + i] *= v_t[i];
358  for (std::size_t j = 0; j < dim; ++j)
359  {
360  data[data_size * b + offset + i]
361  += M(i, j) * data[data_size * b + offset + j];
362  }
363  }
364  }
365 }
366 
367 } // namespace basix::precompute
basix::precompute::prepare_permutation
std::vector< std::size_t > prepare_permutation(const std::vector< std::size_t > &perm)
Definition: precompute.cpp:12
basix::precompute::apply_permutation_to_transpose
void apply_permutation_to_transpose(const std::vector< std::size_t > &perm, const xtl::span< E > &data, std::size_t offset=0, std::size_t block_size=1)
Definition: precompute.h:136
basix::precompute::prepare_matrix
std::tuple< std::vector< std::size_t >, std::vector< double >, xt::xtensor< double, 2 > > prepare_matrix(const xt::xtensor< double, 2 > &matrix)
Definition: precompute.cpp:27
basix::precompute::apply_permutation
void apply_permutation(const std::vector< std::size_t > &perm, const xtl::span< E > &data, std::size_t offset=0, std::size_t block_size=1)
Definition: precompute.h:118
basix::precompute::apply_matrix_to_transpose
void apply_matrix_to_transpose(const std::tuple< std::vector< std::size_t >, std::vector< T >, xt::xtensor< T, 2 >> &matrix, const xtl::span< E > &data, std::size_t offset=0, std::size_t block_size=1)
Definition: precompute.h:340
basix::precompute::apply_matrix
void apply_matrix(const std::tuple< std::vector< std::size_t >, std::vector< T >, xt::xtensor< T, 2 >> &matrix, const xtl::span< E > &data, std::size_t offset=0, std::size_t block_size=1)
Definition: precompute.h:311
basix::precompute
Definition: precompute.h:15