feat: add complex number support with type promotion fixes - #14
Merged
Merged
Conversation
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.
Summary
This PR introduces comprehensive complex number support to NDArray PHP, enabling complex-valued arrays (Complex64 and Complex128) across the entire API. It also fixes scalar type promotion to follow NumPy-compatible semantics and updates linear algebra return types for better consistency.
Context and Motivation
Complex numbers are fundamental to many scientific computing domains including signal processing, quantum mechanics, and control theory. Prior to this PR, NDArray only supported real-valued data types, limiting its applicability for these use cases. Additionally, scalar operations were creating buffers in the output dtype rather than the scalar's native dtype, causing silent data corruption when combining integer arrays with float scalars. This PR addresses both gaps: adding first-class complex support throughout the stack and fixing the scalar type promotion pipeline.
What's Changed
Complex64andComplex128data types with full arithmetic, comparison, linear algebra, and reduction operation supportmagnitude,angle,equals, andtoArraymethodsreal(),imag(),conjugate(),angle(),iscomplex(),isreal()DType::promote()dot()to return scalar for 1D·1D andtrace()to return scalar valuesextract_view_as_c64andextract_view_as_c128helpers for zero-copy complex view extractionastype()to support all complex conversions including Complex128→Float64 and Int64→Complex128Breaking Changes
dot()return type changed fromNDArraytofloat|int|Complex|NDArray(returns scalar for 1D·1D)trace()return type changed fromNDArraytofloat|int|Complex(returns scalar directly)$intArray + 1.5now correctly promotes to Float64 instead of creating corrupted data