|
| 1 | +from types import ModuleType |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from xarray.namedarray._typing import ( |
| 7 | + _arrayapi, |
| 8 | + _DType, |
| 9 | + _ScalarType, |
| 10 | + _ShapeType, |
| 11 | + _SupportsImag, |
| 12 | + _SupportsReal, |
| 13 | +) |
| 14 | +from xarray.namedarray.core import NamedArray |
| 15 | + |
| 16 | + |
| 17 | +def _get_data_namespace(x: NamedArray[Any, Any]) -> ModuleType: |
| 18 | + if isinstance(x._data, _arrayapi): |
| 19 | + return x._data.__array_namespace__() |
| 20 | + else: |
| 21 | + return np |
| 22 | + |
| 23 | + |
| 24 | +def astype( |
| 25 | + x: NamedArray[_ShapeType, Any], dtype: _DType, /, *, copy: bool = True |
| 26 | +) -> NamedArray[_ShapeType, _DType]: |
| 27 | + """ |
| 28 | + Copies an array to a specified data type irrespective of Type Promotion Rules rules. |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + x : NamedArray |
| 33 | + Array to cast. |
| 34 | + dtype : _DType |
| 35 | + Desired data type. |
| 36 | + copy : bool, optional |
| 37 | + Specifies whether to copy an array when the specified dtype matches the data |
| 38 | + type of the input array x. |
| 39 | + If True, a newly allocated array must always be returned. |
| 40 | + If False and the specified dtype matches the data type of the input array, |
| 41 | + the input array must be returned; otherwise, a newly allocated array must be |
| 42 | + returned. Default: True. |
| 43 | +
|
| 44 | + Returns |
| 45 | + ------- |
| 46 | + out : NamedArray |
| 47 | + An array having the specified data type. The returned array must have the |
| 48 | + same shape as x. |
| 49 | +
|
| 50 | + Examples |
| 51 | + -------- |
| 52 | + >>> narr = NamedArray(("x",), np.array([1.5, 2.5])) |
| 53 | + >>> astype(narr, np.dtype(int)).data |
| 54 | + array([1, 2]) |
| 55 | + """ |
| 56 | + if isinstance(x._data, _arrayapi): |
| 57 | + xp = x._data.__array_namespace__() |
| 58 | + return x._new(data=xp.astype(x, dtype, copy=copy)) |
| 59 | + |
| 60 | + # np.astype doesn't exist yet: |
| 61 | + return x._new(data=x._data.astype(dtype, copy=copy)) # type: ignore[attr-defined] |
| 62 | + |
| 63 | + |
| 64 | +def imag( |
| 65 | + x: NamedArray[_ShapeType, np.dtype[_SupportsImag[_ScalarType]]], / # type: ignore[type-var] |
| 66 | +) -> NamedArray[_ShapeType, np.dtype[_ScalarType]]: |
| 67 | + """ |
| 68 | + Returns the imaginary component of a complex number for each element x_i of the |
| 69 | + input array x. |
| 70 | +
|
| 71 | + Parameters |
| 72 | + ---------- |
| 73 | + x : NamedArray |
| 74 | + Input array. Should have a complex floating-point data type. |
| 75 | +
|
| 76 | + Returns |
| 77 | + ------- |
| 78 | + out : NamedArray |
| 79 | + An array containing the element-wise results. The returned array must have a |
| 80 | + floating-point data type with the same floating-point precision as x |
| 81 | + (e.g., if x is complex64, the returned array must have the floating-point |
| 82 | + data type float32). |
| 83 | +
|
| 84 | + Examples |
| 85 | + -------- |
| 86 | + >>> narr = NamedArray(("x",), np.array([1 + 2j, 2 + 4j])) |
| 87 | + >>> imag(narr).data |
| 88 | + array([2., 4.]) |
| 89 | + """ |
| 90 | + xp = _get_data_namespace(x) |
| 91 | + out = x._new(data=xp.imag(x._data)) |
| 92 | + return out |
| 93 | + |
| 94 | + |
| 95 | +def real( |
| 96 | + x: NamedArray[_ShapeType, np.dtype[_SupportsReal[_ScalarType]]], / # type: ignore[type-var] |
| 97 | +) -> NamedArray[_ShapeType, np.dtype[_ScalarType]]: |
| 98 | + """ |
| 99 | + Returns the real component of a complex number for each element x_i of the |
| 100 | + input array x. |
| 101 | +
|
| 102 | + Parameters |
| 103 | + ---------- |
| 104 | + x : NamedArray |
| 105 | + Input array. Should have a complex floating-point data type. |
| 106 | +
|
| 107 | + Returns |
| 108 | + ------- |
| 109 | + out : NamedArray |
| 110 | + An array containing the element-wise results. The returned array must have a |
| 111 | + floating-point data type with the same floating-point precision as x |
| 112 | + (e.g., if x is complex64, the returned array must have the floating-point |
| 113 | + data type float32). |
| 114 | +
|
| 115 | + Examples |
| 116 | + -------- |
| 117 | + >>> narr = NamedArray(("x",), np.array([1 + 2j, 2 + 4j])) |
| 118 | + >>> real(narr).data |
| 119 | + array([1., 2.]) |
| 120 | + """ |
| 121 | + xp = _get_data_namespace(x) |
| 122 | + return x._new(data=xp.real(x._data)) |
0 commit comments