MathGroup Archive 2005

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

Search the Archive

Re: Speeding up Vector Comparison

  • To: mathgroup at smc.vnet.net
  • Subject: [mg61437] Re: Speeding up Vector Comparison
  • From: "Carl K. Woll" <carlw at u.washington.edu>
  • Date: Wed, 19 Oct 2005 02:16:16 -0400 (EDT)
  • Organization: University of Washington
  • References: <dj275j$bhn$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Lee Newman wrote:
> Dear Group,
>
> I have a compiled function that compares two vectors by computing the
> mean of the absolute value of the difference between the two vectors -- 
> subject to the constraint that if both sub-elements of the vector are
> less then a threshold value, they do not contribute to the difference
> (see example below).  The function gets executed about 100million times
> over the course of a simulation.  I've optimized it as much as I can,
> but it is still a bottleneck (~7 hrs).  Can anyone see an obvious way to
> rewrite my function (below) so that it runs faster?
>
> Example (the actual vectors I am using have length=100):
> v1 = {0.9, 0.9, 0.1, 0.1};
> v2 = {0.5, 0.1, 0.5, 0.1};
> threshold=0.5
> for these two vectors, I want to compute the mean of the absolute value
> of the difference between the two vectors, ignoring the last position
> because the last value in *both* vectors is < threshold.
>
> My Function
>
> MeanDiff = Compile[{{vector1, _Real,1}, {vector2, _Real, 1}, {threshold,
> _Real}},
>       Mean@ Abs[Plus @@@ Select[Transpose[{vector1, -vector2}], Max[Abs
> /@ #] > threshold &]]
>       , {{Mean[_], _Real}}];
>
> Note:  I use Plus rather than Subtract because Mathematica doesn't allow
> Subtract with @@@ in compiled functions.  The third argument to Compile
> seems to be necessary, as without it Mathematica complains that the 
> function
> doesn't return a tensor of size 1.
>
> Thanks for any suggestions,
> Lee Newman
>

Here is one possibility:

md2 = Compile[{{v1, _Real, 1}, {v2, _Real, 1}, {t, _Real}},
Module[{valid},
valid = Sign[UnitStep[v1 - t] + UnitStep[v2 - t]];
(valid.Abs[v1 - v2])/Tr[valid]]
]

In my tests it's about an order of magnitude faster than your function, and 
about twice as slow as doing simply Abs[v1-v2].

Carl Woll
Wolfram Research 



  • Prev by Date: Re: interactive web Mathematica examples on Math World not working under firefox?
  • Next by Date: Re: Stylesheets vs. DTDs or XML Schemas
  • Previous by thread: Re: Speeding up Vector Comparison
  • Next by thread: Double integral of a piecewise-constant function