0

I'm trying to check if an 1D array of integers A contains or not, at every one of it's size(A) positions, any of the elements of the set of integers S (also a 1D array), with the general case of size(S) > 1.

The easy and obvious way is to do the following nested loop:

DO i = 1, size(A)
   DO j = 1, size(S)
        IF(A(i) == S(j)) ** do something **
   ENDDO 
ENDDO

The problem is that, for large arrays A and S, this process is very inefficient. Is there an intrinsic FORTRAN subroutine or function that does this faster? Or any other method?

I've tried to do the following, but it doesn't want to compile:

DO i = 1, NNODES
   IF(A(i) == ANY(S)) ** do something **
ENDDO

The error message that appears is the following: "error #6362: The data types of the argument(s) are invalid." I'm using VS2010 with Intel Parallel Studio 2013.

André Almeida
  • 379
  • 1
  • 4
  • 11

2 Answers2

5

The expression

A(i) == ANY(S)

has an integer on the lhs and a logical on the rhs. We'll have none of that C-inspired nonsense of regarding those as comparable types in Fortran thank you very much. Actually, it's worse than that, any returns a logical but takes an array of logicals on input, so any(array_of_int) won't compile.

You could try

ANY(S==A(i))

instead. That should give you a compilable solution.

Now, as for efficiency, you're first snippet is O(n^2). You can do better, asymptotically. Sort both arrays and scan them in tandem, which is O(n + n log n) or something similar. If you need help coding that up, update your question, though I suspect it's already been asked and answered here on SO.

I strongly suspect, and you can check if you care to, that using any inside a single (explicit) loop will also be O(n^2) -- since any has to operate on the most general cases I can't see any realistic alternative to it scanning the array -- another loop in other words.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
0

In addition to High Performance Mark's answere, when you scan the sorted arrays you can, and should, use a binary search algorithm.

wamster
  • 171
  • 12
  • 1
    It is not that simple http://www.pvk.ca/Blog/2012/07/30/binary-search-is-a-pathological-case-for-caches/ – Vladimir F Героям слава Apr 15 '15 at 07:31
  • What the algorithm I outlined does is scan through two arrays in tandem. The operation at each step of the scan is *find next highest value* not *find value n*. I'm not convinced that even in theory a binary search would be faster. In practice, I'll eat my hat if anyone can convince me, with data, that it is. – High Performance Mark Apr 15 '15 at 09:34