7#ifndef DUNE_FUFEM_FORMS_TENSORS_HH
8#define DUNE_FUFEM_FORMS_TENSORS_HH
24namespace Dune::Fufem::Forms::Impl::Tensor {
41 template<
class T, std::
size_t k>
46 static decltype(
auto) resolveIndices(
auto&& t)
51 static decltype(
auto) resolveIndices(
auto&& t,
const auto& i0,
const auto&... i)
53 return resolveIndices(t[i0], i...);
56 template<
class I, I... i>
59 return resolveIndices(t, i...);
75 decltype(
auto)
operator()(
const auto&... i)
const
77 return resolveIndices(t_, i...);
81 template<
class T, std::
size_t k>
88 template<
class T, std::
size_t k>
96 using value_type =
typename RawT::value_type;
107 template<
class I0,
class... I>
109 decltype(
auto)
operator()(
const I0& i0,
const I&... i)
const
114 template<
class I0,
class I1,
class... I>
116 decltype(
auto)
operator()(
const I0& i0,
const I1& i1,
const I&... i)
const
122 template<
class T, std::
size_t k>
129 template<
class T, std::
size_t k>
136 using value_type =
typename RawT::value_type;
147 template<
class I0,
class... I>
149 decltype(
auto)
operator()(
const I0& i0,
const I&... i)
const
154 template<
class I0,
class I1,
class... I>
156 decltype(
auto)
operator()(
const I0& i0,
const I1& i1,
const I&... i)
const
162 template<
class T, std::
size_t k>
168 template<
class Value>
173 using value_type = Value;
180 RankZeroTensor(Value value)
185 friend void sparseForEach(
const RankZeroTensor& tensor, F&& f)
190 template<
class Outer>
191 friend auto compose(
const Outer& outer,
const RankZeroTensor& inner)
193 return Dune::Fufem::Forms::Impl::Tensor::RankZeroTensor(outer(inner()));
211 template<
class Value>
217 template<std::
size_t r,
class Value,
class ForEach>
235 friend void sparseForEach(
const SparseTensor& tensor, F&& f)
240 template<
class Outer>
241 friend auto compose(
const Outer& outer,
const SparseTensor& inner)
244 return Dune::Fufem::Forms::Impl::Tensor::SparseTensor(
246 [outer, innerForEach=inner.forEach_](
auto&& f) {
247 innerForEach([&](const auto& y_i, auto... i) {
266 template<std::
size_t r,
class Value,
class ForEach>
267 struct IsTensor<SparseTensor<r, Value, ForEach>> :
public std::true_type
270 template<std::
size_t r,
class Value,
class ForEach>
276 template<
class Product,
class T0,
class T1>
277 class SparseProductTensor
288 SparseProductTensor(Product product, T0 t0, T1 t1)
295 friend void sparseForEach(
const SparseProductTensor& tensor, F&& f)
298 if constexpr(T0::rank() == 0)
299 sparseForEach(tensor.t1_, [&](
const auto& x1_j,
auto... j) {
300 f(tensor.product_(tensor.t0_(), x1_j), j...);
302 else if constexpr(T1::rank() == 0)
303 sparseForEach(tensor.t0_, [&](
const auto& x0_i,
auto... i) {
304 f(tensor.product_(x0_i, tensor.t1_()), i...);
306 else if constexpr((T0::rank() == 2) and (T1::rank() == 2))
307 sparseForEach(tensor.t0_, [&](
const auto& x0_i,
auto i0,
auto i1) {
308 sparseForEach(tensor.t1_, [&](const auto& x1_j, auto j0, auto j1) {
309 f(tensor.product_(x0_i, x1_j), i0, j0, i1, j1);
316 template<
class Outer>
317 friend auto compose(
const Outer& outer,
const SparseProductTensor& inner)
320 return Dune::Fufem::Forms::Impl::Tensor::SparseProductTensor(
321 [outer, innerProduct=inner.product()](
const auto&... args) { return outer(innerProduct(args...)); },
329 return t0_.nnz() * t1_.nnz();
338 template<std::
size_t k>
343 else if constexpr(k==1)
353 template<
class Product,
class T0,
class T1>
354 struct IsTensor<SparseProductTensor<Product, T0, T1>> :
public std::true_type
357 template<
class Product,
class T0,
class T1>
358 struct IsLazyTensor<SparseProductTensor<Product, T0, T1>> :
public std::true_type
363 template<
class Product,
class X,
class Y>
364 requires (IsTensor<X>::value and IsTensor<Y>::value)
365 auto interleavedOuterProduct(Product product,
const X& x,
const Y& y)
368 if constexpr((X::rank() == 0) and (Y::rank() == 0))
369 return RankZeroTensor(
product(x(), y()));
371 return SparseProductTensor(product, x, y);
376 template<
class K, std::
size_t rank,
class Value,
class ForEach,
class Y>
379 and IsTensor<Y>::value
380 and (rank == Y::rank())
382 void axpy(
const K&
alpha,
const SparseTensor<rank, Value, ForEach>& x, Y& y)
384 sparseForEach(x, [&](
const auto& xi,
auto...i) {
389 template<
class K,
class Product,
class T0,
class T1,
class Y>
392 and IsTensor<Y>::value
393 and (SparseProductTensor<Product, T0, T1>::rank() == Y::rank())
395 void axpy(
const K&
alpha,
const SparseProductTensor<Product, T0, T1>& x, Y& y)
401 if constexpr(T0::rank() == 0)
402 sparseForEach(x.factor(_1), [&](
const auto& x1_j,
auto... j) {
405 else if constexpr(T1::rank() == 0)
406 sparseForEach(x.factor(_0), [&](
const auto& x0_i,
auto... i) {
409 else if constexpr((T0::rank() == 2) and (T1::rank() == 2))
410 sparseForEach(x.factor(_0), [&](
const auto& x0_i,
auto i0,
auto i1) {
411 auto x0_i_alpha = mult(x0_i,alpha);
412 sparseForEach(x.factor(_1), [&](const auto& x1_j, auto j0, auto j1) {
413 y(i0, j0, i1, j1) += Dune::Fufem::asNumber(x.product()(x0_i_alpha, x1_j));
417 static_assert((T0::rank() == 2) and (T1::rank() == 2));
void axpy(const Ta &a, const type &y)
auto operator()(T &&t) -> decltype(this->apply(t, std::index_sequence_for< Args... >{})) const
constexpr void forEach(Range &&range, F &&f)
constexpr T & resolveRef(T &gf) noexcept
virtual void operator()()=0
const T & asNumber(const T &t)
Forward given value as number.
Definition asnumber.hh:23