How to use the fortran distribution function to copy an array by block rather than column?

For example, this code:

program sandbox implicit none real, dimension(2, 2) :: p p = reshape((/ 1, 3, 2, 4 /), shape(p)) print *, spread(P, 2, 2) end program sandbox 

returns this array:

 1 1 2 2 3 3 4 4 

but I'm trying to get him to get it back. "

 1 2 1 2 3 4 3 4 

Is this possible with spread ? In fact, it needs to be generalized, because I can create matrices like

 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 

depending on other variables that I will not know at compile time.

+4
source share
1 answer

Try this (everything in the Fortran 90 standard, except for the designation [ ] instead of (/ /) )

 program sandbox implicit none real, dimension(2, 2) :: p real, dimension(2, 4) :: q integer i print *, "p" p = reshape([1, 3, 2, 4], shape(p)) do i=1, 2 print *, p(i, :) end do print *, "spread (orig)" q = reshape(spread(p, 2, 2), [2, 4]) do i=1, 2 print *, q(i, :) end do print *, "spread" q = reshape(spread(transpose(p), 2, 2), [2, 4], order=[2, 1]) do i=1, 2 print *, q(i, :) end do print *, "[p, p]" q = reshape([p, p], [2, 4]) do i=1, 2 print *, q(i, :) end do end program sandbox 
+4
source

Source: https://habr.com/ru/post/1501932/


All Articles