Skip to content

Commit

Permalink
Add method for computing Jacobians
Browse files Browse the repository at this point in the history
  • Loading branch information
HamletTanyavong committed Nov 13, 2023
1 parent 8ea4654 commit 0393b68
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Mathematics.NET/AutoDiff/AutoDiffExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,35 @@ public static Vector3<Real> Gradient(

return new(gradients[0], gradients[1], gradients[2]);
}

/// <summary>Compute the Jacobian of a vector function using reverse-mode automatic differentiation.</summary>
/// <param name="tape">A gradient tape</param>
/// <param name="fx">The first function</param>
/// <param name="fy">The second function</param>
/// <param name="fz">The third function</param>
/// <param name="x">The point at which to compute the Jacobian</param>
/// <returns>The Jacobian of the vector function</returns>
public static Matrix3x3<Real> Jacobian(
this GradientTape tape,
Func<GradientTape, VariableVector3, Variable> fx,
Func<GradientTape, VariableVector3, Variable> fy,
Func<GradientTape, VariableVector3, Variable> fz,
VariableVector3 x)
{
Matrix3x3<Real> result = new();

_ = fx(tape, x);
tape.ReverseAccumulation(out var gradients);
result[0, 0] = gradients[0]; result[0, 1] = gradients[1]; result[0, 2] = gradients[2];

_ = fy(tape, x);
tape.ReverseAccumulation(out gradients);
result[1, 0] = gradients[0]; result[1, 1] = gradients[1]; result[1, 2] = gradients[2];

_ = fz(tape, x);
tape.ReverseAccumulation(out gradients);
result[2, 0] = gradients[0]; result[2, 1] = gradients[1]; result[2, 2] = gradients[2];

return result;
}
}

0 comments on commit 0393b68

Please sign in to comment.