3
$\begingroup$

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]
$\endgroup$
4
$\begingroup$

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[[1]]@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[1], e[2], 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]]
$\endgroup$
  • $\begingroup$ Thanks for this, I am not entirely sure that I follow exactly how it works though! $\endgroup$ – wilsnunn Nov 10 '18 at 13:28

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.