diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/__init__.pyi | 167 | ||||
-rw-r--r-- | numpy/matrixlib/__init__.pyi | 12 | ||||
-rw-r--r-- | numpy/matrixlib/defmatrix.pyi | 15 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/matrix.pyi | 69 |
4 files changed, 211 insertions, 52 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 560139d48..097773c5d 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -724,53 +724,6 @@ class chararray(ndarray[_ShapeType, _DType_co]): def isnumeric(self): ... def isdecimal(self): ... -class matrix(ndarray[_ShapeType, _DType_co]): - def __new__( - subtype, - data: Any, - dtype: Any = ..., - copy: Any = ..., - ) -> Any: ... - def __array_finalize__(self, obj): ... - def __getitem__(self, index): ... - def __mul__(self, other): ... - def __rmul__(self, other): ... - def __imul__(self, other): ... - def __pow__(self, other): ... - def __ipow__(self, other): ... - def __rpow__(self, other): ... - def tolist(self): ... - def sum(self, axis=..., dtype=..., out=...): ... - def squeeze(self, axis=...): ... - def flatten(self, order=...): ... - def mean(self, axis=..., dtype=..., out=...): ... - def std(self, axis=..., dtype=..., out=..., ddof=...): ... - def var(self, axis=..., dtype=..., out=..., ddof=...): ... - def prod(self, axis=..., dtype=..., out=...): ... - def any(self, axis=..., out=...): ... - def all(self, axis=..., out=...): ... - def max(self, axis=..., out=...): ... - def argmax(self, axis=..., out=...): ... - def min(self, axis=..., out=...): ... - def argmin(self, axis=..., out=...): ... - def ptp(self, axis=..., out=...): ... - def ravel(self, order=...): ... - @property - def T(self): ... - @property - def I(self): ... - @property - def A(self): ... - @property - def A1(self): ... - @property - def H(self): ... - def getT(self): ... - def getA(self): ... - def getA1(self): ... - def getH(self): ... - def getI(self): ... - # Some of these are aliases; others are wrappers with an identical signature round = around round_ = around @@ -3916,3 +3869,123 @@ class poly1d: m: SupportsInt | SupportsIndex = ..., k: None | _ArrayLikeComplex_co | _ArrayLikeObject_co = ..., ) -> poly1d: ... + +class matrix(ndarray[_ShapeType, _DType_co]): + def __new__( + subtype, + data: ArrayLike, + dtype: DTypeLike = ..., + copy: bool = ..., + ) -> matrix[Any, Any]: ... + def __array_finalize__(self, obj: NDArray[Any]) -> None: ... + def __getitem__(self, index): ... # TODO + def __mul__(self, other: ArrayLike) -> matrix[Any, Any]: ... + def __rmul__(self, other: ArrayLike) -> matrix[Any, Any]: ... + def __imul__(self, other: ArrayLike) -> matrix[_ShapeType, _DType_co]: ... + def __pow__(self, other: ArrayLike) -> matrix[Any, Any]: ... + def __ipow__(self, other: ArrayLike) -> matrix[_ShapeType, _DType_co]: ... + + @overload + def sum(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ... + @overload + def sum(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ... + @overload + def sum(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ... + + @overload + def mean(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ... + @overload + def mean(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ... + @overload + def mean(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ... + + @overload + def std(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> Any: ... + @overload + def std(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[Any, Any]: ... + @overload + def std(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ..., ddof: float = ...) -> _NdArraySubClass: ... + + @overload + def var(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> Any: ... + @overload + def var(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[Any, Any]: ... + @overload + def var(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ..., ddof: float = ...) -> _NdArraySubClass: ... + + @overload + def prod(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ... + @overload + def prod(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ... + @overload + def prod(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ... + + @overload + def any(self, axis: None = ..., out: None = ...) -> bool_: ... + @overload + def any(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[bool_]]: ... + @overload + def any(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ... + + @overload + def all(self, axis: None = ..., out: None = ...) -> bool_: ... + @overload + def all(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[bool_]]: ... + @overload + def all(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ... + + @overload + def max(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ... + @overload + def max(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ... + @overload + def max(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ... + + @overload + def min(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ... + @overload + def min(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ... + @overload + def min(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ... + + @overload + def argmax(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> intp: ... + @overload + def argmax(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[intp]]: ... + @overload + def argmax(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ... + + @overload + def argmin(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> intp: ... + @overload + def argmin(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[intp]]: ... + @overload + def argmin(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ... + + @overload + def ptp(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ... + @overload + def ptp(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ... + @overload + def ptp(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ... + + def squeeze(self, axis: None | _ShapeLike = ...) -> matrix[Any, _DType_co]: ... + def tolist(self: matrix[Any, dtype[_SupportsItem[_T]]]) -> List[List[_T]]: ... # type: ignore[typevar] + def ravel(self, order: _OrderKACF = ...) -> matrix[Any, _DType_co]: ... + def flatten(self, order: _OrderKACF = ...) -> matrix[Any, _DType_co]: ... + + @property + def T(self) -> matrix[Any, _DType_co]: ... + @property + def I(self) -> matrix[Any, Any]: ... + @property + def A(self) -> ndarray[_ShapeType, _DType_co]: ... + @property + def A1(self) -> ndarray[Any, _DType_co]: ... + @property + def H(self) -> matrix[Any, _DType_co]: ... + def getT(self) -> matrix[Any, _DType_co]: ... + def getI(self) -> matrix[Any, Any]: ... + def getA(self) -> ndarray[_ShapeType, _DType_co]: ... + def getA1(self) -> ndarray[Any, _DType_co]: ... + def getH(self) -> matrix[Any, _DType_co]: ... diff --git a/numpy/matrixlib/__init__.pyi b/numpy/matrixlib/__init__.pyi index 26453f000..c1b82d2ec 100644 --- a/numpy/matrixlib/__init__.pyi +++ b/numpy/matrixlib/__init__.pyi @@ -1,4 +1,4 @@ -from typing import Any, List +from typing import List from numpy._pytesttester import PytestTester @@ -6,10 +6,12 @@ from numpy import ( matrix as matrix, ) +from numpy.matrixlib.defmatrix import ( + bmat as bmat, + mat as mat, + asmatrix as asmatrix, +) + __all__: List[str] __path__: List[str] test: PytestTester - -def bmat(obj, ldict=..., gdict=...): ... -def asmatrix(data, dtype=...): ... -mat = asmatrix diff --git a/numpy/matrixlib/defmatrix.pyi b/numpy/matrixlib/defmatrix.pyi new file mode 100644 index 000000000..6c86ea1ef --- /dev/null +++ b/numpy/matrixlib/defmatrix.pyi @@ -0,0 +1,15 @@ +from typing import List, Any, Sequence, Mapping +from numpy import matrix as matrix +from numpy.typing import ArrayLike, DTypeLike, NDArray + +__all__: List[str] + +def bmat( + obj: str | Sequence[ArrayLike] | NDArray[Any], + ldict: None | Mapping[str, Any] = ..., + gdict: None | Mapping[str, Any] = ..., +) -> matrix[Any, Any]: ... + +def asmatrix(data: ArrayLike, dtype: DTypeLike = ...) -> matrix[Any, Any]: ... + +mat = asmatrix diff --git a/numpy/typing/tests/data/reveal/matrix.pyi b/numpy/typing/tests/data/reveal/matrix.pyi new file mode 100644 index 000000000..def33f458 --- /dev/null +++ b/numpy/typing/tests/data/reveal/matrix.pyi @@ -0,0 +1,69 @@ +from typing import Any +import numpy as np +import numpy.typing as npt + +mat: np.matrix[Any, np.dtype[np.int64]] +ar_f8: npt.NDArray[np.float64] + +reveal_type(mat * 5) # E: numpy.matrix[Any, Any] +reveal_type(5 * mat) # E: numpy.matrix[Any, Any] +mat *= 5 + +reveal_type(mat**5) # E: numpy.matrix[Any, Any] +mat **= 5 + +reveal_type(mat.sum()) # E: Any +reveal_type(mat.mean()) # E: Any +reveal_type(mat.std()) # E: Any +reveal_type(mat.var()) # E: Any +reveal_type(mat.prod()) # E: Any +reveal_type(mat.any()) # E: numpy.bool_ +reveal_type(mat.all()) # E: numpy.bool_ +reveal_type(mat.max()) # E: {int64} +reveal_type(mat.min()) # E: {int64} +reveal_type(mat.argmax()) # E: {intp} +reveal_type(mat.argmin()) # E: {intp} +reveal_type(mat.ptp()) # E: {int64} + +reveal_type(mat.sum(axis=0)) # E: numpy.matrix[Any, Any] +reveal_type(mat.mean(axis=0)) # E: numpy.matrix[Any, Any] +reveal_type(mat.std(axis=0)) # E: numpy.matrix[Any, Any] +reveal_type(mat.var(axis=0)) # E: numpy.matrix[Any, Any] +reveal_type(mat.prod(axis=0)) # E: numpy.matrix[Any, Any] +reveal_type(mat.any(axis=0)) # E: numpy.matrix[Any, numpy.dtype[numpy.bool_]] +reveal_type(mat.all(axis=0)) # E: numpy.matrix[Any, numpy.dtype[numpy.bool_]] +reveal_type(mat.max(axis=0)) # E: numpy.matrix[Any, numpy.dtype[{int64}]] +reveal_type(mat.min(axis=0)) # E: numpy.matrix[Any, numpy.dtype[{int64}]] +reveal_type(mat.argmax(axis=0)) # E: numpy.matrix[Any, numpy.dtype[{intp}]] +reveal_type(mat.argmin(axis=0)) # E: numpy.matrix[Any, numpy.dtype[{intp}]] +reveal_type(mat.ptp(axis=0)) # E: numpy.matrix[Any, numpy.dtype[{int64}]] + +reveal_type(mat.sum(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.mean(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.std(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.var(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.prod(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.any(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.all(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.max(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.min(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.argmax(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.argmin(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.ptp(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] + +reveal_type(mat.T) # E: numpy.matrix[Any, numpy.dtype[{int64}]] +reveal_type(mat.I) # E: numpy.matrix[Any, Any] +reveal_type(mat.A) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(mat.A1) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(mat.H) # E: numpy.matrix[Any, numpy.dtype[{int64}]] +reveal_type(mat.getT()) # E: numpy.matrix[Any, numpy.dtype[{int64}]] +reveal_type(mat.getI()) # E: numpy.matrix[Any, Any] +reveal_type(mat.getA()) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(mat.getA1()) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(mat.getH()) # E: numpy.matrix[Any, numpy.dtype[{int64}]] + +reveal_type(np.bmat(ar_f8)) # E: numpy.matrix[Any, Any] +reveal_type(np.bmat([[0, 1, 2]])) # E: numpy.matrix[Any, Any] +reveal_type(np.bmat("mat")) # E: numpy.matrix[Any, Any] + +reveal_type(np.asmatrix(ar_f8, dtype=np.int64)) # E: numpy.matrix[Any, Any] |