|
| 1 | +from .array_object import Array |
| 2 | +from .dtypes import Dtype |
| 3 | + |
| 4 | +# TODO implement functions |
| 5 | + |
| 6 | + |
| 7 | +def astype(x: Array, dtype: Dtype, /, *, copy: bool = True) -> Array: |
| 8 | + """ |
| 9 | + Copies an array to a specified data type irrespective of Type Promotion Rules rules. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + x : Array |
| 14 | + Array to cast. |
| 15 | + dtype: Dtype |
| 16 | + Desired data type. |
| 17 | + copy: bool, optional |
| 18 | + Specifies whether to copy an array when the specified dtype matches the data type of the input array x. |
| 19 | + If True, a newly allocated array must always be returned. If False and the specified dtype matches the data |
| 20 | + type of the input array, the input array must be returned; otherwise, a newly allocated array must be returned. |
| 21 | + Default: True. |
| 22 | +
|
| 23 | + Returns |
| 24 | + ------- |
| 25 | + out : Array |
| 26 | + An array having the specified data type. The returned array must have the same shape as x. |
| 27 | +
|
| 28 | + Note |
| 29 | + ---- |
| 30 | + - Casting floating-point NaN and infinity values to integral data types is not specified and is |
| 31 | + implementation-dependent. |
| 32 | + - Casting a complex floating-point array to a real-valued data type should not be permitted. |
| 33 | + Historically, when casting a complex floating-point array to a real-valued data type, libraries such as NumPy have |
| 34 | + discarded imaginary components such that, for a complex floating-point array x, astype(x) equals astype(real(x))). |
| 35 | + This behavior is considered problematic as the choice to discard the imaginary component is arbitrary and |
| 36 | + introduces more than one way to achieve the same outcome (i.e., for a complex floating-point array x, astype(x) and |
| 37 | + astype(real(x)) versus only astype(imag(x))). Instead, in order to avoid ambiguity and to promote clarity, this |
| 38 | + specification requires that array API consumers explicitly express which component should be cast to a specified |
| 39 | + real-valued data type. |
| 40 | + - When casting a boolean input array to a real-valued data type, a value of True must cast to a real-valued number |
| 41 | + equal to 1, and a value of False must cast to a real-valued number equal to 0. |
| 42 | + When casting a boolean input array to a complex floating-point data type, a value of True must cast to a complex |
| 43 | + number equal to 1 + 0j, and a value of False must cast to a complex number equal to 0 + 0j. |
| 44 | + - When casting a real-valued input array to bool, a value of 0 must cast to False, and a non-zero value must cast |
| 45 | + to True. |
| 46 | + When casting a complex floating-point array to bool, a value of 0 + 0j must cast to False, and all other values |
| 47 | + must cast to True. |
| 48 | + """ |
| 49 | + return NotImplemented |
| 50 | + |
| 51 | + |
| 52 | +def can_cast(from_: Dtype | Array, to: Dtype, /) -> bool: |
| 53 | + """ |
| 54 | + Determines if one data type can be cast to another data type according Type Promotion Rules rules. |
| 55 | +
|
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + from_ : Dtype | Array |
| 59 | + Input data type or array from which to cast. |
| 60 | + to : Dtype |
| 61 | + Desired data type. |
| 62 | +
|
| 63 | + Returns |
| 64 | + ------- |
| 65 | + out : bool |
| 66 | + True if the cast can occur according to Type Promotion Rules rules; otherwise, False. |
| 67 | + """ |
| 68 | + return NotImplemented |
| 69 | + |
| 70 | + |
| 71 | +def finfo(type: Dtype | Array, /): # type: ignore[no-untyped-def] |
| 72 | + # TODO add docstring, implementation and return type -> finfo_object |
| 73 | + return NotImplemented |
| 74 | + |
| 75 | + |
| 76 | +def iinfo(type: Dtype | Array, /): # type: ignore[no-untyped-def] |
| 77 | + # TODO add docstring, implementation and return type -> iinfo_object |
| 78 | + return NotImplemented |
| 79 | + |
| 80 | + |
| 81 | +def isdtype(dtype: Dtype, kind: Dtype | str | tuple[Dtype | str, ...]) -> bool: |
| 82 | + """ |
| 83 | + Returns a boolean indicating whether a provided dtype is of a specified data type “kind”. |
| 84 | +
|
| 85 | + Parameters |
| 86 | + ---------- |
| 87 | + dtype : Dtype |
| 88 | + The input dtype. |
| 89 | + kind : Dtype | str | tuple[Dtype | str, ...] |
| 90 | + Data type kind. |
| 91 | + - If kind is a dtype, the function must return a boolean indicating whether the input dtype is equal to the |
| 92 | + dtype specified by kind. |
| 93 | + - If kind is a string, the function must return a boolean indicating whether the input dtype is of a specified |
| 94 | + data type kind. The following dtype kinds must be supported: |
| 95 | + - bool: boolean data types (e.g., bool). |
| 96 | + - signed integer: signed integer data types (e.g., int8, int16, int32, int64). |
| 97 | + - unsigned integer: unsigned integer data types (e.g., uint8, uint16, uint32, uint64). |
| 98 | + - integral: integer data types. Shorthand for ('signed integer', 'unsigned integer'). |
| 99 | + - real floating: real-valued floating-point data types (e.g., float32, float64). |
| 100 | + - complex floating: complex floating-point data types (e.g., complex64, complex128). |
| 101 | + - numeric: numeric data types. Shorthand for ('integral', 'real floating', 'complex floating'). |
| 102 | + - If kind is a tuple, the tuple specifies a union of dtypes and/or kinds, and the function must return a |
| 103 | + boolean indicating whether the input dtype is either equal to a specified dtype or belongs to at least one |
| 104 | + specified data type kind. |
| 105 | +
|
| 106 | + Returns |
| 107 | + ------- |
| 108 | + out : bool |
| 109 | + Boolean indicating whether a provided dtype is of a specified data type kind. |
| 110 | +
|
| 111 | + Note |
| 112 | + ---- |
| 113 | + - A conforming implementation of the array API standard is not limited to only including the dtypes described in |
| 114 | + this specification in the required data type kinds. For example, implementations supporting float16 and bfloat16 |
| 115 | + can include float16 and bfloat16 in the real floating data type kind. Similarly, implementations supporting int128 |
| 116 | + can include int128 in the signed integer data type kind. |
| 117 | + In short, conforming implementations may extend data type kinds; however, data type kinds must remain consistent |
| 118 | + (e.g., only integer dtypes may belong to integer data type kinds and only floating-point dtypes may belong to |
| 119 | + floating-point data type kinds), and extensions must be clearly documented as such in library documentation. |
| 120 | + """ |
| 121 | + return NotImplemented |
| 122 | + |
| 123 | + |
| 124 | +def result_type(*arrays_and_dtypes: Dtype | Array) -> Dtype: |
| 125 | + """ |
| 126 | + Returns the dtype that results from applying the type promotion rules (see Type Promotion Rules) to the arguments. |
| 127 | +
|
| 128 | + Parameters |
| 129 | + ---------- |
| 130 | + arrays_and_dtypes: Dtype | Array |
| 131 | + An arbitrary number of input arrays and/or dtypes. |
| 132 | +
|
| 133 | + Returns |
| 134 | + ------- |
| 135 | + out : Dtype |
| 136 | + The dtype resulting from an operation involving the input arrays and dtypes. |
| 137 | + """ |
| 138 | + return NotImplemented |
0 commit comments