# Converting expression into a number and evaluating function

I was trying to use a lagrange multiplier to find the local maxima of a simple function over the circle:

x,y,c = var('x y c')

f(x,y) = x**2 + y**2 - 1
g(x,y) = x+y
F = diff(f)
G = diff(g)

polys = [f]
for i in range(len(F)):
p = F[i] - c * G[i]
polys.append(p)

list_solutions = solve(polys,x,y,c)

for solution in list_solutions:
print solution
print f({x : solution , y : solution})
print '\n'


At 'print solution' i get the first extremum:

[x == 1/2*sqrt(2), y == 1/2*sqrt(2), c == sqrt(2)]


But at the 'print f({x : solution , y : solution})' line the following error occurs:

TypeError: no canonical coercion from <type 'dict'> to Callable function ring with arguments (x, y)


So i guess x == 1/2*sqrt(2) and the like can't be seem as numbers. I would like to be able to evaluate f(x,y) at the point, so what can i do? Also, the lagrange multiplier algorithm must be on Sage already, but Google gives me nothing. If you can show its command that would be a plus.

edit retag close merge delete

Sort by » oldest newest most voted

Hi JGC

I propose to replace print f({x : solution , y : solution}) by print f(x,y).subs(solution) ?

more