Extracting terms with certain heads in a function












3












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

















    3












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















      3












      3








      3





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









      mastrokmastrok

      27417




      27417






















          2 Answers
          2






          active

          oldest

          votes


















          8












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






          • 2




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





















          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









            8












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






            • 2




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


















            8












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






            • 2




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
















            8












            8








            8





            $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 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 function func
              $endgroup$
              – mastrok
              yesterday






            • 2




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






            • 2




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













            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 yesterday









                J42161217J42161217

                4,598324




                4,598324






























                    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

                    Category:香港粉麵

                    List *all* the tuples!

                    Channel [V]