|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | + |
| 6 | +# Copy-pasted from numpy.lib.stride_tricks 1.11.2. |
| 7 | +def _maybe_view_as_subclass(original_array, new_array): |
| 8 | + if type(original_array) is not type(new_array): |
| 9 | + # if input was an ndarray subclass and subclasses were OK, |
| 10 | + # then view the result as that subclass. |
| 11 | + new_array = new_array.view(type=type(original_array)) |
| 12 | + # Since we have done something akin to a view from original_array, we |
| 13 | + # should let the subclass finalize (if it has it implemented, i.e., is |
| 14 | + # not None). |
| 15 | + if new_array.__array_finalize__: |
| 16 | + new_array.__array_finalize__(original_array) |
| 17 | + return new_array |
| 18 | + |
| 19 | + |
| 20 | +# Copy-pasted from numpy.lib.stride_tricks 1.11.2. |
| 21 | +def _broadcast_to(array, shape, subok, readonly): |
| 22 | + shape = tuple(shape) if np.iterable(shape) else (shape,) |
| 23 | + array = np.array(array, copy=False, subok=subok) |
| 24 | + if not shape and array.shape: |
| 25 | + raise ValueError('cannot broadcast a non-scalar to a scalar array') |
| 26 | + if any(size < 0 for size in shape): |
| 27 | + raise ValueError('all elements of broadcast shape must be non-' |
| 28 | + 'negative') |
| 29 | + needs_writeable = not readonly and array.flags.writeable |
| 30 | + extras = ['reduce_ok'] if needs_writeable else [] |
| 31 | + op_flag = 'readwrite' if needs_writeable else 'readonly' |
| 32 | + broadcast = np.nditer( |
| 33 | + (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras, |
| 34 | + op_flags=[op_flag], itershape=shape, order='C').itviews[0] |
| 35 | + result = _maybe_view_as_subclass(array, broadcast) |
| 36 | + if needs_writeable and not result.flags.writeable: |
| 37 | + result.flags.writeable = True |
| 38 | + return result |
| 39 | + |
| 40 | + |
| 41 | +# Copy-pasted from numpy.lib.stride_tricks 1.11.2. |
| 42 | +def broadcast_to(array, shape, subok=False): |
| 43 | + """Broadcast an array to a new shape. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + array : array_like |
| 48 | + The array to broadcast. |
| 49 | + shape : tuple |
| 50 | + The shape of the desired array. |
| 51 | + subok : bool, optional |
| 52 | + If True, then sub-classes will be passed-through, otherwise |
| 53 | + the returned array will be forced to be a base-class array (default). |
| 54 | +
|
| 55 | + Returns |
| 56 | + ------- |
| 57 | + broadcast : array |
| 58 | + A readonly view on the original array with the given shape. It is |
| 59 | + typically not contiguous. Furthermore, more than one element of a |
| 60 | + broadcasted array may refer to a single memory location. |
| 61 | +
|
| 62 | + Raises |
| 63 | + ------ |
| 64 | + ValueError |
| 65 | + If the array is not compatible with the new shape according to NumPy's |
| 66 | + broadcasting rules. |
| 67 | +
|
| 68 | + Notes |
| 69 | + ----- |
| 70 | + .. versionadded:: 1.10.0 |
| 71 | +
|
| 72 | + Examples |
| 73 | + -------- |
| 74 | + >>> x = np.array([1, 2, 3]) |
| 75 | + >>> np.broadcast_to(x, (3, 3)) |
| 76 | + array([[1, 2, 3], |
| 77 | + [1, 2, 3], |
| 78 | + [1, 2, 3]]) |
| 79 | + """ |
| 80 | + return _broadcast_to(array, shape, subok=subok, readonly=True) |
0 commit comments