Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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[0] , y : solution[1]})
    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[0] , y : solution[1]})' 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 it's command that would be a plus.

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[0] , y : solution[1]})
    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[0] , y : solution[1]})' 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 it's its command that would be a plus.