I got a piece of code here and I can't seem to figure out an efficient way of converting this piece of code to the Fortran 95 equivalent. I have tried several things already, but I'm always stuck on making 1D arrays from matrices and the other way around (the point is to reduce calculation time, and if I convert them, I can't think of another way than using loops again :/).
This is the piece of code:
do i=1,dim
do j=1,dim
Snorm(i,j)=Sval(j)/Sval(i)
Bnorm(i,j)=Bval(j)/Bval(i)
Pnorm(i,j)=Pval(j)/Pval(i)
enddo
enddo
How would you write that in Fortran95 code?
The equivalent of the matrix calculations in R is this:
Snorm <- t(Sval %*% t(1/Sval))
Bnorm <- t(Bval %*% t(1/Bval))
Pnorm <- t(Pval %*% t(1/Pval))
The equivalent of it in Python is this:
Snorm = (numpy.dot((Svalmat.T),(1/Svalmat))).T
Bnorm = (numpy.dot((Bvalmat.T),(1/Bvalmat))).T
Pnorm = (numpy.dot((Pvalmat.T),(1/Pvalmat))).T
with Svalmat etc the equivalent of Sval, but then columnmatrix
Anyone has an idea?
It is not worth changing in my opinion. It is valid Fortran 95. Especially if your goal is the calculation time. Any "clever" tricks with subarrays can introduce array temporaries.
The obvious try is forall
or do concurrent
forall(i=1:dim, j=1:dim)
Snorm(i,j)=Sval(j)/Sval(i)
Bnorm(i,j)=Bval(j)/Bval(i)
Pnorm(i,j)=Pval(j)/Pval(i)
end forall
and the same with do concurrent
.
Notice, that your original order of the loops is probably not efficient.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.