Stan Schymanski wrote:
> Hi Jason,
> 
> That's awesome, thanks a lot! I had a bit of trouble understanding the 
> documentation of partial, but your example helped tremendously. This 
> should definitely be included in any upcoming documentation on 
> fast_float. As far as I understand, partial(F,1) just replaces the first 
> variable in F by 1. If I define something like F = fast_float(a*x^3 + 
> b*x^2 + c*x + d, 'a', 'b', 'c', 'd', 'x'), could I then use partial to 
> replace a, b, d and x and leave c as a variable, or would I have to 
> re-define F to take c as the last argument?
> 

Apparently fast_float things don't take keyword arguments, so you have 
to use a lambda function (that will take keyword arguments) to put 
things in the right order.

sage: var('a,b,c,d,x')
(a, b, c, d, x)
sage: F = fast_float(a*x^3 + b*x^2 + c*x + d, 'a', 'b', 'c', 'd', 'x')
sage: ff=partial(lambda a,b,c,d,x: F(a,b,c,d,x), a=1,b=2,d=5,x=-2)
sage: ff(c=3)
-1.0

alternatively, since arguments to gg (below) are just appended to the 
non-keyword arguments, you can have one "hole" in the non-keyword 
arguments.  To have more than this, you need to do something more special.

sage: gg=partial(lambda a,b,c,d,x: F(a,b,c,d,x), 1,2,d=5,x=-2)
sage: gg(3)
-1.0


It would be nice (and pretty straightforward to write) a partial 
function that took a dictionary like:

partial(f, {0: 10, 'x': 3, 2: 20})

which would make a function that behaved like:

f(10, *, 20, *args, x=3)

(i.e., the keys above are the places in the non-keyword arguments; since 
we don't have a value specified for the position 1 in the non-keyword 
arguments, it is left blank.)  Then your example above would be 
something like:

sage: ff=partial(F, {0:1, 1: 2, 4: 5, 5: -2})
sage: ff(3)
-1.0

Thanks,

Jason




--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to