Return (a * i), that is string multiple concatenation, element-wise. Values in ``i`` of less than 0 are treated as 0 (which yields an empty string). Parameters ---------- a : array_like, with ``StringDType``, ``bytes_`` or ``str_`` dtype i : array_like, with any i
(a, i)
| 148 | @set_module("numpy.strings") |
| 149 | @array_function_dispatch(_multiply_dispatcher) |
| 150 | def multiply(a, i): |
| 151 | """ |
| 152 | Return (a * i), that is string multiple concatenation, |
| 153 | element-wise. |
| 154 | |
| 155 | Values in ``i`` of less than 0 are treated as 0 (which yields an |
| 156 | empty string). |
| 157 | |
| 158 | Parameters |
| 159 | ---------- |
| 160 | a : array_like, with ``StringDType``, ``bytes_`` or ``str_`` dtype |
| 161 | |
| 162 | i : array_like, with any integer dtype |
| 163 | |
| 164 | Returns |
| 165 | ------- |
| 166 | out : ndarray |
| 167 | Output array of ``StringDType``, ``bytes_`` or ``str_`` dtype, |
| 168 | depending on input types |
| 169 | |
| 170 | Examples |
| 171 | -------- |
| 172 | >>> import numpy as np |
| 173 | >>> a = np.array(["a", "b", "c"]) |
| 174 | >>> np.strings.multiply(a, 3) |
| 175 | array(['aaa', 'bbb', 'ccc'], dtype='<U3') |
| 176 | >>> i = np.array([1, 2, 3]) |
| 177 | >>> np.strings.multiply(a, i) |
| 178 | array(['a', 'bb', 'ccc'], dtype='<U3') |
| 179 | >>> np.strings.multiply(np.array(['a']), i) |
| 180 | array(['a', 'aa', 'aaa'], dtype='<U3') |
| 181 | >>> a = np.array(['a', 'b', 'c', 'd', 'e', 'f']).reshape((2, 3)) |
| 182 | >>> np.strings.multiply(a, 3) |
| 183 | array([['aaa', 'bbb', 'ccc'], |
| 184 | ['ddd', 'eee', 'fff']], dtype='<U3') |
| 185 | >>> np.strings.multiply(a, i) |
| 186 | array([['a', 'bb', 'ccc'], |
| 187 | ['d', 'ee', 'fff']], dtype='<U3') |
| 188 | |
| 189 | """ |
| 190 | a = np.asanyarray(a) |
| 191 | |
| 192 | i = np.asanyarray(i) |
| 193 | if not np.issubdtype(i.dtype, np.integer): |
| 194 | raise TypeError(f"unsupported type {i.dtype} for operand 'i'") |
| 195 | i = np.maximum(i, 0) |
| 196 | |
| 197 | # delegate to stringdtype loops that also do overflow checking |
| 198 | if a.dtype.char == "T": |
| 199 | return a * i |
| 200 | |
| 201 | a_len = str_len(a) |
| 202 | |
| 203 | # Ensure we can do a_len * i without overflow. |
| 204 | if np.any(a_len > sys.maxsize / np.maximum(i, 1)): |
| 205 | raise OverflowError("Overflow encountered in string multiply") |
| 206 | |
| 207 | buffersizes = a_len * i |
searching dependent graphs…