The accepted Answer to this similar Question can only be used one time, so it's not suitable for my problem.
I do high-dimensional calculations for which I need to make lists of variables, like so:
aa = Table[a[j], {j, 0, 12}];
This allows me to use the list as arguments for derivatives, like so:
D[p,{aa}]
But I need to be able to assign values to the variables in the list. The following was offered as a solution to this in a different Question:
MapThread[Set, {aa, RandomReal[1, 13]}];
Print[a[0], " ", a[1], " ", a[2]];
(* 0.211593 0.467789 0.572727 *)
If you use that command again, it tries to assign the value to the values instead of the variable.
MapThread[Set, {aa, RandomReal[1, 13]}];
(* Set::setraw: Cannot assign to raw object 0.21159339034304447`. *)
So how can I change I reassign those values?
Hold[aa] /. OwnValues[aa] /. Hold[elems_List] :> With[{vals = RandomReal[1, 13]}, Set @@@ Thread[Hold[elems, vals], List]]
. The complexity of this construction should be a convincing enough argument to avoid the setup like that. Things would totally simplify if you simply assign toa[i]
in a loop. Not to mention that having one and the same variable stand for symbolic entity in differentiation and also be a variable storing a numerical value, doesn't sound like the best thing to do. $\endgroup$Hold[aa] /. OwnValues[aa] /. Hold[elems_List] :> Function[Null, Set[##], {HoldFirst, Listable}][elems,RandomReal[1, 13]]
. $\endgroup$D[p,{aa}]
instead of writing out a huge variable list. $\endgroup$Clear[a,aa]
. $\endgroup$a[i]
symbolic. Instead, compute with numerical values asBlock[{a}, Evaluate[aa] = RandomReal[1, 13]; your-code]
. This way you localize the numerical substitutions to the body of theBlock
. $\endgroup$