Evaluate Only Part of a Function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












I have a list of functions, each containing none or more of the functions Plus, Times, Subtract and Divide. They could be in any arrangement and with any numbers. This is one such example.



HoldForm[Plus[3+Times[7,Subtract[9,Plus[4,5]],Divide[29,Subtract[4,3]]]]]*


There is an error here, The above should read: Plus[3,Times[7



The numbers can be any positive integer. I need to calculate just the Divide and Subtract functions to find out if they evaluate to 1 or 0 respectively as I consider functions in the list that have this property trivial and I want to filter them out.



I am somewhat a beginner with Wolfram/Mathematica code and I have been attempting to extract the Divide and Subtract parts as a list and then calculating just them parts but so-far my attempts at solving this have been totally unfruitful, partly because I can't know beforehand where the Divide or Subtract functions are in the function or how deep they will be.



Is there a way to accomplish this?



Edit: Just to help clarify what I am trying to achieve. I have a list of functions such as



HoldForm[Plus[3,Times[7,Subtract[9,Plus[4,5]],Divide[29,Subtract[4,3]]]]],
HoldForm[Plus[7,Times[2,Divide[16,Subtract[9,Plus[4,1]]]]]],
HoldForm[Plus[3,Times[7,9],29]]



I need to remove the first element from the list because it is a function that divides by 1 (and subtracts 0), making is only trivially different from the 3rd element of the list. After removing the first function I want to display the output like this



$7+2 frac169-(4+1)$,$3+7times 9+29$










share|improve this question























  • Welcome to Mathematica.SE! Can you please put any code snippets in code blocks to improve the readability of the question?
    – Szabolcs
    Sep 13 at 13:05






  • 1




    What would be a result of Subtract[9,Plus[4,5]] if Plus is not included in your list?
    – Kuba♦
    Sep 13 at 13:10










  • In my example, the whole thing should be removed from my list of functions. This is because Subtract[9,Plus[4,5]] will evaluate to 0 and so it means the example is trivial as there will be another function in the list that is the same except it won't Subtract[0]
    – RedPython
    Sep 13 at 13:19











  • In that case why don't you just evaluate the whole thing and say that the result is 3?
    – Szabolcs
    Sep 13 at 13:21










  • Thanks for the advice Szabolcs. Advice appreciated. I have looked up how to do it and will ensure I follow your advice going forward.
    – RedPython
    Sep 13 at 13:21














up vote
7
down vote

favorite












I have a list of functions, each containing none or more of the functions Plus, Times, Subtract and Divide. They could be in any arrangement and with any numbers. This is one such example.



HoldForm[Plus[3+Times[7,Subtract[9,Plus[4,5]],Divide[29,Subtract[4,3]]]]]*


There is an error here, The above should read: Plus[3,Times[7



The numbers can be any positive integer. I need to calculate just the Divide and Subtract functions to find out if they evaluate to 1 or 0 respectively as I consider functions in the list that have this property trivial and I want to filter them out.



I am somewhat a beginner with Wolfram/Mathematica code and I have been attempting to extract the Divide and Subtract parts as a list and then calculating just them parts but so-far my attempts at solving this have been totally unfruitful, partly because I can't know beforehand where the Divide or Subtract functions are in the function or how deep they will be.



Is there a way to accomplish this?



Edit: Just to help clarify what I am trying to achieve. I have a list of functions such as



HoldForm[Plus[3,Times[7,Subtract[9,Plus[4,5]],Divide[29,Subtract[4,3]]]]],
HoldForm[Plus[7,Times[2,Divide[16,Subtract[9,Plus[4,1]]]]]],
HoldForm[Plus[3,Times[7,9],29]]



I need to remove the first element from the list because it is a function that divides by 1 (and subtracts 0), making is only trivially different from the 3rd element of the list. After removing the first function I want to display the output like this



$7+2 frac169-(4+1)$,$3+7times 9+29$










share|improve this question























  • Welcome to Mathematica.SE! Can you please put any code snippets in code blocks to improve the readability of the question?
    – Szabolcs
    Sep 13 at 13:05






  • 1




    What would be a result of Subtract[9,Plus[4,5]] if Plus is not included in your list?
    – Kuba♦
    Sep 13 at 13:10










  • In my example, the whole thing should be removed from my list of functions. This is because Subtract[9,Plus[4,5]] will evaluate to 0 and so it means the example is trivial as there will be another function in the list that is the same except it won't Subtract[0]
    – RedPython
    Sep 13 at 13:19











  • In that case why don't you just evaluate the whole thing and say that the result is 3?
    – Szabolcs
    Sep 13 at 13:21










  • Thanks for the advice Szabolcs. Advice appreciated. I have looked up how to do it and will ensure I follow your advice going forward.
    – RedPython
    Sep 13 at 13:21












up vote
7
down vote

favorite









up vote
7
down vote

favorite











I have a list of functions, each containing none or more of the functions Plus, Times, Subtract and Divide. They could be in any arrangement and with any numbers. This is one such example.



HoldForm[Plus[3+Times[7,Subtract[9,Plus[4,5]],Divide[29,Subtract[4,3]]]]]*


There is an error here, The above should read: Plus[3,Times[7



The numbers can be any positive integer. I need to calculate just the Divide and Subtract functions to find out if they evaluate to 1 or 0 respectively as I consider functions in the list that have this property trivial and I want to filter them out.



I am somewhat a beginner with Wolfram/Mathematica code and I have been attempting to extract the Divide and Subtract parts as a list and then calculating just them parts but so-far my attempts at solving this have been totally unfruitful, partly because I can't know beforehand where the Divide or Subtract functions are in the function or how deep they will be.



Is there a way to accomplish this?



Edit: Just to help clarify what I am trying to achieve. I have a list of functions such as



HoldForm[Plus[3,Times[7,Subtract[9,Plus[4,5]],Divide[29,Subtract[4,3]]]]],
HoldForm[Plus[7,Times[2,Divide[16,Subtract[9,Plus[4,1]]]]]],
HoldForm[Plus[3,Times[7,9],29]]



I need to remove the first element from the list because it is a function that divides by 1 (and subtracts 0), making is only trivially different from the 3rd element of the list. After removing the first function I want to display the output like this



$7+2 frac169-(4+1)$,$3+7times 9+29$










share|improve this question















I have a list of functions, each containing none or more of the functions Plus, Times, Subtract and Divide. They could be in any arrangement and with any numbers. This is one such example.



HoldForm[Plus[3+Times[7,Subtract[9,Plus[4,5]],Divide[29,Subtract[4,3]]]]]*


There is an error here, The above should read: Plus[3,Times[7



The numbers can be any positive integer. I need to calculate just the Divide and Subtract functions to find out if they evaluate to 1 or 0 respectively as I consider functions in the list that have this property trivial and I want to filter them out.



I am somewhat a beginner with Wolfram/Mathematica code and I have been attempting to extract the Divide and Subtract parts as a list and then calculating just them parts but so-far my attempts at solving this have been totally unfruitful, partly because I can't know beforehand where the Divide or Subtract functions are in the function or how deep they will be.



Is there a way to accomplish this?



Edit: Just to help clarify what I am trying to achieve. I have a list of functions such as



HoldForm[Plus[3,Times[7,Subtract[9,Plus[4,5]],Divide[29,Subtract[4,3]]]]],
HoldForm[Plus[7,Times[2,Divide[16,Subtract[9,Plus[4,1]]]]]],
HoldForm[Plus[3,Times[7,9],29]]



I need to remove the first element from the list because it is a function that divides by 1 (and subtracts 0), making is only trivially different from the 3rd element of the list. After removing the first function I want to display the output like this



$7+2 frac169-(4+1)$,$3+7times 9+29$







list-manipulation functions replacement symbolic






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 13 at 14:34

























asked Sep 13 at 13:01









RedPython

1384




1384











  • Welcome to Mathematica.SE! Can you please put any code snippets in code blocks to improve the readability of the question?
    – Szabolcs
    Sep 13 at 13:05






  • 1




    What would be a result of Subtract[9,Plus[4,5]] if Plus is not included in your list?
    – Kuba♦
    Sep 13 at 13:10










  • In my example, the whole thing should be removed from my list of functions. This is because Subtract[9,Plus[4,5]] will evaluate to 0 and so it means the example is trivial as there will be another function in the list that is the same except it won't Subtract[0]
    – RedPython
    Sep 13 at 13:19











  • In that case why don't you just evaluate the whole thing and say that the result is 3?
    – Szabolcs
    Sep 13 at 13:21










  • Thanks for the advice Szabolcs. Advice appreciated. I have looked up how to do it and will ensure I follow your advice going forward.
    – RedPython
    Sep 13 at 13:21
















  • Welcome to Mathematica.SE! Can you please put any code snippets in code blocks to improve the readability of the question?
    – Szabolcs
    Sep 13 at 13:05






  • 1




    What would be a result of Subtract[9,Plus[4,5]] if Plus is not included in your list?
    – Kuba♦
    Sep 13 at 13:10










  • In my example, the whole thing should be removed from my list of functions. This is because Subtract[9,Plus[4,5]] will evaluate to 0 and so it means the example is trivial as there will be another function in the list that is the same except it won't Subtract[0]
    – RedPython
    Sep 13 at 13:19











  • In that case why don't you just evaluate the whole thing and say that the result is 3?
    – Szabolcs
    Sep 13 at 13:21










  • Thanks for the advice Szabolcs. Advice appreciated. I have looked up how to do it and will ensure I follow your advice going forward.
    – RedPython
    Sep 13 at 13:21















Welcome to Mathematica.SE! Can you please put any code snippets in code blocks to improve the readability of the question?
– Szabolcs
Sep 13 at 13:05




Welcome to Mathematica.SE! Can you please put any code snippets in code blocks to improve the readability of the question?
– Szabolcs
Sep 13 at 13:05




1




1




What would be a result of Subtract[9,Plus[4,5]] if Plus is not included in your list?
– Kuba♦
Sep 13 at 13:10




What would be a result of Subtract[9,Plus[4,5]] if Plus is not included in your list?
– Kuba♦
Sep 13 at 13:10












In my example, the whole thing should be removed from my list of functions. This is because Subtract[9,Plus[4,5]] will evaluate to 0 and so it means the example is trivial as there will be another function in the list that is the same except it won't Subtract[0]
– RedPython
Sep 13 at 13:19





In my example, the whole thing should be removed from my list of functions. This is because Subtract[9,Plus[4,5]] will evaluate to 0 and so it means the example is trivial as there will be another function in the list that is the same except it won't Subtract[0]
– RedPython
Sep 13 at 13:19













In that case why don't you just evaluate the whole thing and say that the result is 3?
– Szabolcs
Sep 13 at 13:21




In that case why don't you just evaluate the whole thing and say that the result is 3?
– Szabolcs
Sep 13 at 13:21












Thanks for the advice Szabolcs. Advice appreciated. I have looked up how to do it and will ensure I follow your advice going forward.
– RedPython
Sep 13 at 13:21




Thanks for the advice Szabolcs. Advice appreciated. I have looked up how to do it and will ensure I follow your advice going forward.
– RedPython
Sep 13 at 13:21










2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










Update: Selecting from a list expressions those that do not contain subexpressions that divide by 1 or subtract 0:



list = HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]],
HoldForm[Plus[7, Times[2, Divide[16, Subtract[9, Plus[4, 1]]]]]],
HoldForm[Plus[3, Times[7, 9], 29]];

condition = FreeQ[#, (s_Subtract /; s == 0) | (d_Divide /; d == 1), ∞] &;
Select[list, condition]



7 + 2 16/(9 - (4 + 1)), 3 + 7 9 + 29




enter image description here



Original answer:



exp = Inactivate[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]];
Activate[exp, Divide | Subtract]



3 + 7*(9 - (4 + 5))*29




An alternative way to evaluate completely subexpressions with head Divide or Subtract
using RuleCondition (from WReach's answer in the q/a linked in Sjoerd's answer):



HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]] /. 
e : _Subtract | _Divide :> RuleCondition[e]



3 + 7 0 29







share|improve this answer





























    up vote
    5
    down vote













    The answer of kglr works if you only want to do the evaluations associated with Divide and Subtract. If instead you want every subexpression with these head to evaluate completely, use the following trick:



    Hold[
    Plus[
    3 + Times[7, Subtract[9, Plus[4, 5]],
    Divide[29, Subtract[4, 3]]]]
    ] /. _Subtract) :> With[x = expr, x /; True]



    See also the following answer:



    Replacement inside held expression






    share|improve this answer






















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      );
      );
      , "mathjax-editing");

      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f181814%2fevaluate-only-part-of-a-function%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      5
      down vote



      accepted










      Update: Selecting from a list expressions those that do not contain subexpressions that divide by 1 or subtract 0:



      list = HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]],
      HoldForm[Plus[7, Times[2, Divide[16, Subtract[9, Plus[4, 1]]]]]],
      HoldForm[Plus[3, Times[7, 9], 29]];

      condition = FreeQ[#, (s_Subtract /; s == 0) | (d_Divide /; d == 1), ∞] &;
      Select[list, condition]



      7 + 2 16/(9 - (4 + 1)), 3 + 7 9 + 29




      enter image description here



      Original answer:



      exp = Inactivate[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]];
      Activate[exp, Divide | Subtract]



      3 + 7*(9 - (4 + 5))*29




      An alternative way to evaluate completely subexpressions with head Divide or Subtract
      using RuleCondition (from WReach's answer in the q/a linked in Sjoerd's answer):



      HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]] /. 
      e : _Subtract | _Divide :> RuleCondition[e]



      3 + 7 0 29







      share|improve this answer


























        up vote
        5
        down vote



        accepted










        Update: Selecting from a list expressions those that do not contain subexpressions that divide by 1 or subtract 0:



        list = HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]],
        HoldForm[Plus[7, Times[2, Divide[16, Subtract[9, Plus[4, 1]]]]]],
        HoldForm[Plus[3, Times[7, 9], 29]];

        condition = FreeQ[#, (s_Subtract /; s == 0) | (d_Divide /; d == 1), ∞] &;
        Select[list, condition]



        7 + 2 16/(9 - (4 + 1)), 3 + 7 9 + 29




        enter image description here



        Original answer:



        exp = Inactivate[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]];
        Activate[exp, Divide | Subtract]



        3 + 7*(9 - (4 + 5))*29




        An alternative way to evaluate completely subexpressions with head Divide or Subtract
        using RuleCondition (from WReach's answer in the q/a linked in Sjoerd's answer):



        HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]] /. 
        e : _Subtract | _Divide :> RuleCondition[e]



        3 + 7 0 29







        share|improve this answer
























          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          Update: Selecting from a list expressions those that do not contain subexpressions that divide by 1 or subtract 0:



          list = HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]],
          HoldForm[Plus[7, Times[2, Divide[16, Subtract[9, Plus[4, 1]]]]]],
          HoldForm[Plus[3, Times[7, 9], 29]];

          condition = FreeQ[#, (s_Subtract /; s == 0) | (d_Divide /; d == 1), ∞] &;
          Select[list, condition]



          7 + 2 16/(9 - (4 + 1)), 3 + 7 9 + 29




          enter image description here



          Original answer:



          exp = Inactivate[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]];
          Activate[exp, Divide | Subtract]



          3 + 7*(9 - (4 + 5))*29




          An alternative way to evaluate completely subexpressions with head Divide or Subtract
          using RuleCondition (from WReach's answer in the q/a linked in Sjoerd's answer):



          HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]] /. 
          e : _Subtract | _Divide :> RuleCondition[e]



          3 + 7 0 29







          share|improve this answer














          Update: Selecting from a list expressions those that do not contain subexpressions that divide by 1 or subtract 0:



          list = HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]],
          HoldForm[Plus[7, Times[2, Divide[16, Subtract[9, Plus[4, 1]]]]]],
          HoldForm[Plus[3, Times[7, 9], 29]];

          condition = FreeQ[#, (s_Subtract /; s == 0) | (d_Divide /; d == 1), ∞] &;
          Select[list, condition]



          7 + 2 16/(9 - (4 + 1)), 3 + 7 9 + 29




          enter image description here



          Original answer:



          exp = Inactivate[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]];
          Activate[exp, Divide | Subtract]



          3 + 7*(9 - (4 + 5))*29




          An alternative way to evaluate completely subexpressions with head Divide or Subtract
          using RuleCondition (from WReach's answer in the q/a linked in Sjoerd's answer):



          HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]] /. 
          e : _Subtract | _Divide :> RuleCondition[e]



          3 + 7 0 29








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 13 at 18:24

























          answered Sep 13 at 13:10









          kglr

          162k8188387




          162k8188387




















              up vote
              5
              down vote













              The answer of kglr works if you only want to do the evaluations associated with Divide and Subtract. If instead you want every subexpression with these head to evaluate completely, use the following trick:



              Hold[
              Plus[
              3 + Times[7, Subtract[9, Plus[4, 5]],
              Divide[29, Subtract[4, 3]]]]
              ] /. _Subtract) :> With[x = expr, x /; True]



              See also the following answer:



              Replacement inside held expression






              share|improve this answer


























                up vote
                5
                down vote













                The answer of kglr works if you only want to do the evaluations associated with Divide and Subtract. If instead you want every subexpression with these head to evaluate completely, use the following trick:



                Hold[
                Plus[
                3 + Times[7, Subtract[9, Plus[4, 5]],
                Divide[29, Subtract[4, 3]]]]
                ] /. _Subtract) :> With[x = expr, x /; True]



                See also the following answer:



                Replacement inside held expression






                share|improve this answer
























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  The answer of kglr works if you only want to do the evaluations associated with Divide and Subtract. If instead you want every subexpression with these head to evaluate completely, use the following trick:



                  Hold[
                  Plus[
                  3 + Times[7, Subtract[9, Plus[4, 5]],
                  Divide[29, Subtract[4, 3]]]]
                  ] /. _Subtract) :> With[x = expr, x /; True]



                  See also the following answer:



                  Replacement inside held expression






                  share|improve this answer














                  The answer of kglr works if you only want to do the evaluations associated with Divide and Subtract. If instead you want every subexpression with these head to evaluate completely, use the following trick:



                  Hold[
                  Plus[
                  3 + Times[7, Subtract[9, Plus[4, 5]],
                  Divide[29, Subtract[4, 3]]]]
                  ] /. _Subtract) :> With[x = expr, x /; True]



                  See also the following answer:



                  Replacement inside held expression







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 13 at 14:14

























                  answered Sep 13 at 13:21









                  Sjoerd Smit

                  2,790515




                  2,790515



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181814%2fevaluate-only-part-of-a-function%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?