Prev: buy Tramadol overnight COD, buy cod day next Tramadol, Buy order online Tramadol 180!
Next: calling a recursive function in fortran 77 ?
From: Ragu on 24 Mar 2010 22:56 I am trying to code whether all non-zero values of a DIM=1 array are equal. My intention is to avoid writing a O(n**2) loop. I thought of using intrinsics pack() and any(). However, I cant seem to get past the first step. I am not able to select the first element of the array returned by pack(). I tried the following but could not get it to compile. tempval = pack(array, MASK=(array/=0))(1) I am trying to do something like if(any(pack(array,MASK = (array /=0)) /= (pack(array,MASK = (array / =0))(1)))) [If any element of the array with mask are not equal to the first element of array with mask] Any help or suggestion to help me past the first step is appreciated.
From: steve on 24 Mar 2010 23:02 On Mar 24, 7:56 pm, Ragu <ssragun...(a)gmail.com> wrote: > I am trying to code whether all non-zero values of a DIM=1 array are > equal. My intention is to avoid writing a O(n**2) loop. I thought of > using intrinsics pack() and any(). However, I cant seem to get past > the first step. I am not able to select the first element of the array > returned by pack(). I tried the following but could not get it to > compile. > > tempval = pack(array, MASK=(array/=0))(1) > > I am trying to do something like > > if(any(pack(array,MASK = (array /=0)) /= (pack(array,MASK = (array / > =0))(1)))) > [If any element of the array with mask are not equal to the first > element of array with mask] > > Any help or suggestion to help me past the first step is appreciated. If you want any help, it would be prudent to post a complete, self-contained piece of code. -- steve
From: Ragu on 24 Mar 2010 23:21 On Mar 24, 11:02 pm, steve <kar...(a)comcast.net> wrote: > On Mar 24, 7:56 pm, Ragu <ssragun...(a)gmail.com> wrote: > > > > > I am trying to code whether all non-zero values of a DIM=1 array are > > equal. My intention is to avoid writing a O(n**2) loop. I thought of > > using intrinsics pack() and any(). However, I cant seem to get past > > the first step. I am not able to select the first element of the array > > returned by pack(). I tried the following but could not get it to > > compile. > > > tempval = pack(array, MASK=(array/=0))(1) > > > I am trying to do something like > > > if(any(pack(array,MASK = (array /=0)) /= (pack(array,MASK = (array / > > =0))(1)))) > > [If any element of the array with mask are not equal to the first > > element of array with mask] > > > Any help or suggestion to help me past the first step is appreciated. > > If you want any help, it would be prudent > to post a complete, self-contained piece > of code. > > -- > steve I agree. But I thought that I am asking for help on a 101 level question that does not need any further explanation. I cooked a test program that shows my intentions. program array101 integer :: tempint, ii, jj integer, dimension(4) :: array1 = (/1000, 0, 0, 2000/) ! Code to remove zero values from array1 and test whether other values are equal outer: do ii = 1, 4 if(array1(ii) == 0) cycle outer inner: do jj = ii+1, 4 if(array1(jj) == 0) cycle inner if(array1(ii) /= array1(jj)) then write(*,*) 'MESSAGE: NON ZERO VALUES OF ARRAY1 ARE NOT EQUAL' exit outer endif end do inner ! jj end do outer ! ii ! Lets try to learn fancier intrinsics !tempval = pack(array, MASK=(array/=0))(1) ! OOPS. I can't get the above line to compile !if(any(pack(array,MASK = (array /=0)) /= (pack(array,MASK = (array / !=0))(1)))) then ! write(*,*) 'MESSAGE: NON ZERO VALUES OF ARRAY1 ARE NOT EQUAL' !endif end program array101 Thanks.
From: Ragu on 24 Mar 2010 23:24 On Mar 24, 11:21 pm, Ragu <ssragun...(a)gmail.com> wrote: > On Mar 24, 11:02 pm, steve <kar...(a)comcast.net> wrote: > > > > > On Mar 24, 7:56 pm, Ragu <ssragun...(a)gmail.com> wrote: > > > > I am trying to code whether all non-zero values of a DIM=1 array are > > > equal. My intention is to avoid writing a O(n**2) loop. I thought of > > > using intrinsics pack() and any(). However, I cant seem to get past > > > the first step. I am not able to select the first element of the array > > > returned by pack(). I tried the following but could not get it to > > > compile. > > > > tempval = pack(array, MASK=(array/=0))(1) > > > > I am trying to do something like > > > > if(any(pack(array,MASK = (array /=0)) /= (pack(array,MASK = (array / > > > =0))(1)))) > > > [If any element of the array with mask are not equal to the first > > > element of array with mask] > > > > Any help or suggestion to help me past the first step is appreciated. > > > If you want any help, it would be prudent > > to post a complete, self-contained piece > > of code. > > > -- > > steve > > I agree. But I thought that I am asking for help on a 101 level > question that does not need any further explanation. I cooked a test > program that shows my intentions. > > program array101 > integer :: tempint, ii, jj > integer, dimension(4) :: array1 = (/1000, 0, 0, 2000/) > > ! Code to remove zero values from array1 and test whether other values > are equal > outer: do ii = 1, 4 > if(array1(ii) == 0) cycle outer > inner: do jj = ii+1, 4 > if(array1(jj) == 0) cycle inner > if(array1(ii) /= array1(jj)) then > write(*,*) 'MESSAGE: NON ZERO VALUES OF ARRAY1 ARE NOT EQUAL' > exit outer > endif > end do inner ! jj > end do outer ! ii > > ! Lets try to learn fancier intrinsics > !tempval = pack(array, MASK=(array/=0))(1) > ! OOPS. I can't get the above line to compile > !if(any(pack(array,MASK = (array /=0)) /= (pack(array,MASK = (array / > !=0))(1)))) then > ! write(*,*) 'MESSAGE: NON ZERO VALUES OF ARRAY1 ARE NOT EQUAL' > !endif > end program array101 > > Thanks. The bottom part should be array1 for the variable. ! Lets try to learn fancier intrinsics tempint = pack(array1, MASK=(array1/=0))(1) ! OOPS. I can't get the above line to compile if(any(pack(array1,MASK = (array1 /=0)) /= (pack(array1,MASK = (array1 /=0))(1)))) then write(*,*) 'MESSAGE: NON ZERO VALUES OF ARRAY1 ARE NOT EQUAL' endif
From: glen herrmannsfeldt on 24 Mar 2010 23:36
Ragu <ssragunath(a)gmail.com> wrote: > I am trying to code whether all non-zero values of a DIM=1 array are > equal. My intention is to avoid writing a O(n**2) loop. I thought of > using intrinsic?s pack() and any(). Why would it be O(n**2)? All that I can think of are O(n). > However, I can?t seem to get past > the first step. I am not able to select the first element of the array > returned by pack(). I tried the following but could not get it to > compile. > tempval = pack(array, MASK=(array/=0))(1) Yes, you can't do that. You can subscript arrays but not array expressions. > I am trying to do something like > if(any(pack(array,MASK = (array /=0)) /= (pack(array,MASK = (array / > =0))(1)))) Assuming it was legal, that one might be O(n**2), but in any case likely slower than a single loop through the array. Note that it takes one pass through the array to generate the mask. (Maybe two, as you do it twice.) Another to do the comparison. Maybe another for the second pack. Also, pack has a good chance of needing a temporary array. The only way I know to actually do this is with a user generated temporary array. L=SIZE(pack(array,mask=(array.ne.0)) if(L.eq.0) (do something else) allocate (temp(L)) temp=pack(array,mask=(array.ne.0)) if(any(temp.ne.temp(1)) ... deallocate (temp) Likely four passes through the array, with lots of compares done on each pass. But a nice DO loop with a flag to indicate if a non-zero element has been found yet. You should special case it at the end if there are no non-zero elements in the while array. > [If any element of the array with mask are not equal to the first > element of array with mask] -- glen |