Optimal way to extract “positive part” of a multivariate polynomialGet the first positive coefficient in...

First amendment and employment: Can an employer terminate you for speech?

The pronunciation of "protester"

A stranger from Norway wants to have money delivered to me

Are there any financial disadvantages to living significantly "below your means"?

How can a surrogate pass on genes to a fertilized embryo?

Accidentals - some in brackets, some not

Dereferencing a pointer in a 'for' loop initializer creates a segmentation fault

Non-OR journals which regularly publish OR research

How would I as a DM create a smart phone-like spell/device my players could use?

Why isn’t SHA-3 in wider use?

Why do oscilloscopes use SMPS instead of linear power supply?

Author changing name

How should an administrative assistant reply to student addressing them as "Professor" or "Doctor"?

Why aren’t emergency services using callsigns?

Infeasibility in mathematical optimization models

Am I overreacting to my team leader's unethical requests?

Does two puncture wounds mean venomous snake?

As a 16 year old, how can I keep my money safe from my mother?

In a topological space if there exists a loop that cannot be contracted to a point does there exist a simple loop that cannot be contracted also?

Why did the RAAF procure the F/A-18 despite being purpose-built for carriers?

How can I tell if a flight itinerary is fake?

If a Contingency spell has been cast on a creature, does the Simulacrum spell transfer the contingent spell to its duplicate?

Are there any differences in causality between linear and logistic regression?

What are the uses and limitations of Persuasion, Insight, and Deception against other PCs?



Optimal way to extract “positive part” of a multivariate polynomial


Get the first positive coefficient in polynomial?Multivariate Polynomial ManipulationHow do I find the constant term of a multivariate polynomial?Fishing for monomials in a nested or partially factored polynomial streamFunny behavior when computing dot product of coefficients with high-order polynomialsEfficiently strip off coefficients in front of variables?Easiest way to extract the coefficient of a polynomialGet the homogeneous part of a polynomial






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







4












$begingroup$


I've got multivariate polynomials with numerical coefficients, like e. g.



p - s - p q^2 s^2 + 3 r s^2 + 3 r^2 s^2 - p r^2 s^2 - 2 q r^2 s^2 - 2 r^3 s^2 + s^3


and I would like to take the sum of those monomials with positive coefficients only.



Although for my purposes



FromCoefficientRules[Select[CoefficientRules[poly],Last[#]>0&],Variables[poly]]


seems to be quick enough, it involves translating to another form and back, so I feel there must be a more optimal way to do it, probably using some tricks with the internal representation of polynomials.



Is there?










share|improve this question









$endgroup$





















    4












    $begingroup$


    I've got multivariate polynomials with numerical coefficients, like e. g.



    p - s - p q^2 s^2 + 3 r s^2 + 3 r^2 s^2 - p r^2 s^2 - 2 q r^2 s^2 - 2 r^3 s^2 + s^3


    and I would like to take the sum of those monomials with positive coefficients only.



    Although for my purposes



    FromCoefficientRules[Select[CoefficientRules[poly],Last[#]>0&],Variables[poly]]


    seems to be quick enough, it involves translating to another form and back, so I feel there must be a more optimal way to do it, probably using some tricks with the internal representation of polynomials.



    Is there?










    share|improve this question









    $endgroup$

















      4












      4








      4





      $begingroup$


      I've got multivariate polynomials with numerical coefficients, like e. g.



      p - s - p q^2 s^2 + 3 r s^2 + 3 r^2 s^2 - p r^2 s^2 - 2 q r^2 s^2 - 2 r^3 s^2 + s^3


      and I would like to take the sum of those monomials with positive coefficients only.



      Although for my purposes



      FromCoefficientRules[Select[CoefficientRules[poly],Last[#]>0&],Variables[poly]]


      seems to be quick enough, it involves translating to another form and back, so I feel there must be a more optimal way to do it, probably using some tricks with the internal representation of polynomials.



      Is there?










      share|improve this question









      $endgroup$




      I've got multivariate polynomials with numerical coefficients, like e. g.



      p - s - p q^2 s^2 + 3 r s^2 + 3 r^2 s^2 - p r^2 s^2 - 2 q r^2 s^2 - 2 r^3 s^2 + s^3


      and I would like to take the sum of those monomials with positive coefficients only.



      Although for my purposes



      FromCoefficientRules[Select[CoefficientRules[poly],Last[#]>0&],Variables[poly]]


      seems to be quick enough, it involves translating to another form and back, so I feel there must be a more optimal way to do it, probably using some tricks with the internal representation of polynomials.



      Is there?







      performance-tuning polynomials algebraic-manipulation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 13 hours ago









      მამუკა ჯიბლაძემამუკა ჯიბლაძე

      1,0725 silver badges20 bronze badges




      1,0725 silver badges20 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          3












          $begingroup$

          This will delete the terms written with a leading minus sign:



          nixneg[p_Plus] := DeleteCases[p, _?Internal`SyntacticNegativeQ];
          nixneg[_?Internal`SyntacticNegativeQ] := 0; (* 1-term case: neg *)
          nixneg[p_] := p; (* 1-term case: nonneg *)


          OP's example:



          nixneg[poly]  (* use nixneg[Expand@poly] if needed *)
          (* p + 3 r s^2 + 3 r^2 s^2 + s^3 *)


          Deletes negative constant terms, too:



          nixneg[poly + 100]
          nixneg[poly - 100]
          (*
          100 + p + 3 r s^2 + 3 r^2 s^2 + s^3
          p + 3 r s^2 + 3 r^2 s^2 + s^3
          *)





          share|improve this answer









          $endgroup$























            4












            $begingroup$

            Assuming poly is homogeneous (as in the example in the OP),



            poly /. Times[_?Negative, _] -> 0





            share|improve this answer









            $endgroup$















            • $begingroup$
              Nice! But you mean without constant term rather than homogeneous, right? My polynomials might have constant terms, actually, but I am sure one can deal with them very quickly too
              $endgroup$
              – მამუკა ჯიბლაძე
              11 hours ago










            • $begingroup$
              @მამუკაჯიბლაძე oh, yes, I meant without constant term!
              $endgroup$
              – AccidentalFourierTransform
              11 hours ago










            • $begingroup$
              Sorry! - have to accept the most complete one
              $endgroup$
              – მამუკა ჯიბლაძე
              4 hours ago










            • $begingroup$
              @მამუკაჯიბლაძე no need to apologise, those are great answers too, clearly better than mine :-)
              $endgroup$
              – AccidentalFourierTransform
              4 hours ago



















            3












            $begingroup$

            exp = p - s - p q^2 s^2 + 3 r s^2 + 3 r^2 s^2 - p r^2 s^2 - 2 q r^2 s^2 - 2 r^3 s^2 + s^3


            Few additional ways to use Internal`SyntacticNegativeQ:



            Select[Not @* Internal`SyntacticNegativeQ] @ exp



            p + 3 r s^2 + 3 r^2 s^2 + s^3







            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%2f203555%2foptimal-way-to-extract-positive-part-of-a-multivariate-polynomial%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3












              $begingroup$

              This will delete the terms written with a leading minus sign:



              nixneg[p_Plus] := DeleteCases[p, _?Internal`SyntacticNegativeQ];
              nixneg[_?Internal`SyntacticNegativeQ] := 0; (* 1-term case: neg *)
              nixneg[p_] := p; (* 1-term case: nonneg *)


              OP's example:



              nixneg[poly]  (* use nixneg[Expand@poly] if needed *)
              (* p + 3 r s^2 + 3 r^2 s^2 + s^3 *)


              Deletes negative constant terms, too:



              nixneg[poly + 100]
              nixneg[poly - 100]
              (*
              100 + p + 3 r s^2 + 3 r^2 s^2 + s^3
              p + 3 r s^2 + 3 r^2 s^2 + s^3
              *)





              share|improve this answer









              $endgroup$




















                3












                $begingroup$

                This will delete the terms written with a leading minus sign:



                nixneg[p_Plus] := DeleteCases[p, _?Internal`SyntacticNegativeQ];
                nixneg[_?Internal`SyntacticNegativeQ] := 0; (* 1-term case: neg *)
                nixneg[p_] := p; (* 1-term case: nonneg *)


                OP's example:



                nixneg[poly]  (* use nixneg[Expand@poly] if needed *)
                (* p + 3 r s^2 + 3 r^2 s^2 + s^3 *)


                Deletes negative constant terms, too:



                nixneg[poly + 100]
                nixneg[poly - 100]
                (*
                100 + p + 3 r s^2 + 3 r^2 s^2 + s^3
                p + 3 r s^2 + 3 r^2 s^2 + s^3
                *)





                share|improve this answer









                $endgroup$


















                  3












                  3








                  3





                  $begingroup$

                  This will delete the terms written with a leading minus sign:



                  nixneg[p_Plus] := DeleteCases[p, _?Internal`SyntacticNegativeQ];
                  nixneg[_?Internal`SyntacticNegativeQ] := 0; (* 1-term case: neg *)
                  nixneg[p_] := p; (* 1-term case: nonneg *)


                  OP's example:



                  nixneg[poly]  (* use nixneg[Expand@poly] if needed *)
                  (* p + 3 r s^2 + 3 r^2 s^2 + s^3 *)


                  Deletes negative constant terms, too:



                  nixneg[poly + 100]
                  nixneg[poly - 100]
                  (*
                  100 + p + 3 r s^2 + 3 r^2 s^2 + s^3
                  p + 3 r s^2 + 3 r^2 s^2 + s^3
                  *)





                  share|improve this answer









                  $endgroup$



                  This will delete the terms written with a leading minus sign:



                  nixneg[p_Plus] := DeleteCases[p, _?Internal`SyntacticNegativeQ];
                  nixneg[_?Internal`SyntacticNegativeQ] := 0; (* 1-term case: neg *)
                  nixneg[p_] := p; (* 1-term case: nonneg *)


                  OP's example:



                  nixneg[poly]  (* use nixneg[Expand@poly] if needed *)
                  (* p + 3 r s^2 + 3 r^2 s^2 + s^3 *)


                  Deletes negative constant terms, too:



                  nixneg[poly + 100]
                  nixneg[poly - 100]
                  (*
                  100 + p + 3 r s^2 + 3 r^2 s^2 + s^3
                  p + 3 r s^2 + 3 r^2 s^2 + s^3
                  *)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 9 hours ago









                  Michael E2Michael E2

                  157k13 gold badges215 silver badges511 bronze badges




                  157k13 gold badges215 silver badges511 bronze badges




























                      4












                      $begingroup$

                      Assuming poly is homogeneous (as in the example in the OP),



                      poly /. Times[_?Negative, _] -> 0





                      share|improve this answer









                      $endgroup$















                      • $begingroup$
                        Nice! But you mean without constant term rather than homogeneous, right? My polynomials might have constant terms, actually, but I am sure one can deal with them very quickly too
                        $endgroup$
                        – მამუკა ჯიბლაძე
                        11 hours ago










                      • $begingroup$
                        @მამუკაჯიბლაძე oh, yes, I meant without constant term!
                        $endgroup$
                        – AccidentalFourierTransform
                        11 hours ago










                      • $begingroup$
                        Sorry! - have to accept the most complete one
                        $endgroup$
                        – მამუკა ჯიბლაძე
                        4 hours ago










                      • $begingroup$
                        @მამუკაჯიბლაძე no need to apologise, those are great answers too, clearly better than mine :-)
                        $endgroup$
                        – AccidentalFourierTransform
                        4 hours ago
















                      4












                      $begingroup$

                      Assuming poly is homogeneous (as in the example in the OP),



                      poly /. Times[_?Negative, _] -> 0





                      share|improve this answer









                      $endgroup$















                      • $begingroup$
                        Nice! But you mean without constant term rather than homogeneous, right? My polynomials might have constant terms, actually, but I am sure one can deal with them very quickly too
                        $endgroup$
                        – მამუკა ჯიბლაძე
                        11 hours ago










                      • $begingroup$
                        @მამუკაჯიბლაძე oh, yes, I meant without constant term!
                        $endgroup$
                        – AccidentalFourierTransform
                        11 hours ago










                      • $begingroup$
                        Sorry! - have to accept the most complete one
                        $endgroup$
                        – მამუკა ჯიბლაძე
                        4 hours ago










                      • $begingroup$
                        @მამუკაჯიბლაძე no need to apologise, those are great answers too, clearly better than mine :-)
                        $endgroup$
                        – AccidentalFourierTransform
                        4 hours ago














                      4












                      4








                      4





                      $begingroup$

                      Assuming poly is homogeneous (as in the example in the OP),



                      poly /. Times[_?Negative, _] -> 0





                      share|improve this answer









                      $endgroup$



                      Assuming poly is homogeneous (as in the example in the OP),



                      poly /. Times[_?Negative, _] -> 0






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 13 hours ago









                      AccidentalFourierTransformAccidentalFourierTransform

                      6,5791 gold badge12 silver badges45 bronze badges




                      6,5791 gold badge12 silver badges45 bronze badges















                      • $begingroup$
                        Nice! But you mean without constant term rather than homogeneous, right? My polynomials might have constant terms, actually, but I am sure one can deal with them very quickly too
                        $endgroup$
                        – მამუკა ჯიბლაძე
                        11 hours ago










                      • $begingroup$
                        @მამუკაჯიბლაძე oh, yes, I meant without constant term!
                        $endgroup$
                        – AccidentalFourierTransform
                        11 hours ago










                      • $begingroup$
                        Sorry! - have to accept the most complete one
                        $endgroup$
                        – მამუკა ჯიბლაძე
                        4 hours ago










                      • $begingroup$
                        @მამუკაჯიბლაძე no need to apologise, those are great answers too, clearly better than mine :-)
                        $endgroup$
                        – AccidentalFourierTransform
                        4 hours ago


















                      • $begingroup$
                        Nice! But you mean without constant term rather than homogeneous, right? My polynomials might have constant terms, actually, but I am sure one can deal with them very quickly too
                        $endgroup$
                        – მამუკა ჯიბლაძე
                        11 hours ago










                      • $begingroup$
                        @მამუკაჯიბლაძე oh, yes, I meant without constant term!
                        $endgroup$
                        – AccidentalFourierTransform
                        11 hours ago










                      • $begingroup$
                        Sorry! - have to accept the most complete one
                        $endgroup$
                        – მამუკა ჯიბლაძე
                        4 hours ago










                      • $begingroup$
                        @მამუკაჯიბლაძე no need to apologise, those are great answers too, clearly better than mine :-)
                        $endgroup$
                        – AccidentalFourierTransform
                        4 hours ago
















                      $begingroup$
                      Nice! But you mean without constant term rather than homogeneous, right? My polynomials might have constant terms, actually, but I am sure one can deal with them very quickly too
                      $endgroup$
                      – მამუკა ჯიბლაძე
                      11 hours ago




                      $begingroup$
                      Nice! But you mean without constant term rather than homogeneous, right? My polynomials might have constant terms, actually, but I am sure one can deal with them very quickly too
                      $endgroup$
                      – მამუკა ჯიბლაძე
                      11 hours ago












                      $begingroup$
                      @მამუკაჯიბლაძე oh, yes, I meant without constant term!
                      $endgroup$
                      – AccidentalFourierTransform
                      11 hours ago




                      $begingroup$
                      @მამუკაჯიბლაძე oh, yes, I meant without constant term!
                      $endgroup$
                      – AccidentalFourierTransform
                      11 hours ago












                      $begingroup$
                      Sorry! - have to accept the most complete one
                      $endgroup$
                      – მამუკა ჯიბლაძე
                      4 hours ago




                      $begingroup$
                      Sorry! - have to accept the most complete one
                      $endgroup$
                      – მამუკა ჯიბლაძე
                      4 hours ago












                      $begingroup$
                      @მამუკაჯიბლაძე no need to apologise, those are great answers too, clearly better than mine :-)
                      $endgroup$
                      – AccidentalFourierTransform
                      4 hours ago




                      $begingroup$
                      @მამუკაჯიბლაძე no need to apologise, those are great answers too, clearly better than mine :-)
                      $endgroup$
                      – AccidentalFourierTransform
                      4 hours ago











                      3












                      $begingroup$

                      exp = p - s - p q^2 s^2 + 3 r s^2 + 3 r^2 s^2 - p r^2 s^2 - 2 q r^2 s^2 - 2 r^3 s^2 + s^3


                      Few additional ways to use Internal`SyntacticNegativeQ:



                      Select[Not @* Internal`SyntacticNegativeQ] @ exp



                      p + 3 r s^2 + 3 r^2 s^2 + s^3







                      share|improve this answer









                      $endgroup$




















                        3












                        $begingroup$

                        exp = p - s - p q^2 s^2 + 3 r s^2 + 3 r^2 s^2 - p r^2 s^2 - 2 q r^2 s^2 - 2 r^3 s^2 + s^3


                        Few additional ways to use Internal`SyntacticNegativeQ:



                        Select[Not @* Internal`SyntacticNegativeQ] @ exp



                        p + 3 r s^2 + 3 r^2 s^2 + s^3







                        share|improve this answer









                        $endgroup$


















                          3












                          3








                          3





                          $begingroup$

                          exp = p - s - p q^2 s^2 + 3 r s^2 + 3 r^2 s^2 - p r^2 s^2 - 2 q r^2 s^2 - 2 r^3 s^2 + s^3


                          Few additional ways to use Internal`SyntacticNegativeQ:



                          Select[Not @* Internal`SyntacticNegativeQ] @ exp



                          p + 3 r s^2 + 3 r^2 s^2 + s^3







                          share|improve this answer









                          $endgroup$



                          exp = p - s - p q^2 s^2 + 3 r s^2 + 3 r^2 s^2 - p r^2 s^2 - 2 q r^2 s^2 - 2 r^3 s^2 + s^3


                          Few additional ways to use Internal`SyntacticNegativeQ:



                          Select[Not @* Internal`SyntacticNegativeQ] @ exp



                          p + 3 r s^2 + 3 r^2 s^2 + s^3








                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 8 hours ago









                          kglrkglr

                          210k10 gold badges241 silver badges480 bronze badges




                          210k10 gold badges241 silver badges480 bronze badges

































                              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%2f203555%2foptimal-way-to-extract-positive-part-of-a-multivariate-polynomial%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

                              Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

                              The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

                              Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...