Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 2011-11-24 02:46:01 -0600

DSM gravatar image

The problem is how binding works in Python. sum_of_distances is a Python function, so it's evaluated immediately. So your plot command is really equivalent to

onepoint = sum_of_distances((x,1,1), tags, 30)
plot(onepoint, (x, 0, 10))

And it so happens that:

sage: sum_of_distances((x,1,1), tags, 30)
0

because this requires testing whether sqrt(x^2+9) < 30, which returns false because Sage doesn't know how to prove it's true (and it's not in general, as x is a symbolic variable which could be anything), so cumdist stays at 0.

One way to get what you want is to use a lambda function to delay evaluation:

plot(lambda x: sum_of_distances((x,1,1), tags, 30),(0,10))

image description

(BTW, you might be interested in CartesianProduct and in generator expressions-- they can simplify some code like this.)

The problem is how binding works in Python. sum_of_distances is a Python function, so it's evaluated immediately. So your plot command is really equivalent to

onepoint = sum_of_distances((x,1,1), tags, 30)
plot(onepoint, (x, 0, 10))

And it so happens that:

sage: sum_of_distances((x,1,1), tags, 30)
0

because this requires testing whether (e.g.) sqrt(x^2+9) < 30, which returns false because Sage doesn't know how to prove it's true (and it's not in general, as x is a symbolic variable which could be anything), so cumdist stays at 0.

One way to get what you want is to use a lambda function to delay evaluation:

plot(lambda x: sum_of_distances((x,1,1), tags, 30),(0,10))

image description

(BTW, you might be interested in CartesianProduct and in generator expressions-- they can simplify some code like this.)