Dear team,
I am currently working on a project where I need to compute the divergence of a vector field $f:M\to T_xM$ for a generic manifold $M$ defined using Manifolds.jl. After reviewing your package, I found it promising, but unfortunately, I was unable to achieve my goal. Please note that I am not an expert in Riemannian geometry, so my confusion could be related to a misunderstanding on my part.
To compute the divergence, I attempted to directly trace the Jacobian (though I realize this might not be the best approach). I started with a point $x$ on a sphere and defined a function $f(x,A) = Ax$ that, by construction, always stays in the tangent space ($A$ is skew-symmetric).
In Manifolds.jl, a point on the sphere is represented in embedding coordinates (i.e., as a 3D vector) rather than in 2D spherical coordinates. Since the coordinates are in Cartesian form, I expected the Jacobian of $f$ to simply be $A$ (as $f$ is linear), resulting in a $3\times3$ matrix with trace zero (which would correspond to the divergence of $f$).
However, when I compute it using ManifoldDiff, the result is a $2\times2$ matrix. I initially thought this might be because it computes the Jacobian in intrinsic coordinates, but the trace (which gives the divergence) should not depend on the coordinates. Also, the operation seems to be significantly slower compared to directly computing the Jacobian with ForwardDiff.
Could you help clarify why this is happening? Is there a way to generalise the idea to an arbitrary manifold?
Minimal working example:
using Random, LinearAlgebra, Manifolds, ManifoldDiff, ForwardDiff, DifferentiationInterface
f(x, A) = A * x
seed = 42
rng = Xoshiro(seed)
manifold = Sphere(2)
x = rand(rng, manifold) # 3D vector
@assert is_point(manifold, x) # x is a point on the sphere
A = [0.0 -1.0 -2.0; 1.0 0.0 -3.0; 2.0 3.0 0.0] # 3x3 matrix
@assert A' == -A # A is skew-symmetric: A * x preserves the norm of x
@assert is_vector(manifold, x, f(x, A)) # f(x, A) is in the tangent space
J = ManifoldDiff.jacobian(
manifold,
TangentSpace(manifold, x),
x -> f(x, A),
x,
TangentDiffBackend(AutoForwardDiff())
)
size(J) # (2, 2)
tr(J) # Non-zero trace
Dear team,$f:M\to T_xM$ for a generic manifold $M$ defined using Manifolds.jl. After reviewing your package, I found it promising, but unfortunately, I was unable to achieve my goal. Please note that I am not an expert in Riemannian geometry, so my confusion could be related to a misunderstanding on my part.
I am currently working on a project where I need to compute the divergence of a vector field
To compute the divergence, I attempted to directly trace the Jacobian (though I realize this might not be the best approach). I started with a point$x$ on a sphere and defined a function $f(x,A) = Ax$ that, by construction, always stays in the tangent space ($A$ is skew-symmetric).
In Manifolds.jl, a point on the sphere is represented in embedding coordinates (i.e., as a 3D vector) rather than in 2D spherical coordinates. Since the coordinates are in Cartesian form, I expected the Jacobian of$f$ to simply be $A$ (as $f$ is linear), resulting in a $3\times3$ matrix with trace zero (which would correspond to the divergence of $f$ ).
However, when I compute it using ManifoldDiff, the result is a$2\times2$ matrix. I initially thought this might be because it computes the Jacobian in intrinsic coordinates, but the trace (which gives the divergence) should not depend on the coordinates. Also, the operation seems to be significantly slower compared to directly computing the Jacobian with ForwardDiff.
Could you help clarify why this is happening? Is there a way to generalise the idea to an arbitrary manifold?
Minimal working example: