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

Array Stride Question

Li_Dong
Beginner
452 Views

Hi, all,

I have declared an array as:

integer a(1.5:2.5)

which will create an array with two elements with subscripts 1.5 and 2.5 respectively, and the stride is 1.So I wonder if I can declare an array with stride 0.5. This can help me create a more readable code.

Best regards,

DONG Li

0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
452 Views

The REAL will be converted to integer on the declaration of the array (or report error, requiring integer index).

If you want stride of 0.5 then "declare" in software that the indexes represent 0.5 increments. Then index via function

ingeger function indexByHalves(rIndex)
real :: rIndex
return int(rIndex*2)
end function indexByHalves

You can rename the function if you find it more suitable.

Note, you can also add a bias. e.g. Assume you want to tally a count of temperature measurements in half degree increments through a temperature range of -200:+200 with an array of 801 cells. You would pass in the temperature, add 200, multiply by 2, INT and return the resultant index (with fencing to limit index to within range).

Jim Dempsey

0 Kudos
Li_Dong
Beginner
452 Views

Hi Jim,

Thanks for reply. It is a good idea to use a wrapper index function. I will try it out!

Cheers,

DONG Li

0 Kudos
jimdempseyatthecove
Honored Contributor III
452 Views

Place the code in a module such that your main line code looks relatively clean. Consider using GENERIC INTERFACES such that you can use the same function name with different arguments. Something like

YourOutput = YourFunction(x) ! read at x

YourOutput = YourFunction(x,y) ! write Y into x, then return value at x (which may or may not be Y)

Unfortunately you cannot place a function on the left side of an equation

Jim Dempsey

0 Kudos
Reply