I want to use n variables $$alpha[[i]]$$ (i goes from 1 to n).

I use these variable inside of a function :

SixJ1[j1_, j2_, j3_, j4_, j5_, j6_] := Module[{j},
  j = {j1, j2, j3, j4, j5, j6};
  Sum[alpha[[k]]*j[[k]]*(j[[k]] + 1)*
     SixJSymbol[{j1, j2, j3}, {j4, j5, j6}] + 
    beta*opVolume[j1, j2, j3, j4, j5, j6], {k, 1, 6}]

The problem is that when I call this function, mathematica answers me :

"Part specification alpha[[1]] is longer than depth of object."

I think it is because I never initialised thoose variables but I want to keep them symbolic.

How to avoid these errors ?

  • 2
    $\begingroup$ How about alpha[k]? $\endgroup$ – Michael E2 Jun 14 '17 at 14:15
  • $\begingroup$ I thought that to call an element of a list we needed two bracket : [[ ? $\endgroup$ – StarBucK Jun 14 '17 at 14:16
  • $\begingroup$ Ok, how about alphalist = Array[k, 6]; alphalist[[k]]? (You have to name your variables somehow first.) $\endgroup$ – Michael E2 Jun 14 '17 at 14:17
  • $\begingroup$ In fact your first comment intrigued me. What is the difference between alpha[k] and alpha[[k]]. I know the second is interpreted as en element of a list but I did'nt know we could only use one bracket. What mean exactly the first expression alpha[k] ? $\endgroup$ – StarBucK Jun 14 '17 at 14:20
  • $\begingroup$ Oh in fact it is just the function notation... $\endgroup$ – StarBucK Jun 14 '17 at 14:22

An alternative:

 alpha = Table[Symbol["alpha" <> ToString[i]], {i, 1, n}];

Probably the most elegant way that solves your problem, in my opinion, is the following one:

Subscript[alpha, #] & /@ Range[10]


Table[Subscript[alpha, i], {i, 10}]

If you want to create a list of "indexed" symbols, you can do it as:

ToExpression["alpha" <> ToString[#]] & /@ Range[10]

Or you can do something like:

alpha[#] & /@ Range[10]

, which is actually same as:

Table[alpha[i], {i, 10}]

[[]] - this is Part[], so you can't really use this for indexing.

ToExpression[Table[TemplateApply["alpha<*i*>"], {i, 5}]]

{alpha1, alpha2, alpha3, alpha4, alpha5}


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.