From: Colin Watters on

"Colin Watters" <boss(a)qomputing.com> wrote in message
news:hm00se$a1a$1(a)news.eternal-september.org...
>
> "monir" <monirg(a)mondenet.com> wrote in message
> news:b32d1782-a734-4e4b-930a-fd0e35e68ae6(a)z39g2000vbb.googlegroups.com...
> On Feb 22, 11:16 am, Jim Xia <jim...(a)hotmail.com> wrote:
>> On Feb 22, 11:09 am, nos...(a)see.signature (Richard Maine) wrote:
>>
>> > Jim Xia <jim...(a)hotmail.com> wrote:
>> > > The optional keyword has to be specified on the interface
>> > > declaration,
>> > > not the caller (the call side).
>>
>
>> Thank you all for your prompt and helpful replies.
>> {OPTIONAL attribute, PRESENT intrinsic, modify the declarations of all
>> the dummy arguments, interface declaration, optional keyword,
>> interface & arguments of rank 2 or higher; etc.}
>
>> As some of you have suggested, attached below please find an
>> abbreviated F77 code of the 4-variable sort version, which works fine.
>> (I had to simplify and shorten the variables' names for posting and
>> easy reading).
>
>> The question once again is whether one can use the same 4-variable
>> mySort() routine for 3-variable, 2-variable, and 1-variable sort, as
>> the case maybe, by simply modifying the calling statement in main;
>> using possibly "OPTIONAL" arguments in the argument list or in the
>> interface or some other procedure.
>
>> Your help would be greatly appreciated.
>
>
> Doubtless it will be possible to do what you ask, but it may well prove
> to be very long-winded and unsatisfying. And it depends on eaxctly what
> is going on in subroutine sort44.
>
> One approach is to rewrite sort44 so it doesn't require all the extra
> arguments that accept the "corresponding rearrangements". Instead, return
> just an array of integers (IA) that allow the array RA to be addressed in
> sorted order. Then the code in mysort can be modified to use the
> necessarry "if(present(t))" clauses to ensure the optional arguments are
> manipulated only when available. The "manipulation" would comprise use of
> the IA array to re-arrange them.
>
> Another is to do plant the "if(present"s in sort44, but this as I say may
> be unreasonably difficult (or indeed may be easier), depending on the
> code therein.
>

OK then to give you an idea, below is one of my sort routines with
half-a-dozen or so optional arguments. (Original from Sedgewick,
"Algorithms", Circa 1985 or so.)

--
Qolin

Email: my qname at domain dot com
Domain: qomputing

module shelsrt_mod

implicit none

contains

Subroutine
shelsrt_common(points,vector,vector2,vector3,vector4,vector5,ivec1)
implicit none

!-----arguments
integer points ! no. of points in array
real vector(points) ! base vector
real,optional :: vector2(points) ! vector2
real,optional :: vector3(points) ! vector3
real,optional :: vector4(points) ! vector4
real,optional :: vector5(points) ! vector5
integer,optional :: ivec1 (points) ! integer vector 1

include "pipesim/engines/bjalib/common/debug.i"
!-----locals
integer i,j,h, iv1
real*8 v,v2,v3,v4,v5
if (logging) call login(qlg,'shelsrt_common')

!-----set the initial value of H to be greater than points
h = 1
20 continue
h = 3*h+1
if (h.le.points) goto 20

!-----loop for the various values of h down to 1
100 continue

h = h / 3 ! integer division

!--------traverse the array, comparing elements separated from one another
! by the distance h
do 800 i=h+1, points

v = vector(i)
if(present(vector2))v2 = vector2(i)
if(present(vector3))v3 = vector3(i)
if(present(vector4))v4 = vector4(i)
if(present(vector5))v5 = vector5(i)
if(present(ivec1 ))iv1 = ivec1(i)
j = i

200 continue
if (vector(j-h) .gt. v) then
vector(j) = vector(j-h)
if(present(vector2))vector2(j) = vector2(j-h)
if(present(vector3))vector3(j) = vector3(j-h)
if(present(vector4))vector4(j) = vector4(j-h)
if(present(vector5))vector5(j) = vector5(j-h)
if(present(ivec1 ))ivec1 (j) = ivec1 (j-h)
j = j - h
if ( j .le. h ) goto 500
goto 200
endif

500 continue
vector(j) = v
if(present(vector2))vector2(j) = v2
if(present(vector3))vector3(j) = v3
if(present(vector4))vector4(j) = v4
if(present(vector5))vector5(j) = v5
if(present(ivec1 ))ivec1 (j) = iv1

800 continue

if(h.gt.1) goto 100

if (logging) call logout(qlg,'shelsrt_common')
return
end subroutine

end module



From: monir on
On Feb 23, 2:42 am, "Colin Watters" <b...(a)qomputing.com> wrote:
> "monir" <mon...(a)mondenet.com> wrote in message
>
> news:b32d1782-a734-4e4b-930a-fd0e35e68ae6(a)z39g2000vbb.googlegroups.com...
> On Feb 22, 11:16 am, Jim Xia <jim...(a)hotmail.com> wrote:
>
> > On Feb 22, 11:09 am, nos...(a)see.signature (Richard Maine) wrote:
>
> > > Jim Xia <jim...(a)hotmail.com> wrote:
> > > > The optional keyword has to be specified on the interface
> > > > declaration,
> > > > not the caller (the call side).
>

Colin Watters wrote:
>Doubtless it will be possible to do what you ask, but it may well prove to
>be very long-winded and unsatisfying.
>Another is to do plant the "if(present"s in sort44, but this as I say may
>be unreasonably difficult (or indeed may be easier), depending on the code
>therein.

The use of F90 intrinsic function "Present(C)" appears to be the least-
disrupting solution to try as one of the possible solutions.
I'm assuming that the argument "C" could be a scalar or array
variable, and the function could be used without Interface and/or
declaring "C" as Optional.
Correct ??

Example:
(please tweak as you see fit!)

Program test_Present
real x,y
real z(20)
....................
call myPresent(x,y)
end program test_Present

==========================
Subroutine myPresent (A,B,C)
real A,B
real C(20)
...................
if (present(C)) then
do something
else
do something else
end if
return
end subroutine myPresent

It can't be that simple! Will consult a text.

Meanwhile, I'm trying to understand your sample sort F90 code.

Regards.
Monir
From: Richard Maine on
monir <monirg(a)mondenet.com> wrote:

> The use of F90 intrinsic function "Present(C)" appears to be the least-
> disrupting solution to try as one of the possible solutions.
> I'm assuming that... the function could be used without Interface and/or
> declaring "C" as Optional.

Both those assumptions are false.

An argument can't be optional without having the OPTIONAL attribute.
Period. That's what the attribute means. I'm puzzled as to what you
might imagine the optional attribute would mean if an argument without
it could be omitted.

If it isn't optional, then the PRESENT intrinsic wouldn't be of much use
because the argument would have to always be present. One could imagine
that it might be allowed to use the PRESENT intrinsic on a non-optional
attribute, for which it would always return TRUE, but the standard does
not allow that (and anyway, it wouldn't do anything for you).

And the OPTIONAL attribute requires an explicit interface. Period. There
is the usual collection of ways that you can provide an explicit
interface (making the procedure a module or internal procedure, or
writing an interface body), but one way or other, you do have to have an
explicit interface.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: Colin Watters on
Example, tweaked:


Program test_Present
real x,y
real z(20)
....................
call myPresent(x,y)
end program test_Present

==========================
Subroutine myPresent (A,B,C)
real A,B
real, optional :: C(20)
...................
if (present(C)) then
do something that uses C
else
do something that doesn't use C
end if
return
end subroutine myPresent



--
Qolin

Email: my qname at domain dot com
Domain: qomputing
"monir" <monirg(a)mondenet.com> wrote in message
news:1519187c-81e7-4197-8c4e-78e1691106fd(a)s17g2000vbs.googlegroups.com...
On Feb 23, 2:42 am, "Colin Watters" <b...(a)qomputing.com> wrote:
> "monir" <mon...(a)mondenet.com> wrote in message
>
> news:b32d1782-a734-4e4b-930a-fd0e35e68ae6(a)z39g2000vbb.googlegroups.com...
> On Feb 22, 11:16 am, Jim Xia <jim...(a)hotmail.com> wrote:
>
> > On Feb 22, 11:09 am, nos...(a)see.signature (Richard Maine) wrote:
>
> > > Jim Xia <jim...(a)hotmail.com> wrote:
> > > > The optional keyword has to be specified on the interface
> > > > declaration,
> > > > not the caller (the call side).
>

Colin Watters wrote:
>Doubtless it will be possible to do what you ask, but it may well prove to
>be very long-winded and unsatisfying.
>Another is to do plant the "if(present"s in sort44, but this as I say may
>be unreasonably difficult (or indeed may be easier), depending on the code
>therein.

The use of F90 intrinsic function "Present(C)" appears to be the least-
disrupting solution to try as one of the possible solutions.
I'm assuming that the argument "C" could be a scalar or array
variable, and the function could be used without Interface and/or
declaring "C" as Optional.
Correct ??

It can't be that simple! Will consult a text.

Meanwhile, I'm trying to understand your sample sort F90 code.

Regards.
Monir


From: Richard Maine on
Colin Watters <boss(a)qomputing.com> wrote:

> Example, tweaked:
[elided]

Forgets about the explicit interface requirement.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain