Converting Intrinsic MATLAB Functions ones(), zeros(), sparse(), diag() to FORTRAN
조회 수: 5 (최근 30일)
이전 댓글 표시
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
채택된 답변
dpb
2021년 12월 7일
편집: dpb
2021년 12월 7일
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
dpb
2021년 12월 7일
편집: dpb
2021년 12월 7일
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.
추가 답변 (1개)
dpb
2021년 12월 5일
편집: Walter Roberson
2021년 12월 7일
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
dpb
2021년 12월 7일
편집: dpb
2021년 12월 9일
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" :)
참고 항목
카테고리
Help Center 및 File Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!