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.
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.
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.
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>
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).
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.)
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>
* 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
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]
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
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
* 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>
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.
* 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]
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
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