# how to properly substitute in a matrix?

Let's consider a modified example from Sage reference manual

f(x,y)=x^2*y+y^2+y
solutions=solve(list(f.diff()),[x,y])
the_solution=solutions
H=f.diff(2);  # Hessian matrix


How can i properly substitute the_solution into H?

I have tried:

H(x,y).subs(the_solution) - does not work.

This will work for H(x,y).subs(x==0)

H(x,y).subs_expr(*the_solution) - does not work.

This will work for f(x,y).subs_expr(*the_solution)

edit retag close merge delete

Sort by » oldest newest most voted

Try this:

sage: solutions=solve(list(f.diff()),[x,y],solution_dict=True)
sage: solutions
[{y: 0, x: -I}, {y: 0, x: I}, {y: -1/2, x: 0}]
sage: H.subs(solutions)
[(x, y) |--> -1  (x, y) |--> 0]
[ (x, y) |--> 0  (x, y) |--> 2]


Or this:

sage: H(x,y).subs(solutions)
[-1  0]
[ 0  2]


I'm not sure exactly what output you are looking for.

The documentation for solve has some more information about this keyword.

more

Thanks, kcrisman! Maybe it is possible to include these lines into Sage Reference Manual? In the manual one enters the point manually... I don't know how to do this...

sage: soln = [x.rhs() for x in the_solution]; soln
[0, -1/2]
sage: matrix([f(*soln) for f in H])
[-1  0]
[ 0  2]

more