Array intrinsics with zero-size array argument
Author Message
Array intrinsics with zero-size array argument

What are the array intrinsics SUM, DOT_PRODUCT,MATMUL supposed to
return if one of the arguments is a zero-sized array, e.g. a(1:0)?

Al

Wed, 19 Aug 2009 01:37:33 GMT
Array intrinsics with zero-size array argument

Quote:
> What are the array intrinsics SUM, DOT_PRODUCT,MATMUL supposed to
> return if one of the arguments is a zero-sized array, e.g. a(1:0)?

Besides the standard, one place to look is the fortran 95 Handbook. On
p630 it says "The result of SUM (ARRAY) [...] has the value zero if
ARRAY has size zero and on p580 it says in similar language that
dot_product(x,y) = 0 if size(x) = size(y) = 0. On p599 it defines the
results of MATMUL using the SUM function and does not make exceptions
for arrays with zero-sized dimensions.
Thus the program

program xmatmul_zero_size
integer, parameter :: n1=2,n2=0,n3=3
integer            :: imat(n1,n2),jmat(n2,n3)
imat = 0
jmat = 0
print*,shape(matmul(imat,jmat))
print*,matmul(imat,jmat)
end program xmatmul_zero_size

gives

2 3
0 0 0 0 0 0

on g95, gfortran, and Intel Visual Fortran.

Wed, 19 Aug 2009 02:02:10 GMT
Array intrinsics with zero-size array argument

Quote:
> What are the array intrinsics SUM, DOT_PRODUCT,MATMUL supposed to
> return if one of the arguments is a zero-sized array, e.g. a(1:0)?

SUM:
"The result of SUM (ARRAY) has a value equal to a processor-dependent
approximation to the sum of all the elements of ARRAY or has the value
zero if ARRAY has size zero."

MATMUL:
Well, this returns an array: If you do matmul(a(1:0),a(1:0) you simply
get an array of zero size.

DOT_PRODUCT:
"If VECTOR A is of type integer or real, the result has the value SUM
(VECTOR A*VECTOR B). If the vectors have size zero, the result has the
value zero."

See Fortran standard. Quotes taken from the Fortran 2003 standard.

Tobias

Wed, 19 Aug 2009 02:04:14 GMT
Array intrinsics with zero-size array argument

Quote:
> What are the array intrinsics

SUM(a(1:0)) is 0,

DOT_PRODUCT(a(1:0)) is 1,

MATMUL is defined in terms of SUM and so would return 0.

Regards,

Mike Metcalf

Wed, 19 Aug 2009 02:11:22 GMT
Array intrinsics with zero-size array argument

Quote:

> DOT_PRODUCT(a(1:0)) is 1,

Sorry, I read this as PRODUCT. DOT_PRODUCT is defined in terms of SUM too.

Regards,

Mike Metcalf

Wed, 19 Aug 2009 02:20:18 GMT
Array intrinsics with zero-size array argument
Quote:

>> DOT_PRODUCT(a(1:0)) is 1,

> Sorry, I read this as PRODUCT. DOT_PRODUCT is defined in terms of SUM too.

> Regards,

> Mike Metcalf

Rather than try to memorize the list of zero sized values, I think
it's easier to remember where they come from.  Think about the
underlying DO loop that does the calculation.  It's always
something like:
result = obvious starting value
Do I = 1, size(array)
result = result .operation. array(i)
enddo

For SUM the opvious starting value is zero, for the AND
reduction, the obvious starting value is .TRUE., etc.

This works for all of the reduction functions, not just the few
arithmetic ones mentioned.

Dick hendrickson

Wed, 19 Aug 2009 02:58:44 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages