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. – Leonid Shifrin Nov 15 '15 at 02:36Hold[aa] /. OwnValues[aa] /. Hold[elems_List] :> Function[Null, Set[##], {HoldFirst, Listable}][elems,RandomReal[1, 13]]. – Leonid Shifrin Nov 15 '15 at 02:45D[p,{aa}]instead of writing out a huge variable list. – Jerry Guern Nov 15 '15 at 03:13Clear[a,aa]. – bill s Nov 15 '15 at 03:34Evaluate[aa] = RandomReal[1, 13]and something likeMapIndexed[(a[First@#2 - 1] = #1) &, RandomReal[1, 13]]. – Karsten7 Nov 15 '15 at 05:53(a[# - 1] =.) & /@ Range[Length@aa];can be used toUnsetthe individual indexed variables before re-setting them. – Karsten7 Nov 15 '15 at 05:57Thread[aa -> RandomReal[1, 13]]together with/.instead of setting and re-setting values makes thinks easier overall. – Karsten7 Nov 15 '15 at 06:23a[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. – Leonid Shifrin Nov 15 '15 at 20:52