Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5219

Re: A fast way to compare two vectors

From Yasha Gindikin <gindikin@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: A fast way to compare two vectors
Date 2011-09-26 08:19 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <j5pci6$8hb$1@smc.vnet.net> (permalink)
References <j5m44t$ple$1@smc.vnet.net> <j5mt5u$smm$1@smc.vnet.net>

Show all headers | View raw


Dear Oleksandr, thank you very much for your code. Actually, I was trying to implement the same algorithm. My code was the following:

Compare := Compile[{{a, _Integer, 1}, {b, _Integer, 1}},
   {s1 = s2 = 0; k = n; d1 = d2 = Table[0, {k, 3}];
    While[s1 < 3 && s2 < 3 && k - s1 > 0 && k - s2 > 0, 
     Which[a[[k - s1]] > b[[k - s2]], d1[[s1 + 1]] = a[[k - s1]]; s1++,
       a[[k - s1]] < b[[k - s2]], d2[[s2 + 1]] = b[[k - s2]]; s2++,
      a[[k - s1]] == b[[k - s2]], k--]]; Cm = Max[{s1, s2}];
    Which[Cm > 2, False, s1 - s2 == 1, d2[[s1]] = b[[1]], 
     s2 - s1 == 1, d1[[s2]] = a[[1]], s1 - s2 == 2, d2[[1]] = b[[2]]; 
     d2[[2]] = b[[1]], s2 - s1 == 2, d1[[1]] = a[[2]]; 
     d1[[2]] = a[[1]]]; Cm}];

But due to my very limited programming experience, the code turned out to be not very efficient. It even works faster without the Compile option, which puzzles me a lot. I will try to use your realization instead. Thank you once again!

Regards,
Yasha.

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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