Skip to content

Commit aae8453

Browse files
committed
Merge remote-tracking branch 'upstream/main' into ci_buildwheel_rf
2 parents 7dcc3c1 + e869222 commit aae8453

File tree

12 files changed

+304
-78
lines changed

12 files changed

+304
-78
lines changed

linter_requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pycodestyle==2.7.0
1+
pycodestyle==2.8.0
22
GitPython==3.1.13

numpy/__init__.pyi

Lines changed: 120 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -724,53 +724,6 @@ class chararray(ndarray[_ShapeType, _DType_co]):
724724
def isnumeric(self): ...
725725
def isdecimal(self): ...
726726

727-
class matrix(ndarray[_ShapeType, _DType_co]):
728-
def __new__(
729-
subtype,
730-
data: Any,
731-
dtype: Any = ...,
732-
copy: Any = ...,
733-
) -> Any: ...
734-
def __array_finalize__(self, obj): ...
735-
def __getitem__(self, index): ...
736-
def __mul__(self, other): ...
737-
def __rmul__(self, other): ...
738-
def __imul__(self, other): ...
739-
def __pow__(self, other): ...
740-
def __ipow__(self, other): ...
741-
def __rpow__(self, other): ...
742-
def tolist(self): ...
743-
def sum(self, axis=..., dtype=..., out=...): ...
744-
def squeeze(self, axis=...): ...
745-
def flatten(self, order=...): ...
746-
def mean(self, axis=..., dtype=..., out=...): ...
747-
def std(self, axis=..., dtype=..., out=..., ddof=...): ...
748-
def var(self, axis=..., dtype=..., out=..., ddof=...): ...
749-
def prod(self, axis=..., dtype=..., out=...): ...
750-
def any(self, axis=..., out=...): ...
751-
def all(self, axis=..., out=...): ...
752-
def max(self, axis=..., out=...): ...
753-
def argmax(self, axis=..., out=...): ...
754-
def min(self, axis=..., out=...): ...
755-
def argmin(self, axis=..., out=...): ...
756-
def ptp(self, axis=..., out=...): ...
757-
def ravel(self, order=...): ...
758-
@property
759-
def T(self): ...
760-
@property
761-
def I(self): ...
762-
@property
763-
def A(self): ...
764-
@property
765-
def A1(self): ...
766-
@property
767-
def H(self): ...
768-
def getT(self): ...
769-
def getA(self): ...
770-
def getA1(self): ...
771-
def getH(self): ...
772-
def getI(self): ...
773-
774727
# Some of these are aliases; others are wrappers with an identical signature
775728
round = around
776729
round_ = around
@@ -3916,3 +3869,123 @@ class poly1d:
39163869
m: SupportsInt | SupportsIndex = ...,
39173870
k: None | _ArrayLikeComplex_co | _ArrayLikeObject_co = ...,
39183871
) -> poly1d: ...
3872+
3873+
class matrix(ndarray[_ShapeType, _DType_co]):
3874+
def __new__(
3875+
subtype,
3876+
data: ArrayLike,
3877+
dtype: DTypeLike = ...,
3878+
copy: bool = ...,
3879+
) -> matrix[Any, Any]: ...
3880+
def __array_finalize__(self, obj: NDArray[Any]) -> None: ...
3881+
def __getitem__(self, index): ... # TODO
3882+
def __mul__(self, other: ArrayLike) -> matrix[Any, Any]: ...
3883+
def __rmul__(self, other: ArrayLike) -> matrix[Any, Any]: ...
3884+
def __imul__(self, other: ArrayLike) -> matrix[_ShapeType, _DType_co]: ...
3885+
def __pow__(self, other: ArrayLike) -> matrix[Any, Any]: ...
3886+
def __ipow__(self, other: ArrayLike) -> matrix[_ShapeType, _DType_co]: ...
3887+
3888+
@overload
3889+
def sum(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
3890+
@overload
3891+
def sum(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
3892+
@overload
3893+
def sum(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3894+
3895+
@overload
3896+
def mean(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
3897+
@overload
3898+
def mean(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
3899+
@overload
3900+
def mean(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3901+
3902+
@overload
3903+
def std(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> Any: ...
3904+
@overload
3905+
def std(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[Any, Any]: ...
3906+
@overload
3907+
def std(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ..., ddof: float = ...) -> _NdArraySubClass: ...
3908+
3909+
@overload
3910+
def var(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> Any: ...
3911+
@overload
3912+
def var(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[Any, Any]: ...
3913+
@overload
3914+
def var(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ..., ddof: float = ...) -> _NdArraySubClass: ...
3915+
3916+
@overload
3917+
def prod(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
3918+
@overload
3919+
def prod(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
3920+
@overload
3921+
def prod(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3922+
3923+
@overload
3924+
def any(self, axis: None = ..., out: None = ...) -> bool_: ...
3925+
@overload
3926+
def any(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[bool_]]: ...
3927+
@overload
3928+
def any(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3929+
3930+
@overload
3931+
def all(self, axis: None = ..., out: None = ...) -> bool_: ...
3932+
@overload
3933+
def all(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[bool_]]: ...
3934+
@overload
3935+
def all(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3936+
3937+
@overload
3938+
def max(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
3939+
@overload
3940+
def max(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
3941+
@overload
3942+
def max(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3943+
3944+
@overload
3945+
def min(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
3946+
@overload
3947+
def min(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
3948+
@overload
3949+
def min(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3950+
3951+
@overload
3952+
def argmax(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> intp: ...
3953+
@overload
3954+
def argmax(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[intp]]: ...
3955+
@overload
3956+
def argmax(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3957+
3958+
@overload
3959+
def argmin(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> intp: ...
3960+
@overload
3961+
def argmin(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[intp]]: ...
3962+
@overload
3963+
def argmin(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3964+
3965+
@overload
3966+
def ptp(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
3967+
@overload
3968+
def ptp(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
3969+
@overload
3970+
def ptp(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3971+
3972+
def squeeze(self, axis: None | _ShapeLike = ...) -> matrix[Any, _DType_co]: ...
3973+
def tolist(self: matrix[Any, dtype[_SupportsItem[_T]]]) -> List[List[_T]]: ... # type: ignore[typevar]
3974+
def ravel(self, order: _OrderKACF = ...) -> matrix[Any, _DType_co]: ...
3975+
def flatten(self, order: _OrderKACF = ...) -> matrix[Any, _DType_co]: ...
3976+
3977+
@property
3978+
def T(self) -> matrix[Any, _DType_co]: ...
3979+
@property
3980+
def I(self) -> matrix[Any, Any]: ...
3981+
@property
3982+
def A(self) -> ndarray[_ShapeType, _DType_co]: ...
3983+
@property
3984+
def A1(self) -> ndarray[Any, _DType_co]: ...
3985+
@property
3986+
def H(self) -> matrix[Any, _DType_co]: ...
3987+
def getT(self) -> matrix[Any, _DType_co]: ...
3988+
def getI(self) -> matrix[Any, Any]: ...
3989+
def getA(self) -> ndarray[_ShapeType, _DType_co]: ...
3990+
def getA1(self) -> ndarray[Any, _DType_co]: ...
3991+
def getH(self) -> matrix[Any, _DType_co]: ...

numpy/core/einsumfunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ def einsum_path(*operands, optimize='greedy', einsum_call=False):
987987

988988

989989
def _einsum_dispatcher(*operands, out=None, optimize=None, **kwargs):
990-
# Arguably we dispatch on more arguments that we really should; see note in
990+
# Arguably we dispatch on more arguments than we really should; see note in
991991
# _einsum_path_dispatcher for why.
992992
yield from operands
993993
yield out

numpy/core/src/multiarray/convert_datatype.c

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,40 @@ should_use_min_scalar(npy_intp narrs, PyArrayObject **arr,
15481548
}
15491549

15501550

1551+
/*
1552+
* Utility function used only in PyArray_ResultType for value-based logic.
1553+
* See that function for the meaning and contents of the parameters.
1554+
*/
1555+
static PyArray_Descr *
1556+
get_descr_from_cast_or_value(
1557+
npy_intp i,
1558+
PyArrayObject *arrs[],
1559+
npy_intp ndtypes,
1560+
PyArray_Descr *descriptor,
1561+
PyArray_DTypeMeta *common_dtype)
1562+
{
1563+
PyArray_Descr *curr;
1564+
if (NPY_LIKELY(i < ndtypes ||
1565+
!(PyArray_FLAGS(arrs[i-ndtypes]) & _NPY_ARRAY_WAS_PYSCALAR))) {
1566+
curr = PyArray_CastDescrToDType(descriptor, common_dtype);
1567+
}
1568+
else {
1569+
/*
1570+
* Unlike `PyArray_CastToDTypeAndPromoteDescriptors`, deal with
1571+
* plain Python values "graciously". This recovers the original
1572+
* value the long route, but it should almost never happen...
1573+
*/
1574+
PyObject *tmp = PyArray_GETITEM(arrs[i-ndtypes],
1575+
PyArray_BYTES(arrs[i-ndtypes]));
1576+
if (tmp == NULL) {
1577+
return NULL;
1578+
}
1579+
curr = NPY_DT_CALL_discover_descr_from_pyobject(common_dtype, tmp);
1580+
Py_DECREF(tmp);
1581+
}
1582+
return curr;
1583+
}
1584+
15511585
/*NUMPY_API
15521586
*
15531587
* Produces the result type of a bunch of inputs, using the same rules
@@ -1684,28 +1718,15 @@ PyArray_ResultType(
16841718
result = NPY_DT_CALL_default_descr(common_dtype);
16851719
}
16861720
else {
1687-
result = PyArray_CastDescrToDType(all_descriptors[0], common_dtype);
1721+
result = get_descr_from_cast_or_value(
1722+
0, arrs, ndtypes, all_descriptors[0], common_dtype);
1723+
if (result == NULL) {
1724+
goto error;
1725+
}
16881726

16891727
for (npy_intp i = 1; i < ndtypes+narrs; i++) {
1690-
PyArray_Descr *curr;
1691-
if (NPY_LIKELY(i < ndtypes ||
1692-
!(PyArray_FLAGS(arrs[i-ndtypes]) & _NPY_ARRAY_WAS_PYSCALAR))) {
1693-
curr = PyArray_CastDescrToDType(all_descriptors[i], common_dtype);
1694-
}
1695-
else {
1696-
/*
1697-
* Unlike `PyArray_CastToDTypeAndPromoteDescriptors` deal with
1698-
* plain Python values "graciously". This recovers the original
1699-
* value the long route, but it should almost never happen...
1700-
*/
1701-
PyObject *tmp = PyArray_GETITEM(
1702-
arrs[i-ndtypes], PyArray_BYTES(arrs[i-ndtypes]));
1703-
if (tmp == NULL) {
1704-
goto error;
1705-
}
1706-
curr = NPY_DT_CALL_discover_descr_from_pyobject(common_dtype, tmp);
1707-
Py_DECREF(tmp);
1708-
}
1728+
PyArray_Descr *curr = get_descr_from_cast_or_value(
1729+
i, arrs, ndtypes, all_descriptors[i], common_dtype);
17091730
if (curr == NULL) {
17101731
goto error;
17111732
}

numpy/core/src/umath/loops_exponent_log.dispatch.c.src

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,32 @@ AVX512F_exp_DOUBLE(npy_double * op,
868868
* = p(r)
869869
* = 2((r/2) + 1/3*(r/2)^3 + 1/5*(r/2)^5 + ...)
870870
*/
871+
872+
/* LLVM has a bug where AVX-512F intrinsic `_mm512_mask_mul_pd` emits an
873+
* unmasked operation with a masked store. This can cause FP exceptions to
874+
* occur for the lanes that are suppose to have been masked.
875+
*
876+
* See https://bugs.llvm.org/show_bug.cgi?id=51988
877+
*
878+
* Note, this affects LLVM based compilers like Apple Clang, Clang, and Intel's
879+
* ICX.
880+
*/
881+
#if defined(__clang__)
882+
#if defined(__apple_build_version__)
883+
// Apple Clang
884+
#if __apple_build_version__ > 11000000
885+
// Apple Clang after v11
886+
#define WORKAROUND_LLVM__mm512_mask_mul_pd
887+
#endif
888+
#else
889+
// Clang, not Apple Clang
890+
#if __clang_major__ > 9
891+
// Clang v9+
892+
#define WORKAROUND_LLVM__mm512_mask_mul_pd
893+
#endif
894+
#endif
895+
#endif
896+
871897
static void
872898
AVX512F_log_DOUBLE(npy_double * op,
873899
npy_double * ip,
@@ -954,8 +980,12 @@ AVX512F_log_DOUBLE(npy_double * op,
954980
denormal_mask = _mm512_cmp_epi64_mask(top12, _mm512_set1_epi64(0),
955981
_CMP_EQ_OQ);
956982
denormal_mask = (~zero_mask) & denormal_mask;
983+
__m512d masked_x = x;
984+
#ifdef WORKAROUND_LLVM__mm512_mask_mul_pd
985+
masked_x = avx512_set_masked_lanes_pd(masked_x, zeros_d, (~denormal_mask));
986+
#endif
957987
ix = _mm512_castpd_si512(_mm512_mask_mul_pd(x, denormal_mask,
958-
x, _mm512_set1_pd(0x1p52)));
988+
masked_x, _mm512_set1_pd(0x1p52)));
959989
ix = _mm512_mask_sub_epi64(ix, denormal_mask,
960990
ix, _mm512_set1_epi64(52ULL << 52));
961991

@@ -1039,6 +1069,9 @@ AVX512F_log_DOUBLE(npy_double * op,
10391069
npy_set_floatstatus_divbyzero();
10401070
}
10411071
}
1072+
1073+
#undef WORKAROUND_LLVM__mm512_mask_mul_pd
1074+
10421075
#endif // AVX512F_NOCLANG_BUG
10431076

10441077
#ifdef SIMD_AVX512_SKX

numpy/core/tests/test_dtype.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,14 @@ def test_subscript_scalar(self) -> None:
15901590
assert np.dtype[Any]
15911591

15921592

1593+
def test_result_type_integers_and_unitless_timedelta64():
1594+
# Regression test for gh-20077. The following call of `result_type`
1595+
# would cause a seg. fault.
1596+
td = np.timedelta64(4)
1597+
result = np.result_type(0, td)
1598+
assert_dtype_equal(result, td.dtype)
1599+
1600+
15931601
@pytest.mark.skipif(sys.version_info >= (3, 9), reason="Requires python 3.8")
15941602
def test_class_getitem_38() -> None:
15951603
match = "Type subscription requires python >= 3.9"

numpy/core/tests/test_umath.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,12 @@ def test_log_values(self):
973973
xf = np.log(x)
974974
assert_almost_equal(np.log(x, out=x), xf)
975975

976+
# test log() of max for dtype does not raise
977+
for dt in ['f', 'd', 'g']:
978+
with np.errstate(all='raise'):
979+
x = np.finfo(dt).max
980+
np.log(x)
981+
976982
def test_log_strides(self):
977983
np.random.seed(42)
978984
strides = np.array([-4,-3,-2,-1,1,2,3,4])

numpy/f2py/setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def configuration(parent_package='', top_path=None):
6666
wrapping Fortran 77/90/95 subroutines, accessing common blocks from
6767
Python, and calling Python functions from Fortran (call-backs).
6868
Interfacing subroutines/data from Fortran 90/95 modules is supported.""",
69-
url="https://web.archive.org/web/20140822061353/"\
70-
"http://cens.ioc.ee/projects/f2py2e/",
69+
url="https://numpy.org/doc/stable/f2py/",
7170
keywords=['Fortran', 'f2py'],
7271
**config)

numpy/matrixlib/__init__.pyi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
from typing import Any, List
1+
from typing import List
22

33
from numpy._pytesttester import PytestTester
44

55
from numpy import (
66
matrix as matrix,
77
)
88

9+
from numpy.matrixlib.defmatrix import (
10+
bmat as bmat,
11+
mat as mat,
12+
asmatrix as asmatrix,
13+
)
14+
915
__all__: List[str]
1016
__path__: List[str]
1117
test: PytestTester
12-
13-
def bmat(obj, ldict=..., gdict=...): ...
14-
def asmatrix(data, dtype=...): ...
15-
mat = asmatrix

0 commit comments

Comments
 (0)