added __complex__ support - #3984
Open
aaishwarymishra wants to merge 2 commits into
Open
Conversation
Contributor
Author
|
While working on test my linter were showing me warning that complex is not valid input for mlx array do you think we need to update mlx array arguments? |
zcbenz
reviewed
Aug 4, 2026
| .def( | ||
| "__complex__", | ||
| [](mx::array& a) { | ||
| auto value = PyComplex_AsCComplex(to_scalar(a).ptr()); |
Collaborator
There was a problem hiding this comment.
Can it be done with just return nb::handle(PyComplex_AsCComplex(to_scalar(a).ptr()))?
Collaborator
I'm open to support complex as array input. |
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.
This pull request adds support for converting
mx::arrayobjects to Python complex numbers by implementing thePlease include a description of the problem or feature this PR is addressing. If there is a corresponding issue, include the issue #.
__complex__method. It also extends the test suite to verify this new functionality, ensuring that both real and complex arrays behave as expected when converted to complex numbers.New feature: Python complex conversion support
__complex__method to themx::arrayPython bindings, allowingmx::arrayinstances to be converted to Python complex numbers using the built-incomplex()function.Test improvements
test_to_scalartest to check thatcomplex(mx.array(...))returns the correct complex value for real and complex arrays, and raises aValueErrorfor non-scalar arrays.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesPart of the data-apis/array-api-compat#452 .