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











up vote
3
down vote

favorite












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]









share|improve this question






















  • This looks like a clear example of an XY problem.
    – AccidentalFourierTransform
    Nov 10 at 19:51

















up vote
3
down vote

favorite












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]









share|improve this question






















  • This looks like a clear example of an XY problem.
    – AccidentalFourierTransform
    Nov 10 at 19:51















up vote
3
down vote

favorite









up vote
3
down vote

favorite











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]









share|improve this question













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]






list-manipulation symbols






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 12:31









Daniel Wilson-Nunn

414210




414210












  • This looks like a clear example of an XY problem.
    – AccidentalFourierTransform
    Nov 10 at 19:51




















  • This looks like a clear example of an XY problem.
    – AccidentalFourierTransform
    Nov 10 at 19:51


















This looks like a clear example of an XY problem.
– AccidentalFourierTransform
Nov 10 at 19:51






This looks like a clear example of an XY problem.
– AccidentalFourierTransform
Nov 10 at 19:51












1 Answer
1






active

oldest

votes

















up vote
4
down vote













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]]





share|improve this answer





















  • Thanks for this, I am not entirely sure that I follow exactly how it works though!
    – Daniel Wilson-Nunn
    Nov 10 at 13:28











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185742%2fconverting-linear-combinations-of-symbols-with-numbers-to-linear-combinations%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote













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]]





share|improve this answer





















  • Thanks for this, I am not entirely sure that I follow exactly how it works though!
    – Daniel Wilson-Nunn
    Nov 10 at 13:28















up vote
4
down vote













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]]





share|improve this answer





















  • Thanks for this, I am not entirely sure that I follow exactly how it works though!
    – Daniel Wilson-Nunn
    Nov 10 at 13:28













up vote
4
down vote










up vote
4
down vote









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]]





share|improve this answer












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]]






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 13:06









xzczd

25.5k468243




25.5k468243












  • Thanks for this, I am not entirely sure that I follow exactly how it works though!
    – Daniel Wilson-Nunn
    Nov 10 at 13:28


















  • Thanks for this, I am not entirely sure that I follow exactly how it works though!
    – Daniel Wilson-Nunn
    Nov 10 at 13:28
















Thanks for this, I am not entirely sure that I follow exactly how it works though!
– Daniel Wilson-Nunn
Nov 10 at 13:28




Thanks for this, I am not entirely sure that I follow exactly how it works though!
– Daniel Wilson-Nunn
Nov 10 at 13:28


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185742%2fconverting-linear-combinations-of-symbols-with-numbers-to-linear-combinations%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues