>
>
> I think that I now understand what my problem is, namely that a function 
> created using if:... else: within a def (or inside a lambda function 
> definition) can, it seems, only be used for returning numerical values. Any 
> attempt to use it symbolically just returns the else: alternative.
>
> E.g. 
> def f(x):
>     if x<0:
>         return x^2
>     else:
>         return x
>
> After that, f(-1) returns 1 alright, but f(x) returns x, rather than some 
> version of the function defined. That is why plot(f,(-1,1)) works fine but 
> plot(f(x),(x,-1,1)) draws a straight line.
>

Correct.
 

> The same kind of thing happens using h=lambda x: x^2 if x<0 else x
>
>
Note that this will also happen with y

var('y')
f(y)

returns 

y

The 'x' in your function definition is indeed a dummy variable!  In 
particular, it's not a SYMBOLIC variable - maybe in Mathematica all 
variables are also mathematical?   Anyway, you put something in it that 
isn't less than 0, so it will just return that!  And the "x" you put in (or 
the "y" I put in) certainly isn't less than zero, or greater than zero, or 
equal to zero; it's a symbolic variable.
 

> Is there somewhere in the documentation that spells out this limitation of 
> the various if...else  constructions? 
>
>
Well, this is Python, not Sage per se.  Presumably there are lots of things 
like this.  It's not really a limitation, it's a different language.  
 

> Given my background coming from years of use of Mathematica, this seems 
> like a major drawback of Sage. 
>
>
We do have (limited) piecewise function support, which might clear up your 
current problem.  This is the best I could come up with - again, the 
piecewise support is rudimentary, dating from when it was the ONLY function 
type we really had in 2006 or so.  There are lots of other things you might 
have tried that wouldn't quite work for various reasons.

def f(a):
    return 
Piecewise([[(0,a),2-sqrt(1-(1-x/a)^4)],[(a,2),sqrt(1-((x-a)/(2-a))^6)]])

f(.5).plot()

-- 
-- 
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to 
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org



Reply via email to