Converting Intrinsic MATLAB Functions ones(), zeros(), sparse(), diag() to FORTRAN
1 view (last 30 days)
Show older comments
Hello World,
In MATLAB, we have the very nice and useful functions ones(), zeros(), sparse(), and diag(). I'm trying to convert some of my MATLAB codes which use these functions to FORTRAN, does anyone know how to recreate these functions in FORTRAN or are they already built into FORTRAN?
Thank you!
3 Comments
Accepted Answer
dpb
on 7 Dec 2021
Edited: dpb
on 7 Dec 2021
program testit
implicit none
real(kind=kind(0.0d0)), allocatable :: A(:,:)
integer :: i, j
A = zeros(6,4)
print '(''zeros(6,4)'')'
do i=1, size(A,1)
print '(*(g9.4),1x)', A(i,:)
end do
A = eye(4)
print '(''eye(4)'')'
do i=1, size(A,1)
print '(*(g9.4),1x)', A(i,:)
end do
contains
function zeros(n,m) result(res)
integer, intent(in) :: n,m
real(kind=kind(0.0d0)), allocatable :: res(:,:)
allocate(res(n,m))
res = 0d0
end function
function eye(n) result(res)
integer, intent(in) :: n
real(kind=kind(0.0d0)), allocatable :: res(:,:)
integer :: i
allocate(res(n,n))
res = 0d0
forall(i=1:n) res(i,i)=1d0
end function
end program
Compile and test...
C:\GCC\bin> gfortran tmp\testit.f95 -o tmp\testit.exe
C:\GCC\bin> tmp\testit
zeros(6,4)
0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000
eye(4)
1.000 0.000 0.000 0.000
0.000 1.000 0.000 0.000
0.000 0.000 1.000 0.000
0.000 0.000 0.000 1.000
C:\GCC\bin>
As noted above, Fortran is much like MATLAB already in many ways, w/ 2003 and beyond the automagic reallocation on assignment was much improved.
Above is hardcoded to return a double, unfortunately, something like
function zeros(n,m) result(res)
integer, intent(in) :: n,m
real(kind=kind(res)), allocatable :: res(:,:)
...
doesn't work; the dummy argument doesn't carry along with it an implicit type.
One will have to either specify a precision as an argument or build a version for various precisions.
I believe in the latest Standards with object-oriented code there are features to begin to deal with this, but I've not looked into them at all...that's too new a trick for this old dog!
1 Comment
dpb
on 7 Dec 2021
Edited: dpb
on 7 Dec 2021
ADDENDUM
NB: that Fortran alloctables are not QUITE as far along as MATLAB in not being implicitly allocated to variable dimensions -- you must specify the shape if not the size.
I'm not sure just how far along the Standard is beyond the demonstration above; I've not tried to keep up at all. comp.lang.fortran and the https://fortran-lang.org/community/ are locations where some really bright folks hang out -- I've dropped out over last couple years or so, they're way beyond my expertise with new Standard now and I've no direct need any longer so have retired to the MATLAB Answers forum almost exclusively.
More Answers (1)
dpb
on 5 Dec 2021
Edited: Walter Roberson
on 7 Dec 2021
This will depend upon, firstly, the level of the Fortrand Standard to which you are coding; current Fortran includes a great deal of functionality built into the language that one is used to in MATAB, including dynamic allocation in some areas. (How efficient these are may vary markedly from compiler to compiler and also be dependent upon just how one writes code using the feature, but it is there).
OTOH, if you're still, like TMW, stuck in the age of the dinosaur and writing F77-style code, then everything is static memory allocation and DO...ENDDO loops (or DO...CONTINUE if you're still that far behind as some I've seen are).
is a list of F2018 intrinsics, that includes things like MATMUL, but not an inherent sparse version and Fortran does not have a native sparse data type--of course, that is built on top of MATLAB with implementation code; C/C++ doesn't have it as native type, either (it doesn't even have the array, but gets from stdlib or STL or some other library that builds and makes available the feature.)
The innards of MATLAB is built around BLAS and LAPACk; there are folks working on and libraries for sparse arrays as well although I am not nearly as familiar as weren't really what I dealt with...
F2003 introduced dynamic reallocation on assignment..if the size or the RHS changes, then just as in MATLAB, the LHS is deallocated and then reallocated to the new size/shape.
I've written no F2003+ code "in anger" using new features other than toy code for experimentation at the very first release of ALLOCATABLE() with F95. At that time, it still took a lot of effort to be able to use to try to mimic MATLAB code, but I'm sure now if one uses a compiler and all the new features one can do a lot better.
2 Comments
dpb
on 7 Dec 2021
Edited: dpb
on 9 Dec 2021
gcc version 6.3.0 (x86_64-posix-seh-rev2, Built by MinGW-W64 project)
is the release linked to by MATLAB and I did test it with it. You'll have to see if 6.1.0 is up to it; I have not looked at standard compliance/bugs comparatively.
My standalone version is 11.1.0; I've not gotten brave to see if can substitute it for the older mex install or not.
" and writing FORTRAN .f95 file types"
Just a nit -- "FORTRAN" is now officially "Fortran" :)
See Also
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!