# Converting linear combinations of symbols (with numbers) to linear combinations of list elements

I have a function that generates output that is a list of linear combinations of symbols with numbers, as an example:

{e1, e2, 2 e11, (e12 - e21)/2, e12 + e21, 2 e22}


What I want to be able to do is to take this output and then produce the exact same linear combinations of nested list elements. The way that it works is that the length of the number in the symbol indicates which "level" of the list it comes from, and the number itself states which position in that list. For example, if

list = {{1, 2}, {{3, 2}, {4, 5}}}


then the output should be

{1, 2, 6, -1., 6, 10}


i.e.

{1, 2, 2*3, (2-4)/2, 2+4, 2*5}


It is quite easy to do it without the linear combinations, but with those included, I am a bit stuck as to how to proceed!

symb2idx[list_,symb_]:=Extract[list, #[
ToExpression@StringDrop[SymbolName[symb], 1]] & /@ {IntegerLength,
IntegerDigits} // Flatten]


Try the following:

symbol = {e1, e2, 2 e11, (e12 - e21)/2, e12 + e21, 2 e22};
list = {{1, 2}, {{3, 2}, {4, 5}}};

makeitconvenient =
a_Symbol /; StringTake[ToString@a, 1] === "e" :>
With[{str = Characters@ToString@a}, str[]@ToExpression@Rest@str];

extract = "e"[{num__}] :> list[[Length@{num}, num]];

symbol /. makeitconvenient /. extract
(* {1, 2, 6, -1, 6, 10} *)


Notice if you can make the input be something like

symbol2 = {e, e, 2 e[1, 1], (e[1, 2] - e[2, 1])/2, e[1, 2] + e[2, 1], 2 e[2, 2]};


Then the solution can be simplified to:

symbol2 /. e[num__] :> list[[Length@{num}, num]]

• Thanks for this, I am not entirely sure that I follow exactly how it works though! – wilsnunn Nov 10 '18 at 13:28