On Jan 16, 5:53 pm, Ed Scheinerman <edward.scheiner...@gmail.com> wrote: > I'm confused by the fact that variables defined inside functions can > "leak out" and become global variables. Here's what I've noticed.
The problem is twith the function var. According to its documentation: (var?) "The new variable is both returned and automatically injected into the global namespace. If you need symbolic variable in library code, it is better to use either SR.var() or SR.symbol()." The var function is only intended to be used on an interactive session by the user. If you need variables inside a function you need SR.var def steiner(a,b,c): """ Given three points in the plane, find the point p that minimizes the sum of the distances to those three points. """ x = SR.var('x') y = SR.var('y') p = (x,y) # unknown point # objective function to minimize obj = dist(p,a) + dist(p,b) + dist(p,c) # start search at center of mass of the three points p0 = ( (a[0]+b[0]+c[0])/3., (a[1]+b[1]+c[1])/3. ) print "Starting optimiztion at", p0 print obj.subs(x=p0[0], y=p0[1]) return minimize(obj,p0) Then: sage: x = [1,2,3] sage: y = [4,5,6] sage: x [1, 2, 3] sage: y [4, 5, 6] sage: steiner((1,1), (2,3), (5,2)) Starting optimiztion at (2.6666666666666665, 2.0) 5.4788343901030965 Optimization terminated successfully. Current function value: 5.303240 Iterations: 6 Function evaluations: 7 Gradient evaluations: 7 (2.20179756615, 2.46316721759) sage: x [1, 2, 3] sage: y [4, 5, 6] -- 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 URL: http://www.sagemath.org