Implementing convolution using SymPy












4














I started using SymPy recently, and I implemented convolution using it.



def convolve(f,g,x,lower_limit,upper_limit):
y=Symbol('y')
h = g.subs(x,x-y)
return integrate(f*h,(y,lower_limit,upper_limit))


It seems to work for a few tests I've done.



Would like to know what you think of it, any improvements are appreciated.










share|improve this question





























    4














    I started using SymPy recently, and I implemented convolution using it.



    def convolve(f,g,x,lower_limit,upper_limit):
    y=Symbol('y')
    h = g.subs(x,x-y)
    return integrate(f*h,(y,lower_limit,upper_limit))


    It seems to work for a few tests I've done.



    Would like to know what you think of it, any improvements are appreciated.










    share|improve this question



























      4












      4








      4







      I started using SymPy recently, and I implemented convolution using it.



      def convolve(f,g,x,lower_limit,upper_limit):
      y=Symbol('y')
      h = g.subs(x,x-y)
      return integrate(f*h,(y,lower_limit,upper_limit))


      It seems to work for a few tests I've done.



      Would like to know what you think of it, any improvements are appreciated.










      share|improve this question















      I started using SymPy recently, and I implemented convolution using it.



      def convolve(f,g,x,lower_limit,upper_limit):
      y=Symbol('y')
      h = g.subs(x,x-y)
      return integrate(f*h,(y,lower_limit,upper_limit))


      It seems to work for a few tests I've done.



      Would like to know what you think of it, any improvements are appreciated.







      python signal-processing sympy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 1 '17 at 12:57









      200_success

      128k15150412




      128k15150412










      asked Sep 1 '17 at 9:23









      Itay4

      405




      405






















          2 Answers
          2






          active

          oldest

          votes


















          3














          Problematic Assumptions



          You implicitly assume that x is not Symbol('y'). If it is, then g.subs(x, x-y) will return a different, constant function (g'(x) = g(0)). You could check for this case and handle it specially, or just use a more uncommon symbol to reduce the risk.



          Formatting



          You have inconsistent spacing in your code. PEP8 recommends a space after every comma and on either side of binary operators (like =, -, and *):



          def convolve(f, g, x, lower_limit, upper_limit):
          y = Symbol('y')
          h = g.subs(x, x - y)
          return integrate(f * h, (y, lower_limit, upper_limit))





          share|improve this answer





























            3














            Your code shouldn't work:
            You are calculating:
            $$int_a^b f(t) , g(t - tau) ; dtau$$
            but convolution is defined as:
            $$f(t) , * , g(t) equiv int_{-infty}^{infty} f(tau) , g(t - tau) ; dtau$$
            so the default limits of integration should be $-infty$ to $infty$. More importantly you should use the proper argument for f (the integration variable). Finally, naming the integration variable y feels unusual.



            from sympy import oo, Symbol, integrate
            def convolve(f, g, t, lower_limit=-oo, upper_limit=oo):
            tau = Symbol('__very_unlikely_name__', real=True)
            return integrate(f.subs(t, tau) * g.subs(t, t - tau),
            (tau, lower_limit, upper_limit))





            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.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "196"
              };
              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%2fcodereview.stackexchange.com%2fquestions%2f174538%2fimplementing-convolution-using-sympy%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









              3














              Problematic Assumptions



              You implicitly assume that x is not Symbol('y'). If it is, then g.subs(x, x-y) will return a different, constant function (g'(x) = g(0)). You could check for this case and handle it specially, or just use a more uncommon symbol to reduce the risk.



              Formatting



              You have inconsistent spacing in your code. PEP8 recommends a space after every comma and on either side of binary operators (like =, -, and *):



              def convolve(f, g, x, lower_limit, upper_limit):
              y = Symbol('y')
              h = g.subs(x, x - y)
              return integrate(f * h, (y, lower_limit, upper_limit))





              share|improve this answer


























                3














                Problematic Assumptions



                You implicitly assume that x is not Symbol('y'). If it is, then g.subs(x, x-y) will return a different, constant function (g'(x) = g(0)). You could check for this case and handle it specially, or just use a more uncommon symbol to reduce the risk.



                Formatting



                You have inconsistent spacing in your code. PEP8 recommends a space after every comma and on either side of binary operators (like =, -, and *):



                def convolve(f, g, x, lower_limit, upper_limit):
                y = Symbol('y')
                h = g.subs(x, x - y)
                return integrate(f * h, (y, lower_limit, upper_limit))





                share|improve this answer
























                  3












                  3








                  3






                  Problematic Assumptions



                  You implicitly assume that x is not Symbol('y'). If it is, then g.subs(x, x-y) will return a different, constant function (g'(x) = g(0)). You could check for this case and handle it specially, or just use a more uncommon symbol to reduce the risk.



                  Formatting



                  You have inconsistent spacing in your code. PEP8 recommends a space after every comma and on either side of binary operators (like =, -, and *):



                  def convolve(f, g, x, lower_limit, upper_limit):
                  y = Symbol('y')
                  h = g.subs(x, x - y)
                  return integrate(f * h, (y, lower_limit, upper_limit))





                  share|improve this answer












                  Problematic Assumptions



                  You implicitly assume that x is not Symbol('y'). If it is, then g.subs(x, x-y) will return a different, constant function (g'(x) = g(0)). You could check for this case and handle it specially, or just use a more uncommon symbol to reduce the risk.



                  Formatting



                  You have inconsistent spacing in your code. PEP8 recommends a space after every comma and on either side of binary operators (like =, -, and *):



                  def convolve(f, g, x, lower_limit, upper_limit):
                  y = Symbol('y')
                  h = g.subs(x, x - y)
                  return integrate(f * h, (y, lower_limit, upper_limit))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 7 '17 at 3:01









                  Mego

                  20627




                  20627

























                      3














                      Your code shouldn't work:
                      You are calculating:
                      $$int_a^b f(t) , g(t - tau) ; dtau$$
                      but convolution is defined as:
                      $$f(t) , * , g(t) equiv int_{-infty}^{infty} f(tau) , g(t - tau) ; dtau$$
                      so the default limits of integration should be $-infty$ to $infty$. More importantly you should use the proper argument for f (the integration variable). Finally, naming the integration variable y feels unusual.



                      from sympy import oo, Symbol, integrate
                      def convolve(f, g, t, lower_limit=-oo, upper_limit=oo):
                      tau = Symbol('__very_unlikely_name__', real=True)
                      return integrate(f.subs(t, tau) * g.subs(t, t - tau),
                      (tau, lower_limit, upper_limit))





                      share|improve this answer




























                        3














                        Your code shouldn't work:
                        You are calculating:
                        $$int_a^b f(t) , g(t - tau) ; dtau$$
                        but convolution is defined as:
                        $$f(t) , * , g(t) equiv int_{-infty}^{infty} f(tau) , g(t - tau) ; dtau$$
                        so the default limits of integration should be $-infty$ to $infty$. More importantly you should use the proper argument for f (the integration variable). Finally, naming the integration variable y feels unusual.



                        from sympy import oo, Symbol, integrate
                        def convolve(f, g, t, lower_limit=-oo, upper_limit=oo):
                        tau = Symbol('__very_unlikely_name__', real=True)
                        return integrate(f.subs(t, tau) * g.subs(t, t - tau),
                        (tau, lower_limit, upper_limit))





                        share|improve this answer


























                          3












                          3








                          3






                          Your code shouldn't work:
                          You are calculating:
                          $$int_a^b f(t) , g(t - tau) ; dtau$$
                          but convolution is defined as:
                          $$f(t) , * , g(t) equiv int_{-infty}^{infty} f(tau) , g(t - tau) ; dtau$$
                          so the default limits of integration should be $-infty$ to $infty$. More importantly you should use the proper argument for f (the integration variable). Finally, naming the integration variable y feels unusual.



                          from sympy import oo, Symbol, integrate
                          def convolve(f, g, t, lower_limit=-oo, upper_limit=oo):
                          tau = Symbol('__very_unlikely_name__', real=True)
                          return integrate(f.subs(t, tau) * g.subs(t, t - tau),
                          (tau, lower_limit, upper_limit))





                          share|improve this answer














                          Your code shouldn't work:
                          You are calculating:
                          $$int_a^b f(t) , g(t - tau) ; dtau$$
                          but convolution is defined as:
                          $$f(t) , * , g(t) equiv int_{-infty}^{infty} f(tau) , g(t - tau) ; dtau$$
                          so the default limits of integration should be $-infty$ to $infty$. More importantly you should use the proper argument for f (the integration variable). Finally, naming the integration variable y feels unusual.



                          from sympy import oo, Symbol, integrate
                          def convolve(f, g, t, lower_limit=-oo, upper_limit=oo):
                          tau = Symbol('__very_unlikely_name__', real=True)
                          return integrate(f.subs(t, tau) * g.subs(t, t - tau),
                          (tau, lower_limit, upper_limit))






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 24 mins ago

























                          answered Aug 27 at 21:10









                          JakeI

                          313




                          313






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Code Review 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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2fcodereview.stackexchange.com%2fquestions%2f174538%2fimplementing-convolution-using-sympy%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

                              List directoties down one level, excluding some named directories and files

                              list processes belonging to a network namespace

                              list systemd RuntimeDirectory mounts