In SageMath, (version 4.7), I do this in the notebook:
JavaScript
x
3
1
var("x y")
2
dens(x, y) = 2 if y <= x else 0
3
and this gives no error. However, after that,
dens(1, 1)
returns0
,dens(1, 0.5)
returns0
,- and so on!
In fact, I found no way to get the answer 2
.
What am I doing wrong?
Advertisement
Answer
You’re using the Sage function declaration syntax — f(x,y) = something-or-other — but on the right hand side you’re not putting a Sage expression but a Python one. This is evaluated when it’s declared. By which I mean:
JavaScript
1
11
11
1
sage: var("x y")
2
(x, y)
3
sage: bool(y <= x)
4
False
5
sage: dens = 2 if y <= x else 0
6
sage: dens
7
0
8
sage: dens(x,y) = 2 if y <= x else 0
9
sage: dens
10
(x, y) |--> 0
11
If you only care about the values that the function take (say, you’re plotting it), you can simply use a Python function. If you want to differentiate it etc. you’re in for a harder go of it, I’m afraid.