Conversation
Implements both NumPy resize entry points with their distinct fill semantics,
plus the ARC-refcount plumbing that ndarray.resize's refcheck requires.
np.resize(a, new_shape) - function (Manipulation/np.resize.cs)
* Enlarges by tiling REPEATED COPIES of `a` in C-order; result is always
C-contiguous and any input layout is raveled first.
* Fills via an exact-sized doubling byte-tile: allocates exactly new_size and
grows the filled region by self-copying (dtype-agnostic raw memory,
O(new_size) bytes, O(log(new_size/size)) memcpys) - beats NumPy's
concatenate((a,)*repeats)[:new_size] which over-allocates then slices.
* Empty source or zero new-size -> zeros(new_shape, a.dtype).
* Negative dim -> ArgumentException ("all elements of `new_shape` must be
non-negative"), matching np.resize's wording.
ndarray.resize(new_shape, refcheck=true) - in-place (Manipulation/NDArray.resize.cs)
* Grows with ZEROS, shrinks by truncation, operating on the raw contiguous
buffer (so an F-contiguous resize relabels memory column-major).
* Mirrors NumPy's guards verbatim (IncorrectShapeException): single-segment
only; when byte size changes it must own its data (not a view) and - under
refcheck - not be shared. refcheck:false bypasses the sharing check.
* Same-size resize is a pure in-place reshape (no ownership/reference check).
* ARC-correct buffer swap: add-ref the fresh block, release the old (freed
when uniquely owned, kept alive for any sharer under refcheck:false).
* params overload takes long[] (long-indexing aligned), not int[].
IArraySlice.IsUniquelyReferenced - new refcount probe
* Added to IArraySlice, ArraySlice<T>, UnmanagedMemoryBlock<T> (+ inner
Disposer). True when the block is held by <= 1 logical reference; non-owning
Wrap allocations (external/pinned) report true (immortal refcount). Backs
resize's "references or is referenced by another array" guard.
Tests: 39 ResizeTests (Manipulation/np.resize.Test.cs) - function tiling/zeros/
scalar/empty/negative/dtype + method grow/shrink/reshape/no-op + view & F-order
+ refcheck sharing guards + error parity. All green on this base.
Docs: .claude/CLAUDE.md Shape Manipulation section documents both entry points.
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.
Overview
Adds NumPy's two
resizeentry points to NumSharp with full 2.x behavioral parity: thenp.resize(a, new_shape)function and the in-placendarray.resize(new_shape, refcheck=true)method. They deliberately differ in fill semantics, exactly like NumPy.What's added
np.resize(a, new_shape)— functionain C-order; result is always C-contiguous, any input layout is raveled first.new_sizeand grows the filled region by self-copying (dtype-agnostic,O(new_size)bytes,O(log(new_size/size))memcpys) — avoids NumPy'sconcatenate((a,)*repeats)[:new_size]over-allocation.zeros.ArgumentException("all elements of \new_shape` must be non-negative"`).ndarray.resize(new_shape, refcheck=true)— in-placeIncorrectShapeException): single-segment only; on byte-size change must own its data (not a view) and — underrefcheck— not be shared.refcheck:falsebypasses.refcheck:false).paramsoverload takeslong[](long-indexing aligned).IArraySlice.IsUniquelyReferenced— refcount probeIArraySlice,ArraySlice<T>,UnmanagedMemoryBlock<T>(+ innerDisposer).truewhen the block is held by ≤ 1 logical reference; non-owningWrapallocations (external/pinned) reporttrue(immortal refcount). Backs resize's "references or is referenced by another array" guard.Tests
39
ResizeTests(test/NumSharp.UnitTest/Manipulation/np.resize.Test.cs): function tiling / zeros / scalar / empty / negative / dtype, method grow / shrink / reshape / no-op, view & F-order,refchecksharing guards, and error-parity. All green onnet10.0.Breaking changes
None — both entry points are new API.
Notes
Branch is based on
origin/masterand contains only the resize feature.