Home > Archive > Fortran > January 2006 > Nested dummy procedures
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Nested dummy procedures
|
|
| Stanislaus 2006-01-29, 6:59 pm |
| Hello,
Does any body know how make code below more smart? Do not look whta
functions do -- it's example.
Code:
module Parameters
implicit none
integer, parameter :: K = 10, L = 10
end module Parameters
Code:
program DummyProcedures
use Parameters
implicit none
real(8) X(L)
X = Newton( MyFunc, X )
contains
function Newton( Func, Xi ) result(X)
real(8) :: X(L)
interface
function Func( V )
use Parameters
real(8), intent(in) :: V(L)
real(8) Func(K)
end
end interface
real(8), intent(in) :: Xi(L)
real(8) M(K, L)
M = DiffVectorByVector( Func, Xi )
X = M(1, :)
end function Newton
function DiffVectorByVector( Func, V ) result(M)
interface
function Func( V )
use Parameters
real(8), intent(in) :: V(L)
real(8) Func(K)
end
end interface
real(8), intent(in) :: V(L)
real(8) :: M(K, L)
M(1:K, 1) = Func( V )
end function DiffVectorByVector
function MyFunc( V )
real(8), intent(in) :: V(L)
real(8) MyFunc(K)
MyFunc = V(1:Min( K, L ))
end function MyFunc
end program DummyProcedures
I mean, when dummy function returns scalar all looking good.
Code:
program DummyScalarProcedure
implicit none
integer, parameter :: K = 10, L = 10
real(8) X(L)
X = Newton( MyFunc, X )
contains
function Newton( Func, Xi ) result(X)
real(8) :: X(L)
real(8) Func
real(8), intent(in) :: Xi(L)
real(8) M(K, L)
M = DiffVectorByVector( Func, Xi )
X = M(1, :)
end function Newton
function DiffVectorByVector( Func, V ) result(M)
real(8) Func
real(8), intent(in) :: V(L)
real(8) :: M(K, L)
M(K, 1) = Func( V )
end function DiffVectorByVector
function MyFunc( V )
real(8), intent(in) :: V(L)
real(8) MyFunc
MyFunc = V(Min( K, L ))
end function MyFunc
end program DummyScalarProcedure
It seems that when I declare dummy function as real(8) Func(K) compiler
thinks that it can be only array, but not a function which returns
array of length K! But real(8) Func -- can be function wich returns
scalar. So I had to use interfaces => module or triple declaration of K
and L. May be there is a way of writing more smart code in this case.
I'll be glad any help.
Stanislaus
| |
| Jugoslav Dujic 2006-01-30, 3:57 am |
| Stanislaus wrote:
| Hello,
|
| Does any body know how make code below more smart? Do not look whta
| functions do -- it's example.
<snip code>
| It seems that when I declare dummy function as real(8) Func(K) compiler
| thinks that it can be only array, but not a function which returns
| array of length K! But real(8) Func -- can be function wich returns
| scalar. So I had to use interfaces => module or triple declaration of K
| and L. May be there is a way of writing more smart code in this case.
| I'll be glad any help.
Sorry, I don't follow you 100%, so I apologize in advance if I'm
answering the wrong question...
I don't see anything particularly bad with the code you posted
(apart from the fact that M and X are never initialized, and wrong
rank of M is copied into X, but that's probably due to simplification).
I'm not a fan of functions returning arrays, because I foresee
potential efficiency problems -- compilers tend to generate
lots of temporary array copies in this way; I'd prefer subroutines
instead (but that could lead to even longer code). Also, you might
use assumed-shape (:) instead of explicit-size arrays (K).
However, in both cases, you CAN'T avoid explicit interface
blocks for dummy procedures. Both functions returning arrays and
subroutines with assumed-shape arguments require explicit interface,
and interface block is the only way to achieve it if the procedure
is a dummy argument.
You can make the code more elegant and slightly less error-prone
if you move the interface body to an include file and INCLUDE it
in necessary places, but that's more or less it.
--
Jugoslav
___________
www.xeffort.com
Please reply to the newsgroup.
You can find my real e-mail on my home page above.
|
|
|
|
|