user5304759
user5304759

Reputation:

Implied do loops in Fortran

How to do the same thing with implied do loops? I want to replace the existing loop for Vcvo and Vcvonew with two implied do loops?

This is my code:

Program DO_LOOPS

Implicit none

Integer::i,j
Integer,parameter::BrCv=15,BrSat=24

Real,dimension(0:BrCv-1,BrSat)::Vcvo
Real,dimension(BrCv-1,BrSat)::Vcvonew

Do i=0,BrCv-1
   do j=1,BrSat

Vcvo(i,j)=i+j*BrCv/BrSat

 end do
End do

Do i=1,BrCv-1
   do j=1,BrSat


   Vcvonew(i,j)=Vcvo(i,j)

   end do
End do

Stop
End program DO_LOOPS

Upvotes: 1

Views: 2188

Answers (1)

Edmondo Giovannozzi
Edmondo Giovannozzi

Reputation: 438

An implied do loop will be:

Vcvo = reshape([((i + j*real(BrCv)/real(Brsat), i=0,BrCv-1), j=1,Brsat)],[BrCv,BrSat])

for the first one.

For the second one you don't need an implied loop. A simple array assignment, as already suggested, will do:

Vcvonew = vcvo(1:,:)

The first one doesn't look to me clearer than an explicit loop. But if you want a more compact solution you can use do concurrent:

do concurrent (i = 0:BrCv-1, j=1:Brsat)
    Vcvo(i,j) = i + j*real(BrCv)/real(Brsat)
end do

You spare a couple of lines.

I have also taken the liberty to transform BrCv and BrSat to real before dividing them. It may not be what you want, but it is something you have pay attention.

Upvotes: 3

Related Questions