defmatrix.pyi 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. from _typeshed import Incomplete
  2. from collections.abc import Mapping, Sequence
  3. from types import EllipsisType
  4. from typing import Any, ClassVar, Literal as L, Self, SupportsIndex, TypeAlias, overload
  5. from typing_extensions import TypeVar
  6. import numpy as np
  7. from numpy._typing import (
  8. ArrayLike,
  9. DTypeLike,
  10. NDArray,
  11. _AnyShape,
  12. _ArrayLikeInt_co,
  13. _NestedSequence,
  14. _ShapeLike,
  15. )
  16. __all__ = ["asmatrix", "bmat", "matrix"]
  17. _T = TypeVar("_T")
  18. _ArrayT = TypeVar("_ArrayT", bound=np.ndarray)
  19. _BoolOrIntArrayT = TypeVar("_BoolOrIntArrayT", bound=NDArray[np.integer | np.bool])
  20. _ScalarT = TypeVar("_ScalarT", bound=np.generic)
  21. _ShapeT_co = TypeVar("_ShapeT_co", bound=_2D, default=_2D, covariant=True)
  22. _DTypeT_co = TypeVar("_DTypeT_co", bound=np.dtype, default=np.dtype, covariant=True)
  23. _2D: TypeAlias = tuple[int, int]
  24. _Matrix: TypeAlias = matrix[_2D, np.dtype[_ScalarT]]
  25. _ToIndex1: TypeAlias = slice | EllipsisType | NDArray[np.integer | np.bool] | _NestedSequence[int] | None
  26. _ToIndex2: TypeAlias = tuple[_ToIndex1, _ToIndex1 | SupportsIndex] | tuple[_ToIndex1 | SupportsIndex, _ToIndex1]
  27. class matrix(np.ndarray[_ShapeT_co, _DTypeT_co]):
  28. __array_priority__: ClassVar[float] = 10.0 # pyright: ignore[reportIncompatibleMethodOverride]
  29. def __new__(
  30. subtype, # pyright: ignore[reportSelfClsParameterName]
  31. data: ArrayLike,
  32. dtype: DTypeLike | None = None,
  33. copy: bool = True,
  34. ) -> _Matrix[Incomplete]: ...
  35. #
  36. @overload # type: ignore[override]
  37. def __getitem__(
  38. self, key: SupportsIndex | _ArrayLikeInt_co | tuple[SupportsIndex | _ArrayLikeInt_co, ...], /
  39. ) -> Incomplete: ...
  40. @overload
  41. def __getitem__(self, key: _ToIndex1 | _ToIndex2, /) -> matrix[_2D, _DTypeT_co]: ...
  42. @overload
  43. def __getitem__(self: _Matrix[np.void], key: str, /) -> _Matrix[Incomplete]: ...
  44. @overload
  45. def __getitem__(self: _Matrix[np.void], key: list[str], /) -> matrix[_2D, _DTypeT_co]: ... # pyright: ignore[reportIncompatibleMethodOverride]
  46. #
  47. def __mul__(self, other: ArrayLike, /) -> _Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
  48. def __rmul__(self, other: ArrayLike, /) -> _Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
  49. #
  50. def __pow__(self, other: ArrayLike, /) -> _Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
  51. def __rpow__(self, other: ArrayLike, /) -> _Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
  52. # keep in sync with `prod` and `mean`
  53. @overload # type: ignore[override]
  54. def sum(self, axis: None = None, dtype: DTypeLike | None = None, out: None = None) -> Incomplete: ...
  55. @overload
  56. def sum(self, axis: _ShapeLike, dtype: DTypeLike | None = None, out: None = None) -> _Matrix[Incomplete]: ...
  57. @overload
  58. def sum(self, axis: _ShapeLike | None, dtype: DTypeLike | None, out: _ArrayT) -> _ArrayT: ...
  59. @overload
  60. def sum(self, axis: _ShapeLike | None = None, dtype: DTypeLike | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
  61. # keep in sync with `sum` and `mean`
  62. @overload # type: ignore[override]
  63. def prod(self, axis: None = None, dtype: DTypeLike | None = None, out: None = None) -> Incomplete: ...
  64. @overload
  65. def prod(self, axis: _ShapeLike, dtype: DTypeLike | None = None, out: None = None) -> _Matrix[Incomplete]: ...
  66. @overload
  67. def prod(self, axis: _ShapeLike | None, dtype: DTypeLike | None, out: _ArrayT) -> _ArrayT: ...
  68. @overload
  69. def prod(self, axis: _ShapeLike | None = None, dtype: DTypeLike | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
  70. # keep in sync with `sum` and `prod`
  71. @overload # type: ignore[override]
  72. def mean(self, axis: None = None, dtype: DTypeLike | None = None, out: None = None) -> Incomplete: ...
  73. @overload
  74. def mean(self, axis: _ShapeLike, dtype: DTypeLike | None = None, out: None = None) -> _Matrix[Incomplete]: ...
  75. @overload
  76. def mean(self, axis: _ShapeLike | None, dtype: DTypeLike | None, out: _ArrayT) -> _ArrayT: ...
  77. @overload
  78. def mean(self, axis: _ShapeLike | None = None, dtype: DTypeLike | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
  79. # keep in sync with `var`
  80. @overload # type: ignore[override]
  81. def std(self, axis: None = None, dtype: DTypeLike | None = None, out: None = None, ddof: float = 0) -> Incomplete: ...
  82. @overload
  83. def std(self, axis: _ShapeLike, dtype: DTypeLike | None = None, out: None = None, ddof: float = 0) -> _Matrix[Incomplete]: ...
  84. @overload
  85. def std(self, axis: _ShapeLike | None, dtype: DTypeLike | None, out: _ArrayT, ddof: float = 0) -> _ArrayT: ...
  86. @overload
  87. def std( # pyright: ignore[reportIncompatibleMethodOverride]
  88. self, axis: _ShapeLike | None = None, dtype: DTypeLike | None = None, *, out: _ArrayT, ddof: float = 0
  89. ) -> _ArrayT: ...
  90. # keep in sync with `std`
  91. @overload # type: ignore[override]
  92. def var(self, axis: None = None, dtype: DTypeLike | None = None, out: None = None, ddof: float = 0) -> Incomplete: ...
  93. @overload
  94. def var(self, axis: _ShapeLike, dtype: DTypeLike | None = None, out: None = None, ddof: float = 0) -> _Matrix[Incomplete]: ...
  95. @overload
  96. def var(self, axis: _ShapeLike | None, dtype: DTypeLike | None, out: _ArrayT, ddof: float = 0) -> _ArrayT: ...
  97. @overload
  98. def var( # pyright: ignore[reportIncompatibleMethodOverride]
  99. self, axis: _ShapeLike | None = None, dtype: DTypeLike | None = None, *, out: _ArrayT, ddof: float = 0
  100. ) -> _ArrayT: ...
  101. # keep in sync with `all`
  102. @overload # type: ignore[override]
  103. def any(self, axis: None = None, out: None = None) -> np.bool: ...
  104. @overload
  105. def any(self, axis: _ShapeLike, out: None = None) -> _Matrix[np.bool]: ...
  106. @overload
  107. def any(self, axis: _ShapeLike | None, out: _ArrayT) -> _ArrayT: ...
  108. @overload
  109. def any(self, axis: _ShapeLike | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
  110. # keep in sync with `any`
  111. @overload # type: ignore[override]
  112. def all(self, axis: None = None, out: None = None) -> np.bool: ...
  113. @overload
  114. def all(self, axis: _ShapeLike, out: None = None) -> _Matrix[np.bool]: ...
  115. @overload
  116. def all(self, axis: _ShapeLike | None, out: _ArrayT) -> _ArrayT: ...
  117. @overload
  118. def all(self, axis: _ShapeLike | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
  119. # keep in sync with `min` and `ptp`
  120. @overload # type: ignore[override]
  121. def max(self: NDArray[_ScalarT], axis: None = None, out: None = None) -> _ScalarT: ...
  122. @overload
  123. def max(self, axis: _ShapeLike, out: None = None) -> matrix[_2D, _DTypeT_co]: ...
  124. @overload
  125. def max(self, axis: _ShapeLike | None, out: _ArrayT) -> _ArrayT: ...
  126. @overload
  127. def max(self, axis: _ShapeLike | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
  128. # keep in sync with `max` and `ptp`
  129. @overload # type: ignore[override]
  130. def min(self: NDArray[_ScalarT], axis: None = None, out: None = None) -> _ScalarT: ...
  131. @overload
  132. def min(self, axis: _ShapeLike, out: None = None) -> matrix[_2D, _DTypeT_co]: ...
  133. @overload
  134. def min(self, axis: _ShapeLike | None, out: _ArrayT) -> _ArrayT: ...
  135. @overload
  136. def min(self, axis: _ShapeLike | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
  137. # keep in sync with `max` and `min`
  138. @overload
  139. def ptp(self: NDArray[_ScalarT], axis: None = None, out: None = None) -> _ScalarT: ...
  140. @overload
  141. def ptp(self, axis: _ShapeLike, out: None = None) -> matrix[_2D, _DTypeT_co]: ...
  142. @overload
  143. def ptp(self, axis: _ShapeLike | None, out: _ArrayT) -> _ArrayT: ...
  144. @overload
  145. def ptp(self, axis: _ShapeLike | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
  146. # keep in sync with `argmin`
  147. @overload # type: ignore[override]
  148. def argmax(self: NDArray[_ScalarT], axis: None = None, out: None = None) -> np.intp: ...
  149. @overload
  150. def argmax(self, axis: _ShapeLike, out: None = None) -> _Matrix[np.intp]: ...
  151. @overload
  152. def argmax(self, axis: _ShapeLike | None, out: _BoolOrIntArrayT) -> _BoolOrIntArrayT: ...
  153. @overload
  154. def argmax(self, axis: _ShapeLike | None = None, *, out: _BoolOrIntArrayT) -> _BoolOrIntArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
  155. # keep in sync with `argmax`
  156. @overload # type: ignore[override]
  157. def argmin(self: NDArray[_ScalarT], axis: None = None, out: None = None) -> np.intp: ...
  158. @overload
  159. def argmin(self, axis: _ShapeLike, out: None = None) -> _Matrix[np.intp]: ...
  160. @overload
  161. def argmin(self, axis: _ShapeLike | None, out: _BoolOrIntArrayT) -> _BoolOrIntArrayT: ...
  162. @overload
  163. def argmin(self, axis: _ShapeLike | None = None, *, out: _BoolOrIntArrayT) -> _BoolOrIntArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
  164. # the second overload handles the (rare) case that the matrix is not 2-d
  165. @overload
  166. def tolist(self: _Matrix[np.generic[_T]]) -> list[list[_T]]: ... # pyright: ignore[reportIncompatibleMethodOverride]
  167. @overload
  168. def tolist(self) -> Incomplete: ... # pyright: ignore[reportIncompatibleMethodOverride]
  169. # these three methods will at least return a `2-d` array of shape (1, n)
  170. def squeeze(self, /, axis: _ShapeLike | None = None) -> matrix[_2D, _DTypeT_co]: ...
  171. def ravel(self, /, order: L["K", "A", "C", "F"] | None = "C") -> matrix[_2D, _DTypeT_co]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
  172. def flatten(self, /, order: L["K", "A", "C", "F"] | None = "C") -> matrix[_2D, _DTypeT_co]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
  173. # matrix.T is inherited from _ScalarOrArrayCommon
  174. def getT(self) -> Self: ...
  175. @property
  176. def I(self) -> _Matrix[Incomplete]: ... # noqa: E743
  177. def getI(self) -> _Matrix[Incomplete]: ...
  178. @property
  179. def A(self) -> np.ndarray[_2D, _DTypeT_co]: ...
  180. def getA(self) -> np.ndarray[_2D, _DTypeT_co]: ...
  181. @property
  182. def A1(self) -> np.ndarray[_AnyShape, _DTypeT_co]: ...
  183. def getA1(self) -> np.ndarray[_AnyShape, _DTypeT_co]: ...
  184. @property
  185. def H(self) -> matrix[_2D, _DTypeT_co]: ...
  186. def getH(self) -> matrix[_2D, _DTypeT_co]: ...
  187. def bmat(
  188. obj: str | Sequence[ArrayLike] | NDArray[Any],
  189. ldict: Mapping[str, Any] | None = None,
  190. gdict: Mapping[str, Any] | None = None,
  191. ) -> _Matrix[Incomplete]: ...
  192. def asmatrix(data: ArrayLike, dtype: DTypeLike | None = None) -> _Matrix[Incomplete]: ...