Re: [sympy] Re: How to avoid distributing a constant factor after differentiation?
On Wed, Feb 21, 2024 at 4:09 PM Chris Smith wrote: > > > There is a distribute() context manager > > I had forgotten about that, thanks for the reminder! We probably shouldn't advertise it too widely. Like I said, it's not as bad as the evaluate() context manager, but it has some of the same fundamental issues. The main point of it is that there is a global flag in the core to turn the distribution off, which should make it easier to remove it if anyone ever wants to put in that work. For instance we could make a decorator for the tests that makes sure a specific test passes with automatic distribution turned off (similar to this one https://github.com/sympy/sympy/blob/ae13ee38f54aa9c8944ef7d103dda778d2a39dbd/sympy/testing/pytest.py#L291). That way we can start fixing the code incrementally, instead of taking an "all or nothing" approach, which has failed in the past. Aaron Meurer > > /c > > On Wednesday, February 21, 2024 at 4:39:22 PM UTC-6 Aaron Meurer wrote: >> >> There is a distribute() context manager which lets you disable >> automatic distribution, though it's not pretty: >> >> >>> from sympy.core.parameters import distribute >> >>> with distribute(False): >> ... print(expr.diff(t)) >> 3*a*(t - t0)**2 + 2*b*(t - t0) >> >> While this is less dangerous than the similar evaluate() context >> manager, it is possible this could break something if you put too much >> under the context. >> >> As Chris said, we do want to eventually remove this automatic >> behavior, but it hasn't been easy to do as a lot of things depend on >> it currently. Rearranging things after the fact as Chris suggests is >> probably the better solution. There's really no guarantees about what >> the form of an expression from diff() will look like. >> >> Aaron Meurer >> >> On Sun, Feb 18, 2024 at 12:10 PM Chris Smith wrote: >> > >> > Autodistribution of Number into an Add is how SymPy works and there is no >> > flag for differentiation (or for many functions) that would prevent it. >> > Simply pass the expression to `factor_terms` to get it cleaned up. (But >> > that will extract a factor of `t-t0`, too, which you might not want so you >> > could use `Add(*[factor_terms(i) for i in expr.diff(t).args])` in this >> > case.) >> > >> > Some day autodistribution will go away and I expect that we will then ask >> > how to get constants to distribute into simple expressions. >> > >> > /c >> > >> > On Sunday, February 18, 2024 at 4:52:12 AM UTC-6 matthia...@gmail.com >> > wrote: >> >> >> >> Hi all. >> >> >> >> I have a simple expression: >> >> >> >> >>> import sympy as sp >> >> >>> a, b, t, t0 = sp.symbols('a b t t0') >> >> >>> expr = a*(t - t0)**3 + b*(t - t0)**2 >> >> >> >> And I would like to differentiate it with respect to t: >> >> >> >> >>> expr.diff(t) >> >> 3*a*(t - t0)**2 + b*(2*t - 2*t0) >> >> >> >> Why is the constant "2" distributed in the second term? >> >> It seems like an additional step that SymPy does, which doesn't really >> >> "improve" the situation in this case. >> >> Maybe there is a more general advantage that's just not visible in >> >> this simple case? >> >> But if that is so, would it be possible to tell SymPy to skip the >> >> distributing? >> >> >> >> To be clear, this is the result I was expecting: >> >> >> >> >>> expr.diff(t) >> >> 3*a*(t - t0)**2 + 2*b*(t - t0) >> >> >> >> For context, this question came up in a slightly more complicated >> >> situation: >> >> https://github.com/AudioSceneDescriptionFormat/splines/issues/31 >> >> >> >> cheers, >> >> Matthias >> > >> > -- >> > You received this message because you are subscribed to the Google Groups >> > "sympy" group. >> > To unsubscribe from this group and stop receiving emails from it, send an >> > email to sympy+un...@googlegroups.com. >> > To view this discussion on the web visit >> > https://groups.google.com/d/msgid/sympy/389f5dd9-1498-455a-b6cc-ffbbff89a9d7n%40googlegroups.com. > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sympy+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/7a4673b7-4c4e-4101-a4e5-9056847d000en%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6KEApm0Uj3Vrj_TC4HaF8acDPVUBQPu%3D4PL%3D-uYELtXTg%40mail.gmail.com.
Re: [sympy] Re: How to avoid distributing a constant factor after differentiation?
> There is a distribute() context manager I had forgotten about that, thanks for the reminder! /c On Wednesday, February 21, 2024 at 4:39:22 PM UTC-6 Aaron Meurer wrote: > There is a distribute() context manager which lets you disable > automatic distribution, though it's not pretty: > > >>> from sympy.core.parameters import distribute > >>> with distribute(False): > ... print(expr.diff(t)) > 3*a*(t - t0)**2 + 2*b*(t - t0) > > While this is less dangerous than the similar evaluate() context > manager, it is possible this could break something if you put too much > under the context. > > As Chris said, we do want to eventually remove this automatic > behavior, but it hasn't been easy to do as a lot of things depend on > it currently. Rearranging things after the fact as Chris suggests is > probably the better solution. There's really no guarantees about what > the form of an expression from diff() will look like. > > Aaron Meurer > > On Sun, Feb 18, 2024 at 12:10 PM Chris Smith wrote: > > > > Autodistribution of Number into an Add is how SymPy works and there is > no flag for differentiation (or for many functions) that would prevent it. > Simply pass the expression to `factor_terms` to get it cleaned up. (But > that will extract a factor of `t-t0`, too, which you might not want so you > could use `Add(*[factor_terms(i) for i in expr.diff(t).args])` in this > case.) > > > > Some day autodistribution will go away and I expect that we will then > ask how to get constants to distribute into simple expressions. > > > > /c > > > > On Sunday, February 18, 2024 at 4:52:12 AM UTC-6 matthia...@gmail.com > wrote: > >> > >> Hi all. > >> > >> I have a simple expression: > >> > >> >>> import sympy as sp > >> >>> a, b, t, t0 = sp.symbols('a b t t0') > >> >>> expr = a*(t - t0)**3 + b*(t - t0)**2 > >> > >> And I would like to differentiate it with respect to t: > >> > >> >>> expr.diff(t) > >> 3*a*(t - t0)**2 + b*(2*t - 2*t0) > >> > >> Why is the constant "2" distributed in the second term? > >> It seems like an additional step that SymPy does, which doesn't really > >> "improve" the situation in this case. > >> Maybe there is a more general advantage that's just not visible in > >> this simple case? > >> But if that is so, would it be possible to tell SymPy to skip the > distributing? > >> > >> To be clear, this is the result I was expecting: > >> > >> >>> expr.diff(t) > >> 3*a*(t - t0)**2 + 2*b*(t - t0) > >> > >> For context, this question came up in a slightly more complicated > >> situation: > https://github.com/AudioSceneDescriptionFormat/splines/issues/31 > >> > >> cheers, > >> Matthias > > > > -- > > You received this message because you are subscribed to the Google > Groups "sympy" group. > > To unsubscribe from this group and stop receiving emails from it, send > an email to sympy+un...@googlegroups.com. > > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/389f5dd9-1498-455a-b6cc-ffbbff89a9d7n%40googlegroups.com > . > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/7a4673b7-4c4e-4101-a4e5-9056847d000en%40googlegroups.com.
Re: [sympy] Re: How to avoid distributing a constant factor after differentiation?
There is a distribute() context manager which lets you disable automatic distribution, though it's not pretty: >>> from sympy.core.parameters import distribute >>> with distribute(False): ... print(expr.diff(t)) 3*a*(t - t0)**2 + 2*b*(t - t0) While this is less dangerous than the similar evaluate() context manager, it is possible this could break something if you put too much under the context. As Chris said, we do want to eventually remove this automatic behavior, but it hasn't been easy to do as a lot of things depend on it currently. Rearranging things after the fact as Chris suggests is probably the better solution. There's really no guarantees about what the form of an expression from diff() will look like. Aaron Meurer On Sun, Feb 18, 2024 at 12:10 PM Chris Smith wrote: > > Autodistribution of Number into an Add is how SymPy works and there is no > flag for differentiation (or for many functions) that would prevent it. > Simply pass the expression to `factor_terms` to get it cleaned up. (But that > will extract a factor of `t-t0`, too, which you might not want so you could > use `Add(*[factor_terms(i) for i in expr.diff(t).args])` in this case.) > > Some day autodistribution will go away and I expect that we will then ask how > to get constants to distribute into simple expressions. > > /c > > On Sunday, February 18, 2024 at 4:52:12 AM UTC-6 matthia...@gmail.com wrote: >> >> Hi all. >> >> I have a simple expression: >> >> >>> import sympy as sp >> >>> a, b, t, t0 = sp.symbols('a b t t0') >> >>> expr = a*(t - t0)**3 + b*(t - t0)**2 >> >> And I would like to differentiate it with respect to t: >> >> >>> expr.diff(t) >> 3*a*(t - t0)**2 + b*(2*t - 2*t0) >> >> Why is the constant "2" distributed in the second term? >> It seems like an additional step that SymPy does, which doesn't really >> "improve" the situation in this case. >> Maybe there is a more general advantage that's just not visible in >> this simple case? >> But if that is so, would it be possible to tell SymPy to skip the >> distributing? >> >> To be clear, this is the result I was expecting: >> >> >>> expr.diff(t) >> 3*a*(t - t0)**2 + 2*b*(t - t0) >> >> For context, this question came up in a slightly more complicated >> situation: https://github.com/AudioSceneDescriptionFormat/splines/issues/31 >> >> cheers, >> Matthias > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sympy+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/389f5dd9-1498-455a-b6cc-ffbbff89a9d7n%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2BfoChaZwhJRiOOp_HTRcbj%2B7EMm99u-xz-NgTuM59-eA%40mail.gmail.com.
[sympy] TrendPy: Time Series Regression with Python ON PIP now
Dear SymPy Community, I just wanted to share with you, that the TrendPy Project about Time Series Regressions with Python https://github.com/zolabar/trendPy is now uploaded to pip (as trendpy2, i.e. pip install trendpy2). Reminder: The trendpy2 package makes it easy to approximate time series regressions in a determinstic way. The following trends are supported: linear polynomial exponential trigonometric “free”, for max. three parameters, e.g. (the intial guess for a, b, c is 1.) A standalone feature of the trendpy2 package is, that it combines least-squares approaches, Fourier analysis approaches, numerical Python packages as Numpy and Scipy and the symbolic Python package Sympy for time series regressions. SymPy is makes it possible to easlily implement a "free" regression approach. A streamlit web app is now released, additionally to the voila web app. It can be tried out using this link (also linked in the github page) https://zolabar-trendpy-trendpy2-app-kfqshb.streamlit.app/ Testdata in https://github.com/zolabar/trendPy/tree/main/data Enjoy! Feedback is welcome ;) Regards, Zoufiné -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/f7477991-caba-46f7-894a-df81f513269cn%40googlegroups.com.