Stan Schymanski wrote:
> Hi Jason,
> 
> Thanks a lot for that! This brings me back to the original problem. I 
> expected plot and find_root to replace the variable with a float and 
> call the function, but it does not seem to do that. For example, it 
> would be nice if the following worked:
> 
> var('a b c d x dummy')
> F = fast_float(a*x^3 + b*x^2 + c*x + d, 'a', 'b', 'c', 'd', 'x')
> ff = lambda a,b,c,d,x: F(a,b,c,d,x)
> plot(ff(a=1,b=2,c=3,d=4,x=dummy),(dummy,0,6))
>      
> 
> Traceback (click to the left for traceback)
> ...
> TypeError: a float is required
> 
> This works:
> [ff(a=1,b=2,c=3,d=4,x=dummy) for dummy in srange(6)]
> 
> [4.0, 10.0, 26.0, 58.0, 112.0, 194.0]
> 
> Why don't plot and find_root just replace 'dummy' with a float and run 
> ff just like in the list?
> 


Unfortunately, I don't that will ever work, as Python does not do 
automatic currying (did I use the right term?).  The list comprehension 
in python is a special case, where the function is not evaluated until 
you have a value for x.  However, in the case of plot, the function ff 
is evaluated first, and since x is missing, there is an error.

However, in your case, it would probably make a lot more sense to do this:

f = a*x^3+b*x^2+c*x+d
plot(f.subs(a=1,b=2,c=3,d=4), (x, 0, 6))

This uses the fact that f is a symbolic expression, then the subs 
command substitutes in variables and returns another expression. 
fast_float is called by plot automatically.

If find_root doesn't automatically use fast_float (it should!), then you 
could do something like:

f = a*x^3+b*x^2+c*x+d
find_root(fast_float(f.subs(a=1,b=2,c=3,d=4), 'x'), (x, 0, 6))

(or whatever the syntax is).

Again, this only works for Sage symbolic expressions...

Sorry I didn't think of this earlier.  For general python functions, you 
could use the partial trick we talked about.

It might make sense to have some sort of partial evaluation available 
for fast_float functions, so if a few arguments were given, it behaved 
like the partial trick above.

Thanks,

Jason



--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to