From: Matthias Maier Date: Wed, 16 Sep 2015 20:12:58 +0000 (-0500) Subject: Generalize "scalar_product", deprecate remaining functions X-Git-Tag: v8.4.0-rc2~385^2~8 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=94b7cd68c59762dbc3f021a129c8806dedf55fdf;p=dealii.git Generalize "scalar_product", deprecate remaining functions --- diff --git a/include/deal.II/base/tensor.h b/include/deal.II/base/tensor.h index 535b6596e3..031f1cd5e3 100644 --- a/include/deal.II/base/tensor.h +++ b/include/deal.II/base/tensor.h @@ -1442,7 +1442,7 @@ operator- (const Tensor &p, const Tensor //@} /** - * @name Contraction operations on Tensors + * @name Contraction operations and the outer product for tensor objects */ //@{ @@ -1468,6 +1468,7 @@ operator- (const Tensor &p, const Tensor * number is returned as an unwrapped number type. * * @relates Tensor + * @author Matthias Maier, 2015 */ template @@ -1487,7 +1488,32 @@ operator * (const Tensor &src1, /** - * Full contraction of three tensors: Return a scalar Number that is the + * The scalar product, or (generalized) Frobenius inner product of two + * tensors of equal rank: Return a scalar number that is the result of a + * full contraction of a tensor @p left and @p right: + * @f[ + * \sum_{i_1,..,i_{r} + * \text{left}_{i_1,..,i_r} + * \text{right}_{i_1,..,i_r} + * @f] + * + * @relates Tensor + * @author Matthias Maier, 2015 + */ +template +inline +typename ProductType::type +scalar_product (const Tensor &left, + const Tensor &right) +{ + typename ProductType::type result; + TensorAccessors::contract(result, left, right); + return result; +} + + +/** + * Full contraction of three tensors: Return a scalar number that is the * result of a full contraction of a tensor @p left of rank @p rank_1, a * tensor @p middle of rank $(\text{rank_1}+\text{rank_2})$ and a tensor @p * right of rank @p rank_2: @@ -1524,6 +1550,7 @@ contract3 (const Tensor &left, * @f] * * @relates Tensor + * @author Matthias Maier, 2015 */ template @@ -1538,300 +1565,6 @@ outer_product(const Tensor &src1, } -//@} -/** - * @name To be refactored - */ -//@{ - - -/** - * Double contract two tensors of rank 2, thus computing the Frobenius inner - * product sumi,j src1[i][j]*src2[i][j]. - * - * @relates Tensor - * @author Guido Kanschat, 2000 - */ -template -inline -Number double_contract (const Tensor<2, dim, Number> &src1, - const Tensor<2, dim, Number> &src2) -{ - Number res = 0.; - for (unsigned int i=0; iindex1 of the first tensor, and - * index2 of the second tensor. Thus, if index1==2, - * index2==1, the result is the usual contraction, but if for example - * index1==1, index2==2, then the result is dest[i][k] = - * sum_j src1[j][i] src2[k][j]. - * - * Note that the number of the index is counted from 1 on, not from zero as - * usual. - * - * @relates Tensor - * @author Wolfgang Bangerth, 1998 - */ -template -inline -void contract (Tensor<2,dim,Number> &dest, - const Tensor<2,dim,Number> &src1, const unsigned int index1, - const Tensor<2,dim,Number> &src2, const unsigned int index2) -{ - dest.clear (); - - switch (index1) - { - case 1: - switch (index2) - { - case 1: - for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index2))); - }; - break; - case 2: - switch (index2) - { - case 1: - for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index2))); - }; - break; - - default: - Assert (false, (typename Tensor<2,dim,Number>::ExcInvalidTensorContractionIndex (index1))); - }; -} - - -/** - * Contract a tensor of rank 3 with a tensor of rank 1. The contraction is - * performed over index index1 of the first tensor. - * - * Note that the number of the index is counted from 1 on, not from zero as - * usual. - * - * @relates Tensor - * @author Wolfgang Bangerth, 1998 - */ -template -inline -void contract (Tensor<2,dim,Number> &dest, - const Tensor<3,dim,Number> &src1, const unsigned int index1, - const Tensor<1,dim,Number> &src2) -{ - dest.clear (); - - switch (index1) - { - case 1: - for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index1))); - }; -} - - -/** - * Contract a tensor of rank 3 with a tensor of rank 2. The contraction is - * performed over index index1 of the first tensor, and - * index2 of the second tensor. Thus, if index1==3, - * index2==1, the result is the usual contraction, but if for example - * index1==1, index2==2, then the result is - * dest[i][j][k] = sum_l src1[l][i][j] src2[k][l]. - * - * Note that the number of the index is counted from 1 on, not from zero as - * usual. - * - * @relates Tensor - */ -template -inline -void contract (Tensor<3,dim,Number> &dest, - const Tensor<3,dim,Number> &src1, const unsigned int index1, - const Tensor<2,dim,Number> &src2, const unsigned int index2) -{ - dest.clear (); - - switch (index1) - { - case 1: - switch (index2) - { - case 1: - for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index2))); - } - - break; - case 2: - switch (index2) - { - case 1: - for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index2))); - } - - break; - case 3: - switch (index2) - { - case 1: - for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index2))); - } - - break; - default: - Assert (false, - (typename Tensor<3,dim,Number>::ExcInvalidTensorContractionIndex (index1))); - } -} - - -/** - * Contract the last two indices of src1 with the two indices - * src2, creating a rank-2 tensor. This is the matrix-vector product - * analog operation between tensors of rank 4 and rank 2. - * - * @relates Tensor - * @author Wolfgang Bangerth, 2005 - */ -template -inline -void double_contract (Tensor<2,dim,Number> &dest, - const Tensor<4,dim,Number> &src1, - const Tensor<2,dim,Number> &src2) -{ - dest.clear (); - for (unsigned int i=0; ioperator* for this - * operation since the product between two tensors is usually assumed to be - * the contraction over the last index of the first tensor and the first index - * of the second tensor, for example $(a\cdot b)_{ij}=\sum_k a_{ik}b_{kj}$. - * - * @relates Tensor - * @author Wolfgang Bangerth, 2008 - */ -template -inline -Number -scalar_product (const Tensor<2,dim,Number> &t1, - const Tensor<2,dim,Number> &t2) -{ - Number s = 0; - for (unsigned int i=0; i &t) DEAL_II_NAMESPACE_CLOSE // include deprecated non-member functions operating on Tensor -#include +// #include #endif diff --git a/include/deal.II/base/tensor_deprecated.h b/include/deal.II/base/tensor_deprecated.h index 3c3fef6da9..6fdd0d897d 100644 --- a/include/deal.II/base/tensor_deprecated.h +++ b/include/deal.II/base/tensor_deprecated.h @@ -28,6 +28,88 @@ DEAL_II_NAMESPACE_OPEN */ //@{ + +/** + * Double contract two tensors of rank 2, thus computing the Frobenius inner + * product sumi,j src1[i][j]*src2[i][j]. + * + * @deprecated Use the contract function that takes indices as template + * arguments and returns its result instead. + * @relates Tensor + */ +template +inline +Number double_contract (const Tensor<2, dim, Number> &src1, + const Tensor<2, dim, Number> &src2) DEAL_II_DEPRECATED; + + +/** + * Contract the last two indices of src1 with the two indices + * src2, creating a rank-2 tensor. This is the matrix-vector product + * analog operation between tensors of rank 4 and rank 2. + * + * @deprecated Use the contract function that takes indices as template + * arguments and returns its result instead. + * @relates Tensor + */ +template +inline +void double_contract (Tensor<2,dim,Number> &dest, + const Tensor<4,dim,Number> &src1, + const Tensor<2,dim,Number> &src2) DEAL_II_DEPRECATED; + +/** + * Contract a tensor of rank 2 with a tensor of rank 2. The contraction is + * performed over index index1 of the first tensor, and + * index2 of the second tensor. Note that the number of the index + * is counted from 1 on, not from zero as usual. + * + * @deprecated Use the contract function that takes indices as template + * arguments and returns its result instead. + * @relates Tensor + */ +template +inline +void contract (Tensor<2,dim,Number> &dest, + const Tensor<2,dim,Number> &src1, + const unsigned int index1, + const Tensor<2,dim,Number> &src2, + const unsigned int index3) DEAL_II_DEPRECATED; + +/** + * Contract a tensor of rank 3 with a tensor of rank 1. The contraction is + * performed over index index1 of the first tensor. Note that the + * number of the index is counted from 1 on, not from zero as usual. + * + * @deprecated Use the contract function that takes indices as template + * arguments and returns its result instead. + * @relates Tensor + */ +template +inline +void contract (Tensor<2,dim,Number> &dest, + const Tensor<3,dim,Number> &src1, + const unsigned int index1, + const Tensor<1,dim,Number> &src2) DEAL_II_DEPRECATED; + +/** + * Contract a tensor of rank 3 with a tensor of rank 2. The contraction is + * performed over index index1 of the first tensor, and + * index2 of the second tensor. Note that the number of the index + * is counted from 1 on, not from zero as usual. + * + * @deprecated Use the contract function that takes indices as template + * arguments and returns its result instead. + * @relates Tensor + */ +template +inline +void contract (Tensor<3,dim,Number> &dest, + const Tensor<3,dim,Number> &src1, + const unsigned int index1, + const Tensor<2,dim,Number> &src2, + const unsigned int index2) DEAL_II_DEPRECATED; + /** * Single contraction for tensors: contract the last index of a tensor @p * src1 of rank @p rank_1 with the first index of a tensor @p src2 of rank @@ -125,6 +207,212 @@ Number determinant (const Tensor<1,1,Number> &t) DEAL_II_DEPRECATED; /* ----------------------------- Definitions: ------------------------------- */ +template +inline +Number double_contract (const Tensor<2, dim, Number> &src1, + const Tensor<2, dim, Number> &src2) +{ + Number res = 0.; + for (unsigned int i=0; i +inline +void double_contract (Tensor<2,dim,Number> &dest, + const Tensor<4,dim,Number> &src1, + const Tensor<2,dim,Number> &src2) +{ + dest.clear (); + for (unsigned int i=0; i +inline +void contract (Tensor<2,dim,Number> &dest, + const Tensor<2,dim,Number> &src1, const unsigned int index1, + const Tensor<2,dim,Number> &src2, const unsigned int index2) +{ + dest.clear (); + + switch (index1) + { + case 1: + switch (index2) + { + case 1: + for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index2))); + }; + break; + case 2: + switch (index2) + { + case 1: + for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index2))); + }; + break; + + default: + Assert (false, (typename Tensor<2,dim,Number>::ExcInvalidTensorContractionIndex (index1))); + }; +} + +template +inline +void contract (Tensor<2,dim,Number> &dest, + const Tensor<3,dim,Number> &src1, const unsigned int index1, + const Tensor<1,dim,Number> &src2) +{ + dest.clear (); + + switch (index1) + { + case 1: + for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index1))); + }; +} + +template +inline +void contract (Tensor<3,dim,Number> &dest, + const Tensor<3,dim,Number> &src1, const unsigned int index1, + const Tensor<2,dim,Number> &src2, const unsigned int index2) +{ + dest.clear (); + + switch (index1) + { + case 1: + switch (index2) + { + case 1: + for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index2))); + } + + break; + case 2: + switch (index2) + { + case 1: + for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index2))); + } + + break; + case 3: + switch (index2) + { + case 1: + for (unsigned int i=0; i::ExcInvalidTensorContractionIndex (index2))); + } + + break; + default: + Assert (false, + (typename Tensor<3,dim,Number>::ExcInvalidTensorContractionIndex (index1))); + } +} + template inline void contract (Tensor &dest, diff --git a/tests/base/symmetric_tensor_07.cc b/tests/base/symmetric_tensor_07.cc index 2ca7ba0aac..83c31a9612 100644 --- a/tests/base/symmetric_tensor_07.cc +++ b/tests/base/symmetric_tensor_07.cc @@ -56,7 +56,8 @@ void test () as[i][j] = aa[i][j] = (1. + (i+1)*(j+1)); bs = ts * as; - double_contract (ba, ta, aa); + // contract indices 2 <-> 0, 3 <-> 1 + ba = contract<2, 0, 3, 1>(ta, aa); for (unsigned int i=0; i,Tensor<2,dim>) +// check scalar_product(Tensor<2,dim>,Tensor<2,dim>) #include "../tests.h" #include @@ -30,7 +30,7 @@ void test_constant() for (unsigned int i=0; i