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]
list-manipulation symbols
add a comment |
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
This looks like a clear example of an XY problem.
– AccidentalFourierTransform
Nov 10 at 19:51
add a comment |
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
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
list-manipulation symbols
asked Nov 10 at 12:31
wilsnunn
465210
465210
This looks like a clear example of an XY problem.
– AccidentalFourierTransform
Nov 10 at 19:51
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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]]
Thanks for this, I am not entirely sure that I follow exactly how it works though!
– wilsnunn
Nov 10 at 13:28
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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]]
Thanks for this, I am not entirely sure that I follow exactly how it works though!
– wilsnunn
Nov 10 at 13:28
add a comment |
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]]
Thanks for this, I am not entirely sure that I follow exactly how it works though!
– wilsnunn
Nov 10 at 13:28
add a comment |
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]]
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]]
answered Nov 10 at 13:06
xzczd
25.8k469246
25.8k469246
Thanks for this, I am not entirely sure that I follow exactly how it works though!
– wilsnunn
Nov 10 at 13:28
add a comment |
Thanks for this, I am not entirely sure that I follow exactly how it works though!
– wilsnunn
Nov 10 at 13:28
Thanks for this, I am not entirely sure that I follow exactly how it works though!
– wilsnunn
Nov 10 at 13:28
Thanks for this, I am not entirely sure that I follow exactly how it works though!
– wilsnunn
Nov 10 at 13:28
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
This looks like a clear example of an XY problem.
– AccidentalFourierTransform
Nov 10 at 19:51