Skip to content

Fix MATLAB Eigen::Ref Jacobian output capture and overload dispatch #192

Description

@dellaert

@thatdudegrantt This follows up on the Eigen Ref<MatrixXd> Jacobian-output work and Copilot's review finding in GTSAM PR #2612.

Problem

The generated C++ MEX collector correctly writes the primary return value and Jacobians to out[0], out[1], etc., but the generated MATLAB .m stubs do not reliably expose those outputs.

Two related problems are present:

  1. Jacobian overload calls capture only one MEX result:

    varargout{1} = <mex>(...);

    This discards the additional Jacobian outputs. Instance and static method generation both need an LHS that captures all requested outputs, such as:

    [varargout{1:nargout}] = <mex>(...);
  2. A non-Jacobian overload can appear first with the same non-Ref varargin signature. It has no nargout constraint, so it matches and returns before the later nargout == N Jacobian overload is reached.

For example, generated transformFrom dispatch currently has the ordinary one-argument overload before the one-argument-plus-nargout == 3 Jacobian overload.

Expected behavior

Calls such as:

[result, Hself, Hpoint] = pose.transformFrom(point);
[result, H] = pose.inverse();
[result, Hxi] = gtsam.Pose3.Expmap(xi);

should select the Jacobian overload and return every MEX output.

Suggested scope

  • Generate a multi-output MATLAB LHS for Eigen Ref output overloads.
  • Apply the behavior consistently to instance and static methods.
  • Prioritize Jacobian overload dispatch or constrain the non-Jacobian sibling by nargout.
  • Extend test_eigen_ref_jacobians to assert the generated .m assignment and ordering/selection, not only the nargout predicate and C++ out[] writes.

The existing focused test passes despite the broken generated MATLAB stub because those two properties are not currently asserted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions