I want to assign a certain value to an element of a tensor. This element is identified by an array obtained from some evaluations, so we fix it. Here i present an oversimplified core part of my code:

DimensionA = 3;
v = {1, 2, 3};
Array[TensorA, Table[10, DimensionA], 0];  
TensorA @@ v = 12;

at this point i receive an error

Set::write: Tag Apply in TensorA@@{1,2,3} is Protected.

Obviously if i do TensorA[1, 2, 3] = 12; it is all ok, but my aim is to give in input a vector with an arbitrary number of indices. Please show me how I should input the vector to the tensor without receiving this error.

  • 1
    $\begingroup$ Note that half the code in this question is extraneous. v = {1, 2, 3}; Evaluate[TensorA @@ v] = 12 is sufficient to set TensorA[1, 2, 3] to 12. $\endgroup$ – bbgodfrey Oct 7 '18 at 17:31
  • $\begingroup$ Thank you, here there is good related answer right here : mathematica.stackexchange.com/questions/183259/… $\endgroup$ – Mordin Solus Oct 7 '18 at 20:25
  • $\begingroup$ I wrote it but did not post a similar answer here, because it would have been the same as the one by @ThatGravityGuy. Thank you for the kind words. $\endgroup$ – bbgodfrey Oct 7 '18 at 20:39

Just force the Apply before Set.

Evaluate[TensorA @@ v] = 12

Also for the dimensions specification Table[10, DimensionA], I suggest using ConstantArray[10, DimensionA] instead.

| improve this answer | |

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.