|
49 | 49 |
|
50 | 50 |
|
51 | 51 | __all__ = [
|
| 52 | + "diag_indices", |
| 53 | + "diag_indices_from", |
52 | 54 | "nonzero",
|
53 | 55 | ]
|
54 | 56 |
|
55 | 57 |
|
| 58 | +def diag_indices(n, ndim=2): |
| 59 | + """ |
| 60 | + Return the indices to access the main diagonal of an array. |
| 61 | +
|
| 62 | + This returns a tuple of indices that can be used to access the main |
| 63 | + diagonal of an array `a` with ``a.ndim >= 2`` dimensions and shape |
| 64 | + (n, n, ..., n). For ``a.ndim = 2`` this is the usual diagonal, for |
| 65 | + ``a.ndim > 2`` this is the set of indices to access ``a[i, i, ..., i]`` |
| 66 | + for ``i = [0..n-1]``. |
| 67 | +
|
| 68 | + For full documentation refer to :obj:`numpy.diag_indices`. |
| 69 | +
|
| 70 | + See also |
| 71 | + -------- |
| 72 | + :obj:`diag_indices_from` : Return the indices to access the main |
| 73 | + diagonal of an n-dimensional array. |
| 74 | +
|
| 75 | + Examples |
| 76 | + -------- |
| 77 | + Create a set of indices to access the diagonal of a (4, 4) array: |
| 78 | +
|
| 79 | + >>> import dpnp as np |
| 80 | + >>> di = np.diag_indices(4) |
| 81 | + >>> di |
| 82 | + (array([0, 1, 2, 3]), array([0, 1, 2, 3])) |
| 83 | + >>> a = np.arange(16).reshape(4, 4) |
| 84 | + >>> a |
| 85 | + array([[ 0, 1, 2, 3], |
| 86 | + [ 4, 5, 6, 7], |
| 87 | + [ 8, 9, 10, 11], |
| 88 | + [12, 13, 14, 15]]) |
| 89 | + >>> a[di] = 100 |
| 90 | + >>> a |
| 91 | + array([[100, 1, 2, 3], |
| 92 | + [ 4, 100, 6, 7], |
| 93 | + [ 8, 9, 100, 11], |
| 94 | + [ 12, 13, 14, 100]]) |
| 95 | +
|
| 96 | + Now, we create indices to manipulate a 3-D array: |
| 97 | +
|
| 98 | + >>> d3 = np.diag_indices(2, 3) |
| 99 | + >>> d3 |
| 100 | + (array([0, 1]), array([0, 1]), array([0, 1])) |
| 101 | +
|
| 102 | + And use it to set the diagonal of an array of zeros to 1: |
| 103 | +
|
| 104 | + >>> a = np.zeros((2, 2, 2), dtype=int) |
| 105 | + >>> a[d3] = 1 |
| 106 | + >>> a |
| 107 | + array([[[1, 0], |
| 108 | + [0, 0]], |
| 109 | + [[0, 0], |
| 110 | + [0, 1]]]) |
| 111 | +
|
| 112 | + """ |
| 113 | + |
| 114 | + if not use_origin_backend(): |
| 115 | + return dpnp_diag_indices(n, ndim) |
| 116 | + |
| 117 | + return call_origin(numpy.diag_indices, n, ndim) |
| 118 | + |
| 119 | + |
| 120 | +def diag_indices_from(arr): |
| 121 | + """ |
| 122 | + Return the indices to access the main diagonal of an n-dimensional array. |
| 123 | +
|
| 124 | + For full documentation refer to :obj:`numpy.diag_indices_from`. |
| 125 | +
|
| 126 | + See also |
| 127 | + -------- |
| 128 | + :obj:`diag_indices` : Return the indices to access the main |
| 129 | + diagonal of an array. |
| 130 | +
|
| 131 | + """ |
| 132 | + |
| 133 | + is_a_dparray = isinstance(arr, dparray) |
| 134 | + |
| 135 | + if (not use_origin_backend(arr) and is_a_dparray): |
| 136 | + # original limitation |
| 137 | + if not arr.ndim >= 2: |
| 138 | + checker_throw_value_error("diag_indices_from", "arr.ndim", arr.ndim, "at least 2-d") |
| 139 | + |
| 140 | + # original limitation |
| 141 | + # For more than d=2, the strided formula is only valid for arrays with |
| 142 | + # all dimensions equal, so we check first. |
| 143 | + if not numpy.alltrue(numpy.diff(arr.shape) == 0): # TODO: replace alltrue and diff funcs with dpnp own ones |
| 144 | + checker_throw_value_error("diag_indices_from", "arr.shape", arr.shape, |
| 145 | + "All dimensions of input must be of equal length") |
| 146 | + |
| 147 | + return dpnp_diag_indices(arr.shape[0], arr.ndim) |
| 148 | + |
| 149 | + return call_origin(numpy.diag_indices_from, arr) |
| 150 | + |
| 151 | + |
56 | 152 | def nonzero(a):
|
57 | 153 | """
|
58 | 154 | Return the indices of the elements that are non-zero.
|
|
0 commit comments