Ask Your Question

How am I able to take the gradient of a symbolic function twice?

asked 2023-02-04 00:47:54 +0200

sushisalad gravatar image

I have introduced a Euclidean space using

E.<x,y,z> = EuclideanSpace()

then defined a symbolic function using

f(x,y) = x^2 + y^2

then I introduce the manifold library and take the gradient

from sage.manifolds.operators import *

which give me the expected result, however if I try to take the gradient again, I am met with an error:

'FreeModule_ambient_field_with_category.element_class' object has no attribute 'gradient'

I have no idea what this means. I am not very experience with mathematical spaces and fields and all that fun stuff yet, so I was wondering if someone could help me understand what this means and how to get around it.


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2023-02-05 11:02:26 +0200

achrzesz gravatar image

In SageMath Manifolds grad needs a scalar field as an argument, so you can't use the operation twice since the first application gives a vector field.

In symbolic computations you can use twice the corresponding operations diff or jacobian

f(x,y) = x^2+y^2
g(x,y)=f.diff()(x,y) # gradient

(2*x, 2*y)

g.diff()(x,y)        # Hessian

[2 0]
[0 2]


g=jacobian(f, (x,y)); g

[2*x 2*y]


[2 0]
[0 2]

For completeness


(2*x, 2*y)


[2 0]
[0 2]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2023-02-04 00:47:54 +0200

Seen: 63 times

Last updated: Feb 05