Skip to content

Array broadcasting in expressions  #252

Open
@egiovan

Description

@egiovan

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

Boradcasting 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Clause 9Standard Clause 9: Use of data objects

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions