For Programmers: Free Programming Magazines  


Home > Archive > Fortran > October 2006 > rolling dice









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 rolling dice
Elijah Cardon

2006-10-02, 4:04 am

I have seven six-sided dice. Let's say I have them in my hand. I would
like a program to simulate the decisions I would make upon the conjectural
act of observance.

Let m equals six be given. Let n equals seven be given. You might get this
problem in school and have to do control loops for, maybe two, dimensions.
You feel a little silly writing seven telescoping DO loops. How do I
populate the n-tuples recursively? EC


Jugoslav Dujic

2006-10-02, 4:04 am

Elijah Cardon wrote:
| I have seven six-sided dice. Let's say I have them in my hand. I would
| like a program to simulate the decisions I would make upon the conjectural
| act of observance.
|
| Let m equals six be given. Let n equals seven be given. You might get this
| problem in school and have to do control loops for, maybe two, dimensions.
| You feel a little silly writing seven telescoping DO loops. How do I
| populate the n-tuples recursively? EC

I can post a general-purpose code emulating n nested loops, but I don't see
how it relates to your problem: isn't it easier to just write:

integer:: iDie(7)

do i=1,7
call random_number(x)
iDie(i) = 1 + FLOOR(x*6)
end do
!do something with iDie here

I'm probably missing something here -- but so does your question?

--
Jugoslav
___________
www.xeffort.com

Please reply to the newsgroup.
You can find my real e-mail on my home page above.
beliavsky@aol.com

2006-10-02, 8:03 am

Elijah Cardon wrote:
> I have seven six-sided dice. Let's say I have them in my hand. I would
> like a program to simulate the decisions I would make upon the conjectural
> act of observance.
>
> Let m equals six be given. Let n equals seven be given. You might get this
> problem in school and have to do control loops for, maybe two, dimensions.
> You feel a little silly writing seven telescoping DO loops. How do I
> populate the n-tuples recursively? EC


If one wants the equivalent of an N-dimensional loop over integer
ranges, say 1 to imax(1), 1 to imax(2), ..., 1 to imax(N), one can
represent the "state" as an integer with N elements
ii(1:N). The main task is to write code that produces the state
following the current one.
The code below does so, assuming the left-most element of the state
vector changes first.
The slightly tricky case to handle is when ii(1) = imax(1), in which
case one sets ii(1) to 1 and finds the N-1 dimensional state following
ii(2:N).

module loop_grid_mod
implicit none
private
public :: next_in_grid
contains
subroutine next_in_grid(imax,ivec)
! find the next vector in an N-dimensional grid (do loop)
integer, intent(in) :: imax(:)
integer, intent(in out) :: ivec(:)
integer :: i,n
n = size(imax)
if (size(ivec) /= n) stop "in next_in_grid, need size(imax) =
size(ivec)"
do i=1,n
if (ivec(i) < imax(i)) then
ivec(i) = ivec(i) + 1
exit
else
ivec(i) = 1
end if
end do
end subroutine next_in_grid
end module loop_grid_mod

program xloop_grid
! driver for next_in_grid
use loop_grid_mod, only: next_in_grid
implicit none
integer, parameter :: n = 3, imax(n) = (/2,4,3/)
integer :: i,icheck,ivec(n),iprod(n)
ivec = 1
iprod(1) = 1
do i=2,n
iprod(i) = iprod(i-1)*imax(i-1)
end do
do i=1,product(imax)
icheck = 1 + dot_product(ivec-1,iprod)
write (*,"(100i6)") i,icheck,ivec
call next_in_grid(imax,ivec)
end do
end program xloop_grid

output:

1 1 1 1 1
2 2 2 1 1
3 3 1 2 1
4 4 2 2 1
5 5 1 3 1
6 6 2 3 1
7 7 1 4 1
8 8 2 4 1
9 9 1 1 2
10 10 2 1 2
11 11 1 2 2
12 12 2 2 2
13 13 1 3 2
14 14 2 3 2
15 15 1 4 2
16 16 2 4 2
17 17 1 1 3
18 18 2 1 3
19 19 1 2 3
20 20 2 2 3
21 21 1 3 3
22 22 2 3 3
23 23 1 4 3
24 24 2 4 3

Herman D. Knoble

2006-10-02, 8:03 am

Elijah: I may misunderstand your question. But I think the code at:
http://ftp.cac.psu.edu/pub/ger/fortran/hdk/loopnest.f90
will make available n loop indices of variable lengths.

Skip Knoble

On Mon, 2 Oct 2006 04:31:18 -0400, "Elijah Cardon" <invalid@invalid.net> wrote:

-|I have seven six-sided dice. Let's say I have them in my hand. I would
-|like a program to simulate the decisions I would make upon the conjectural
-|act of observance.
-|
-|Let m equals six be given. Let n equals seven be given. You might get this
-|problem in school and have to do control loops for, maybe two, dimensions.
-|You feel a little silly writing seven telescoping DO loops. How do I
-|populate the n-tuples recursively? EC
-|

Elijah Cardon

2006-10-02, 7:03 pm


"Jugoslav Dujic" <jdujic@yahoo.com> wrote in message
news:4oc587Fdmrp0U1@individual.net...
> Elijah Cardon wrote:
> | I have seven six-sided dice. Let's say I have them in my hand. I would
> | like a program to simulate the decisions I would make upon the
> conjectural
> | act of observance.
> |
> | Let m equals six be given. Let n equals seven be given. You might get
> this
> | problem in school and have to do control loops for, maybe two,
> dimensions.
> | You feel a little silly writing seven telescoping DO loops. How do I
> | populate the n-tuples recursively? EC
>
> I can post a general-purpose code emulating n nested loops, but I don't
> see
> how it relates to your problem: isn't it easier to just write:
>
> integer:: iDie(7)
>
> do i=1,7
> call random_number(x)
> iDie(i) = 1 + FLOOR(x*6)
> end do
> !do something with iDie here
>
> I'm probably missing something here -- but so does your question?

The title of the post was a little misleading because I don't intend to
simulate the act of rolling, at least not yet. I want to enumerate the
outcome set as does beliavsky and presumably, the link that Mr. Knoble
posted. Since I'm new again to fortran, it will take me a bit to pick
through those posts. EC


Elijah Cardon

2006-10-02, 7:03 pm


<beliavsky@aol.com> wrote in message
news:1159794025.210022.218810@b28g2000cwb.googlegroups.com...
> Elijah Cardon wrote:
>
> If one wants the equivalent of an N-dimensional loop over integer
> ranges, say 1 to imax(1), 1 to imax(2), ..., 1 to imax(N), one can
> represent the "state" as an integer with N elements
> ii(1:N). The main task is to write code that produces the state
> following the current one.
> The code below does so, assuming the left-most element of the state
> vector changes first.
> The slightly tricky case to handle is when ii(1) = imax(1), in which
> case one sets ii(1) to 1 and finds the N-1 dimensional state following
> ii(2:N).
>
> module loop_grid_mod
> implicit none
> private
> public :: next_in_grid
> contains
> subroutine next_in_grid(imax,ivec)
> ! find the next vector in an N-dimensional grid (do loop)
> integer, intent(in) :: imax(:)
> integer, intent(in out) :: ivec(:)
> integer :: i,n
> n = size(imax)
> if (size(ivec) /= n) stop "in next_in_grid, need size(imax) =
> size(ivec)"
> do i=1,n
> if (ivec(i) < imax(i)) then
> ivec(i) = ivec(i) + 1
> exit
> else
> ivec(i) = 1
> end if
> end do
> end subroutine next_in_grid
> end module loop_grid_mod
>
> program xloop_grid
> ! driver for next_in_grid
> use loop_grid_mod, only: next_in_grid
> implicit none
> integer, parameter :: n = 3, imax(n) = (/2,4,3/)
> integer :: i,icheck,ivec(n),iprod(n)
> ivec = 1
> iprod(1) = 1
> do i=2,n
> iprod(i) = iprod(i-1)*imax(i-1)
> end do
> do i=1,product(imax)
> icheck = 1 + dot_product(ivec-1,iprod)
> write (*,"(100i6)") i,icheck,ivec
> call next_in_grid(imax,ivec)
> end do
> end program xloop_grid
>
> output:
>
> 1 1 1 1 1
> 2 2 2 1 1
> 3 3 1 2 1
> 4 4 2 2 1
> 5 5 1 3 1
> 6 6 2 3 1
> 7 7 1 4 1
> 8 8 2 4 1
> 9 9 1 1 2
> 10 10 2 1 2
> 11 11 1 2 2
> 12 12 2 2 2
> 13 13 1 3 2
> 14 14 2 3 2
> 15 15 1 4 2
> 16 16 2 4 2
> 17 17 1 1 3
> 18 18 2 1 3
> 19 19 1 2 3
> 20 20 2 2 3
> 21 21 1 3 3
> 22 22 2 3 3
> 23 23 1 4 3
> 24 24 2 4 3

This is the type of output I'm looking for. I can't quite tell exactly
because it doesn't compile for me:
http://www.billfordx.net/screendumps/dump3.htm
Ideas? EC


Michael Metcalf

2006-10-02, 7:03 pm


"Elijah Cardon" <invalid@invalid.net> wrote in message
news:12i2u0rpo4ep10c@corp.supernews.com...
>
> <beliavsky@aol.com> wrote in message

if (size(ivec) /= n) stop "in next_in_grid, need size(imax) =
size(ivec)

Your newsreader has wrapped this one line into two. Simply join them again.

Regards,

Mike Metcalf


Elijah Cardon

2006-10-02, 7:03 pm


"Herman D. Knoble" <SkipKnobleLESS@SPAMpsu.DOT.edu> wrote in message
news:v452i21t7fi0vr2dgpkpbg984kc94p36bn@
4ax.com...
> Elijah: I may misunderstand your question. But I think the code at:
> http://ftp.cac.psu.edu/pub/ger/fortran/hdk/loopnest.f90
> will make available n loop indices of variable lengths.
>
> Skip Knoble
>
> On Mon, 2 Oct 2006 04:31:18 -0400, "Elijah Cardon" <invalid@invalid.net>
> wrote:
>
> -|I have seven six-sided dice. Let's say I have them in my hand. I would
> -|like a program to simulate the decisions I would make upon the
> conjectural
> -|act of observance.
> -|
> -|Let m equals six be given. Let n equals seven be given. You might get
> this
> -|problem in school and have to do control loops for, maybe two,
> dimensions.
> -|You feel a little silly writing seven telescoping DO loops. How do I
> -|populate the n-tuples recursively? EC
> -|

!=======================================
===============================
! This file: http://ftp.aset.psu.edu/pub/ger/for...dk/loopnest.f90
!
! ROUTINE: LOOPNEST FORTRAN
!
! PURPOSE: To illustrate how to program an variable number of loop
! index values.
! COPYRIGHT (c) H. D. Knoble 03/16/88
! The Pennsylvania State University
! Center for Academic Computing
! hdkLESS at NOSPAMpsu dot edu
!
!=======================================
===============================
!
IMPLICIT NONE
INTEGER :: NumberOfLoops,t,z,TotalTrips
INTEGER, DIMENSION(10) :: LoopIndex, TripCount
LOGICAL :: PREVIOUS
CHARACTER (LEN=80) :: Line
!
Print *, "Please enter the number of loops to be generated:"
READ(*,"(A)") Line
if(Line == "") GoTo 99
READ(Line,*,END=99) NumberOfLoops
if (NumberOfLoops < 1) GoTo 99
Print *, "Please enter #trips for each loop (",NumberOfLoops, &
" positive integers):"
READ(*,"(A)") Line
if(Line == "") GoTo 99
READ(Line,*,End=99) (TripCount(t),t=1,NumberOfLoops)
!
!---Algorithm Begin
!
! Input: NumberOfLoops, and the trip count set:
! (TripCount(1), TripCount(2), ... TripCount(NumberOfLoops).
! Output: Each index set, one at a time, in lexicographic order.
!
!--- Initialize.
TotalTrips=1
DO t=1,NumberOfLoops
TotalTrips=TotalTrips*TripCount(t)
if (TotalTrips < 1 ) then
print *, "Zero trip count or Integer overflow at", &
& "TripCount=",TripCount(t)," t=",t
GoTo 99
endif
LoopIndex(t)=TripCount(t)
END DO
!--- The two nested coded loops following the comments are equivalent
! to an indefinite number "NumberOfLoops" of loops illustrated in
! the comments:
! do LoopIndex(1)=1 to TripCount(1)
! do LoopIndex(2)=1 to TripCount(2)
! ...
! do LoopIndex(NumberOfLoops) = 1 to TripCount(NumberOfLoops)
! PRINT *, (LoopIndex(t),t=1,NumberOfLoops)
! enddo; enddo; , ... , enddo
!
DO z=1,TotalTrips
previous=.TRUE.
DO t=NumberOfLoops,1,-1
!-------- Generate the index set "LoopIndex" in lexicographic order,
if(previous) LoopIndex(t)=1+MOD(LoopIndex(t),TripCoun
t(t))
previous=previous .AND. (LoopIndex(t) == 1)
END DO
!------ and use them here (or output them) one set at a time.
Print *, (LoopIndex(t),t=1,NumberOfLoops)
END DO
!---Algorithm End
99 STOP
END PROGRAM

With the addition of the statement END PROGRAM, this compiles. I was
shocked to see a message from a compiler saying "END PROGRAM missing" as
opposed to "wrong argument type in level of indirection." Anyways, I don't
get any output here. EC


Elijah Cardon

2006-10-02, 7:03 pm


"Michael Metcalf" <michaelmetcalf@compuserve.com> wrote in message
news:rQeUg.2983$tN.2315@trndny06...
>
> "Elijah Cardon" <invalid@invalid.net> wrote in message
> news:12i2u0rpo4ep10c@corp.supernews.com...
> if (size(ivec) /= n) stop "in next_in_grid, need size(imax) =
> size(ivec)
>
> Your newsreader has wrapped this one line into two. Simply join them
> again.

That'll do it. The first thing I wanted to do was confirm a conjecture I
had made about the freqency with which a non-zero number of ones occurs when
rolling 4 3-sided dice.
http://www.billfordx.net/screendumps/dump4.htm
I'll go back and take a look at syntax now. EC


Herman D. Knoble

2006-10-03, 8:12 am

Elijah: There is an END statement in the code. I can't imagine what
happened for you to miss it.

Regards.
Skip

On Mon, 2 Oct 2006 16:43:21 -0400, "Elijah Cardon" <invalid@invalid.net> wrote:

-|
-|"Herman D. Knoble" <SkipKnobleLESS@SPAMpsu.DOT.edu> wrote in message
- |news:v452i21t7fi0vr2dgpkpbg984kc94p36bn
@4ax.com...
-|> Elijah: I may misunderstand your question. But I think the code at:
-|> http://ftp.cac.psu.edu/pub/ger/fortran/hdk/loopnest.f90
-|> will make available n loop indices of variable lengths.
-|>
-|> Skip Knoble
-|>
-|> On Mon, 2 Oct 2006 04:31:18 -0400, "Elijah Cardon" <invalid@invalid.net>
-|> wrote:
-|>
-|> -|I have seven six-sided dice. Let's say I have them in my hand. I would
-|> -|like a program to simulate the decisions I would make upon the
-|> conjectural
-|> -|act of observance.
-|> -|
-|> -|Let m equals six be given. Let n equals seven be given. You might get
-|> this
-|> -|problem in school and have to do control loops for, maybe two,
-|> dimensions.
-|> -|You feel a little silly writing seven telescoping DO loops. How do I
-|> -|populate the n-tuples recursively? EC
-|> -|
- |!======================================
================================
-|! This file: http://ftp.aset.psu.edu/pub/ger/for...dk/loopnest.f90
-|!
-|! ROUTINE: LOOPNEST FORTRAN
-|!
-|! PURPOSE: To illustrate how to program an variable number of loop
-|! index values.
-|! COPYRIGHT (c) H. D. Knoble 03/16/88
-|! The Pennsylvania State University
-|! Center for Academic Computing
-|! hdkLESS at NOSPAMpsu dot edu
-|!
- |!======================================
================================
-|!
-| IMPLICIT NONE
-| INTEGER :: NumberOfLoops,t,z,TotalTrips
-| INTEGER, DIMENSION(10) :: LoopIndex, TripCount
-| LOGICAL :: PREVIOUS
-| CHARACTER (LEN=80) :: Line
-|!
-| Print *, "Please enter the number of loops to be generated:"
-| READ(*,"(A)") Line
-| if(Line == "") GoTo 99
-| READ(Line,*,END=99) NumberOfLoops
-| if (NumberOfLoops < 1) GoTo 99
-| Print *, "Please enter #trips for each loop (",NumberOfLoops, &
-| " positive integers):"
-| READ(*,"(A)") Line
-| if(Line == "") GoTo 99
-| READ(Line,*,End=99) (TripCount(t),t=1,NumberOfLoops)
-|!
-|!---Algorithm Begin
-|!
-|! Input: NumberOfLoops, and the trip count set:
-|! (TripCount(1), TripCount(2), ... TripCount(NumberOfLoops).
-|! Output: Each index set, one at a time, in lexicographic order.
-|!
-|!--- Initialize.
-| TotalTrips=1
-| DO t=1,NumberOfLoops
-| TotalTrips=TotalTrips*TripCount(t)
-| if (TotalTrips < 1 ) then
-| print *, "Zero trip count or Integer overflow at", &
-| & "TripCount=",TripCount(t)," t=",t
-| GoTo 99
-| endif
-| LoopIndex(t)=TripCount(t)
-| END DO
-|!--- The two nested coded loops following the comments are equivalent
-|! to an indefinite number "NumberOfLoops" of loops illustrated in
-|! the comments:
-|! do LoopIndex(1)=1 to TripCount(1)
-|! do LoopIndex(2)=1 to TripCount(2)
-|! ...
-|! do LoopIndex(NumberOfLoops) = 1 to TripCount(NumberOfLoops)
-|! PRINT *, (LoopIndex(t),t=1,NumberOfLoops)
-|! enddo; enddo; , ... , enddo
-|!
-| DO z=1,TotalTrips
-| previous=.TRUE.
-| DO t=NumberOfLoops,1,-1
-|!-------- Generate the index set "LoopIndex" in lexicographic order,
-| if(previous) LoopIndex(t)=1+MOD(LoopIndex(t),TripCoun
t(t))
-| previous=previous .AND. (LoopIndex(t) == 1)
-| END DO
-|!------ and use them here (or output them) one set at a time.
-| Print *, (LoopIndex(t),t=1,NumberOfLoops)
-| END DO
-|!---Algorithm End
-|99 STOP
-|END PROGRAM
-|
-|With the addition of the statement END PROGRAM, this compiles. I was
-|shocked to see a message from a compiler saying "END PROGRAM missing" as
-|opposed to "wrong argument type in level of indirection." Anyways, I don't
-|get any output here. EC
-|

Elijah Cardon

2006-10-03, 7:02 pm


"Herman D. Knoble" <SkipKnobleLESS@SPAMpsu.DOT.edu> wrote in message
news:44k4i25hvmffcg83t59imrc5mgoedt1052@
4ax.com...
> Elijah: There is an END statement in the code. I can't imagine what
> happened for you to miss it.
>
> Regards.
> Skip
> -|
> -|"Herman D. Knoble" <SkipKnobleLESS@SPAMpsu.DOT.edu> wrote in message
> - |news:v452i21t7fi0vr2dgpkpbg984kc94p36bn
@4ax.com...
> -|> Elijah: I may misunderstand your question. But I think the code at:
> -|> http://ftp.cac.psu.edu/pub/ger/fortran/hdk/loopnest.f90
> -|> will make available n loop indices of variable lengths.

<snip>
> -|>
> -|> Skip Knoble
> -|>
> -|!------ and use them here (or output them) one set at a time.
> -|shocked to see a message from a compiler saying "END PROGRAM missing" as
> -|opposed to "wrong argument type in level of indirection." Anyways, I
> don't
> -|get any output here. EC

I selected with the mouse and probably missed the tail of it. Since I've
got output with beliavsky's implementation and lack maturity with syntax and
IDE, I think I'm well advised to focus on it. Am I right to think that I
can pull the subroutine out like this and ask sensical questions about
execution?
subroutine next_in_grid(imax,ivec)
! find the next vector in an N-dimensional grid (do loop)
integer, intent(in) :: imax(:)
integer, intent(in out) :: ivec(:)
integer :: i,n
n = size(imax)
if (size(ivec) /= n) stop "in next_in_grid, need size(imax) =size(ivec)"
do i=1,n
if (ivec(i) < imax(i)) then
ivec(i) = ivec(i) + 1
exit
else
ivec(i) = 1
end if
end do
end subroutine next_in_grid
! end source
Q1) What do the following tokens mean in the above context? {:: , : , /=}

Q2) Can someone talk through the execution? EC


Richard E Maine

2006-10-03, 7:02 pm

Elijah Cardon <invalid@invalid.net> wrote:

> Q1) What do the following tokens mean in the above context? {:: , : , /=}


For questions at that level, I really strongly recommend you buy one of
the several fine textbooks on the language. The textbook authors already
went to the trouble of writing that stuff down. It seems like a waste of
effort to recraft the words again for every individual learner. The
newsgroup is fine for many things, but it is a supplement to the
textbookss, not a replacement for them. As a replacement it would be
darned inefficient and a step backwards.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
Craig Powers

2006-10-03, 7:02 pm

Elijah Cardon wrote:

> subroutine next_in_grid(imax,ivec)
> ! find the next vector in an N-dimensional grid (do loop)
> integer, intent(in) :: imax(:)
> integer, intent(in out) :: ivec(:)
> integer :: i,n
> n = size(imax)
> if (size(ivec) /= n) stop "in next_in_grid, need size(imax) =size(ivec)"
> do i=1,n
> if (ivec(i) < imax(i)) then
> ivec(i) = ivec(i) + 1
> exit
> else
> ivec(i) = 1
> end if
> end do
> end subroutine next_in_grid
> ! end source


> Q1) What do the following tokens mean in the above context? {:: , : , /=}


:: separates the attributes being declared (type and intent for the
dummy arguments, type for the local variables) from the entity for which
they are being declared.

imax(:) means that imax is an assumed-shape array of rank 1.

/= is a synonym for .NE., it's the binary not-equal operator.
Elijah Cardon

2006-10-04, 7:01 pm


<beliavsky@aol.com> wrote in message
news:1159916965.140211.36870@k70g2000cwa.googlegroups.com...
> Elijah Cardon wrote:
>
>
> Fortran 95/2003 Explained, by Metcalf, Reid, and Cohen. A much shorter
> summary of the language, primarily by Metcalf, is at
> http://en.wikipedia.org/wiki/Fortran_language_features .

The Golden Plates look a little expensive to me. Did I see right that they
want north of a hundred bucks? I'll chase down that link. EC


Richard E Maine

2006-10-04, 7:01 pm

Elijah Cardon <invalid@invalid.net> wrote:

> <beliavsky@aol.com> wrote in message
> news:1159916965.140211.36870@k70g2000cwa.googlegroups.com...


[color=darkred]
> The Golden Plates look a little expensive to me. Did I see right that they
> want north of a hundred bucks?


Eh? Amazon shows the list price as $60. As technical books go, that's
fairly low these days.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
Elijah Cardon

2006-10-05, 8:00 am


<beliavsky@aol.com> wrote in message
news:1159794025.210022.218810@b28g2000cwb.googlegroups.com...
> Elijah Cardon wrote:

[bla bla bla]
> If one wants the equivalent of an N-dimensional loop over integer
> ranges, say 1 to imax(1), 1 to imax(2), ..., 1 to imax(N), one can
> represent the "state" as an integer with N elements
> ii(1:N). The main task is to write code that produces the state
> following the current one.
> The code below does so, assuming the left-most element of the state
> vector changes first.
> The slightly tricky case to handle is when ii(1) = imax(1), in which
> case one sets ii(1) to 1 and finds the N-1 dimensional state following
> ii(2:N).
>
> module loop_grid_mod
> implicit none
> private
> public :: next_in_grid
> contains
> subroutine next_in_grid(imax,ivec)
> ! find the next vector in an N-dimensional grid (do loop)
> integer, intent(in) :: imax(:)
> integer, intent(in out) :: ivec(:)
> integer :: i,n
> n = size(imax)
> if (size(ivec) /= n) stop "in next_in_grid, need size(imax) =
> size(ivec)"
> do i=1,n
> if (ivec(i) < imax(i)) then
> ivec(i) = ivec(i) + 1
> exit
> else
> ivec(i) = 1
> end if
> end do
> end subroutine next_in_grid
> end module loop_grid_mod
>
> program xloop_grid
> ! driver for next_in_grid
> use loop_grid_mod, only: next_in_grid
> implicit none
> integer, parameter :: n = 3, imax(n) = (/2,4,3/)
> integer :: i,icheck,ivec(n),iprod(n)
> ivec = 1
> iprod(1) = 1
> do i=2,n
> iprod(i) = iprod(i-1)*imax(i-1)
> end do
> do i=1,product(imax)
> icheck = 1 + dot_product(ivec-1,iprod)
> write (*,"(100i6)") i,icheck,ivec
> call next_in_grid(imax,ivec)
> end do
> end program xloop_grid

<snip>
It would seem that dot_product comes with the food in fortran. Did I get
that by having written the first statement as:
module loop_grid_mod
as opposed to
module any_old_name
?

If this day goes anything like I hope, I'll have M&R by day's end. EC


beliavsky@aol.com

2006-10-05, 8:00 am


Elijah Cardon wrote:

<snip>

> <snip>
> It would seem that dot_product comes with the food in fortran. Did I get
> that by having written the first statement as:
> module loop_grid_mod
> as opposed to
> module any_old_name
> ?


No, dot_product is an intrinsic function introduced in Fortran 90. As
the line

use loop_grid_mod, only: next_in_grid

indicates, the only think imported from loop_grid_mod to the main
program is next_in_grid.

>
> If this day goes anything like I hope, I'll have M&R by day's end. EC


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com