Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5285
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: A fast way to compare two vectors |
| Date | 2011-09-29 06:14 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <j612c9$m5i$1@smc.vnet.net> (permalink) |
| References | <j5m44t$ple$1@smc.vnet.net> <j5mt5u$smm$1@smc.vnet.net> |
On Wed, 28 Sep 2011 07:47:42 +0100, Yasha Gindikin <gindikin@gmail.com>
wrote:
> Dear Oleksandr, thank you for your kind explanations. I would be happy
> if the compiled function simply returned the values of diffs1 and
> diffs2. Assigning values to global variables is not obligatory for me,
> especially if it is time-consuming.
>
> Unfortunately, there are problems. Trying to calculate the compiled
> functio n hyperfastVectorCompare[a, b, 4] with a = {1, 2, 3, 4, 5} and b
> = {1, 2, 6, 7, 12} yields "CompiledFunction::cflist: Nontensor object
> generated; proceeding with uncompiled evaluation", with the returned
> values being {{3, 4, 5}, {3}}.
>
> It looks like that when local variables diffs1 and diffs2 have different
> length, the compiled function can not return those.
>
> Is there any chance for the compiled function to return the local
> variables of different length (diffs1, diffs2 and diffct), without
> assigning those to the global variables? Thank you!
>
My mistake was to implicitly assume that we would not run off the end of
one of the vectors with more than one difference remaining, and thus that
diffs1 and diffs2 would be of the same length for any even number of
requested differences. As your example demonstrates, this is not
necessarily the case.
Unfortunately, compiled functions are restricted to returning full-rank
tensors, so to return the correct result in this case we must either pad
the shorter vector or use an uncompiled function to catch the two lists of
differences and build the result. The latter is probably the better
solution in terms of not returning unexpected values in the list of
differences, and can be achieved with minor modifications:
compareCompiled = Compile[{
{vec1, _Integer, 1}, {vec2, _Integer, 1},
{MAXDIFFS, _Integer, 0}
},
Block[{
i1 = 1, i2 = 1, diffct = 0,
(* Bags occupy scalar registers; contents returned as vectors *)
b1 = Internal`Bag@Most[{0}], v1,
b2 = Internal`Bag@Most[{0}], v2
},
(* Run along the lists, recording differences as we go *)
While[
i1 <= Length[vec1] && i2 <= Length[vec2] && diffct < MAXDIFFS,
Which[
vec1[[i1]] < vec2[[i2]], diffct++; Internal`StuffBag[b1, i1++],
vec1[[i1]] > vec2[[i2]], diffct++; Internal`StuffBag[b2, i2++],
True, i1++; i2++
]
];
(* Fix up in case we ran off the end of one of the lists *)
While[i1 <= Length[vec1] && diffct < MAXDIFFS,
diffct++; Internal`StuffBag[b1, i1++];
];
While[i2 <= Length[vec2] && diffct < MAXDIFFS,
diffct++; Internal`StuffBag[b2, i2++];
];
(* Move bag contents into vector registers and clear bags *)
v1 = Internal`BagPart[b1, All]; b1 = 0;
v2 = Internal`BagPart[b2, All]; b2 = 0;
(* Return contents of vector registers in external variables *)
Evaluate[compare`d1 = v1; compare`d2 = v2;];
diffct
]
];
(* Wrapper function to catch returned values and build the result *)
With[{cc = compareCompiled},
compare[vec1_, vec2_, MAXDIFFS_] :=
Block[{compare`d1, compare`d2},
{cc[vec1, vec2, MAXDIFFS], compare`d1, compare`d2}
]
];
Now compare[{1, 2, 3, 4, 5}, {1, 2, 6, 7, 12}, 4] gives the expected
result of {4, {3, 4, 5}, {3}} without any errors (where the elements of
this list are {diffct, diffs1, diffs2}). However, this does effectively
involve the use of global variables and so there will be a (small)
performance penalty. Depending on the length of your vectors and how you
use the result, it may be better just to return all of the differences, in
which case the result will always be a full-rank tensor that can be
returned directly from the compiled code; Ray Koopman has already given
examples of functions that do this.
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A fast way to compare two vectors Yasha Gindikin <gindikin@gmail.com> - 2011-09-25 02:37 +0000
Re: A fast way to compare two vectors "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-09-25 09:44 +0000
Re: A fast way to compare two vectors "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-09-26 08:16 +0000
Re: A fast way to compare two vectors Yasha Gindikin <gindikin@gmail.com> - 2011-09-26 08:19 +0000
Re: A fast way to compare two vectors "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-09-27 00:08 +0000
Re: A fast way to compare two vectors Yasha Gindikin <gindikin@gmail.com> - 2011-09-28 06:47 +0000
Re: A fast way to compare two vectors "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-09-29 06:14 +0000
Re: A fast way to compare two vectors Ray Koopman <koopman@sfu.ca> - 2011-09-25 09:46 +0000
Re: A fast way to compare two vectors Ray Koopman <koopman@sfu.ca> - 2011-09-26 08:16 +0000
Re: A fast way to compare two vectors Yasha Gindikin <gindikin@gmail.com> - 2011-09-26 08:13 +0000
Re: A fast way to compare two vectors Ray Koopman <koopman@sfu.ca> - 2011-09-27 22:52 +0000
Re: A fast way to compare two vectors Ray Koopman <koopman@sfu.ca> - 2011-09-27 10:23 +0000
Re: A fast way to compare two vectors Ray Koopman <koopman@sfu.ca> - 2011-09-29 06:13 +0000
Re: A fast way to compare two vectors "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-09-30 08:06 +0000
Re: A fast way to compare two vectors Ray Koopman <koopman@sfu.ca> - 2011-10-01 07:13 +0000
Re: A fast way to compare two vectors Yasha Gindikin <gindikin@gmail.com> - 2011-10-03 08:22 +0000
Re: A fast way to compare two vectors Ray Koopman <koopman@sfu.ca> - 2011-10-07 09:09 +0000
csiph-web