On Aug 27, 2007, at 6:29 PM, William Stein wrote:
> On 8/27/07, Justin Walker <[EMAIL PROTECTED]> wrote:
>>
>> Hi, all,
>>
>> I want to create a "function" within a function, and return it as
>> the value of the latter function, something along the lines of:
>>
>> def g(a,b,c):
>> var('x y')
>> f = a*x^2 + b*y + c
>> return f
>>
>> This works, almost. I have to invoke the return value as a
>> function returning a function:
>>
>> sage: f = g(1,2,3)
>> sage: f(0)
>> 2*y + 3
>> sage: f(0)(1)
>> 5
>>
>> I can't treat it as a function of two variables:
>>
>> sage: f(0,1)
>> ---------------------------------------------------------------------
>> ------
>> <type 'exceptions.TypeError'> Traceback (most recent
>> call last)
>>
>> /SandBox/Justin/sb/Sage/Code/<ipython console> in <module>()
>>
>> <type 'exceptions.TypeError'>: __call__() takes at most 2
>> arguments (3 given)
>> sage:
>>
>> It's not clear from the doc what should happen, but this is a bit
>> of a surprise.
> This is what you really want to do (use a *callable* symbolic
> expression):
>
> sage: def g(a,b,c):
> f(x,y) = a*x^2 + b*y + c
> return f
> ....:
> sage: h = g(1,2,3)
> sage: h
> (x, y) |--> 2*y + x^2 + 3
> sage: h(2,3)
> 13
That works; just what I wanted. Thanks.
I found it in the refman, but I don't know that I could have figured
it out.
Does it make sense to differentiate between "f = g()" and "f() = g()"?
Justin
--
Justin C. Walker, Curmudgeon-At-Large
Institute for the Absorption of Federal Funds
--------
If you're not confused,
You're not paying attention
--------
--~--~---------~--~----~------------~-------~--~----~
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://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~----------~----~----~----~------~----~------~--~---