From 8d0d32b3739670f917f4c4384c6a3753f1806dea Mon Sep 17 00:00:00 2001 From: David Feltell Date: Thu, 16 Dec 2021 23:27:37 +0000 Subject: [PATCH] Fix TensorMap compound assignment operators Compound assignment operators such as `+=` are provided where the source is a `TensorMap` and the destination is an `AbstractTensor`. However, since the destination tensor was const-qualified, use of these operators resulted in a compile error. --- Fastor/tensor/TensorMap.h | 8 +++---- tests/test_tensormap/test_tensormap.cpp | 30 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Fastor/tensor/TensorMap.h b/Fastor/tensor/TensorMap.h index 1367a6b..5b2a452 100644 --- a/Fastor/tensor/TensorMap.h +++ b/Fastor/tensor/TensorMap.h @@ -148,19 +148,19 @@ FASTOR_INLINE void assign(AbstractTensor &dst, const TensorMap -FASTOR_INLINE void assign_add(const AbstractTensor &dst, const TensorMap &src) { +FASTOR_INLINE void assign_add(AbstractTensor &dst, const TensorMap &src) { trivial_assign_add(dst.self(),src); } template -FASTOR_INLINE void assign_sub(const AbstractTensor &dst, const TensorMap &src) { +FASTOR_INLINE void assign_sub(AbstractTensor &dst, const TensorMap &src) { trivial_assign_sub(dst.self(),src); } template -FASTOR_INLINE void assign_mul(const AbstractTensor &dst, const TensorMap &src) { +FASTOR_INLINE void assign_mul(AbstractTensor &dst, const TensorMap &src) { trivial_assign_mul(dst.self(),src); } template -FASTOR_INLINE void assign_div(const AbstractTensor &dst, const TensorMap &src) { +FASTOR_INLINE void assign_div(AbstractTensor &dst, const TensorMap &src) { trivial_assign_div(dst.self(),src); } diff --git a/tests/test_tensormap/test_tensormap.cpp b/tests/test_tensormap/test_tensormap.cpp index 5a59f24..ba94963 100644 --- a/tests/test_tensormap/test_tensormap.cpp +++ b/tests/test_tensormap/test_tensormap.cpp @@ -55,6 +55,36 @@ void run() { FASTOR_EXIT_ASSERT(std::abs(a.sum() - ma.sum()) < Tol); } + // Compound assignment operators. + { + Tensor src; + src = 3; + TensorMap msrc(src); + Tensor dst; + dst = 0; + Tensor check; + + // += + check = 3; + dst += msrc; + FASTOR_EXIT_ASSERT(all_of(dst == check)); + + // *= + check = 9; + dst *= msrc; + FASTOR_EXIT_ASSERT(all_of(dst == check)); + + // -= + check = 6; + dst -= msrc; + FASTOR_EXIT_ASSERT(all_of(dst == check)); + + // /= + check = 2; + dst /= msrc; + FASTOR_EXIT_ASSERT(all_of(dst == check)); + } + // Map a const array and copy-assign it to a non-const tensor. { const T data[] = {1, 2, 3};