Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]"]

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?

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

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}]]
share|improve this answer
Thank you for your help. – MTS May 2 '12 at 15:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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