8
$\begingroup$

I am struggling with a few errors when using symbolic tensors. I am using mathematica 9.0.1.0, linux x86.

The following code generates what seems to me an incorrect tensor, this is the smallest example I could find:

$Assumptions = {V ∈ Vectors[3, Reals]};

tp = TensorProduct;

TensorContract[
  V~tp~TensorContract[V~tp~V~tp~V~tp~V~tp~V~tp~V, {{1, 3}, {2, 6}}]~tp~V, 
  {{1, 2}, {3, 4}}
] // TensorExpand

This generates the error:

TensorContract::lvreps: Contractions {{2,4},{3,7},{1,4},{6,8}} contain repeated levels.

Where I think the result should have been:

TensorContract[
  V~tp~V~tp~V~tp~V~tp~V~tp~V~tp~V~tp~V,
  {{2, 4}, {3,7}, {1, 5}, {6, 8}}
] 

Next it what seems like a bug:

$Assumptions =  {
  R ∈ Matrices[{3, 3}, Reals, Symmetric[{1, 2}]], 
  F ∈ Matrices[{3, 3}], 
  Inverse[R] ∈ Matrices[{3, 3}, Reals, Symmetric[{1, 2}]], 
  Inverse[F] ∈ Matrices[{3, 3}]
};

F.Inverse[R.F] // TensorReduce

The message generated was

Mathematica has detected an internal error: vMessage ENULL

The answer should have been

Inverse[R]

I emailed support and they answered that this error is not generated on a windows machine in Mathematica 10. I thought I might just log it here.

These two errors together have made it impossible for me to use symbolic tensors. If any one can please suggest workarounds, or comment if these same errors where generated in version 10 on a Linux computer.

Related post on using symbolic inverse, though it is not a complete fix as it generates IdentityMatrix[n] which is incompatible with symbolic tensor manipulation.

$\endgroup$
3
  • $\begingroup$ I think you should mail support again concerning the first error -- I'm seeing the same on 10.0.1 on both OS X and the cloud. It looks a bug to me. As for the inverse error message, I don't see it after I clean up your $Assumptions code (there were some improperly pasted characters that I removed); it just gives me F.MatrixPower[R.F, -1]. $\endgroup$ – Teake Nutma Oct 10 '14 at 11:17
  • $\begingroup$ Yes now I will email support. I also still get the error for the inverse matrix, but perhaps it is restricted to version 9. Thanks for the clean up. $\endgroup$ – Artur Gower Oct 10 '14 at 12:49
  • $\begingroup$ After upgrading to Mathematica 10.1 the above bugs and errors have been solved. Only detail being that F.MatrixPower[R.F, -1] does not simplify to MatrixPower[R,-1] as it should (with the correct assumptions). $\endgroup$ – Artur Gower Apr 25 '15 at 11:20
2
$\begingroup$

As I see no clear pattern to the errors caused by TensorExpand, we can not pick out the special cases that case this error and just avoid them. So instead I choose to implement my own function TensorReduceContract to reduce tensor contractions. It is used like this:

tp = TensorProduct;

$Assumptions = {m  ∈ Arrays[ {3, 3, 3, 3, 3, 3, 3, 3}, Reals], n  ∈ Arrays[ {3, 3}, Reals], p  ∈ Arrays[ {3, 3}, Reals]};

TensorContract[n~tp~TensorContract[m, {{2, 4}, {3, 5}, {6, 8}}]~tp~p,{{2, 5}, {4, 6}}] // TensorReduceContract

>Out = TensorContract[ n~tp~m~tp~p, {{2, 11}, {4, 6}, {5, 7}, {8,10}, {9, 12}}] 

Rather than

TensorContract[n~tp~TensorContract[m, list2]~tp~p, list1] // TensorExpand

> Out = TensorContract::lvreps: Contractions {{4,6},{5,7},{8,10},{2,11},{6,12}} contain repeated levels. 

> Out= TensorContract[ n~tp~m~tp~p, {{4, 6}, {5, 7}, {8, 10}, {2,11}, {6, 12}}]

Tensor contraction is defined here. Here is how to code TensorReduceContract:

TensorReduceContract[tensor_] := tensor //. subTensorReduceContract

subTensorReduceContract = {
TensorContract[
 n_~tp~TensorContract[m_, list1_]~tp~p_,list2_] :> TensorContract[n~tp~m~tp~p,
  Sort[Join[ 
   list1 + TensorRank[n],
   Map[
    Complement[ 
       Range[TensorRank[n~tp~m~tp~p]], 
       Flatten[list1 + TensorRank[n]] ][[#]] &, list2]
   ]]
  ] 
}; 

Finally, the quality check below shows that TensorExpand almost never differs (after millions of trials) from TensorReduceContract. Here is a randomized check:

setRandomContraction[rank_]:=
(
setContract= RandomChoice[Range[rank], RandomChoice[Range[rank]] ]//Intersection; 
setContract=Drop[setContract, Length[setContract]~Mod~2];
If[Length[setContract]>1,ArrayReshape[setContract,  {Length[setContract]/2,2}],setContract ]
);

tensorRanks =ConstantArray[3,#]&/@Range[20];

Do[ $Assumptions=(# ∈ Arrays[ RandomChoice[tensorRanks],Reals])&/@{n,m,p};

    setContractm = setRandomContraction[ TensorRank[m]];
    setContract = setRandomContraction[ TensorRank[n~tp~m~tp~p] - 2 Length[setContractm]];

   If[ ( TensorExpand[#] =!= TensorReduceContract[#]) & @ TensorContract[n~tp~TensorContract[m, setContractm]~tp~p, setContract]
   ,  
     Print["Tensor Expand Differs: ", TensorContract[n~tp~TensorContract[m, setContractm]~tp~p, setContract] ];
  ];
, {1000000}]

The main cases that these two TensorReduceContract and TensorExpand differ are due, respectively, to the following equivalent results:

> Out = TensorContract[n~tp~m~tp~p,{{40,41},{43,45},{46,47},{48,49}}]
> Out = n~tp~TensorContract[m,{{4,5},{7,9},{10,11},{12,13}}]~tp~p
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.