194 Commits
Author SHA1 Message Date
Iason Krommydas a74be27658 ENH: support reduction loops in ufunc.reduceat (#32212)
Signed-off-by: Iason Krommydas <iason.krom@gmail.com>
2026-10-28 11:56:29 -06:00
Abhinay Reddy Keesara c5cf6f3f95 DEP: Deprecate np.dot when a.ndim >= 2 and b.ndim > 2 (#32759)
When a.ndim >= 2 and b.ndim > 2, np.dot() and ndarray.dot() place b's leading dimensions in the middle of the result:

a = np.ones((2, 3))
b = np.ones((4, 3, 5))
np.dot(a, b).shape  # (2, 4, 5) - the 4 ends up in the middle

@/matmul gives (4, 2, 5). This PR deprecates the dot behavior for these inputs and suggests numpy.tensordot(a, b, axes=[-1, -2]) for the same result, or @/matmul for batched matrix multiplication.
2026-10-20 21:03:14 +02:00
pereradrianandAdrián Muñoz Perera 8ba30a04ad DOC: point to C-API include instructions at top of Array API page [sk… (#33094)
Co-authored-by: Adrián Muñoz Perera <10870103+pereradrian@users.noreply.github.com>
2026-10-19 21:23:12 +03:00
Dragos-Georgian IonandInessa Pawson 82b2cf5518 DOC: fixing `ndarray` declaration documentation (#32933)
Co-authored-by: Inessa Pawson <inessapawson@gmail.com>
2026-10-08 09:43:37 +02:00
Nathan Goldbaum 6068016edd ENH: infer sizes when casting or creating arrays from StringDType arrays (#32097)
Adds a new helper stringdtype_find_fixed_width_descr and uses 
it in a new code path in find_descriptor_from_array that works
like the existing object string and datetime code paths in that
function that scan array values to infer the destination dtype.

Also adds tests for this new behavior.
2026-08-28 19:23:43 -04:00
Nathan Goldbaum 1785d72cac MAINT: soft-deprecate PyArray_malloc/realloc/free in favor of PyMem_Raw APIs 2026-08-18 10:38:51 -06:00
Iason Krommydas 10613a8579 ENH: add mechanism to register reduction loops to ufuncs for multi-output reductions (#31816)
Signed-off-by: Iason Krommydas <iason.krom@gmail.com>
2026-07-30 12:53:37 -06:00
e87191ec1f ENH: New-style ArrayMethods for partitioning (#31614)
Co-authored-by: Nathan Goldbaum <nathan.goldbaum@gmail.com>
Co-authored-by: Sebastian Berg <sebastianb@nvidia.com>
2026-07-16 14:19:09 +02:00
Sebastian Berg 80a35f1b07 DOC: Correct backcompat sentence and reduce NumPy 2 docs (#31869) 2026-07-09 10:24:00 -06:00
Shoumik Chakravarty 8811d183a4 DOC: fix duplicated-word and grammar typos in C-API reference (#31802) 2026-06-30 08:57:54 -06:00
Sebastian Berg 5e403264ee BUG: Relax finfo to be easier accessible for all user dtypes (#31563)
This allows `finfo` to be used for downstream user dtypes that do not subclass `inexact` or are "complex".  Where complex is defined by whether `arr.imag/arr.real` return the same "real" dtype correspondance.
2026-06-22 08:58:11 +02:00
Codex beb055de72 Fix typo in array.rst: 'of of' → 'of' 2026-06-14 10:07:24 +05:30
Maanas AroraandSebastian Berg f898c60088 ENH: Descending partitioning (#31511)
This PR adds `descending` as an option to partitioning and expands the internal implementations
to support it.

User DTypes are not yet supported as they currently always use the sort fallback. Additional changes
should allow to (a) use the sort as a fallback in a more reliable way and (b) change partitioning to support
a similar new-style API with registration for user DTypes.

Co-authored-by: Sebastian Berg <sebastianb@nvidia.com>
2026-06-10 14:17:17 +02:00
Sebastian Berg eb4d200f9d ENH: Create a path for legacy dtypes to transition to new DType API (#31302)
This adds and importantly backports a way for legacy dtypes to transition to the new API.
I.e. with one caveat I found for now (array dtype printing prints as np.array([1.0], dtype=repr(dtype)) and the repr now shouldn't be bfloat16 for an ml_dtype bfloat16), this allows transitioning a package like ml_dtypes over.

The advantage of this is that it allows using the new API even back to NumPy 2 without any real serious downsides (for dtypes that are currently on the legacy API).
This is a pretty big deal, I think.  Because the new API does offer a few improvements that can be reliably overridden also for NumPy 2.0 and it makes it a lot simpler to opt-in to new API e.g. for NumPy 2.4+ (with a runtime check).
2026-05-14 14:39:13 +02:00
Sebastian Berg 30dcf3cf9e ENH: Add internal real/imag ufuncs, use for attributes and refine object dtype (#30984)
Restructure real/imag attributes to use an `ArrayMethod` that can be registered similar to other ufuncs and sorting functions.

Object dtype is changed here to use `getattr(element, "real", element)` and `getattr(element, "imag", 0)` as a step in a more reliable direction. The returned array is marked as read-only to prevent trying to modify `.real/.imag` results directly.

(If the object changes are problematic, they can be modified again. The main reason for this is also to allow user DTypes to define this correctly.)
2026-03-29 11:48:00 +02:00
Kumar AdityaandNathan Goldbaum b996b34012 ENH: add getters for accessing struct fields in cython (#30994)
This PR adds getters for accessing struct fields in cython. In future these would call an ABI function to get the actual fields struct on opaque PyObject builds.

Co-authored-by: Nathan Goldbaum <nathan.goldbaum@gmail.com>
2026-03-24 14:41:06 +01:00
Nathan Goldbaum 54d9fd17ae DOC: Apply Marten's comments and rephrase slightly 2026-03-21 09:19:43 -06:00
Nathan Goldbaum 185b41aee4 Respond to Matti's comments 2026-03-20 13:55:45 -06:00
Nathan GoldbaumandMatti Picus 939c604437 Update doc/source/reference/c-api/array.rst
Co-authored-by: Matti Picus <matti.picus@gmail.com>
2026-03-20 13:23:46 -06:00
Nathan Goldbaum 86eb95bd5c DOC: document caveats of ndarray.resize on 3.14 and newer 2026-03-16 12:50:18 -06:00
Vineet Kumar 91451d506a DOC: fix typo in array.rst regarding sorting requirements 2026-01-27 20:22:55 +05:30
M Bussonnier 067f8b0d39 DOC: Improve cross-links in thread safety documentation (#30373)
* DOC: Improve cross-links in thread safety documentation

Those are mostly cosmetic changes and extra cross-links to improve the
documentation. In particular I'd like to use this page as an example of
good top-level documentation for threading/freethreading in the
py-threading-guide https://py-free-threading.github.io/

This also does a number of updates to the misc.rst file which was
basically too indented, and missing a bunch of links. I think many of
the info misc.rst on how to interop with C/C++ could be removed, and likely
all the information on seterr/geterr moved to routines.err.rst but I don't
want to include this moves as part of this PR as it's likely more
subject to discussion.

There are minor updates in conf.py to avoid 1 sphinx warnings, and I
know the thread safety backlink in geterr/seterr/...etc, are a bit overkill,
but as numpy will be used as an example and in general is referred to as
one of the core scientific Python project I thinks it's OK go go a bit
further than usual.

[skip actions]
[skip cirrus]

* typo
2025-12-11 09:42:18 -07:00
Joren HammudogluandCharles Harris cee14ff2a6 DOC: prefer passing scalar types to `dtype=` in examples (#30394)
* DOC: prefer scalar types for ``dtype=``

* DOC: revert doc change in `mtrand.pyx`

Co-authored-by: Charles Harris <charlesr.harris@gmail.com>

---------

Co-authored-by: Charles Harris <charlesr.harris@gmail.com>
2025-12-08 13:42:42 -07:00
Marten Henric van Kerkwijk 930b0c241b MAINT,BUG: create PyArray_Resize_int, which returns int, and use it internally.
Also fix the minimum allocation used inside, which should be 1, not elsize.
And use the default function to deallocate dimension & strides.
2025-11-12 12:54:42 -05:00
M Bussonnier 576e0a10f9 DOC: Fix some broken refs and Typos. (#30196)
The default role is set to autolink (which fallbacks to emphasis if not resolved)
And I believe there is typo in ALIGNMENT vs ALIGNENT.

I would suggest moving away from autolink for default role,
and have it raise an error if not resolved.
This would also make sure users use double backticks for code.

[skip actions][skip azp][skip cirrus]
2025-11-11 09:50:01 -08:00
Marten van Kerkwijk b78f89101f ENH: In spec registration, allow looking up ufuncs in any module. (#29931)
This allows the addition of loops from specs also from more complicated names such as numpy:strings.str_len.
2025-10-13 20:42:44 +02:00
Swayam a2519b560f DOC: Documentation related finfo refactors and new slot addition in #29836 (#29889)
This is a follow-up PR of work happened in gh-29836

* The deprecated MachAr runtime discovery mechanism has been removed.
* np.finfo fetches the constants provided by the compiler macros
* new slot to fetch the dtype related constants
2025-10-13 13:39:58 +02:00
Maanas AroraandSebastian Berg edd072fa5d ENH: Add registration for sort and argsort in new convenience ufunc registration function
Includes the following commits:

DOC: Document `PyUFunc_AddLoopsFromSpecs` and new sorting / argsorting arraymethods

ENH: Update field names in PyUFunc_LoopSlot struct

ENH: Update C-API hash (15, NumPy 2.4)

ENH: Add 'PyUFunc_LoopSlots' to ufunc_funcs_api with MinVersion "2.4"

ENH: Update hash for version 21 (NumPy 2.4.0)

ENH: Move PyUFunc_LoopSlot struct definition and update version hash for NumPy 2.4.0

REF: Move PyUFunc_LoopSlot struct definition to dtype_api.h

DOC: Clarify ArrayMethod API registration process in array.rst

DOC: Update sorting parameters struct in array.rst to use correct member (flags)

ENH: Clarify version 21 description in cversions

REF: Remove unnecessary error message for non-ufunc attributes in PyUFunc_AddLoopsFromSpecs

REF: Refactor ufunc initialization and consolidate sort loops in sfloat_init_ufuncs

REF: Simplify ufunc import by replacing PyObject_GetAttrString with npy_import

REF: Rename ufunc_name to name in PyUFunc_LoopSlot

REF: Initialize res variable to -1 in sfloat_init_ufuncs for clarity

STYLE: Refactor error message formatting in PyUFunc_AddLoopsFromSpecs for clarity

REF: Simplify redundant Py_DECREF call in PyUFunc_AddLoopsFromSpecs

DOC: Improve documentation for PyUFunc_AddLoopsFromSpecs and sorting

ENH: Optimize ufunc handling in PyUFunc_AddLoopsFromSpecs by using cached imports for sort and argsort

DOC: Clarify pointer type

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>

REF: Optimize array definition of loop slots for sfloat

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>

REF: Improve error message for missing ufunc by removing module name

BUG: Fix incorrect use of `npy_cache_import_runtime`

REF: Reuse dtypes variable in sfloat ufunc init

REF: Cache sort and argsort functions in npy_runtime_imports_struct

BUG: Add missing header file for and add error handling to `numpy_cache_import_runtime`

BUG: Improve error handling for numpy runtime imports in PyUFunc_AddLoopsFromSpecs

BUG: Replace npy_cache_import_runtime with direct npy_import calls for sort and argsort

DOC: Fix code annotations and references in sorting and argsorting docs

BUG: Optimize numpy import calls for sort and argsort in PyUFunc_AddLoopsFromSpecs

BUG: Remove unused references to _sort and _argsort in npy_runtime_imports_struct

BUG: Replace npy_import calls with npy_cache_import_runtime for sort and argsort in PyUFunc_AddLoopsFromSpecs

DOC: Add versionadded directive for PyUFunc_AddLoopsFromSpecs in array.rst

BUG: Add missing Py_DECREF for ufunc in PyUFunc_AddLoopsFromSpecs

BUG: Replace npy_cache_import calls with npy_import for sort and argsort, simplify decrefs
2025-10-12 10:59:51 -04:00
abb8dac07f ENH: add a casting option 'same_value' and use it in np.astype (#29129)
* start implementing same_value casting

* work through more places that check 'cast', add a TODO

* add a test, percolate casting closer to inner loops

* use SAME_VALUE_CAST flag for one inner loop variant

* aligned test of same_value passes. Need more tests

* handle unaligned casting with 'same_value'

* extend tests to use source-is-complex

* fix more interfaces to pass casting around, disallow using 'same_value' in raw_array_assign_scalar and raw_array_wheremasked_assign_scalar

* raise in places that have a kwarg casting, besides np.astype

* refactor based on review comments

* CHAR_MAX,MIN -> SCHAR_MAX,MIN

* copy context flags

* add 'same_value' to typing stubs

* document new feature

* test, check exact float->int casting: refactor same_value check into a function

* enable astype same_value casting for scalars

* typo

* fix ptr-to-src_value -> value casting errors

* fix linting and docs, ignore warning better

* gcc warning is different

* fixes from review, typos

* fix compile warning ignore and make filter in tests more specific, disallow non-numeric 'same_value'

* fix warning filters

* emit PyErr inside the loop

* macOS can emit FPEs when touching NAN

* Fix can-cast logic everywhere for same-value casts (only allow numeric)

* reorder and simplify, from review

* revert last commit and remove redundant checks

* gate and document SAME_VALUE_CASTING for v2.4

* make SAME_VALUE a flag, not an enum

* fixes from review

* fixes from review

* inline inner casting loop and float16 -> float64 calls

* typo

* fixes for AVX512 and debug builds, inline npy_halfbits_to_doublebits only on specific platforms

* use optimize instead of inline

---------

Co-authored-by: Matti Picus <matti.picus#@gmail.com>
Co-authored-by: Sebastian Berg <sebastianb@nvidia.com>
2025-09-17 20:16:04 +02:00
Charles Harris c2eeeba0c3 MAINT: Remove NPY_SORT_NANFIRST from docs and code. 2025-09-03 19:04:55 -06:00
Charles Harris 22be8c5db4 DOC: Documentation improvements from review. 2025-09-02 16:21:23 -06:00
Charles Harris 9f42dca359 ENH: Add extended sorting APIs
This PR adds new sorting APIs that can be used for extending our current
sort types. The new C-APIs do not have the *which* argument of the
legacy C-APIs, instead they have *flags* request flag API. In this
transition, heapsort can no longer be called directly, introsort
(quicksort) is called instead. The sort/argsort methods get new keyword
arguments used to set the flags when *kind* is not given, so *flags* is
invisible at the Python level. The new keywords are:

- stable -- whether the sort should stable, default is False,
- descending -- whether to sort in descending order, default is False,
- nanfirst -- whether NaNs sort to the last or first, default is False.

The main difference here is that the keywords select by functionality,
whereas *kind* selects by algorithm. Note that descending and nanfirst
are not yet implemented, so an error is raised if either is specified.

The added C-API functions are:

- (API)PyArray_SortEx,
- (API)PyArray_ArgSortEx,

Those are the two functions that need changes depending on how we want
to implement adding new sort algorithms.  They should be pretty easy to
adapt to whatever we choose to do.

The legacy C-API functions, PyArray_Sort and PyArray_ArgSort, have been
modified to call through the new C-API functions. In order to do so
the *kind* function has been encoded in the *flags* like so:

- "quicksort" - default (0)
- "heapsort"  - default (0)
- "mergesort" - stable  (1)

I note that the partitioning/selection functions have not been modified,
we may want to do that a some point.
2025-09-02 13:37:51 -06:00
ianlv ec34c6d3f6 chore: remove redundant words in comment (#29306) 2025-07-02 10:38:32 +02:00
Sebastian Berg a1e8dde1fd Update doc/source/reference/c-api/array.rst 2025-03-07 09:30:46 +01:00
Hrisca Daniel (uidn3651) d2a259d95f use "const PyArrayObject *" for all the flag checking functions as requested in the PR review 2025-03-07 09:58:25 +02:00
Hrisca Daniel (uidn3651) eb04a84fa1 DOC: fix documentation for Flag checking functions and macros
the array argument type should be PyArrayObject instead of PyObject

See #28434
2025-03-06 10:00:17 +02:00
Tyler Reddy 40e6489d8a DOC: fix C API docs for PyArray_Size (#28382)
* The actual C function signature is `PyArray_Size(PyObject *op)`,
and the compiler will issue a warning if you follow the current
C API docs that specify `PyArrayObject*`, so clean this up. I noticed
this while working on gh-28355, but it was also mentioned as an aside
7 years ago in gh-10977.

[skip azp] [skip cirrus] [skip actions]
2025-02-24 14:34:36 +01:00
mattip b7ee98bf16 BUG: handle possible error for PyTraceMallocTrack
DOC: update documentation for PyDataMem_* functions
2024-10-15 21:17:26 +03:00
Santhana Mikhail Antony S c3a78be439 Removed all outdated versionadded/changed directives ISSUES#27239 2024-09-23 09:59:46 +00:00
musvaage 21886d2239 typos 2024-09-04 11:50:34 -05:00
Sebastian Berg b3ddf2fd33 MAINT: Remove any promotion-state switching logic (#27156)
This is the first level, no following code simplifications, just
straight up deletions of any branching.

I kept a UserWarning, in case someone had the bad idea to permanently
set the environment variable and think they can rely on it.
Although would be happy to just delete that as well.

* DOC: Add release note for promotion state removal

I think we can just do that, but maybe it's prudent to just point
it out anyway...

* DOC: Mention semi-private removed functions and tweak docs

* Adress Marten's last comment
2024-08-31 11:13:31 +02:00
Nathan Goldbaum 3113704a74 Merge pull request #27203 from guan404ming/PyArray-CheckAxis-doc 2024-08-22 07:06:47 -06:00
GUAN MINGandNathan Goldbaum ad7755086e DOC: update doc/source/reference/c-api/array.rst
Co-authored-by: Nathan Goldbaum <nathan.goldbaum@gmail.com>
2024-08-22 14:52:00 +08:00
Nathan Goldbaum 1e4f04f687 Move NUMUSERTYPES thread safety discussion to legacy DType API docs 2024-08-16 10:52:01 -06:00
GUAN MING 10f443e271 DOC: update PyArray_CheckAxis doc 2024-08-13 22:46:55 +08:00
Sebastian Berg 241a5300ab DOC: Add a note that one should free the proto struct
Really, heap allocating should be very rare and I am not even sure
it ever worked (in the error paths at least), but people should
be able to work around it easily enough, so give a pointer.

Close gh-26763
2024-07-09 13:32:52 +02:00
Dimitri Papadopoulos c7a9216878 DOC: fix typos found by codespell in documentation 2024-07-06 15:36:09 +02:00
Mateusz Sokół de897afd80 MNT: Remove set_string_function 2024-06-04 17:06:24 +02:00
Leo Singer 42e7987f38 MAINT: fix typo 2024-05-15 23:14:06 -04:00
Yuki K 8966ad05c3 DOC: Fix some typos and incorrect markups
[skip cirrus] [skip azp] [skip actions]
2024-05-10 12:05:04 +00:00