Extracting terms with certain heads in a function Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Pattern matching list of elements with different headsWhy does the Derivative function not appear in the tree form?How to “invert” a ruleReplace Complex Head with ListHow to obtain a list of all build-in system level Heads?question about using Heads->True in Cases command
How to answer "Have you ever been terminated?"
What is homebrew?
Significance of Cersei's obsession with elephants?
Is it cost-effective to upgrade an old-ish Giant Escape R3 commuter bike with entry-level branded parts (wheels, drivetrain)?
What do you call the main part of a joke?
Wu formula for manifolds with boundary
Amount of permutations on an NxNxN Rubik's Cube
Generate an RGB colour grid
How could we fake a moon landing now?
Circuit to "zoom in" on mV fluctuations of a DC signal?
Irreducible of finite Krull dimension implies quasi-compact?
Why are both D and D# fitting into my E minor key?
Why are there no cargo aircraft with "flying wing" design?
What causes the direction of lightning flashes?
Dating a Former Employee
Why do we bend a book to keep it straight?
Using et al. for a last / senior author rather than for a first author
Impact on credit score of opening and closing accounts
Fantasy story; one type of magic grows in power with use, but the more powerful they are, they more they are drawn to travel to their source
Do square wave exist?
Ports Showing Closed/Filtered in Nmap Scans
How to react to hostile behavior from a senior developer?
What's the meaning of "fortified infraction restraint"?
Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?
Extracting terms with certain heads in a function
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Pattern matching list of elements with different headsWhy does the Derivative function not appear in the tree form?How to “invert” a ruleReplace Complex Head with ListHow to obtain a list of all build-in system level Heads?question about using Heads->True in Cases command
$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 6 hours ago
mastrokmastrok
26917
26917
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
6 hours ago
2
$begingroup$
You could also use the 1-arg form ofCases, e.g.takeHeads = Cases[_g1 | _g2].
$endgroup$
– Carl Woll
4 hours ago
$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
4 hours ago
$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
4 hours ago
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
6 hours ago
2
$begingroup$
You could also use the 1-arg form ofCases, e.g.takeHeads = Cases[_g1 | _g2].
$endgroup$
– Carl Woll
4 hours ago
$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
4 hours ago
$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
4 hours ago
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
6 hours ago
2
$begingroup$
You could also use the 1-arg form ofCases, e.g.takeHeads = Cases[_g1 | _g2].
$endgroup$
– Carl Woll
4 hours ago
$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
4 hours ago
$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
4 hours ago
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 5 hours ago
march
17.6k22870
17.6k22870
answered 6 hours ago
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
6 hours ago
2
$begingroup$
You could also use the 1-arg form ofCases, e.g.takeHeads = Cases[_g1 | _g2].
$endgroup$
– Carl Woll
4 hours ago
$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
4 hours ago
$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
4 hours ago
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
6 hours ago
2
$begingroup$
You could also use the 1-arg form ofCases, e.g.takeHeads = Cases[_g1 | _g2].
$endgroup$
– Carl Woll
4 hours ago
$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
4 hours ago
$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
4 hours ago
$begingroup$
thank you, the method using cases is very useful. I did not know that Cases works inside the function
func$endgroup$
– mastrok
6 hours ago
$begingroup$
thank you, the method using cases is very useful. I did not know that Cases works inside the function
func$endgroup$
– mastrok
6 hours ago
2
2
$begingroup$
You could also use the 1-arg form of
Cases, e.g. takeHeads = Cases[_g1 | _g2].$endgroup$
– Carl Woll
4 hours ago
$begingroup$
You could also use the 1-arg form of
Cases, e.g. takeHeads = Cases[_g1 | _g2].$endgroup$
– Carl Woll
4 hours ago
$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
4 hours ago
$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
4 hours ago
$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
4 hours ago
$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
4 hours ago
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 6 hours ago
J42161217J42161217
4,573324
4,573324
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