Revision history [back]

Another classic Sage trap...

sage: def h(x):
....:     if x<0: return x
....:     return x^2
....:


What happens ?

• h is indeed a function:

sage: h

<function h="" at="" 0x7fdd6ede8d90="">

• It works:

sage: h(-1.5)

-1.50000000000000

• But ... what is h(x) ?

sage: h(x)

x^2

• But why ?

Because:

sage: x<0
x < 0


The expression x<0 is not False. Therefore, the if... statement of h evaluates to x^2.

Now, in the call plot(h(x),(-1.5,1.5)), the first argument is evaluated as the expression x^2, which is then evaluated for all values specified by the second argument (whether this one is (-1.5, 1.5) or (x, -1.5, 1.5)).

Oppose this to plot(h, (-1.5, 1.5)), where the first argument is evaluated to a function, which is then evaluated for all the values specified by the second argument.

This is also true in the plot(lambda x: x if x<0 else x, (-1.5, 1.5)) call, whose first argument evaluates to a function:

sage: lambda x: x if x<0 else x^2
<function <lambda> at 0x7fdd67e2b9d8>


This trap is somewhat specific to Sage, since Python does not have evaluable symbolic expressions...

HTH,

Another classic Sage trap...

sage: def h(x):
....:     if x<0: return x
....:     return x^2
....:


What happens ?

• h is indeed a function:

sage: h

<function h="" at="" 0x7fdd6ede8d90="">

• It works:

sage: h(-1.5)

-1.50000000000000

• But ... what is h(x) ?

sage: h(x)

x^2

• But why ?

Because:

sage: x<0
x < 0


The expression x<0 is not False. Therefore, the if... statement of h evaluates to x^2.

Now, in the call plot(h(x),(-1.5,1.5)), the first argument is evaluated as the expression x^2, which is then evaluated evaluated for all values specified by the second argument (whether this one is (-1.5, 1.5) or (x, -1.5, 1.5)).

Oppose this to plot(h, (-1.5, 1.5)), where the first argument is evaluated to a function, which is then evaluated called for all the values specified by the second argument.

This is also true in the plot(lambda x: x if x<0 else x, (-1.5, 1.5)) call, whose first argument evaluates to a function:

sage: lambda x: x if x<0 else x^2
<function <lambda> at 0x7fdd67e2b9d8>


This trap is somewhat specific to Sage, since Python does not have evaluable symbolic expressions...

HTH,

Another classic Sage trap...

sage: def h(x):
....:     if x<0: return x
....:     return x^2
....:


What happens ?

• h is indeed a function:

sage: h

sage: h
<function h="" at="" 0x7fdd6ede8d90="">h at 0x7fdd6ede8d90>

• It works:

sage: h(-1.5)

-1.50000000000000

sage: h(-1.5)
-1.50000000000000

• But ... what is h(x) ?

sage: h(x)

x^2

sage: h(x)
x^2

• But why ?

Because:

sage: x<0
x < 0


The When the Python variable x is bound to the symbolic variable x, the expression x<0 is not evaluates to itself, which is neither False. nor 0. Therefore, it is logically equivalent to True, and the if... statement of h evaluates to x^2.

Now, in the call plot(h(x),(-1.5,1.5)), the first argument is evaluated as the expression x^2, which is then evaluated for all values specified by the second argument (whether this one is (-1.5, 1.5) or (x, -1.5, 1.5)).

Oppose this to plot(h, (-1.5, 1.5)), where the first argument is evaluated to a function, which is then called for all the values specified by the second argument.

This is also true in the plot(lambda x: x if x<0 else x, (-1.5, 1.5)) call, whose first argument evaluates to a function:

sage: lambda x: x if x<0 else x^2
<function <lambda> at 0x7fdd67e2b9d8>


This trap is somewhat specific to Sage, since Python does not have evaluable symbolic expressions...

HTH,