Improve type_caster for complex number types. #854
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.
Results for this PR were obtained with Python 3.12.8 and g++ 14.2.0.
Currently, after importing the module defined in
tests/test_stl.cpp
ast
and definingthe following raises a TypeError:
Instead, it is expected that the
__complex__
method be called and the assertion be correct.Note that the Stable ABI for PyComplex_RealAsDouble,ImagAsDouble was fixed in version 3.13. See cpython issue 109598
Also, the current complex type_caster does not honor
nb::arg().noconvert()
.The following test passes
but given
the following tests
DID NOT RAISE <class 'TypeError'>
and fail:This PR re-writes the complex type_caster.
Regarding performance, given the extension
and measuring time with
I obtained the following results:
For the last two lines above, note that
I would expect that compiling
new abi3
with-DPy_LIMITED_API=0x030D0000
would reducecaddf(np.complex64)
by 2X, but I don't have Python 3.13 handy.