Prev: Determining Screen Resolution
Next: Random_number
From: Gordon Sande on 11 Feb 2010 15:10 On 2010-02-11 15:14:49 -0400, happycamper <jim.mcclean(a)pgs.com> said: > On Feb 11, 12:59�pm, dpb <n...(a)non.net> wrote: >> happycamper wrote: >>> On Feb 11, 12:38 pm, dpb <n...(a)non.net> wrote: >>>> happycamper wrote: >>>>> Can you associate a pointer or something to a 3d matrix in such a way >>>>> that >>>>> access through the pointer gives a 1D view or the 3d matrix? >>>> Yes, or EQUIVALENCE to a 1D array is another simple way. >> >>>> -- >> >>> How about in modules? What would be the syntax? >> >> Don't see it matters module or not (other than the obvious of being in >> scope). �That is, can't equivalence a module-variable to another outsid > e >> the module but only within so wouldn't work if the module source isn't >> available. >> >> Is that the question? >> >> -- > > This does not compile: > > subroutine test(nz,nx,ny,arr) > integer :: nz,nx,ny > real :: arr(nz,nx,ny) > real :: arr1d(nz*nx*ny) > equivalence (arr1,arr) > end subroutine test(nz,nx,ny,arr1d,arr3d) integer :: nz,nx,ny real :: arr3d(nz,nx,ny) real :: arr1d(nz*nx*ny) <.. use arr1d and arr3d but DO NOT assign into either ..> end > > > program test > real , allocatable :: arr(:,:,:) > > > integer :: nz=5,nx=5,ny=5 > integer :: i,j,k,val > > > allocate(arr(nz,nx,ny)) > > do i = 1, ny > do j = 1, nx > do k = 1, nz > val = k+j+i > arr(k,j,i) = float(val) > > enddo > enddo > enddo > call test(nz,nx,ny,arr) call test(nz,nx,ny,arr,arr) > end program test > > I want to look at an allocated 3d array as a 1d array at some point. Use can use the semantics of argument association to obtain something which is a good imitation of equivalence as long as you do not violate the requirements on aliased arguments. Sometimes you can violate those requirements and get away with it, but as they say "do not try this at home as the performers are trained professionals..." If you do not want to look at both views at once then drop the unwanted one from test and it is perfectly standard conforming. Reshaping by use of sequence association across a call is both a feature and curse of Fortran. It is to be used with caution and not in the presence of children. Be very sure you understand the sequence mapping.
From: user1 on 11 Feb 2010 15:12 You have an argument variable equivalenced to a local variable, which is not allowed. You also have a main program and a subroutine of the same name - test. Here is a variation of your example that seemed to work for me. subroutine sub1(nz,nx,ny,arr) integer :: nz,nx,ny real :: arr(nz*nx*ny) n=nz*nx*ny do i=1,n print *,arr(i) end do return end program test real , allocatable :: arr(:,:,:) integer :: nz=5,nx=5,ny=5 integer :: i,j,k,val allocate(arr(nz,nx,ny)) do i = 1, ny do j = 1, nx do k = 1, nz val = k+j+i arr(k,j,i) = float(val) enddo enddo enddo call sub1(nz,nx,ny,arr) end program test
From: robin on 11 Feb 2010 19:13
"happycamper" <jim.mcclean(a)pgs.com> wrote in message news:ccb6eff9-0e9f-453f-8019-d1efc13e9f49(a)w31g2000yqk.googlegroups.com... >This does not compile: >subroutine test(nz,nx,ny,arr) >integer :: nz,nx,ny >real :: arr(nz,nx,ny) >real :: arr1d(nz*nx*ny) >equivalence (arr1,arr) >end Well, it won't. EQUIVALENCE requires that size be static. |