[sympy] defining a derivative
Dear List, i am writing since i would like to define the output of the derivative of a function, and i don't have a clue of how to achieve it to explain what i wish to do, let's consider the following script from sympy import * u = symbols('u') der = symbols('der') e = symbols('e', cls=Function)(u) s = symbols('s', cls=Function)(e) Derivative(e,u) = der #essentially i would like to teach to sympy to use a symbol for the Derivative ---> but here i get "SyntaxError: can't assign to function call" print(diff(e,u)) print(diff(s,e)) print(diff(s,u)) #here i would like "der" to be replaced within the chain rule any suggestion would be very welcome... regards Riccardo -- 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 post to this group, send email to sympy@googlegroups.com. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/a4511ef2-2a10-4dbe-b6a0-01fe2fc47a05%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [sympy] defining a derivative
If you want to define advanced things you need to subclass from Function rather than using symbols(cls=Function). For derivatives, you should define fdiff, which should return the derivative of the function without consideration of the chain rule. For example, search for "fdiff" in this file to see some examples for exp, log, and LambertW https://github.com/sympy/sympy/blob/master/sympy/functions/elementary/exponential.py. Aaron Meurer On Fri, Oct 14, 2016 at 4:53 AM, Riccardo Rossiwrote: > Dear List, > > i am writing since i would like to define the output of the derivative of a > function, and i don't have a clue of how to achieve it > > to explain what i wish to do, let's consider the following script > > from sympy import * > > u = symbols('u') > der = symbols('der') > e = symbols('e', cls=Function)(u) > s = symbols('s', cls=Function)(e) > Derivative(e,u) = der #essentially i would like to teach to sympy to use a > symbol for the Derivative > ---> but here i get "SyntaxError: can't assign to function call" > > print(diff(e,u)) > print(diff(s,e)) > print(diff(s,u)) #here i would like "der" to be replaced within the chain > rule > > any suggestion would be very welcome... > > regards > Riccardo > > > -- > 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 post to this group, send email to sympy@googlegroups.com. > Visit this group at https://groups.google.com/group/sympy. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/a4511ef2-2a10-4dbe-b6a0-01fe2fc47a05%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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 post to this group, send email to sympy@googlegroups.com. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2Bn_jrwzkANbe6tSF_EdrM2LeZrFDjEyVLA7puoNvCCuA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: [sympy] Re: Lambdify performance with repeated subexpressions
To achieve this with lambdify you should call cse() first, then lambdify each expression separately. We ought to build a wrapper to make this easier (or a cse=True flag to lambdify). Aaron Meurer On Fri, Oct 14, 2016 at 11:06 AM, Björn Dahlgrenwrote: > > > On Friday, 14 October 2016 15:09:46 UTC+2, Albert Pető wrote: >> >> Hi, I plan to heavily use a function generated with lambdify from a sympy >> expression which has repeated occurences of some subexpressions. > > > This functionality is available in symengine: > https://github.com/symengine/symengine.py/blob/master/symengine/lib/symengine_wrapper.pyx#L2827 > (which you might want to consider if you are concerned about speed). If you > want to stay pure python and only use sympy you can look at the > source code there for how to achieve this (we are creating a closure). > > Best, > Björn > > -- > 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 post to this group, send email to sympy@googlegroups.com. > Visit this group at https://groups.google.com/group/sympy. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/45c5d1d6-b5ea-458d-92f4-096cd4b51b8d%40googlegroups.com. > > For more options, visit https://groups.google.com/d/optout. -- 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 post to this group, send email to sympy@googlegroups.com. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6Kw-S0g%2Bi5OQs_-ukgv13e%3D%2Bujhy0_DQrc6LyVVrwnVEw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
[sympy] Re: Lambdify performance with repeated subexpressions
On Friday, 14 October 2016 15:09:46 UTC+2, Albert Pető wrote: > > Hi, I plan to heavily use a function generated with lambdify from a sympy > expression which has repeated occurences of some subexpressions. > This functionality is available in symengine: https://github.com/symengine/symengine.py/blob/master/symengine/lib/symengine_wrapper.pyx#L2827 (which you might want to consider if you are concerned about speed). If you want to stay pure python and only use sympy you can look at the source code there for how to achieve this (we are creating a closure). Best, Björn -- 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 post to this group, send email to sympy@googlegroups.com. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/45c5d1d6-b5ea-458d-92f4-096cd4b51b8d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[sympy] Lambdify performance with repeated subexpressions
Hi, I would like to evaluate certain functions created with lambdify. Those functions would come from rotations and would have a lot of trigonometric function invocations in them with the same parameters. For example, lets suppose that it would contain cos(a) many times. I plan to heavily use those generated functions, but I am concerned about the performance loss of evaluating cos(a) multiple times in one function invocation, when it would suffice to evaluate it only once. I am not sure how lambdify treats these cases, i.e. if it would call cos(a) multiple times or not, or if I have to specify it explicitly. Can somebody tell me how it works? Thanks for your help! :) -- 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 post to this group, send email to sympy@googlegroups.com. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/dc6f1538-7bfe-4522-bced-4f6271a6fdbb%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[sympy] Lambdify performance with repeated subexpressions
Hi, I plan to heavily use a function generated with lambdify from a sympy expression which has repeated occurences of some subexpressions. Specifically it will have a lot of cosine and sine expressions with the same arguments. I don't know if lambdify can notice this pattern and evaluate that subexpression only once, or if it will call it (the numeric sine and cosine functions) as many times as it appears, or if I have to specify it somehow. I am afraid that it would mean a considerable performance loss if, for example, it will have to call 2-3 times as many trigonometric functions as a hand-written code, since I plan to use it a lot. Can somebody provide me some information? Thanks in advance :) -- 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 post to this group, send email to sympy@googlegroups.com. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/aeccf597-596e-4b59-b86f-c6c91210a844%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[sympy] Re: defining a derivative
What I am using right now dear Riccardo is the following: Suppose that my variable sigma(define as a symbol) depends of u, but at the same time this u is vectorial variable, which means for example in 3D the variable will be : u = [u_0, u_1, u_2], all the components of u are defined as symbols. Now I only need to do the following: In: sigma(*u) #so sigma depends of u_0, u_1, u_2, and if we use diff: In: print(sigma) Out: sigma(u_0,u_1,u_2) In: diff(sigma, u_1) Out: Derivative(sigma(u_0,u_1,u_2), u_1) -- 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 post to this group, send email to sympy@googlegroups.com. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/66686d14-5f92-4378-b08a-5d304c177b44%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.