My intention is having a large collection of variables (symbols to be more specific) in a list and substituting all the variables at once with a value using rule instead of going through each one, one at a time. I will explain in more detail bellow if that wasn't clear:
I was trying to substitute the values of a vector/list in mathematica with rules but it didn't work as expected.
So consider a function that takes a list:
fsquared[x_List] := x^2
then consider pre defining (hard coding the list you want to use):
xxx = {xxx1, xxx2}
then consider substituting ALL the values of the list, but instead of doing it one by one, substituting all at once:
fsquared[xxx] /. xxx -> {3, 4}
the intended behaviour/output should be
{9,16}
but mathematica outputs:
{xxx1^2, xxx2^2}
Why is that? I wanted it to substitute each value inside the list with 3 and 4 i.e. the intended behavior is:
fsquared[xxx] /. {xxx1 -> 3, xxx2 -> 4}
the above definitively works, however, when the list of variables I want to substitute gets really long, the above method becomes a little ridiculous.
As in:
fsquared[xxx] /. {xxx1 -> 1, xxx2 -> 2, xxx3 -> 3, xxx4 -> 4, xxx5 -> 5, xxx6 -> 6, xxx7 -> 7}
I will also provide a screen shot to easy readability:
I've tried googling various things like unpacking lists, but so far, I've had no luck searching this. I am assuming something like this must exist, otherwise, it seems rather annoying to have a list of symbols and later decide to change their value and the only want to do it 1 by 1? yikes, that sucks.
I am aware that pattern matching and rule substitution is based on syntactic matching rather than semantic comparison on expressions. But still, is there no way to get this functionality that I am requesting using some built in function?
One caveat that I didn't mention is that I might have nested lists. So something like:
t1 = {a,b}
a = {a1, a2, a3}
b = {b1, b2, b3}
and I'd want:
t1 -> {{1,2,3},{4,5,6}}
or even something a tiny by more complicated like:
t2 = {c,d}
c = {c1, c2, c3}
d = {d1, d2, d3}
then define:
tBig = {t1,t2}
and it would be awesome if the following worked:
tBig -> {{{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}}
if it would substitute each variable/symbol, one by one.
fsquared[xxx] /. Thread[xxx -> {3, 4}]
. When you usefsquared[xxx] /. xxx -> {3, 4}
Mathematica tries to replace literal{xxx1, xxx2}
, but such thing is not present in yourfsquared[xxx]
expression. $\endgroup$xxx1
, orxxx2
. If I understand you correctly, what you want isxxx:={xxx1,xxx2}
. Then you can dofsquared/@xxx
, or add theListable
attribute to fsquared. Because of:=
anytime you updatexxx1
, orxxx2
re-evaluatingfsquared/@xxx
will return the result with the newly definedxxx1
, orxxx2
. $\endgroup$Thread[xxx -> {3, 4}] = {xxx1 -> 3, xxx2 -> 4}
. For your second comment, thanks for clarifying! As I noted in my question, I suspected that mathematica was only doing syntactic replacement, rather than semantic replacement and substituting as I intended it to do. $\endgroup${{x1, x2},{x3, x4}} -> {{1,2},{3,4}}
should also be covered. Let me see what your suggestions does in that (recursive) case... $\endgroup$Flatten
your lists before passing them toRule
:Thread[Flatten@tBig -> Flatten@{{{1, 2, 3}, {4, 5, 6}}, {{7, 8, 9}, {10, 11, 12}}}]
. You may need to specify a level in second argument ofFlatten
if you replace symbol with aList
. Or to make sure that given nested lists have compatible shapesMapThread
Rule
over them at appropriate level and then flatten:Flatten@MapThread[ Rule, {tBig, {{{1, 2, 3}, {4, 5, 6}}, {{7, 8, 9}, {10, 11, 12}}}}, 3]
$\endgroup$