I have placed a similar issue in fortran proposal (Array broadcasting in expressions · Issue #252 · j3-fortran/fortran_proposals · GitHub) before reading that it is better to place it here in advance to have more comments.

Array broadcasting is very common in Python with numpy and in Matlab.

Broadcasting means that in an array expression if an array has a unitary dimension along an axis that dimension is automatically expanded to match the size of the other array. For example if an array has size [3,1] and another has size [1,4] both array are expanded as they have size [3,4]. In the actual Fortran one has to use **spread** to achieve the same result.

Another nice feature of numpy is the possibility to add unitary dimensions on the fly with **None** . Python example:

```
a = np.ones(2)
b= np.ones(3)
a[:,None] * b[None,:]
```

Moreover some functions that reduce the dimensionality like **sum** can have an extra arguments **keepdims** that doesn’t reduce the dimensionality but make that dimension unitary. This is useful as one can write (in Python) somenthing like:

`a/np.mean(a, axis=0,keepdims=True)`

So I suggest the following feature:

Use the symbol “+” (or whatever other symbol) to add a dimension to a section of an array. The function sum and other that reduce the dimensionality should have an extra keyword argument like **keepdims** that doesn’t eliminate that dimension.

As an example:

```
integer :: a(3), b(4), c(3,4), d(4), i
a = [(i=1,3)]
b = [(i=1,4)]
c = a(:,+) + b(+,:)
d = c/sum(c, dim=1, keepdim=.true.)
```

Contrary what happen to Matlab and Python adding the extra dimension is obligatory (no implicit unary dimensions).

The point is that you may not know, in general, if an expression will involve broadcasting at compile time (except for those simple examples), so at run time the processor has to check the extent of all the arrays involved in the array expression, and that may affect the speed. On the other hand it may be safer to require the processor to report if in array expressions the dimensions are not compatible. It may make the program a little slower but safer (with an explicit loop the burden should be all on the shoulder of the programmer).

Or one can decide to make things more explicit adding an attribute to arrays, like **broadcastable** which indicates that any unitary dimension of that array can be expanded.

A section with an added dimension with “+” like in the previous example will be **broadcastable** , and the return of a function like the sum with **keepdim** will be **broadcastable** (or one uses a different function like **sum_keepdim**, which returns a **broadcastable** array, while the old one return a normal array).

All other array expressions can remain as before, without the overhead to check the extent of all the involved arrays.

```
integer, broadcastable :: a(3,1), b(1,4)
integer :: c(3,4)
integer :: dd(3,1), ee(1,4)
c = a + b ! broadcasting
! c = dd + ee ! illegal in current and future Fortran
```