5
$\begingroup$

I am trying to create a function called TensorBasis that takes as input a list (thought of as a list of names of basis vectors of a vector space) and an integer d and output the lexicographically ordered basis of the d-fold tensor product of the vector space with itself.

For instance, the output of TensorBasis[{v1,v2},2] should be

{ v1 [\CircleTimes] v1, v1 [\CircleTimes] v2, v2 [\CircleTimes] v1, v2 [\CircleTimes] v2 },

and so on.

I have written the following code:

TensorBasis[labels_, d_] := Module[
   {dim = Length[labels], basis, j, k, indexlist, tmp},

   basis = ConstantArray[0, dim^d];
   tmp = ConstantArray[0, d];

   For[j = 1, j <= dim^d, j++,
   (* Indexlist stores the indices of the jth element of the basis for the d-fold tensor product  *)

   indexlist = InverseLex[dim, d, j];

   (* tmp holds the list of individual factors that will make up the jth element of the basis *)
    For[k = 1, k <= d, k++,
      tmp[[k]] = labels[[indexlist[[k]] ]]
    ];

   (* jth element of the basis is created by tensoring together all elements of tmp *)
    basis[[j]] = Infix[tmp, "\[CircleTimes]"]
  ];
Return[basis]
];

The function InverseLex just gives me the appropriate list of indices for the basis element I am trying to construct. I am sure that that function is working properly. But the output is all weird. The problem is that in the output, the [CircleTimes] (i.e. tensor product) symbol is replaced by some junk: in Plain Text form it is

{Subscript[v, 1]â Subscript[v, 1],Subscript[v, 1]â Subscript[v, 2],Subscript[v, 2]â Subscript[v, 1],Subscript[v, 2]â Subscript[v, 2]}

When I use the Infix notation with "[CircleTimes]" otherwise it works fine. Why won't it work inside this function?

$\endgroup$
4
  • $\begingroup$ With mma v8.0.4.0 in Windows your code gives the expected output. You can also try using basis[[j]] = CircleTimes[Sequence @@ tmp]]. $\endgroup$
    – kglr
    May 2, 2012 at 8:03
  • $\begingroup$ Mma 8.0.1.0 on OSX also gives the desired output. Maybe you have a font issue. $\endgroup$
    – kadrach
    May 2, 2012 at 10:48
  • $\begingroup$ Hmmm, perhaps it's a Linux issue then if it's working ok on Windows and Mac.... $\endgroup$
    – MTS
    May 2, 2012 at 15:11
  • $\begingroup$ @kguler, your advice worked. Thanks! I am pretty new to Mathematica and didn't know about the Sequence command. $\endgroup$
    – MTS
    May 2, 2012 at 15:48

1 Answer 1

6
$\begingroup$

Infix is only an output form. You most probably want the expression which you'd get when typing v1 ⊗ v2 into Mathematica, which is entered with either \[CircleTimes] or Escc*Esc.

As you can see by typing v1 ⊗ v2 //FullForm, this is CircleTimes[v1, v2]. The same is true for longer chains of , i.e. v1 ⊗ v2 ⊗ … ⊗ vn has the internal form CircleTimes[v1, v2, …, vn].

Also note that a complicated procedural code isn't necessary either, because Mathematica already brings the building blocks you need. Therefore the function you want should be written

TensorBasis[labels_List,d_Integer] := Outer[CircleTimes, Sequence@@Table[labels,{d}]]
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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