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










2












$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!










share|improve this question









$endgroup$
















    2












    $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!










    share|improve this question









    $endgroup$














      2












      2








      2





      $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!










      share|improve this question









      $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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 6 hours ago









      mastrokmastrok

      26917




      26917




















          2 Answers
          2






          active

          oldest

          votes


















          6












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







          share|improve this answer











          $endgroup$












          • $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




            $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$
            @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



















          3












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







          share|improve this answer









          $endgroup$













            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
            );



            );













            draft saved

            draft discarded


















            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









            6












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







            share|improve this answer











            $endgroup$












            • $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




              $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$
              @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
















            6












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







            share|improve this answer











            $endgroup$












            • $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




              $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$
              @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














            6












            6








            6





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







            share|improve this answer











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








            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 function func
              $endgroup$
              – mastrok
              6 hours ago






            • 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$
              @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$
              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




              $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$
              @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












            3












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







            share|improve this answer









            $endgroup$

















              3












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







              share|improve this answer









              $endgroup$















                3












                3








                3





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







                share|improve this answer









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








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 6 hours ago









                J42161217J42161217

                4,573324




                4,573324



























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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

                    How to create a command for the “strange m” symbol in latex? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)How do you make your own symbol when Detexify fails?Writing bold small caps with mathpazo packageplus-minus symbol with parenthesis around the minus signGreek character in Beamer document titleHow to create dashed right arrow over symbol?Currency symbol: Turkish LiraDouble prec as a single symbol?Plus Sign Too Big; How to Call adfbullet?Is there a TeX macro for three-legged pi?How do I get my integral-like symbol to align like the integral?How to selectively substitute a letter with another symbol representing the same letterHow do I generate a less than symbol and vertical bar that are the same height?

                    The Calvary Singular or Plural The 2019 Stack Overflow Developer Survey Results Are InAre collective nouns always plural, or are certain ones singular?Is “audience” singular or plural?“Wasn't” vs. “weren't” in a vernacular sentence“My last couple of years” — singular or plural?Is 'rest' singular or plural?Is “all but one” singular or plural?Whether to use the singular or plural form of basis?Singular and Plural for numbersIs there a plural form of teeth?performance: plural vs singular?singular or plural nouns?Singular and Plural

                    Category:Tremithousa Media in category "Tremithousa"Navigation menuUpload media34° 49′ 02.7″ N, 32° 26′ 37.32″ EOpenStreetMapGoogle EarthProximityramaReasonatorScholiaStatisticsWikiShootMe