sinvalos
(Sinval Santos)
September 3, 2022, 5:28pm
1
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}
danl
(Daniel Lichtblau)
September 3, 2022, 8:22pm
3
Use Nearest with a radius setting. Much faster.
sinvalos
(Sinval Santos)
September 4, 2022, 10:47am
4
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}
sinvalos
(Sinval Santos)
September 4, 2022, 10:58am
5
Thanks Daniel for the suggestion. I will try.
sinvalos
(Sinval Santos)
September 5, 2022, 4:28pm
6
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
danl
(Daniel Lichtblau)
September 5, 2022, 5:17pm
7
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 *)
sinvalos
(Sinval Santos)
September 5, 2022, 5:39pm
8
Thanks again for the tips Daniel.