Skip to content

Set routines: implement np.* set operations (isin, intersect1d, union1d, setdiff1d, setxor1d, unique_*) #622

Description

@Nucs

Overview

NumSharp ships np.unique (with the full return_index/return_inverse/return_counts/axis overload) but none of NumPy's set-operation routines built on top of it. This tracks adding the 9 missing numpy set functions -- every one is a thin composition over primitives NumSharp already has, so there are no new kernels to write.

Problem

NumPy's Set routines category (numpy/lib/_arraysetops_impl.py) exposes 10 functions; NumSharp implements 1 (unique). The membership / intersection / union / difference operations -- bread-and-butter for label handling, dedup, and data wrangling -- are all absent, along with the NumPy 2.x Array-API unique_* variants.

Verified codebase state

  • Implemented: np.unique -- including unique(return_index, return_inverse, return_counts, axis, equal_nan) (Manipulation/np.unique.cs).
  • Missing (all 9 below): verified absent from the np.* surface (reflection over the compiled np) and as NDArray methods / internal helpers (source grep across src/NumSharp.Core).

Proposal

Add the 9 functions under the main np.* namespace (NumPy keeps them there; several are re-exported in np.lib). All are pure composition -- the unique_* family is one-liners because unique's return flags already exist; the binary ops compose unique/sort/searchsorted.

Task checklist (ordered by real-world usage)

  • isin (33,936) -- element-wise membership test element in test_elements (broadcasts over element)
  • intersect1d (22,168) -- sorted unique elements common to both arrays
  • setdiff1d (20,912) -- sorted unique elements of ar1 not in ar2
  • union1d (15,824) -- sorted unique union of two arrays
  • unique_values (4,440) -- Array-API: the unique values only
  • unique_inverse (4,056) -- Array-API: unique values + inverse-reconstruction indices
  • unique_counts (3,824) -- Array-API: unique values + counts
  • setxor1d (2,496) -- sorted unique symmetric difference
  • unique_all (2,068) -- Array-API: unique values + indices + inverse + counts

Evidence -- GitHub usage ranking

# Function GitHub hits What it does Implementable via
1 np.isin 33,936 element-wise membership test element in test_elements (broadcasts over element) unique+searchsorted
2 np.intersect1d 22,168 sorted unique elements common to both arrays unique+searchsorted
3 np.setdiff1d 20,912 sorted unique elements of ar1 not in ar2 built on isin
4 np.union1d 15,824 sorted unique union of two arrays unique(concatenate)
5 np.unique_values 4,440 Array-API: the unique values only unique wrapper
6 np.unique_inverse 4,056 Array-API: unique values + inverse-reconstruction indices unique(return_inverse)
7 np.unique_counts 3,824 Array-API: unique values + counts unique(return_counts)
8 np.setxor1d 2,496 sorted unique symmetric difference unique+count
9 np.unique_all 2,068 Array-API: unique values + indices + inverse + counts unique(return_index,inverse,counts)

Combined usage ~ 109,724 hits. isin alone (~34K) is among the most-used missing functions across the whole set+sort+indexing sweep.

Usage = GitHub REST code-search total_count in each function's canonical call form (np.<fn>(, or np.<fn>[ for the index-expression objects). It is an approximate estimate (noisy plus/minus ~10% on large sets) and counts only the dominant np. alias. Measured 2026-07-19; treat as relative demand, not exact.

Scope / Non-goals

  • Composition only -- no new IL kernels; reuse unique/sort/searchsorted.
  • in1d (deprecated pre-2.0 alias of isin) is not in scope.
  • 1-D semantics per NumPy (inputs raveled unless documented otherwise); axis= only where NumPy supports it.

Related issues

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NumPy 2.x ComplianceAligns behavior with NumPy 2.x (NEPs, breaking changes)apiPublic API surface (np.*, NDArray methods, operators)coreInternal engine: Shape, Storage, TensorEngine, iteratorsenhancementNew feature or requestmissing feature/sNumPy function not yet implemented in NumSharp

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions