Skip to content

feat: add complex number support with type promotion fixes - #14

Merged
CodeWithKyrian merged 1 commit into
mainfrom
feat/complex-number-support
Apr 19, 2026
Merged

CodeWithKyrian merged 1 commit into
mainfrom
feat/complex-number-support

Conversation

@CodeWithKyrian

@CodeWithKyrian CodeWithKyrian commented Apr 19, 2026

Copy link
Copy Markdown
Contributor

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

  • Added Complex64 and Complex128 data types with full arithmetic, comparison, linear algebra, and reduction operation support
  • Added Complex value object with magnitude, angle, equals, and toArray methods
  • Added element-wise complex operations: real(), imag(), conjugate(), angle(), iscomplex(), isreal()
  • Fixed scalar type promotion: scalar buffers now created in scalar dtype, Rust computes output dtype via DType::promote()
  • Updated dot() to return scalar for 1D·1D and trace() to return scalar values
  • Added extract_view_as_c64 and extract_view_as_c128 helpers for zero-copy complex view extraction
  • Updated astype() to support all complex conversions including Complex128→Float64 and Int64→Complex128
  • Added comprehensive test suite: 1130 tests covering complex creation, arithmetic, linear algebra, broadcasting, slicing, comparisons, and edge cases
  • Updated documentation: added complex types to data-types guide, new type-promotion guide, API reference for all new methods, and Complex class documentation

Breaking Changes

  • dot() return type changed from NDArray to float|int|Complex|NDArray (returns scalar for 1D·1D)
  • trace() return type changed from NDArray to float|int|Complex (returns scalar directly)
  • Scalar operation behavior change: $intArray + 1.5 now correctly promotes to Float64 instead of creating corrupted data

@CodeWithKyrian
CodeWithKyrian merged commit 515d0a3 into main Apr 19, 2026
14 checks passed
@CodeWithKyrian
CodeWithKyrian deleted the feat/complex-number-support branch April 19, 2026 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant