|
| 1 | +import torch |
| 2 | + |
| 3 | +from . import _decorators, _helpers |
| 4 | +from ._detail import _flips, _util |
| 5 | +from ._detail import implementations as _impl |
| 6 | + |
| 7 | + |
| 8 | +def nonzero(a): |
| 9 | + (tensor,) = _helpers.to_tensors(a) |
| 10 | + result = tensor.nonzero(as_tuple=True) |
| 11 | + return _helpers.tuple_arrays_from(result) |
| 12 | + |
| 13 | + |
| 14 | +def argwhere(a): |
| 15 | + (tensor,) = _helpers.to_tensors(a) |
| 16 | + result = torch.argwhere(tensor) |
| 17 | + return _helpers.array_from(result) |
| 18 | + |
| 19 | + |
| 20 | +def clip(a, min=None, max=None, out=None): |
| 21 | + # np.clip requires both a_min and a_max not None, while ndarray.clip allows |
| 22 | + # one of them to be None. Follow the more lax version. |
| 23 | + # Also min/max as arg names: follow numpy naming. |
| 24 | + tensor, t_min, t_max = _helpers.to_tensors_or_none(a, min, max) |
| 25 | + result = _impl.clip(tensor, t_min, t_max) |
| 26 | + return _helpers.result_or_out(result, out) |
| 27 | + |
| 28 | + |
| 29 | +def repeat(a, repeats, axis=None): |
| 30 | + tensor, t_repeats = _helpers.to_tensors(a, repeats) # XXX: scalar repeats |
| 31 | + result = torch.repeat_interleave(tensor, t_repeats, axis) |
| 32 | + return _helpers.array_from(result) |
| 33 | + |
| 34 | + |
| 35 | +# ### diag et al ### |
| 36 | + |
| 37 | + |
| 38 | +def diagonal(a, offset=0, axis1=0, axis2=1): |
| 39 | + (tensor,) = _helpers.to_tensors(a) |
| 40 | + result = _impl.diagonal(tensor, offset, axis1, axis2) |
| 41 | + return _helpers.array_from(result) |
| 42 | + |
| 43 | + |
| 44 | +@_decorators.dtype_to_torch |
| 45 | +def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None): |
| 46 | + (tensor,) = _helpers.to_tensors(a) |
| 47 | + result = _impl.trace(tensor, offset, axis1, axis2, dtype) |
| 48 | + return _helpers.result_or_out(result, out) |
| 49 | + |
| 50 | + |
| 51 | +@_decorators.dtype_to_torch |
| 52 | +def eye(N, M=None, k=0, dtype=float, order="C", *, like=None): |
| 53 | + _util.subok_not_ok(like) |
| 54 | + if order != "C": |
| 55 | + raise NotImplementedError |
| 56 | + result = _impl.eye(N, M, k, dtype) |
| 57 | + return _helpers.array_from(result) |
| 58 | + |
| 59 | + |
| 60 | +@_decorators.dtype_to_torch |
| 61 | +def identity(n, dtype=None, *, like=None): |
| 62 | + _util.subok_not_ok(like) |
| 63 | + result = torch.eye(n, dtype=dtype) |
| 64 | + return _helpers.array_from(result) |
| 65 | + |
| 66 | + |
| 67 | +def diag(v, k=0): |
| 68 | + (tensor,) = _helpers.to_tensors(v) |
| 69 | + result = torch.diag(tensor, k) |
| 70 | + return _helpers.array_from(result) |
| 71 | + |
| 72 | + |
| 73 | +def diagflat(v, k=0): |
| 74 | + (tensor,) = _helpers.to_tensors(v) |
| 75 | + result = torch.diagflat(tensor, k) |
| 76 | + return _helpers.array_from(result) |
| 77 | + |
| 78 | + |
| 79 | +def diag_indices(n, ndim=2): |
| 80 | + result = _impl.diag_indices(n, ndim) |
| 81 | + return _helpers.tuple_arrays_from(result) |
| 82 | + |
| 83 | + |
| 84 | +def diag_indices_from(arr): |
| 85 | + (tensor,) = _helpers.to_tensors(arr) |
| 86 | + result = _impl.diag_indices_from(tensor) |
| 87 | + return _helpers.tuple_arrays_from(result) |
| 88 | + |
| 89 | + |
| 90 | +def fill_diagonal(a, val, wrap=False): |
| 91 | + tensor, t_val = _helpers.to_tensors(a, val) |
| 92 | + result = _impl.fill_diagonal(tensor, t_val, wrap) |
| 93 | + return _helpers.array_from(result) |
| 94 | + |
| 95 | + |
| 96 | +# ### sorting ### |
| 97 | + |
| 98 | +# ### sort and partition ### |
| 99 | + |
| 100 | + |
| 101 | +def sort(a, axis=-1, kind=None, order=None): |
| 102 | + (tensor,) = _helpers.to_tensors(a) |
| 103 | + result = _impl.sort(tensor, axis, kind, order) |
| 104 | + return _helpers.array_from(result) |
| 105 | + |
| 106 | + |
| 107 | +def argsort(a, axis=-1, kind=None, order=None): |
| 108 | + (tensor,) = _helpers.to_tensors(a) |
| 109 | + result = _impl.argsort(tensor, axis, kind, order) |
| 110 | + return _helpers.array_from(result) |
| 111 | + |
| 112 | + |
| 113 | +def searchsorted(a, v, side="left", sorter=None): |
| 114 | + a_t, v_t, sorter_t = _helpers.to_tensors_or_none(a, v, sorter) |
| 115 | + result = torch.searchsorted(a_t, v_t, side=side, sorter=sorter_t) |
| 116 | + return _helpers.array_from(result) |
| 117 | + |
| 118 | + |
| 119 | +# ### swap/move/roll axis ### |
| 120 | + |
| 121 | + |
| 122 | +def moveaxis(a, source, destination): |
| 123 | + (tensor,) = _helpers.to_tensors(a) |
| 124 | + result = _impl.moveaxis(tensor, source, destination) |
| 125 | + return _helpers.array_from(result) |
| 126 | + |
| 127 | + |
| 128 | +def swapaxes(a, axis1, axis2): |
| 129 | + (tensor,) = _helpers.to_tensors(a) |
| 130 | + result = _flips.swapaxes(tensor, axis1, axis2) |
| 131 | + return _helpers.array_from(result) |
| 132 | + |
| 133 | + |
| 134 | +def rollaxis(a, axis, start=0): |
| 135 | + (tensor,) = _helpers.to_tensors(a) |
| 136 | + result = _flips.rollaxis(a, axis, start) |
| 137 | + return _helpers.array_from(result) |
| 138 | + |
| 139 | + |
| 140 | +# ### shape manipulations ### |
| 141 | + |
| 142 | + |
| 143 | +def squeeze(a, axis=None): |
| 144 | + (tensor,) = _helpers.to_tensors(a) |
| 145 | + result = _impl.squeeze(tensor, axis) |
| 146 | + return _helpers.array_from(result, a) |
| 147 | + |
| 148 | + |
| 149 | +def reshape(a, newshape, order="C"): |
| 150 | + (tensor,) = _helpers.to_tensors(a) |
| 151 | + result = _impl.reshape(tensor, newshape, order=order) |
| 152 | + return _helpers.array_from(result, a) |
| 153 | + |
| 154 | + |
| 155 | +def transpose(a, axes=None): |
| 156 | + (tensor,) = _helpers.to_tensors(a) |
| 157 | + result = _impl.transpose(tensor, axes) |
| 158 | + return _helpers.array_from(result, a) |
| 159 | + |
| 160 | + |
| 161 | +def ravel(a, order="C"): |
| 162 | + (tensor,) = _helpers.to_tensors(a) |
| 163 | + result = _impl.ravel(tensor) |
| 164 | + return _helpers.array_from(result, a) |
| 165 | + |
| 166 | + |
| 167 | +# leading underscore since arr.flatten exists but np.flatten does not |
| 168 | +def _flatten(a, order="C"): |
| 169 | + (tensor,) = _helpers.to_tensors(a) |
| 170 | + result = _impl._flatten(tensor) |
| 171 | + return _helpers.array_from(result, a) |
| 172 | + |
| 173 | + |
| 174 | +# ### Type/shape etc queries ### |
| 175 | + |
| 176 | + |
| 177 | +def real(a): |
| 178 | + (tensor,) = _helpers.to_tensors(a) |
| 179 | + result = torch.real(tensor) |
| 180 | + return _helpers.array_from(result) |
| 181 | + |
| 182 | + |
| 183 | +def imag(a): |
| 184 | + (tensor,) = _helpers.to_tensors(a) |
| 185 | + result = _impl.imag(tensor) |
| 186 | + return _helpers.array_from(result) |
| 187 | + |
| 188 | + |
| 189 | +def round_(a, decimals=0, out=None): |
| 190 | + (tensor,) = _helpers.to_tensors(a) |
| 191 | + result = _impl.round(tensor, decimals) |
| 192 | + return _helpers.result_or_out(result, out) |
| 193 | + |
| 194 | + |
| 195 | +around = round_ |
| 196 | +round = round_ |
0 commit comments