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: [mg18718] Re: [mg18691] Position of Sign change in long list
  • From: "David Park" <djmp at earthlink.net>
  • Date: Sat, 17 Jul 1999 02:36:39 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Martin,

You definitely don't want to use the Abs and Min test. It will not always find the
first change of sign. For example, it finds the second change of sign with

test3 = Table[Sin[Pi i/20000. + 1], {i, 40000}];

Your second routine is probably not too bad for efficiency. The following routine
works much faster if an early change of sign can be detected. The success of this
will depend upon the smoothness of the points or luck.

signtest[list_] := Module[{len = Length[list], n,
    sign1}, n = Floor[len/32]; sign1 = Sign[list[[1]]];
    While[Sign[list[[n]]] == sign1 && n < len, n *= 2];
    With[{st = Sign[Take[list, n]]},
     Length[Split[st][[1]]]]]

But we won't know the most efficient routine until we hear from Carl Woll and a few
others.

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: best line through a set of 3D points
  • Next by Date: Re: Cumulative distribution of Gauss
  • Previous by thread: Re: Position of Sign change in long list
  • Next by thread: Re: Position of Sign change in long list