In python (using numpy), I can broadcast an array to a different shape:
>>> import numpy as np
>>> a = np.array([2,3,4])
>>> b = np.zeros((3,2))
>>> b[:,:] = np.zeros((3,2))
>>> b[:,:] = a[:,np.newaxis] #<-- np.newaxis allows `a` to be "broadcasted" to the same shape as b.
>>> b
array([[ 2., 2.],
[ 3., 3.],
[ 4., 4.]])
>>> c = np.zeros((2,3))
>>> c[:,:] = a[np.newaxis,:]
>>> c
array([[ 2., 3., 4.],
[ 2., 3., 4.]])
Is there any way to achieve the same effect in fortran? I have a subroutine which expects a 2D
array to be passed in -- I would like to "broadcast" my 1-D arrays up to 2-D as I've demonstrated above. As it seems that it is likely to matter, my 2D array does have an explicit interface.
As a side note, I thought that this functionality might be provided by the reshape
intrinsic, -- Something like:
real,dimension(3) :: arr1d
reshape(arr1d, (/3,3/), order=(/1,/1))
but after reading the docs, I don't think that this is possible since order
seems to need to include all the numbers 1 to "N".
Edit: To be a little more clear, I'm looking for a simply way to create a couple of transforms on an input a
such that:
case 1
b(i,j) .eq. a(i) !for all j, or even just j=1,2
and
case 2
b(j,i) .eq. a(i) !for all j, or even just j=1,2
bonus points1 for arbitrary dimensionality:
b(i,j,k) .eq. a(i,j)
b(i,k,j) .eq. a(i,j)
etc.
1disclaimer -- I don't actually have SO super powers to bestow bonus points upon the answerer ;-)