On Wed, 19 Aug 2009, KvS wrote: > > > > On Aug 19, 11:06 pm, Robert Bradshaw <rober...@math.washington.edu> > wrote: >> On Wed, 19 Aug 2009, KvS wrote: >> >>> Dear all, >> >>> just started exploring Sage (via sagenb.org), I'm very enthousiastic >>> about the concept and am very eager to leave 'black box' Mathematica >>> asap. One issue however I can't seem to get my head around, namely >>> what exactly is the 'right' way to think of and work with Sage- >>> functions (as opposed to function constructs in the Python language)? >> >>> E.g. when trying to plot a piecewise function, this works: >> >>> f1 = lambda x:x >>> f2 = lambda x:x^2 >>> f = Piecewise([[(0,1),f1],[(1,2),f2]]) >>> P = f.plot() >> >>> whereas this (and several modifications of it I tried): >> >>> x=var('x') >>> f1(x)=x >>> f2(x)=x^2 >>> f(x)=Piecewise([[(0,1),f1(x)],[(1,2),f2(x)]]) >>> P=f.plot() >> >>> throws a TypeError: >> >>> File "ring.pyx", line 272, in >>> sage.symbolic.ring.SymbolicRing._element_constructor_ (sage/symbolic/ >>> ring.cpp:4456) >>> TypeError >> >>> Personally I would prefer the second approach as I would like to use >>> only Sage-functions for mathematical functions (so not use lambda: >>> etc.) to keep a notion of distinction between the mathematical objects >>> on the one hand and the Python code on the other hand that controls >>> the program flow. But it seems that I just don't really understand how >>> to do that. Why is the second piece of code wrong and what would be >>> the 'right' way to do it? Is there a function construct in Sage like >>> the concept of a 'pure function' in Mathematica, so something like >>> f=Function(x,x^2), where x is only a dummy that has no link with any x >>> that might be defined before this command? >> >>> Many thanks in advance for your time. >> >> Probably what you want to do is >> >> sage: f(x) = x^2 >> >> Note that piecewise functions have a lot of rough edges, so are probably >> not the best examples for "how things should work." >> >> - Robert > > You mean this should be the 'right' way to define a Sage-function? > Don't you need x=var('x') as well?
It's just syntactic sugar. > And how would you define a Sage- > function inside a Python class then, this: > > class Test: > def __init__(): > x=var('x') > self.func(x)=sin(x) > > t=Test() > > yields a syntax error? > Do (x^3-x).function(x) See x.function? for more documentation (where x here is a variable/expression). - Robert --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---