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]
list-manipulation symbols
add a comment |
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]
list-manipulation symbols
This looks like a clear example of an XY problem.
– AccidentalFourierTransform
Nov 10 at 19:51
add a comment |
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]
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
Daniel Wilson-Nunn
414210
414210
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
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]]
Thanks for this, I am not entirely sure that I follow exactly how it works though!
– Daniel Wilson-Nunn
Nov 10 at 13:28
add a comment |
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]]
Thanks for this, I am not entirely sure that I follow exactly how it works though!
– Daniel Wilson-Nunn
Nov 10 at 13:28
add a comment |
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]]
Thanks for this, I am not entirely sure that I follow exactly how it works though!
– Daniel Wilson-Nunn
Nov 10 at 13:28
add a comment |
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]]
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.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
add a comment |
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
add a comment |
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