Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.3K Views
|
7 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Determining points of intersection between two tables takes long time?

Posted 3 years ago

I use the procedure below to determine the points of intersection between two tables, but it takes a long time. Does anyone know a procedure that accomplishes this task more quickly? Grateful,
Sinval

ta5 = Table[s5, {y, 0, \[Pi], \[Pi]/60}, {x, 0, 2 \[Pi], \[Pi]/60}] //N // Flatten[#, 1] &;
ta6 = Table[s6, {y, 0, \[Pi], \[Pi]/60}, {x, 0, 2 \[Pi], \[Pi]/60}] //N // Flatten[#, 1] &;

Intersection[ta5, ta6, SameTest -> (Norm[#1 - #2] < 0.005 &)]; // Timing
                  {254.016, Null}
POSTED BY: Sinval Santos
7 Replies
Posted 3 years ago

Thanks again for the tips Daniel.

POSTED BY: Sinval Santos

I would make a few changes. One is to avoid computing pairwise differences as that increases the computational complexity. Another is to remove duplicates before beginning. A third is to restrict to one nearest point per point queried since that suffices to determine if it is a near-intersection point.

With these considerations, here is the code I use.

Timing[
 ipoints = 
   Nearest[DeleteDuplicates@ta5, DeleteDuplicates@ta6, {1, 0.005}] // Flatten;]
Length[ipoints]

(* Out[908]= {0.015253, Null}

Out[909]= 435 *)
POSTED BY: Daniel Lichtblau
Posted 3 years ago

Hello Daniel. Using the procedure below, with the Nearest function, I was able to accomplish the task much faster.

s5 = {Sin[y] Cos[x], Sin[y] Sin[x], Cos[y]};
s6 = {Sin[x - y] Cos[x + y], Sin[x + y] Sin[x - y], Cos[y]};

ta5 = Table[s5, {y, 0, \[Pi], \[Pi]/60}, {x, 0, 2 \[Pi], \[Pi]/60}] //N // Flatten[#, 1] &;
ta6 = Table[s6, {y, 0, \[Pi], \[Pi]/60}, {x, 0, 2 \[Pi], \[Pi]/60}] //N // Flatten[#, 1] &;

pair = Outer[{#1, #2} &, ta5, ta6, 1] // Flatten[#, 1] &;
list = Map[Sqrt[Abs[(#[[1]] - #[[2]]) . (#[[1]] - #[[2]])]] &, pair] -> pair // Thread;

points = Nearest[list, 0, {All, 0.005}]; // Timing
       {1.84375, Null}

Is there a more elegant or more practical way to do this using Nearest?

Regards, Sinval

POSTED BY: Sinval Santos
Posted 3 years ago

Thanks Daniel for the suggestion. I will try.

POSTED BY: Sinval Santos
Posted 3 years ago

I apologize to the community, because I forgot to put the expressions that generated the tables. If the complete code.

s5 = {Sin[y] Cos[x], Sin[y] Sin[x], Cos[y]};
s6 = {Sin[x - y] Cos[x + y], Sin[x + y] Sin[x - y], Cos[y]};

ta5 = Table[s5, {y, 0, \[Pi], \[Pi]/60}, {x, 0, 2 \[Pi], \[Pi]/60}] //N // Flatten[#, 1] &;
ta6 = Table[s6, {y, 0, \[Pi], \[Pi]/60}, {x, 0, 2 \[Pi], \[Pi]/60}] //N // Flatten[#, 1] &;

Intersection[ta5, ta6, SameTest -> (Norm[#1 - #2] < 0.005 &)]; // Timing
                  {254.016, Null}
POSTED BY: Sinval Santos

Use Nearest with a radius setting. Much faster.

POSTED BY: Daniel Lichtblau
Posted 3 years ago

What are s5 and s6?

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard