Determines output shape from broadcasting arrays. Parameters ---------- shapes : tuples The shapes of the arguments. Returns ------- output_shape : tuple Raises ------ ValueError If the input shapes cannot be successfully broadcast together
(*shapes)
| 5052 | |
| 5053 | |
| 5054 | def broadcast_shapes(*shapes): |
| 5055 | """ |
| 5056 | Determines output shape from broadcasting arrays. |
| 5057 | |
| 5058 | Parameters |
| 5059 | ---------- |
| 5060 | shapes : tuples |
| 5061 | The shapes of the arguments. |
| 5062 | |
| 5063 | Returns |
| 5064 | ------- |
| 5065 | output_shape : tuple |
| 5066 | |
| 5067 | Raises |
| 5068 | ------ |
| 5069 | ValueError |
| 5070 | If the input shapes cannot be successfully broadcast together. |
| 5071 | """ |
| 5072 | if len(shapes) == 1: |
| 5073 | return shapes[0] |
| 5074 | out = [] |
| 5075 | for sizes in zip_longest(*map(reversed, shapes), fillvalue=-1): |
| 5076 | if np.isnan(sizes).any(): |
| 5077 | dim = np.nan |
| 5078 | else: |
| 5079 | dim = 0 if 0 in sizes else np.max(sizes).item() |
| 5080 | if any(i not in [-1, 0, 1, dim] and not np.isnan(i) for i in sizes): |
| 5081 | raise ValueError( |
| 5082 | "operands could not be broadcast together with " |
| 5083 | "shapes {}".format(" ".join(map(str, shapes))) |
| 5084 | ) |
| 5085 | out.append(dim) |
| 5086 | return tuple(reversed(out)) |
| 5087 | |
| 5088 | |
| 5089 | def elemwise(op, *args, out=None, where=True, dtype=None, name=None, **kwargs): |