On Sep 13, 2007, at 19:21 , William Stein wrote:
>
> On 9/13/07, Justin C. Walker <[EMAIL PROTECTED]> wrote:
>> A question about callable functions: I would like to create one, say
>> "f(x,y)=x^2+y^2". There are several ways to do this:
>
> Why. What problem are you trying to solve?
The simple answer is just what I said: I want to create functions
(polynomials, really), in a procedure, that I can pass around to
other code, and invoke them as functions (compute "f(a,b)" for example).
>> - f = x^2+y^2
>> This works but "f(a,b)" fails (it's not really "callable")
>
> This is a symbolic expression. You can do:
> f(x=a, y=b)
> or
> f.subs(x=a, y=b)
>> - R.<x,y> = PolynomialRing(ZZ)
>> g = x^2+y^2
>> This works and "g(a,b)" does what I want. It seems kind
>> of heavy-weight, though (I've now got a polynomial ring
>> floating around when I don't really need it).
>
> Actually polynomial rings aren't very big. They're just a few
> bites. :-)
That's a comfort :-} I'll do it if I need to, but I don't fully
understand the implications. For example, if I create two such, can
I "combine" them ['combine' being ill-defined for the moment]? For
the obvious definition, the answer is 'yes', and two created
separately have the same parent, but I don't know whether there is
something lurking in the underbrush...
>> sage: h(x,y)=x^2+y^2
>> sage: h
>> (x, y) |--> y^2 + x^2
>> sage: h(0,1)
>> 1
>> sage: print h(0,1)
>> 1
>> sage: type(h(0,1))
>> <class 'sage.calculus.calculus.SymbolicArithmetic'>
>>
>> So, questions:
>>
>> - why is h(0,1) not an 'Integer'? Does that matter? I have some
>> code (hacks, admittedly) that wants to be sure it's dealing with
>> integers.
>
> h(0,1) is a symbolic expression. If you want an integer, you would
> have to write Integer(h(0,1)).
Ewww...That means that I would have to track where my "integers" come
from. Thankfully, your answers below let me avoid this.
>> - The fact that 'print h(0,1)' produces a bizarre result is really
>> a bother, because I can't bank on formatting (unless I am missing
>> something; it wouldn't be the first time, of course).
>
> It's ascii art. If you just need a normal string without any
> ascii art, do
> print repr(h(0,1))
See above (Ewww...).
>> - Is there some 4th alternative that I missed?
>
> Yes, there are two more, which might be fine, depending on
> your application:
>
> def h(x,y):
> return x^2 + y^2
> and
> h = lambda x,y: x^2 + y^2
>
> Probably the very last one is exactly what you really want,
> unless you want to do arithmetic with it (h^2 doesn't make
> any sense).
This seems to work well for me. While "h^2" may make no sense, "h
(x,y)^2" does, and works as I expect. Does this mean that 'h' is a
"second-class citizen"? :-}
>> To complicate matters, I want to create these things in code, and
>> return the result, to be used later, either directly or in other
>> code.
>
> The code analogue of "h(x,y) = x^2 + y^2" is:
>
> sage: preparse('h(x,y) = x^2 + y^2')
> '_=var("x,y");h=symbolic_expression(x**Integer(2) +
> y**Integer(2)).function(x,y)'
>
> I.e., make x and y symbolic, make the expression x^2 + y^2, and
> finally make it into a function of x and y.
I don't see what's going on here. When I try this, I get a string
that I can't "eval". What am I missing? Or was this not intended as
a solution, but rather an explanation?
>> Comments?
>>
>> Thanks for the help, in advance.
>
> Please ask more questions.
OK: Why is there air? :-}
Thanks for the (clear) explanations.
Justin
--
Justin C. Walker, Curmudgeon-At-Large
Institute for the Absorption of Federal Funds
--------
Men are from Earth.
Women are from Earth.
Deal with it.
--------
--~--~---------~--~----~------------~-------~--~----~
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/
-~----------~----~----~----~------~----~------~--~---