Relatively arbitrary example:
Consider a two-dimensional array r
integer :: nDim = 3, nPoints = 5
real, dimension(nDim,nPoints) :: r
which corresponds to a number of points with x, y and z coordinates.
Suppose I have a problem which requires this array to be reshaped to instead be one dimensional and looking like (x1, y1, z1, x2, y2, z2,..., z5)
which I can obviously achieve like so
real, dimension(nDim*nPoints) :: r_reshaped ! Imagine this declaration is in the correct place
r_reshaped = reshape(r, (/ nDim*nPoints/) )
which I can then assign into a different array.
If my problem required constant switching between the arrays in these two shapes, with this method, despite the fact that nothing actually needs to change in memory due to the layout of the array, I am constantly assigning values to the two separate memory locations of r
and r_reshaped
which seems unnecessary.
The question is, is there a way to have two references to the same piece of memory wherein fortran can perceive each of the two references as an array of different shapes? That way, any calculations/updates/use/etc of the array in either shape would affect 1 piece of memory only.
I'm assuming this can be done using pointers, though I have not used pointers in fortran before. Also, I would appreciate that if the solution changes should the array be dynamically allocated, please flag that.
Please feel free to clarify anything. Cheers