Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Implicit array lengths

WSinc
New Contributor I
828 Views
0 Kudos
2 Replies
WSinc
New Contributor I
828 Views

I wish to pass an array length implicitly, for example:

real  A(12,12),avg

call average(a(3:8,5),avg)

in this case AVERAGE would take the average of 6 elements of column 5.

Or we could have

real c(5)

call average(c,avg)

Where we take the average of all 5 elements of array C.

How would one do this without having to SAY HOW MANY elements to process?

Actually AVERAGE could be a function, and I suppose there is one already in the math library.

This is just an example - - - 

0 Kudos
Steven_L_Intel1
Employee
828 Views

This is done using assumed-shape arrays, which require an explicit interface. An example:

module myfuncs
contains
subroutine average (a,avg)
real, intent(in) :: a(:,:)
real, intent(out) :: avg
avg = sum(array)/real(size(array))
end subroutine average
end module myfuncs

You would then add "use myfuncs" to make the declaration visible. Note that this example requires a two-dimension array. In order to allow arrays of other dimensions, you'd need to define one routine for each number of dimensions and then have a generic declaration to allow the compiler to pick.

Fortran 2015 will have a new feature called assumed-rank where you can have one routine accept an array of any rank (number of dimensions), but there are severe restrictions on what you can do with the argument when you get it. We don't support this feature yet.

0 Kudos
Reply