sympy match has difficulty with modulo










1















Following this StackOverflow question, and a fix provided by @smichr, I tried the following:



>>> from sympy import *
>>> k,m,n = symbols("k m n", integer=True)
>>> (3*k+4)%2 - k%2
0 # it works with the fix above. Thank you!
>>> p = Wild('p')
>>> q = Wild('q')
>>> e = (2*k+7)%5 + 7*k+7
>>> e
7*k + Mod(2*k + 2, 5) + 7
>>> e.match(p%5+p)
>>> e.match((p+5*q)%5+p)
>>> e.match(p%5+p+5*q)
q_: k + 1, p_: 2*k + 2


I expected the first or second e.match to work for me (p:7*k+7 for first one and p:7*k+7,q:-k for second one), but it didn't. Is this a bug? If yes, is there a fix/workaround?










share|improve this question




























    1















    Following this StackOverflow question, and a fix provided by @smichr, I tried the following:



    >>> from sympy import *
    >>> k,m,n = symbols("k m n", integer=True)
    >>> (3*k+4)%2 - k%2
    0 # it works with the fix above. Thank you!
    >>> p = Wild('p')
    >>> q = Wild('q')
    >>> e = (2*k+7)%5 + 7*k+7
    >>> e
    7*k + Mod(2*k + 2, 5) + 7
    >>> e.match(p%5+p)
    >>> e.match((p+5*q)%5+p)
    >>> e.match(p%5+p+5*q)
    q_: k + 1, p_: 2*k + 2


    I expected the first or second e.match to work for me (p:7*k+7 for first one and p:7*k+7,q:-k for second one), but it didn't. Is this a bug? If yes, is there a fix/workaround?










    share|improve this question


























      1












      1








      1








      Following this StackOverflow question, and a fix provided by @smichr, I tried the following:



      >>> from sympy import *
      >>> k,m,n = symbols("k m n", integer=True)
      >>> (3*k+4)%2 - k%2
      0 # it works with the fix above. Thank you!
      >>> p = Wild('p')
      >>> q = Wild('q')
      >>> e = (2*k+7)%5 + 7*k+7
      >>> e
      7*k + Mod(2*k + 2, 5) + 7
      >>> e.match(p%5+p)
      >>> e.match((p+5*q)%5+p)
      >>> e.match(p%5+p+5*q)
      q_: k + 1, p_: 2*k + 2


      I expected the first or second e.match to work for me (p:7*k+7 for first one and p:7*k+7,q:-k for second one), but it didn't. Is this a bug? If yes, is there a fix/workaround?










      share|improve this question
















      Following this StackOverflow question, and a fix provided by @smichr, I tried the following:



      >>> from sympy import *
      >>> k,m,n = symbols("k m n", integer=True)
      >>> (3*k+4)%2 - k%2
      0 # it works with the fix above. Thank you!
      >>> p = Wild('p')
      >>> q = Wild('q')
      >>> e = (2*k+7)%5 + 7*k+7
      >>> e
      7*k + Mod(2*k + 2, 5) + 7
      >>> e.match(p%5+p)
      >>> e.match((p+5*q)%5+p)
      >>> e.match(p%5+p+5*q)
      q_: k + 1, p_: 2*k + 2


      I expected the first or second e.match to work for me (p:7*k+7 for first one and p:7*k+7,q:-k for second one), but it didn't. Is this a bug? If yes, is there a fix/workaround?







      python match sympy modulo






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 18:15







      Salem Derisavi

















      asked Nov 14 '18 at 17:04









      Salem DerisaviSalem Derisavi

      978




      978






















          1 Answer
          1






          active

          oldest

          votes


















          1














          (Copying the same thing I wrote on the SymPy issue)



          match has very limited mathematical knowledge. It doesn't know that the 2*k in the Mod can be replaced with 7*k.



          I'm not sure if this would be easy to fix. match is already pretty complicated with the limited mathematical matching it does. A better design would likely be needed to solve problems like this. Actually this strikes me as the sort of problem that you would need an SMT solver to solve, but maybe there are simpler algorithms that could do it.



          Sorry this doesn't give a direct answer on how to solve the problem.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f53305361%2fsympy-match-has-difficulty-with-modulo%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            (Copying the same thing I wrote on the SymPy issue)



            match has very limited mathematical knowledge. It doesn't know that the 2*k in the Mod can be replaced with 7*k.



            I'm not sure if this would be easy to fix. match is already pretty complicated with the limited mathematical matching it does. A better design would likely be needed to solve problems like this. Actually this strikes me as the sort of problem that you would need an SMT solver to solve, but maybe there are simpler algorithms that could do it.



            Sorry this doesn't give a direct answer on how to solve the problem.






            share|improve this answer



























              1














              (Copying the same thing I wrote on the SymPy issue)



              match has very limited mathematical knowledge. It doesn't know that the 2*k in the Mod can be replaced with 7*k.



              I'm not sure if this would be easy to fix. match is already pretty complicated with the limited mathematical matching it does. A better design would likely be needed to solve problems like this. Actually this strikes me as the sort of problem that you would need an SMT solver to solve, but maybe there are simpler algorithms that could do it.



              Sorry this doesn't give a direct answer on how to solve the problem.






              share|improve this answer

























                1












                1








                1







                (Copying the same thing I wrote on the SymPy issue)



                match has very limited mathematical knowledge. It doesn't know that the 2*k in the Mod can be replaced with 7*k.



                I'm not sure if this would be easy to fix. match is already pretty complicated with the limited mathematical matching it does. A better design would likely be needed to solve problems like this. Actually this strikes me as the sort of problem that you would need an SMT solver to solve, but maybe there are simpler algorithms that could do it.



                Sorry this doesn't give a direct answer on how to solve the problem.






                share|improve this answer













                (Copying the same thing I wrote on the SymPy issue)



                match has very limited mathematical knowledge. It doesn't know that the 2*k in the Mod can be replaced with 7*k.



                I'm not sure if this would be easy to fix. match is already pretty complicated with the limited mathematical matching it does. A better design would likely be needed to solve problems like this. Actually this strikes me as the sort of problem that you would need an SMT solver to solve, but maybe there are simpler algorithms that could do it.



                Sorry this doesn't give a direct answer on how to solve the problem.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 22:10









                asmeurerasmeurer

                58.2k16113186




                58.2k16113186





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


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

                    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%2fstackoverflow.com%2fquestions%2f53305361%2fsympy-match-has-difficulty-with-modulo%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







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Museum of Modern and Contemporary Art of Trento and Rovereto

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully