Thanks for the answer (and for the question too). I had been thinking of a way to implement methods that would be independent of number of coordinates. I just ran some test code and this method works great. I think I can use this for the proposed vector calculus module. Also, I suppose we can use this for calculating gradients, and perhaps with some changes, for calculating curl too (by providing Lambda with a tuple of functions for each component of the vector field).
On Thursday, April 18, 2013 5:02:13 AM UTC+5:30, Aaron Meurer wrote: > > You can do the whole thing symbolically with Lambda > > [Lambda(f, Derivative(f, coord)) for coord in coordinates] > > Unlike the normal list comprehension, Derivative is unevaluated, so > you create the object before you actually have f, and unlike the > lambda version, you can access and manipulate the object using .args > .subs and all the other standard SymPy constructs. > > The disadvantage here is that when you call the functions, it will > give you an unevaluated Derivative. You'll have to call .doit() to > evaluate them. Maybe we should create a Doit() object that calls > doit() on its argument and returns as an unevaluated Doit() if nothing > changes. Something like > > class Doit(Expr): > def __new__(cls, obj): > obj_doit = obj.doit() > if obj_doit == obj: > return super(Doit, cls).__new__(cls, obj) > return obj_doit > > Then you could use > > Dx, Dy, Dz = [Lambda(f, Doit(Derivative(f, coord))) for coord in > coordinates] > > and Dx(f) would evaluate if the derivative of f can be computed, > otherwise it would give Doit(Derivative(f, x)), which would still > automatically do itself if you later substitute f with something that > can be differentiated. If you want it to work only the first time, it > would be something like > > class DoitNow(Expr): > def __new__(cls, obj, evaluate=True): > if evaluate: > return obj.doit() > return super(DoitNow, cls).__new__(cls, obj) > > Dx, Dy, Dz = [Lambda(f, DoitNow(Derivative(f, coord), evaluate=False)) > for coord in coordinates] > > Really, though, we should just build this behavior into Lambda as an > option. > > Aaron Meurer > > On Wed, Apr 17, 2013 at 2:59 PM, thomas hisch <t.h...@gmail.com<javascript:>> > wrote: > > Maybe you want to use nested lambdas: > > > > map(lambda c: lambda f: Diff(f,c), coords) > > > > > > On Wednesday, April 17, 2013 10:11:14 PM UTC+2, brombo wrote: > >> > >> Consider the following > >> > >> coords = symbols('x,y,z') > >> > >> I wish to contruct the list of functions: > >> > >> [Dx,Dy,Dx] > >> > >> where > >> > >> def Dx(f): > >> return(diff(f,coord[0])) > >> > >> etc. > >> > >> but I want to build the list [Dx,Dy,Dz] with a for loop and not have to > >> do a separate > >> > >> def Dx(f): > >> return(diff(f,coord[0])) > >> > >> def Dy(f): > >> return(diff(f,coord[1])) > >> > >> def Dz(f): > >> return(diff(f,coord[2])) > >> > >> since when I am writing the code I don't know how long coords will be. > > > > -- > > 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 <javascript:>. > > To post to this group, send email to sy...@googlegroups.com<javascript:>. > > > Visit this group at http://groups.google.com/group/sympy?hl=en-US. > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > -- 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 http://groups.google.com/group/sympy?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.