       Re: Position of Sign change in long list

• To: mathgroup at smc.vnet.net
• Subject: [mg18709] Re: [mg18691] Position of Sign change in long list
• From: "Wolf, Hartmut" <hwolf at debis.com>
• Date: Sat, 17 Jul 1999 02:36:33 -0400
• Organization: debis Systemhaus
• References: <199907150546.BAA15968@smc.vnet.net.>
• Sender: owner-wri-mathgroup at wolfram.com

```Hello Martin,

Martin Rommel schrieb:
>
> 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][]]]
>
> If you can think of a still faster or more elegant solution please let me
> know!
>

The timings I found on your data "test" were:

In:= With[{a=Abs[test]},Position[a,Min[a]]] //Timing
Out= {0.651 Second,{{3408}}}
-- is that too slow??

In:= With[{st=Sign[test]},Length[Split[st][]]] //Timing
Out= {0.17 Second,3408}

whereas e.g.

In:= scata[l_]:=
Module[{s=Sign[l[]],n=0},Catch[If[Sign[#]=!=s,Throw[n],n++]& /@ l]]
In:= scata[test]//Timing
Out= {0.5 Second,3408}

In:= ff[s0_,x_]:=If[Sign[x]=!=s0,Throw[n],n++;s0]
In:= Block[{l=test,n=0},
Catch[Fold[ff,Sign[[]],l]]  ]//Timing
Out= {0.631 Second,3408}

So your second algorithm looks superior. However I have found another
one, which gives:

In:= scBS[test]//Timing
Out= {0. Second,3408}

up another sample of test data:

In:= Length[test2]
Out= 10000

With that let's repeat:

In:= With[{st=Sign[test2]},Length[Split[st][]]] //Timing
Out= {0.211 Second,4}
-- not quite as good as before

In:= scata[test2]//Timing
Out= {0.07 Second,4}
-- *much* better than before

In:= Block[{l=test2,n=0},
Catch[Fold[ff,Sign[[]],l]]  ]//Timing
Out= {0.07 Second,4}
-- also much better

but!!!!!!

In:= With[{a=Abs[test2]},Position[a,Min[a]]] //Timing
Out= {0.881 Second,{{4259}}}

and "mine" too

In:= scBS[test2]//Timing
Out= {0. Second,8318}
-- again lightning fast,
-- but also ***wrong***

Now, why did you consider your "Min"-algorithm at all in first place?

I believe, thats why your problem is *not* "need to find the position
where the sign changes for the first time" but more something like: "If
there is only one zero-crossing in the sample, then find it." For *that*
problem, your "Min"-algorithm is still not quite correct (there is doubt
if the point is the last before or the first after the crossing -- but
you can easily fix that), but *then* you can also use "mine":

In:= scBS[lis_]:=
Module[{s1=Sign[lis[]],l=1,r=Length[lis],m},
While[l=!=r,
If[Sign[lis[[m=Floor[(l+r)/2]]]]=!=s1,r=m,l=m+1]
];
If[Sign[lis[[l]]]=!=s1,l-1,"no sign change"]
]

which is O[log n], whereas your first problem can't be solved better
than O[n].

What was my sample test2?

In:= test2=Table[Random[]-0.5,{10000}];

Perhaps your problem isn't quite "If there is only one zero-crossing..."
but obviously the data come from some physical or computational process
which is not completely random. So perhaps you can make assumptions of
your data, e.g. there is a maximum possible frequency in them. In that
case you may scan your data with binary searches on parts not longer
than the "wavelength", beginning at the left, and stopping after having
found the first zero crossing.

The general rule is: put all your knowledge of the data into the
algorithm, and you will get *your* fastest ever possible.

What about elegance? Elegant solutions come from very simple
assumptions. So your "Split"-algoritm is very elegant, not always best,
but possibly best for that pre-knowledge of the data you have.

Kind regards, hw

```

• Prev by Date: Re: Cumulative distribution of Gauss
• Next by Date: Re: LaTex on a Mac
• Previous by thread: Re: Position of Sign change in long list
• Next by thread: Re: Position of Sign change in long list