Extracting terms with certain heads in a function
$begingroup$
Given a function with several arguments
func[a,b,g1[x],g1[x,y],g2[1],g2[g1[1]],3]
I would like to extract all arguments with the head g1 and g2 as a list. So the output I am looking for is
{g1[x],g1[x,y],g2[1],g2[g1[1]]}
The way I used to extract one head, say g1, is simply using the rule
/.func[l___,x__g1,r___]:> {x}
However, with two heads, this method does not work. I could write a module to do that but I wonder if there is a simpler way like the above rule for one head. Thank you so much!
head
$endgroup$
add a comment |
$begingroup$
Given a function with several arguments
func[a,b,g1[x],g1[x,y],g2[1],g2[g1[1]],3]
I would like to extract all arguments with the head g1 and g2 as a list. So the output I am looking for is
{g1[x],g1[x,y],g2[1],g2[g1[1]]}
The way I used to extract one head, say g1, is simply using the rule
/.func[l___,x__g1,r___]:> {x}
However, with two heads, this method does not work. I could write a module to do that but I wonder if there is a simpler way like the above rule for one head. Thank you so much!
head
$endgroup$
add a comment |
$begingroup$
Given a function with several arguments
func[a,b,g1[x],g1[x,y],g2[1],g2[g1[1]],3]
I would like to extract all arguments with the head g1 and g2 as a list. So the output I am looking for is
{g1[x],g1[x,y],g2[1],g2[g1[1]]}
The way I used to extract one head, say g1, is simply using the rule
/.func[l___,x__g1,r___]:> {x}
However, with two heads, this method does not work. I could write a module to do that but I wonder if there is a simpler way like the above rule for one head. Thank you so much!
head
$endgroup$
Given a function with several arguments
func[a,b,g1[x],g1[x,y],g2[1],g2[g1[1]],3]
I would like to extract all arguments with the head g1 and g2 as a list. So the output I am looking for is
{g1[x],g1[x,y],g2[1],g2[g1[1]]}
The way I used to extract one head, say g1, is simply using the rule
/.func[l___,x__g1,r___]:> {x}
However, with two heads, this method does not work. I could write a module to do that but I wonder if there is a simpler way like the above rule for one head. Thank you so much!
head
head
asked yesterday
mastrokmastrok
27417
27417
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Either
takeHeads = Cases[#, _g1 | _g2] &;
func[a, b, g1[x], g1[x, y], g2[1], g2[g1[1]], 3] // takeHeads
{g1[x], g1[x, y], g2[1], g2[g1[1]]}
or define func itself as
func = Cases[{##}, _g1 | _g2] &;
func[a, b, g1[x], g1[x, y], g2[1], g2[g1[1]], 3]
{g1[x], g1[x, y], g2[1], g2[g1[1]]}
$endgroup$
$begingroup$
thank you, the method using cases is very useful. I did not know that Cases works inside the functionfunc
$endgroup$
– mastrok
yesterday
2
$begingroup$
You could also use the 1-arg form ofCases, e.g.takeHeads = Cases[_g1 | _g2].
$endgroup$
– Carl Woll
yesterday
$begingroup$
@mastrok If that surprised you thatCasesworked on your customfuncexpression have a look at Everything is an Expression. Hope this helps to understand why this worked.
$endgroup$
– Thies Heidecke
yesterday
$begingroup$
@Thies Heidecke Yes, I know that everything is an expression. However I thought that Cases only works with List only but not a general head. Thanks!
$endgroup$
– mastrok
yesterday
add a comment |
$begingroup$
Select[{##&@@func[a,b,g1[x],g1[x,y],g2[1],g2[g1[1]],3]},h=#;Head@#==h&]&/@{g1,g2}
{{g1[x], g1[x, y]}, {g2[1], g2[g1[1]]}}
$endgroup$
add a comment |
Your Answer
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%2f195406%2fextracting-terms-with-certain-heads-in-a-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Either
takeHeads = Cases[#, _g1 | _g2] &;
func[a, b, g1[x], g1[x, y], g2[1], g2[g1[1]], 3] // takeHeads
{g1[x], g1[x, y], g2[1], g2[g1[1]]}
or define func itself as
func = Cases[{##}, _g1 | _g2] &;
func[a, b, g1[x], g1[x, y], g2[1], g2[g1[1]], 3]
{g1[x], g1[x, y], g2[1], g2[g1[1]]}
$endgroup$
$begingroup$
thank you, the method using cases is very useful. I did not know that Cases works inside the functionfunc
$endgroup$
– mastrok
yesterday
2
$begingroup$
You could also use the 1-arg form ofCases, e.g.takeHeads = Cases[_g1 | _g2].
$endgroup$
– Carl Woll
yesterday
$begingroup$
@mastrok If that surprised you thatCasesworked on your customfuncexpression have a look at Everything is an Expression. Hope this helps to understand why this worked.
$endgroup$
– Thies Heidecke
yesterday
$begingroup$
@Thies Heidecke Yes, I know that everything is an expression. However I thought that Cases only works with List only but not a general head. Thanks!
$endgroup$
– mastrok
yesterday
add a comment |
$begingroup$
Either
takeHeads = Cases[#, _g1 | _g2] &;
func[a, b, g1[x], g1[x, y], g2[1], g2[g1[1]], 3] // takeHeads
{g1[x], g1[x, y], g2[1], g2[g1[1]]}
or define func itself as
func = Cases[{##}, _g1 | _g2] &;
func[a, b, g1[x], g1[x, y], g2[1], g2[g1[1]], 3]
{g1[x], g1[x, y], g2[1], g2[g1[1]]}
$endgroup$
$begingroup$
thank you, the method using cases is very useful. I did not know that Cases works inside the functionfunc
$endgroup$
– mastrok
yesterday
2
$begingroup$
You could also use the 1-arg form ofCases, e.g.takeHeads = Cases[_g1 | _g2].
$endgroup$
– Carl Woll
yesterday
$begingroup$
@mastrok If that surprised you thatCasesworked on your customfuncexpression have a look at Everything is an Expression. Hope this helps to understand why this worked.
$endgroup$
– Thies Heidecke
yesterday
$begingroup$
@Thies Heidecke Yes, I know that everything is an expression. However I thought that Cases only works with List only but not a general head. Thanks!
$endgroup$
– mastrok
yesterday
add a comment |
$begingroup$
Either
takeHeads = Cases[#, _g1 | _g2] &;
func[a, b, g1[x], g1[x, y], g2[1], g2[g1[1]], 3] // takeHeads
{g1[x], g1[x, y], g2[1], g2[g1[1]]}
or define func itself as
func = Cases[{##}, _g1 | _g2] &;
func[a, b, g1[x], g1[x, y], g2[1], g2[g1[1]], 3]
{g1[x], g1[x, y], g2[1], g2[g1[1]]}
$endgroup$
Either
takeHeads = Cases[#, _g1 | _g2] &;
func[a, b, g1[x], g1[x, y], g2[1], g2[g1[1]], 3] // takeHeads
{g1[x], g1[x, y], g2[1], g2[g1[1]]}
or define func itself as
func = Cases[{##}, _g1 | _g2] &;
func[a, b, g1[x], g1[x, y], g2[1], g2[g1[1]], 3]
{g1[x], g1[x, y], g2[1], g2[g1[1]]}
edited yesterday
march
17.7k22870
17.7k22870
answered yesterday
CoolwaterCoolwater
15.4k32553
15.4k32553
$begingroup$
thank you, the method using cases is very useful. I did not know that Cases works inside the functionfunc
$endgroup$
– mastrok
yesterday
2
$begingroup$
You could also use the 1-arg form ofCases, e.g.takeHeads = Cases[_g1 | _g2].
$endgroup$
– Carl Woll
yesterday
$begingroup$
@mastrok If that surprised you thatCasesworked on your customfuncexpression have a look at Everything is an Expression. Hope this helps to understand why this worked.
$endgroup$
– Thies Heidecke
yesterday
$begingroup$
@Thies Heidecke Yes, I know that everything is an expression. However I thought that Cases only works with List only but not a general head. Thanks!
$endgroup$
– mastrok
yesterday
add a comment |
$begingroup$
thank you, the method using cases is very useful. I did not know that Cases works inside the functionfunc
$endgroup$
– mastrok
yesterday
2
$begingroup$
You could also use the 1-arg form ofCases, e.g.takeHeads = Cases[_g1 | _g2].
$endgroup$
– Carl Woll
yesterday
$begingroup$
@mastrok If that surprised you thatCasesworked on your customfuncexpression have a look at Everything is an Expression. Hope this helps to understand why this worked.
$endgroup$
– Thies Heidecke
yesterday
$begingroup$
@Thies Heidecke Yes, I know that everything is an expression. However I thought that Cases only works with List only but not a general head. Thanks!
$endgroup$
– mastrok
yesterday
$begingroup$
thank you, the method using cases is very useful. I did not know that Cases works inside the function
func$endgroup$
– mastrok
yesterday
$begingroup$
thank you, the method using cases is very useful. I did not know that Cases works inside the function
func$endgroup$
– mastrok
yesterday
2
2
$begingroup$
You could also use the 1-arg form of
Cases, e.g. takeHeads = Cases[_g1 | _g2].$endgroup$
– Carl Woll
yesterday
$begingroup$
You could also use the 1-arg form of
Cases, e.g. takeHeads = Cases[_g1 | _g2].$endgroup$
– Carl Woll
yesterday
$begingroup$
@mastrok If that surprised you that
Cases worked on your custom func expression have a look at Everything is an Expression. Hope this helps to understand why this worked.$endgroup$
– Thies Heidecke
yesterday
$begingroup$
@mastrok If that surprised you that
Cases worked on your custom func expression have a look at Everything is an Expression. Hope this helps to understand why this worked.$endgroup$
– Thies Heidecke
yesterday
$begingroup$
@Thies Heidecke Yes, I know that everything is an expression. However I thought that Cases only works with List only but not a general head. Thanks!
$endgroup$
– mastrok
yesterday
$begingroup$
@Thies Heidecke Yes, I know that everything is an expression. However I thought that Cases only works with List only but not a general head. Thanks!
$endgroup$
– mastrok
yesterday
add a comment |
$begingroup$
Select[{##&@@func[a,b,g1[x],g1[x,y],g2[1],g2[g1[1]],3]},h=#;Head@#==h&]&/@{g1,g2}
{{g1[x], g1[x, y]}, {g2[1], g2[g1[1]]}}
$endgroup$
add a comment |
$begingroup$
Select[{##&@@func[a,b,g1[x],g1[x,y],g2[1],g2[g1[1]],3]},h=#;Head@#==h&]&/@{g1,g2}
{{g1[x], g1[x, y]}, {g2[1], g2[g1[1]]}}
$endgroup$
add a comment |
$begingroup$
Select[{##&@@func[a,b,g1[x],g1[x,y],g2[1],g2[g1[1]],3]},h=#;Head@#==h&]&/@{g1,g2}
{{g1[x], g1[x, y]}, {g2[1], g2[g1[1]]}}
$endgroup$
Select[{##&@@func[a,b,g1[x],g1[x,y],g2[1],g2[g1[1]],3]},h=#;Head@#==h&]&/@{g1,g2}
{{g1[x], g1[x, y]}, {g2[1], g2[g1[1]]}}
answered yesterday
J42161217J42161217
4,598324
4,598324
add a comment |
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.
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%2f195406%2fextracting-terms-with-certain-heads-in-a-function%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