Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Position of Sign change in long list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg18730] Re: [mg18691] Position of Sign change in long list
  • From: "David Park" <djmp at earthlink.net>
  • Date: Sat, 17 Jul 1999 02:36:47 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Martin,

Here is a better routine, than my first try, for finding the first sign change in a
list. It samples a shorter list drawn from the longer list to try to find an early
sign change, then truncates the longer list.

signtest3[list_, nmax_] := Module[{len = Length[list], n, samplelist,
    len2, shortlist},
   samplelist = Reverse[NestList[Floor[#1/2] & , len,
       Min[nmax, Floor[-(Log[2] - Log[len])/Log[2]]]]];
    len2 = Length[samplelist];
    shortlist =  Flatten[{list[[1]], Table[list[[samplelist[[i]]]],  {i, 1, len2}]}];
     n = With[{st = Sign[shortlist]}, Length[Split[st][[1]]]];
    With[{st = Sign[Take[list, samplelist[[n]]]]},
     Length[Split[st][[1]]]]]

I compared this with your second routine on the following test cases:

test1 = Table[Sin[Pi i/20000. + 1], {i, 40000}];
test2 = Table[Sin[Pi i/40000. + 1], {i, 80000}];
test3 = Table[Sin[Pi i/40000. + 2], {i, 80000}];
test4 = Table[Sin[Pi i/40000. - 0.05], {i, 80000}];
test5 = Table[Sin[Pi i/40000. + 1] + Random[Real, {-1, 1}], {i, 80000}];

The timings were, on a Pentium 166 MH, in seconds with the above routine second:

test1   0.11    0.05
test2   0.16    0.11
test3   0.50    0.05
test4   0.22    0
test5   0.33    0

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/

>I have a long list of data points and need to find the position where the
>sign changes for the first time.
>My first attempt was
>
>test=Table[Sin[Pi i/5000. +1],{i,10000}];
>
>With[{a=Abs[test]},Position[a,Min[a]]]
>
>which works, but is slow. It turns out that Min is relatively slow and
>avoiding it can save time. The following is almost a factor 4 faster:
>
>With[{st=Sign[test]},Length[Split[st][[1]]]]
>
>If you can think of a still faster or more elegant solution please let me
>know!
>
>Martin
>




  • Prev by Date: Re: Cumulative distribution of Gauss
  • Next by Date: Help
  • Previous by thread: Re: Position of Sign change in long list
  • Next by thread: Re: Position of Sign change in long list