Fix Eigen::Ref<MatrixXd> Jacobian output args in MATLAB wrapper#189
Merged
dellaert merged 1 commit intoJul 22, 2026
Merged
Conversation
Eigen::Ref<MatrixXd> arguments are Jacobian output parameters in C++
but were previously treated as inputs in the MATLAB wrapper, making
all Jacobian overloads permanently unreachable with the error:
'Arguments do not match any overload of function gtsam.Pose3.transformFrom'
Root cause: Ref<MatrixXd> parses as a TemplatedType with typename.name
'Ref', not a plain Type with is_ref set. The wrapper emitted bogus
isa(varargin{N},'Eigen.RefMatrixXd') checks that always return false,
and tried to unwrap_shared_ptr<MatrixXd> from in[] on the C++ side.
Fix:
- Add is_eigen_ref() to CheckMixin to detect Ref<MatrixXd> TemplatedType
- Exclude Ref args from varargin count and isa() checks in .m dispatch
- Add nargout == N gate so Jacobian overload is selected when caller
requests extra outputs
- Allocate Eigen::MatrixXd locals in MEX instead of reading from in[]
- Write Jacobian locals to out[1], out[2], ... after the primary return
- Exclude Ref args from checkArguments count (nargin validation)
Fixes borglab/gtsam#2492
dellaert
approved these changes
Jul 22, 2026
Member
|
Awesome, @thatdudegrantt !!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes borglab/gtsam#2492
Problem
When calling methods like
Pose3.transformFromwith Jacobian outputarguments from MATLAB, every overload using
Eigen::Ref<Eigen::MatrixXd>was permanently unreachable, producing:
Root cause:
Eigen::Ref<Eigen::MatrixXd>is parsed as aTemplatedTypewith
typename.name == 'Ref', not a plainTypewithis_refset. Thewrapper codegen had no special handling for this pattern and emitted:
Eigen.RefMatrixXdis not a MATLAB class, so this check always returnsfalse. On the C++ MEX side, the wrapper also incorrectly tried to
unwrap_shared_ptr<MatrixXd>fromin[], which would segfault evenif dispatch somehow succeeded.
Fix
gtwrap/matlab_wrapper/mixins.pyeigen_ref_typestuple ('MatrixXd')is_eigen_ref()to detectRef<MatrixXd>TemplatedType argsgtwrap/matlab_wrapper/wrapper.py_wrap_method_check_statement: exclude Ref args fromlength(varargin)count and
isa()checks; addnargout == Ngate instead_unwrap_argument: allocateEigen::MatrixXdlocals for Ref argsinstead of reading from
in[]_wrapper_unwrap_arguments: don't advancearg_idfor Ref args(they don't consume an
in[]slot); captureeigen_ref_argsbeforemethodcan be reassigned to a string for templated methodswrap_collector_function_return: write Jacobian locals toout[1],out[2], ... after the primary return valuegenerate_collector_function: exclude Ref args fromcheckArgumentsnargin count
Generated output (before/after)
Before (broken so
Eigen.RefMatrixXdnever matches):After (correct, now dispatches on real inputs + nargout):
C++ MEX after:
Testing
Added
test_eigen_ref_jacobianstotests/test_matlab_wrapper.pywithfixture
tests/fixtures/eigen_ref.icovering four cases:transformFrom)inverse)between)Expmap)All 11 tests pass.
Environment