How to convert this 2D code segment with random number into the FORTRAN code?

Nx = 4
Ny = 4
c1 = 0.12
rr = 0.0001
for i=1:Nx
for j=1:Ny
D(i,j) =c1 + rr*(0.5-rand);
end
end
It contains random number in Matlab code and it is also in 2D.

8 Comments

The question should fit into Fortran blogs.
Matlab has some ways to convert it into fortran code. I am looking for those ways and want to learn. Thankyou for your suggestions.
"Matlab has some ways to convert it into fortran code."
I'm unaware of any MATLAB-supplied tools for such; to what are you referring, specifically?
You can write mex functions in Fortran (altho TMW has made it harder and harder and harder and has basically dropped Fortran support over the last several years).
There are some 3rd party tools, but I've no experience with any of them having finally with retirement from active consulting just gotten tired of beating head against the wall and gave up using mex entirely.
Sad state of affairs for scientific computing toolset, but "that's the way it is", at least in my view from here.
I read about mex functions and wanted to know how to use it for conversion of Matlab code to Fortran.
But it does look like from your message that it is difficult and harder.
I still do not understand why the scientists always talk of Fortran without knowing the current affairs for the learners.
" mex functions and wanted to know how to use it for conversion of Matlab code to Fortran"
Perhaps we have a communications problem here of misunderstanding what is meant by "conversion of Matlab code to Fortran" in your original question.
I interpreted it to mean some tool that could translate MATLAB source code to Fortran -- what I was pointing out is that there is not such a function supplied in MATLAB but there are some other tools that do/can do the job for at least subsets of MATLAB syntax.
You can write mex code in Fortran, yes; but it is the job of the coder to write the code and do the translation from one language to the other.
What I was/am complaining about re: Mathworks and Fortran for mex is they have reduced to supporting only the Intel Fortran compiler and taken out support for gfortran and there are sizable gaps between the supplied interface libraries between C/C++ and Fortran as far as capabilities.
In addition, where years ago one could simply bypass all the nonsense of the convoluted mess of batch and scripts run by the mex command and just compile/link outside the mex environment, at about R2017 or so, one could no longer link to a vendor library not on the official support list (I still had clients using the old DEC/Compaq Visual Studio F95 compiler at the time). Those built .mex files wouldn't have the magic code in them of the supported compiler and so wouldn't load in the newer releases. Plus, they make it so you can't run a mex file from an earlier release on the new one; you have to recompile whether anything changed or not because they bury a relase number in there that was never able to figure out how to bypass.
All in all, it forced me to stay with an earlier release for that client until finally retired -- without the client, it simply wasn't/isn't worth the hassle to fool with...if I need Fortran, I'll just go run the compiler and forget about Matlab unless just use it to import files...
If one still had a real client, it appears the only choice would be to bite the bullet and go Intel commercial to get Fortran.
There are some others who have kept up more than I and may know of better news more recently, but that's where I was 4-5 years ago...
I have recently started to work in Fortran as compelled by the boss to write in it. Before that i have worked in Matlab. Honestly, i really do not know much about coding in Matlab either. I just learned to write some codes by following the previous works and by asking others. But since i have started to work in Fortran and have to learn to code in it, it sounds extremely difficult to understand how some basic things in Matlab need to be understood and to be learnt to write in Fortran. Apparently, it is itself a new dimension of studies to understand how code works in different platforms.
Looks like a lot is still to come during this time. Most of the books about Fortran also lacks fundamentals about solving different tpes of problems there and are focussed on how Fortran works to teach those who are already good at other programming languages. For someone like me with little knowlede of Matlab and is beginner in Fortran, these complexities are hard to comprehend at the moment. But fortunately, i have met good people like you on different platforms to help me out.
Thanks a lot for your words.
Which Fortran compiler are you using on what platform and for what kinds of tasks?
Fotran and MATLAB have much in common in terms of basic syntax; in fact the earliest MATLAB was actually written in FORTRAN.
If you want/need support on the Fortran side, the usenet comp.lang.fortran group is still active and for the most part pretty lenient on newbies.
The biggest drawback of Fortran vis a vis MATLAB is the lack of the higher level abstractions for data structures like the table, cell arrays, etc. But, if you're simply "crunching numbers", oft times you can outperform MATLAB significantly with compiled code-- but, then again, if you can write your algorithm to get it into a vectorized form and down in the bowels of machine code in the BLAS routines or the like; you're essentially running the identical code because that's what MATLAB is built on at its very core for those operations. It's all the stuff added on around it that is the speed hog...
There are a lot of books -- from you description I'd probably recommend https://www.amazon.com/Fortran-90-95-Scientists-Engineers/dp/0072825758 as a starter...it's not a reference but not a "for dummies", either.
It's still at the F95 Standard, though and there are a lot of new features in F2003/2008 that are now in most compilers.
I've not kept up with it much having now been out of the consulting gig so I'm not so sure about what to recommend there.
I have started with these compilers:
and was learning from different online sources like:
Also i have bought many books about Fortran and trying to understand and compiling the examples there.
With the passage of time different problems seem to occur. As suggested by people i started to work on new compiler for fortran i.e. Code::blocks. To plot different graphs with GNU was kind of hard to understand and implement. The help to integerate it into that compiler from different forums was never sufficient. So i moved to another compiler.
Few days back i bought the comiler: Simplyfortran. It is not very expensive to buy for a student. Also it has some fundamental libraries and plotting facilities.
Then comes another area. How to use scientific libraries which are the bakbone of my coding program? For instance i need fast fourier transformation of my 2D matrix. It is so easy in matlab to just write fft(variable) and get the results. But my compiler does not have it. GNU scientific library is full of resources of subrouritnes like these but i need to buy it for my compiler too. There is an advantage to buy that package that it gives me around 100 packages to add in my compiler. But still they do not claim that it will always work on the compiler.
NAG compiler with Numerical algorithm libraries seems to be the perfect choice to work with Fortran. But then it is very expensive and could not be afforded.
I am working on Mathematical modeling related to 2D partial differential equations. Therefore, the fundamental algorithm is in 2D and could be written in Fortran with some effort. Although playing around with matrixes with saving different files should not be that hard but rather time consuming for the newbie.
I think it is mostly because no one is around working actively on Fortran but on C, C++ or Java, i could not get much from these people.
Also my Boss has never worked on Fortran, and is also not willing to spend money to buy any compiler.
Therefore, the discussion or proper understanding of Fortran is not easy atleast at this stage.
Apparently, the only advantage is that the code is already written in matlab. I need to convert it into fortran language only. But then the functionalities provided by matlab are so comfortable that Fortran seems to be a daunting task. The blessing in disguise is the increase in my understanding of the most famous scientific computing language.
I keep my fingers crossed.

Sign in to comment.

 Accepted Answer

James Response in the above is correct. I would only say that it can be further modernized with the new, more specific, Fortran 2003 syntax, which makes it compiler and architecture independent:
use iso_fortran_env, only: IK => int32, RK => real64
integer(IK), parameter :: Nx = 4_IK
integer(IK), parameter :: Ny = 4_IK
real(RK), parameter :: c1 = 0.12_RK
real(RK), parameter :: rr = 0.0001_RK
real(RK) :: D(Nx,Ny)
call random_number(D)
D = c1 + rr * (0.5_RK - D);
write(*,*) D
end

1 Comment

Thanks a lot. You have written in the best way and i have learnt from it some new ways to write it in better ways.

Sign in to comment.

More Answers (1)

For your particular case, looks like the MATLAB code could reduce to:
Nx = 4;
Ny = 4;
c1 = 0.12;
rr = 0.0001;
D = c1 + rr*(0.5-rand(Nx,Ny));
The Fortran equivalent could be
integer, parameter :: Nx = 4
integer, parameter :: Ny = 4
double precision :: c1 = 0.12d0
double precision :: rr = 0.0001d0
double precision D(Nx,Ny)
call random_number(D)
D = c1 + rr*(0.5d0-D);

Categories

Products

Asked:

on 18 Aug 2020

Commented:

on 19 Aug 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!