Ask Your Question

Revision history [back]

There is a subtle difference between "symbolic expressions" and "callable symbolic expressions", which are also termed "functions". Writing

sage: f=x^2+y^3

makes f a symbolic expression, which displays as

sage: f
x^2 + y^3

On the other hand, writing

sage: g(x,y)=x^2+y^3

makes g a callable symbolic expression, which displays as

sage: g
(x, y) |--> x^2 + y^3

With f, you need to use f.substitute to substitute values, but with g, since you have already informed sage of the variable order, you can use it like a function

sage: g(1,3)
28

The way you define your finction determines what kind of expression the corresponding Hessian is; note the difference in syntax below:

First method:

sage: x=var('x')
sage: y=var('y')
sage: z=matrix(2,1,[ [1],[1] ])
sage: f=x^2+y^3
sage: H=f.hessian()
sage: H.substitute(x=z[0,0],y=z[1,0])
[2 0]
[0 6]

sage: f
x^2 + y^3
sage: H
[  2   0]
[  0 6*y]

Second method, making g a callable symbolic expression:

sage: x=var('x')
sage: y=var('y')
sage: z=matrix(2,1,[ [1],[1] ])
sage: g(x,y)=x^2+y^3
sage: H=g.hessian()
sage: H(z[0,0],z[1,0])
[2 0]
[0 6]

sage: g
(x, y) |--> x^2 + y^3
sage: H
[  (x, y) |--> 2   (x, y) |--> 0]
[  (x, y) |--> 0 (x, y) |--> 6*y]