       Re: recode procedural algorithm to faster functional module

• To: mathgroup at smc.vnet.net
• Subject: [mg44059] Re: [mg44034] recode procedural algorithm to faster functional module
• From: "Peter Pein" <nospam at spam.no>
• Date: Sun, 19 Oct 2003 01:11:20 -0400 (EDT)
• References: <200310180712.DAA28079@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hi Robert,
I renamed your function procConcurrence and tried the following:

In:=
procConcurrence[v1_List, v2_List] := Module[{c = 0},
Do[Do[Which[
v1[[i]][] == v2[[k]][], c++,
v1[[i]][] < v2[[k]][] && v1[[i]][] > v2[[k]][], c++,
v1[[i]][] > v2[[k]][] && v1[[i]][] < v2[[k]][], c++],
{i, Length[v1], 1, -1}], {k, Length[v2], 1, -1}]; c]

In:=
(* This function replaces every element of v1 by a boolean list,
which indicates whether the el. of v1 overlaps corresponding
elements of v2. Then all the 'True's are counted.
*)
Concurrence[v1_List, v2_List] := Count[
v1 /. {a_?AtomQ, b_} ->
(v2 /. {c_?AtomQ, d_} -> (a <= c < b || c < a < d)),
True, 2]

In:=
(*Test *)
#[{{1, 2}, {4, 5},     {7, 8},     {9.2, 9.3}, {11.1, 11.2}},
{{1, 2}, {4.1, 4.9}, {7.2, 8.2}, {9, 10},    {11.3, 11.4}}] & /@
{Concurrence, procConcurrence}

Out=
{4, 4}

In:=
(#1[{{1, 3.5}}, {{2, 3}, {4, 5}}] & ) /@ {Concurrence, procConcurrence}

Out=
{1, 1}

In:=
v1 = Table[{i - Random[], i + Random[]}, {i, 100}];
v2 = Table[{i - Random[], i + Random[]}, {i, 1000}];

In:=
(Timing[#1[v1, v2]] & ) /@ {Concurrence, procConcurrence}
(Timing[#1[v2, v1]] & ) /@ {Concurrence, procConcurrence}

Out=
{{3.435 Second, 202}, {21.14 Second, 202}}
Out=
{{3.405 Second, 202}, {21.111 Second, 202}}

...and you're right: it _is_ faster :-))

Peter

--
Peter Pein, Berlin
petsie at arcAND.de
replace && by || to write to me

----- Original Message -----
From: "Prince-Wright, Robert G SEPCO" <robert.prince-wright at shell.com>
To: mathgroup at smc.vnet.net
Subject: [mg44059] [mg44034] recode procedural algorithm to faster functional module

>
> and hopefully runs a lot faster. The Lists V1 and V2 represent two time
> series with 'bricks' laid out along them. The Bricks have varying length
> set by v[[i]][] and v[[i]][] and the idea is to count the number
> of times there is an overlap. I can only see the dumb procedural way of
> taking each brick and checking if it overlaps in time with another!
>
>
>
> Concurrence[v1_List, v2_List,nsim_]:=Module [
>
> {k=1,c=0},
> Do[
> Do[
> Which[
> v1[[i]][] == v2[[k]][], c=c+1,
> v1[[i]][] < v2[[k]][] && v1[[i]][] > v2[[k]][], c=c+1,
> v1[[i]][] > v2[[k]][] && v1[[i]][] < v2[[k]][], c=c+1
> ];
> (*Write[ strm1, {v1[[i]][],v2[[i]][],c}];*)
> , {i,1,nsim}
> ], {k,1,nsim}
> ];
> c
> ]
>
> I've have a PowerPoint illustration if this is unclear and can e-mail it.
>
> thanks
> Robert Prince-Wright
>

```

• Prev by Date: Re: Problem with Sum
• Next by Date: Re: What Happens to Garbage in Mathematica?
• Previous by thread: recode procedural algorithm to faster functional module
• Next by thread: Re: Re: recode procedural algorithm to faster functional module