m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

405 lines
11 KiB

6 months ago
  1. """A module with private type-check-only `numpy.ufunc` subclasses.
  2. The signatures of the ufuncs are too varied to reasonably type
  3. with a single class. So instead, `ufunc` has been expanded into
  4. four private subclasses, one for each combination of
  5. `~ufunc.nin` and `~ufunc.nout`.
  6. """
  7. from typing import (
  8. Any,
  9. Generic,
  10. List,
  11. Optional,
  12. overload,
  13. Tuple,
  14. TypeVar,
  15. Union,
  16. )
  17. from numpy import ufunc, _Casting, _OrderKACF
  18. from numpy.typing import NDArray
  19. from ._shape import _ShapeLike
  20. from ._scalars import _ScalarLike_co
  21. from ._array_like import ArrayLike, _ArrayLikeBool_co, _ArrayLikeInt_co
  22. from ._dtype_like import DTypeLike
  23. from typing_extensions import Literal, SupportsIndex
  24. _T = TypeVar("_T")
  25. _2Tuple = Tuple[_T, _T]
  26. _3Tuple = Tuple[_T, _T, _T]
  27. _4Tuple = Tuple[_T, _T, _T, _T]
  28. _NTypes = TypeVar("_NTypes", bound=int)
  29. _IDType = TypeVar("_IDType", bound=Any)
  30. _NameType = TypeVar("_NameType", bound=str)
  31. # NOTE: In reality `extobj` should be a length of list 3 containing an
  32. # int, an int, and a callable, but there's no way to properly express
  33. # non-homogenous lists.
  34. # Use `Any` over `Union` to avoid issues related to lists invariance.
  35. # NOTE: `reduce`, `accumulate`, `reduceat` and `outer` raise a ValueError for
  36. # ufuncs that don't accept two input arguments and return one output argument.
  37. # In such cases the respective methods are simply typed as `None`.
  38. # NOTE: Similarly, `at` won't be defined for ufuncs that return
  39. # multiple outputs; in such cases `at` is typed as `None`
  40. # NOTE: If 2 output types are returned then `out` must be a
  41. # 2-tuple of arrays. Otherwise `None` or a plain array are also acceptable
  42. class _UFunc_Nin1_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
  43. @property
  44. def __name__(self) -> _NameType: ...
  45. @property
  46. def ntypes(self) -> _NTypes: ...
  47. @property
  48. def identity(self) -> _IDType: ...
  49. @property
  50. def nin(self) -> Literal[1]: ...
  51. @property
  52. def nout(self) -> Literal[1]: ...
  53. @property
  54. def nargs(self) -> Literal[2]: ...
  55. @property
  56. def signature(self) -> None: ...
  57. @property
  58. def reduce(self) -> None: ...
  59. @property
  60. def accumulate(self) -> None: ...
  61. @property
  62. def reduceat(self) -> None: ...
  63. @property
  64. def outer(self) -> None: ...
  65. @overload
  66. def __call__(
  67. self,
  68. __x1: _ScalarLike_co,
  69. out: None = ...,
  70. *,
  71. where: Optional[_ArrayLikeBool_co] = ...,
  72. casting: _Casting = ...,
  73. order: _OrderKACF = ...,
  74. dtype: DTypeLike = ...,
  75. subok: bool = ...,
  76. signature: Union[str, _2Tuple[Optional[str]]] = ...,
  77. extobj: List[Any] = ...,
  78. ) -> Any: ...
  79. @overload
  80. def __call__(
  81. self,
  82. __x1: ArrayLike,
  83. out: Union[None, NDArray[Any], Tuple[NDArray[Any]]] = ...,
  84. *,
  85. where: Optional[_ArrayLikeBool_co] = ...,
  86. casting: _Casting = ...,
  87. order: _OrderKACF = ...,
  88. dtype: DTypeLike = ...,
  89. subok: bool = ...,
  90. signature: Union[str, _2Tuple[Optional[str]]] = ...,
  91. extobj: List[Any] = ...,
  92. ) -> NDArray[Any]: ...
  93. def at(
  94. self,
  95. __a: NDArray[Any],
  96. __indices: _ArrayLikeInt_co,
  97. ) -> None: ...
  98. class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
  99. @property
  100. def __name__(self) -> _NameType: ...
  101. @property
  102. def ntypes(self) -> _NTypes: ...
  103. @property
  104. def identity(self) -> _IDType: ...
  105. @property
  106. def nin(self) -> Literal[2]: ...
  107. @property
  108. def nout(self) -> Literal[1]: ...
  109. @property
  110. def nargs(self) -> Literal[3]: ...
  111. @property
  112. def signature(self) -> None: ...
  113. @overload
  114. def __call__(
  115. self,
  116. __x1: _ScalarLike_co,
  117. __x2: _ScalarLike_co,
  118. out: None = ...,
  119. *,
  120. where: Optional[_ArrayLikeBool_co] = ...,
  121. casting: _Casting = ...,
  122. order: _OrderKACF = ...,
  123. dtype: DTypeLike = ...,
  124. subok: bool = ...,
  125. signature: Union[str, _3Tuple[Optional[str]]] = ...,
  126. extobj: List[Any] = ...,
  127. ) -> Any: ...
  128. @overload
  129. def __call__(
  130. self,
  131. __x1: ArrayLike,
  132. __x2: ArrayLike,
  133. out: Union[None, NDArray[Any], Tuple[NDArray[Any]]] = ...,
  134. *,
  135. where: Optional[_ArrayLikeBool_co] = ...,
  136. casting: _Casting = ...,
  137. order: _OrderKACF = ...,
  138. dtype: DTypeLike = ...,
  139. subok: bool = ...,
  140. signature: Union[str, _3Tuple[Optional[str]]] = ...,
  141. extobj: List[Any] = ...,
  142. ) -> NDArray[Any]: ...
  143. def at(
  144. self,
  145. __a: NDArray[Any],
  146. __indices: _ArrayLikeInt_co,
  147. __b: ArrayLike,
  148. ) -> None: ...
  149. def reduce(
  150. self,
  151. array: ArrayLike,
  152. axis: Optional[_ShapeLike] = ...,
  153. dtype: DTypeLike = ...,
  154. out: Optional[NDArray[Any]] = ...,
  155. keepdims: bool = ...,
  156. initial: Any = ...,
  157. where: _ArrayLikeBool_co = ...,
  158. ) -> Any: ...
  159. def accumulate(
  160. self,
  161. array: ArrayLike,
  162. axis: SupportsIndex = ...,
  163. dtype: DTypeLike = ...,
  164. out: Optional[NDArray[Any]] = ...,
  165. ) -> NDArray[Any]: ...
  166. def reduceat(
  167. self,
  168. array: ArrayLike,
  169. indices: _ArrayLikeInt_co,
  170. axis: SupportsIndex = ...,
  171. dtype: DTypeLike = ...,
  172. out: Optional[NDArray[Any]] = ...,
  173. ) -> NDArray[Any]: ...
  174. # Expand `**kwargs` into explicit keyword-only arguments
  175. @overload
  176. def outer(
  177. self,
  178. __A: _ScalarLike_co,
  179. __B: _ScalarLike_co,
  180. *,
  181. out: None = ...,
  182. where: Optional[_ArrayLikeBool_co] = ...,
  183. casting: _Casting = ...,
  184. order: _OrderKACF = ...,
  185. dtype: DTypeLike = ...,
  186. subok: bool = ...,
  187. signature: Union[str, _3Tuple[Optional[str]]] = ...,
  188. extobj: List[Any] = ...,
  189. ) -> Any: ...
  190. @overload
  191. def outer( # type: ignore[misc]
  192. self,
  193. __A: ArrayLike,
  194. __B: ArrayLike,
  195. *,
  196. out: Union[None, NDArray[Any], Tuple[NDArray[Any]]] = ...,
  197. where: Optional[_ArrayLikeBool_co] = ...,
  198. casting: _Casting = ...,
  199. order: _OrderKACF = ...,
  200. dtype: DTypeLike = ...,
  201. subok: bool = ...,
  202. signature: Union[str, _3Tuple[Optional[str]]] = ...,
  203. extobj: List[Any] = ...,
  204. ) -> NDArray[Any]: ...
  205. class _UFunc_Nin1_Nout2(ufunc, Generic[_NameType, _NTypes, _IDType]):
  206. @property
  207. def __name__(self) -> _NameType: ...
  208. @property
  209. def ntypes(self) -> _NTypes: ...
  210. @property
  211. def identity(self) -> _IDType: ...
  212. @property
  213. def nin(self) -> Literal[1]: ...
  214. @property
  215. def nout(self) -> Literal[2]: ...
  216. @property
  217. def nargs(self) -> Literal[3]: ...
  218. @property
  219. def signature(self) -> None: ...
  220. @property
  221. def at(self) -> None: ...
  222. @property
  223. def reduce(self) -> None: ...
  224. @property
  225. def accumulate(self) -> None: ...
  226. @property
  227. def reduceat(self) -> None: ...
  228. @property
  229. def outer(self) -> None: ...
  230. @overload
  231. def __call__(
  232. self,
  233. __x1: _ScalarLike_co,
  234. __out1: None = ...,
  235. __out2: None = ...,
  236. *,
  237. where: Optional[_ArrayLikeBool_co] = ...,
  238. casting: _Casting = ...,
  239. order: _OrderKACF = ...,
  240. dtype: DTypeLike = ...,
  241. subok: bool = ...,
  242. signature: Union[str, _3Tuple[Optional[str]]] = ...,
  243. extobj: List[Any] = ...,
  244. ) -> _2Tuple[Any]: ...
  245. @overload
  246. def __call__(
  247. self,
  248. __x1: ArrayLike,
  249. __out1: Optional[NDArray[Any]] = ...,
  250. __out2: Optional[NDArray[Any]] = ...,
  251. *,
  252. out: _2Tuple[NDArray[Any]] = ...,
  253. where: Optional[_ArrayLikeBool_co] = ...,
  254. casting: _Casting = ...,
  255. order: _OrderKACF = ...,
  256. dtype: DTypeLike = ...,
  257. subok: bool = ...,
  258. signature: Union[str, _3Tuple[Optional[str]]] = ...,
  259. extobj: List[Any] = ...,
  260. ) -> _2Tuple[NDArray[Any]]: ...
  261. class _UFunc_Nin2_Nout2(ufunc, Generic[_NameType, _NTypes, _IDType]):
  262. @property
  263. def __name__(self) -> _NameType: ...
  264. @property
  265. def ntypes(self) -> _NTypes: ...
  266. @property
  267. def identity(self) -> _IDType: ...
  268. @property
  269. def nin(self) -> Literal[2]: ...
  270. @property
  271. def nout(self) -> Literal[2]: ...
  272. @property
  273. def nargs(self) -> Literal[4]: ...
  274. @property
  275. def signature(self) -> None: ...
  276. @property
  277. def at(self) -> None: ...
  278. @property
  279. def reduce(self) -> None: ...
  280. @property
  281. def accumulate(self) -> None: ...
  282. @property
  283. def reduceat(self) -> None: ...
  284. @property
  285. def outer(self) -> None: ...
  286. @overload
  287. def __call__(
  288. self,
  289. __x1: _ScalarLike_co,
  290. __x2: _ScalarLike_co,
  291. __out1: None = ...,
  292. __out2: None = ...,
  293. *,
  294. where: Optional[_ArrayLikeBool_co] = ...,
  295. casting: _Casting = ...,
  296. order: _OrderKACF = ...,
  297. dtype: DTypeLike = ...,
  298. subok: bool = ...,
  299. signature: Union[str, _4Tuple[Optional[str]]] = ...,
  300. extobj: List[Any] = ...,
  301. ) -> _2Tuple[Any]: ...
  302. @overload
  303. def __call__(
  304. self,
  305. __x1: ArrayLike,
  306. __x2: ArrayLike,
  307. __out1: Optional[NDArray[Any]] = ...,
  308. __out2: Optional[NDArray[Any]] = ...,
  309. *,
  310. out: _2Tuple[NDArray[Any]] = ...,
  311. where: Optional[_ArrayLikeBool_co] = ...,
  312. casting: _Casting = ...,
  313. order: _OrderKACF = ...,
  314. dtype: DTypeLike = ...,
  315. subok: bool = ...,
  316. signature: Union[str, _4Tuple[Optional[str]]] = ...,
  317. extobj: List[Any] = ...,
  318. ) -> _2Tuple[NDArray[Any]]: ...
  319. class _GUFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
  320. @property
  321. def __name__(self) -> _NameType: ...
  322. @property
  323. def ntypes(self) -> _NTypes: ...
  324. @property
  325. def identity(self) -> _IDType: ...
  326. @property
  327. def nin(self) -> Literal[2]: ...
  328. @property
  329. def nout(self) -> Literal[1]: ...
  330. @property
  331. def nargs(self) -> Literal[3]: ...
  332. # NOTE: In practice the only gufunc in the main name is `matmul`,
  333. # so we can use its signature here
  334. @property
  335. def signature(self) -> Literal["(n?,k),(k,m?)->(n?,m?)"]: ...
  336. @property
  337. def reduce(self) -> None: ...
  338. @property
  339. def accumulate(self) -> None: ...
  340. @property
  341. def reduceat(self) -> None: ...
  342. @property
  343. def outer(self) -> None: ...
  344. @property
  345. def at(self) -> None: ...
  346. # Scalar for 1D array-likes; ndarray otherwise
  347. @overload
  348. def __call__(
  349. self,
  350. __x1: ArrayLike,
  351. __x2: ArrayLike,
  352. out: None = ...,
  353. *,
  354. casting: _Casting = ...,
  355. order: _OrderKACF = ...,
  356. dtype: DTypeLike = ...,
  357. subok: bool = ...,
  358. signature: Union[str, _3Tuple[Optional[str]]] = ...,
  359. extobj: List[Any] = ...,
  360. axes: List[_2Tuple[SupportsIndex]] = ...,
  361. ) -> Any: ...
  362. @overload
  363. def __call__(
  364. self,
  365. __x1: ArrayLike,
  366. __x2: ArrayLike,
  367. out: Union[NDArray[Any], Tuple[NDArray[Any]]],
  368. *,
  369. casting: _Casting = ...,
  370. order: _OrderKACF = ...,
  371. dtype: DTypeLike = ...,
  372. subok: bool = ...,
  373. signature: Union[str, _3Tuple[Optional[str]]] = ...,
  374. extobj: List[Any] = ...,
  375. axes: List[_2Tuple[SupportsIndex]] = ...,
  376. ) -> NDArray[Any]: ...