Skip to content

feat: add fromScalar() for creating 0-dimensional scalar arrays - #28

Merged
CodeWithKyrian merged 1 commit into
mainfrom
feat/from-scalar-factory
Jun 6, 2026
Merged

CodeWithKyrian merged 1 commit into
mainfrom
feat/from-scalar-factory

Conversation

@CodeWithKyrian

Copy link
Copy Markdown
Contributor

This PR adds NDArray::fromScalar() to create 0-dimensional arrays from a single PHP value.

Motivation and Context

The codebase already supports 0-dimensional arrays internally (e.g. from reductions on scalars), and has toScalar() to extract the value back, but there was no symmetric factory method to create a 0-d array from a PHP value. Users had to resort to NDArray::full(42, []) which is unintuitive.

What's Changed

  • Added NDArray::fromScalar(bool|Complex|float|int $value, ?DType $dtype = null): self — delegates to full() with shape []
  • Added from_scalar() global function alias in Functions.php
  • Added 7 test cases covering int, float, bool, Complex, explicit dtype, broadcasting, and the global function
  • Added documentation to the quick-start guide

Breaking Changes

None.

@CodeWithKyrian
CodeWithKyrian merged commit 894a7d5 into main Jun 6, 2026
14 checks passed
@CodeWithKyrian
CodeWithKyrian deleted the feat/from-scalar-factory branch June 6, 2026 22: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