Hey all: I'm back with a follow up question on the topic of substitution in Sage. How can i work the following example? I have an expression that involves the derivative of a function, and into that expression i want to substitute the value of the derivative. Simple, eh? I tried Mike's ** trick and variations thereof, but that doesn't seem to work:
sage: var('x,y') (x, y) sage: f= function('y',x) sage: g= diff(f,x) sage: h= diff(f^2,x) sage: print h d 2 y(x) (-- (y(x))) dx sage: print h.substitute(**{str(g):cos(x)}) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) /Users/arai021/<ipython console> in <module>() /Applications/sage/local/lib/python2.5/site-packages/sage/calculus/ calculus.py in substitute(self, in_dict, **kwds) 3605 X = self.simplify() 3606 kwds = self.__parse_in_dict(in_dict, kwds) -> 3607 kwds = self.__varify_kwds(kwds) 3608 return X._recursive_sub(kwds) 3609 /Applications/sage/local/lib/python2.5/site-packages/sage/calculus/ calculus.py in __varify_kwds(self, kwds) 3660 3661 """ -> 3662 return dict([(var(k) if isinstance(k,str) else k, w) for k,w in kwds.iteritems()]) 3663 3664 def substitute_over_ring(self, in_dict=None, ring=None, **kwds): /Applications/sage/local/lib/python2.5/site-packages/sage/calculus/ calculus.py in var(s, create) 5297 return tuple([var(x.strip()) for x in s.split(',')]) 5298 elif ' ' in s: -> 5299 return tuple([var(x.strip()) for x in s.split()]) 5300 try: 5301 v = _vars[s] /Applications/sage/local/lib/python2.5/site-packages/sage/calculus/ calculus.py in var(s, create) 5306 raise ValueError, "the variable '%s' has not been defined"%var 5307 pass -> 5308 v = SymbolicVariable(s) 5309 _vars[s] = v 5310 _syms[s] = v /Applications/sage/local/lib/python2.5/site-packages/sage/calculus/ calculus.py in __init__(self, name) 5140 raise ValueError, "variable name must be nonempty" 5141 elif not is_python_identifier.match(name): -> 5142 raise ValueError, "variable name is not a valid Python identifier" 5143 5144 def __hash__(self): ValueError: variable name is not a valid Python identifier Alex On Sep 19, 1:18 pm, Alex Raichev <[EMAIL PROTECTED]> wrote: > Sweet! Thanks, Mike. > > On Sep 19, 11:56 am, "Mike Hansen" <[EMAIL PROTECTED]> wrote: > > > On Thu, Sep 18, 2008 at 4:54 PM, Alex Raichev <[EMAIL PROTECTED]> wrote: > > > > Now, with the above in mind, how do you write a function to evaluate > > > that sine limit given a variable from the user? We know the following > > > does not work. > > > > sage: var('x') > > > x > > > sage: def limmy(w): > > > ....: return limit(sin(w)/w,w=0) > > > sage: limmy(x) > > > sin(x)/x > > > > More generally, how do you execute functions requiring keyword > > > arguments, such as limit(), when you don't know the symbolic variable > > > names involved (but have references to them)? > > > Here's one way to do it: > > > sage: def limmy(w): > > ....: return limit(sin(w)/w,**{str(w):0}) > > ....: > > sage: limmy(x) > > 1 > > sage: limmy(var('w')) > > 1 > > > Not the most elegant, but it works. > > > --Mike --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---