Skip to content

Fix: broadcast during slice assign and align assign API - #19

Merged
CodeWithKyrian merged 1 commit into
mainfrom
fix/slice-assign-broadcast
May 13, 2026
Merged

CodeWithKyrian merged 1 commit into
mainfrom
fix/slice-assign-broadcast

Conversation

@CodeWithKyrian

Copy link
Copy Markdown
Contributor

This PR tightens slice assign() on the PHP side, validates broadcast compatibility in Rust before calling ndarray’s assign, drops the old reshape-to-destination behavior, and extends tests for broadcasted NDArray assignment and clearer shape errors.

Motivation and Context

ndarray’s assign already broadcasts the right-hand side to the destination view’s shape, but only when the shapes are broadcast-compatible; otherwise it can panic. The library had been rejecting or reshaping on the PHP side in ways that did not match that model. Aligning with ndarray means checking broadcast feasibility up front (using the same rules as elsewhere in the crate), returning a controlled shape error instead of risking a panic, and letting a single Rust path perform the assignment.

What’s Changed

  • Pre-flight broadcast check for slice assignment in the Rust FFI using the existing broadcast-shape rules, returning a shape error when the source cannot broadcast to the destination view.
  • PHP assign() simplified to scalar fill or NDArray assign without private helpers, no element-count equality requirement when shapes broadcast, and no automatic reshape/copy to force matching shapes before assign.
  • Stricter parameter typing for assign() (scalars and NDArray only), with documentation updated to describe broadcast behavior.
  • Slicing tests updated for the new error message, added cases for broadcast assignment into slices.

Breaking Changes

assign() no longer accepts any type; callers must pass an NDArray or a scalar (including Complex). Previously that case threw InvalidArgumentException; it now fails with a TypeError from the stricter signature.

@CodeWithKyrian
CodeWithKyrian merged commit cbb5665 into main May 13, 2026
14 checks passed
@CodeWithKyrian
CodeWithKyrian deleted the fix/slice-assign-broadcast branch May 13, 2026 20:17
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