I pass an optional mask to a subroutine. Within this routine I then have many statements of the kind
...
if (present(Mask)) then
where(Mask)
y(:) = A(:)*B(:)
end where
else
y(:) = A(:)*B(:)
end if
...
This is very ugly in regards of code duplication. Are there any advices to do this in a better way? I have many statements like this in my code and I need to perform the operations on whole arrays.
Edit: At least a part of the problem can be solved through elemental functions. Because I have the same operations on the right side I can write
...
if (present(Mask)) then
where(Mask)
y(:) = multiply(A(:),B(:))
end where
else
y(:) = multiply(A(:),B(:))
end if
...
elemental real function multiply(a,b)
real, intent(in) :: a,b
multiply = a*b
end function
This way, I have at least a single place for the operations.