2
$\begingroup$

Assume we have the following coordinates:

coord = Table[{i, j}, {j, 1.0, -1.0, -0.1}, {i, -1.0, 1.0, 0.1}]

What is the most efficient way to calculate the radius r. i.e. r = sqrt(i^2+j^2) and set r to 0 for all r whose value is greater than 1.

So basically it will get a list like {0,0,0,0,0.989,0.979...0,0,0}.

I already have my own solution, which is not elegant I think. And I hope the algorithm could be as fast as possible. So I would like to hear more sophisticated ideas.

Thanks!

$\endgroup$

4 Answers 4

2
$\begingroup$

To find the distances, you can use Total:

coord = Table[{i, j}, {j, 1.0, -1.0, -0.1}, {i, -1.0, 1.0, 0.1}];
d = Sqrt @ Total[coord^2, {3}];

(since coord has dimensions $21 \times 21 \times 2$, the second argument of Total is {3}). Then, one idea is to use Clip:

res = Clip[d, {0, 1}, {1, 0}]

{{0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}, {0., 0., 0., 0., 0., 0., 0.984886, 0.948683, 0.921954, 0.905539, 0.9, 0.905539, 0.921954, 0.948683, 0.984886, 0., 0., 0., 0., 0., 0.}, {0., 0., 0., 0., 1., 0.943398, 0.894427, 0.8544, 0.824621, 0.806226, 0.8, 0.806226, 0.824621, 0.8544, 0.894427, 0.943398, 0., 0., 0., 0., 0.}, {0., 0., 0., 0.989949, 0.921954, 0.860233, 0.806226, 0.761577, 0.728011, 0.707107, 0.7, 0.707107, 0.728011, 0.761577, 0.806226, 0.860233, 0.921954, 0.989949, 0., 0., 0.}, {0., 0., 1., 0.921954, 0.848528, 0.781025, 0.72111, 0.67082, 0.632456, 0.608276, 0.6, 0.608276, 0.632456, 0.67082, 0.72111, 0.781025, 0.848528, 0.921954, 1., 0., 0.}, {0., 0., 0.943398, 0.860233, 0.781025, 0.707107, 0.640312, 0.583095, 0.538516, 0.509902, 0.5, 0.509902, 0.538516, 0.583095, 0.640312, 0.707107, 0.781025, 0.860233, 0.943398, 0., 0.}, {0., 0.984886, 0.894427, 0.806226, 0.72111, 0.640312, 0.565685, 0.5, 0.447214, 0.412311, 0.4, 0.412311, 0.447214, 0.5, 0.565685, 0.640312, 0.72111, 0.806226, 0.894427, 0.984886, 0.}, {0., 0.948683, 0.8544, 0.761577, 0.67082, 0.583095, 0.5, 0.424264, 0.360555, 0.316228, 0.3, 0.316228, 0.360555, 0.424264, 0.5, 0.583095, 0.67082, 0.761577, 0.8544, 0.948683, 0.}, {0., 0.921954, 0.824621, 0.728011, 0.632456, 0.538516, 0.447214, 0.360555, 0.282843, 0.223607, 0.2, 0.223607, 0.282843, 0.360555, 0.447214, 0.538516, 0.632456, 0.728011, 0.824621, 0.921954, 0.}, {0., 0.905539, 0.806226, 0.707107, 0.608276, 0.509902, 0.412311, 0.316228, 0.223607, 0.141421, 0.1, 0.141421, 0.223607, 0.316228, 0.412311, 0.509902, 0.608276, 0.707107, 0.806226, 0.905539, 0.}, {1., 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.}, {0., 0.905539, 0.806226, 0.707107, 0.608276, 0.509902, 0.412311, 0.316228, 0.223607, 0.141421, 0.1, 0.141421, 0.223607, 0.316228, 0.412311, 0.509902, 0.608276, 0.707107, 0.806226, 0.905539, 0.}, {0., 0.921954, 0.824621, 0.728011, 0.632456, 0.538516, 0.447214, 0.360555, 0.282843, 0.223607, 0.2, 0.223607, 0.282843, 0.360555, 0.447214, 0.538516, 0.632456, 0.728011, 0.824621, 0.921954, 0.}, {0., 0.948683, 0.8544, 0.761577, 0.67082, 0.583095, 0.5, 0.424264, 0.360555, 0.316228, 0.3, 0.316228, 0.360555, 0.424264, 0.5, 0.583095, 0.67082, 0.761577, 0.8544, 0.948683, 0.}, {0., 0.984886, 0.894427, 0.806226, 0.72111, 0.640312, 0.565685, 0.5, 0.447214, 0.412311, 0.4, 0.412311, 0.447214, 0.5, 0.565685, 0.640312, 0.72111, 0.806226, 0.894427, 0.984886, 0.}, {0., 0., 0.943398, 0.860233, 0.781025, 0.707107, 0.640312, 0.583095, 0.538516, 0.509902, 0.5, 0.509902, 0.538516, 0.583095, 0.640312, 0.707107, 0.781025, 0.860233, 0.943398, 0., 0.}, {0., 0., 0., 0.921954, 0.848528, 0.781025, 0.72111, 0.67082, 0.632456, 0.608276, 0.6, 0.608276, 0.632456, 0.67082, 0.72111, 0.781025, 0.848528, 0.921954, 0., 0., 0.}, {0., 0., 0., 0.989949, 0.921954, 0.860233, 0.806226, 0.761577, 0.728011, 0.707107, 0.7, 0.707107, 0.728011, 0.761577, 0.806226, 0.860233, 0.921954, 0.989949, 0., 0., 0.}, {0., 0., 0., 0., 1., 0.943398, 0.894427, 0.8544, 0.824621, 0.806226, 0.8, 0.806226, 0.824621, 0.8544, 0.894427, 0.943398, 0., 0., 0., 0., 0.}, {0., 0., 0., 0., 0., 0., 0.984886, 0.948683, 0.921954, 0.905539, 0.9, 0.905539, 0.921954, 0.948683, 0.984886, 0., 0., 0., 0., 0., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}

Since speed is a concern, here is the timing for a list of 10^7 points:

SeedRandom[1]
pts = RandomReal[10, {10^7, 2}];
d = Sqrt @ Total[pts^2, {2}]; //AbsoluteTiming
Clip[d, {0, 1}, {1, 0}]; //AbsoluteTiming

{0.334351, Null}

{0.034603, Null}

$\endgroup$
1
  • $\begingroup$ Thank you very much! $\endgroup$
    – cj9435042
    Commented May 3, 2018 at 3:27
3
$\begingroup$

How about this?

 coord = Join @@Table[{i, j}, {j, 1.0, -1.0, -0.1}, {i, -1.0, 1.0, 0.1}]; 
   If[# > 1, 0, #] & /@ (Sqrt[#[[1]]^2 + #[[2]]^2] & /@ coord)

Or you can do this.

coord = Join @@Table[{i, j, Sqrt[i^2 + j^2]}, {j, 1.0, -1.0, -0.1}, {i, -1.0, 1.0, 0.1}];
If[# > 1, 0, #] & /@ coord[[All, 3]]
$\endgroup$
1
  • $\begingroup$ Thanks a lot, this one is pretty neat. $\endgroup$
    – cj9435042
    Commented May 3, 2018 at 3:27
2
$\begingroup$

Dot is a bit faster than Total:

coord = Table[{i, j}, {j, 1.0, -1.0, -0.01}, {i, -1.0, 1.0, 0.01}];
a = Clip[Sqrt@Total[coord^2, {3}], {0, 1}, {1, 0}]; // 
  RepeatedTiming // First
b = Clip[Sqrt@Dot[coord^2, ConstantArray[1., 2]], {0., 1.}, {0., 0.}]; // RepeatedTiming // First
a == b

0.00133

0.000428

True

Note however that this is a dangerous operation to perform with finite precision as it is discontinuous. For example, compare this:

coord = Table[{i, j}, {j, 1.0, -1.0, -0.1}, {i, -1.0, 1.0, 0.1}];
coordI = Table[{i, j}, {j, 1, -1, -1/10}, {i, -1, 1, 1/10}];

b = Clip[Sqrt@Dot[coord^2, ConstantArray[1., 2]], {0., 1.}, {0., 0.}];
bI = Clip[Sqrt@Dot[coordI^2, ConstantArray[1, 2]], {0., 1.}, {0., 0.}];
b == bI

False

So make sure that nobody's life depends on this code.

$\endgroup$
2
  • $\begingroup$ thanks for this enlightening idea! $\endgroup$
    – cj9435042
    Commented May 4, 2018 at 13:53
  • $\begingroup$ You're welcome! $\endgroup$ Commented May 4, 2018 at 13:58
0
$\begingroup$

This is another simple way:

transf[list_List] := 
  With[{n = list[[1]]^2 + list[[2]]^2}, If[n > 1, 0, Sqrt[n]]];
 Flatten@Map[transf, coord, {2}] // AbsoluteTiming

{0.00214796, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.984886, 0.948683, 0.921954, 0.905539, 0.9, 0.905539, 0.921954, 0.948683, 0.984886, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1., 0.943398, 0.894427, 0.8544, 0.824621, 0.806226, 0.8, 0.806226, 0.824621, 0.8544, 0.894427, 0.943398, 1., 0, 0, 0, 0, 0, 0, 0, 0.989949, 0.921954, 0.860233, 0.806226, 0.761577, 0.728011, 0.707107, 0.7, 0.707107, 0.728011, 0.761577, 0.806226, 0.860233, 0.921954, 0.989949, 0, 0, 0, 0, 0, 1., 0.921954, 0.848528, 0.781025, 0.72111, 0.67082, 0.632456, 0.608276, 0.6, 0.608276, 0.632456, 0.67082, 0.72111, 0.781025, 0.848528, 0.921954, 1., 0, 0, 0, 0, 0.943398, 0.860233, 0.781025, 0.707107, 0.640312, 0.583095, 0.538516, 0.509902, 0.5, 0.509902, 0.538516, 0.583095, 0.640312, 0.707107, 0.781025, 0.860233, 0.943398, 0, 0, 0, 0.984886, 0.894427, 0.806226, 0.72111, 0.640312, 0.565685, 0.5, 0.447214, 0.412311, 0.4, 0.412311, 0.447214, 0.5, 0.565685, 0.640312, 0.72111, 0.806226, 0.894427, 0.984886, 0, 0, 0.948683, 0.8544, 0.761577, 0.67082, 0.583095, 0.5, 0.424264, 0.360555, 0.316228, 0.3, 0.316228, 0.360555, 0.424264, 0.5, 0.583095, 0.67082, 0.761577, 0.8544, 0.948683, 0, 0, 0.921954, 0.824621, 0.728011, 0.632456, 0.538516, 0.447214, 0.360555, 0.282843, 0.223607, 0.2, 0.223607, 0.282843, 0.360555, 0.447214, 0.538516, 0.632456, 0.728011, 0.824621, 0.921954, 0, 0, 0.905539, 0.806226, 0.707107, 0.608276, 0.509902, 0.412311, 0.316228, 0.223607, 0.141421, 0.1, 0.141421, 0.223607, 0.316228, 0.412311, 0.509902, 0.608276, 0.707107, 0.806226, 0.905539, 0, 1., 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1., 0, 0.905539, 0.806226, 0.707107, 0.608276, 0.509902, 0.412311, 0.316228, 0.223607, 0.141421, 0.1, 0.141421, 0.223607, 0.316228, 0.412311, 0.509902, 0.608276, 0.707107, 0.806226, 0.905539, 0, 0, 0.921954, 0.824621, 0.728011, 0.632456, 0.538516, 0.447214, 0.360555, 0.282843, 0.223607, 0.2, 0.223607, 0.282843, 0.360555, 0.447214, 0.538516, 0.632456, 0.728011, 0.824621, 0.921954, 0, 0, 0.948683, 0.8544, 0.761577, 0.67082, 0.583095, 0.5, 0.424264, 0.360555, 0.316228, 0.3, 0.316228, 0.360555, 0.424264, 0.5, 0.583095, 0.67082, 0.761577, 0.8544, 0.948683, 0, 0, 0.984886, 0.894427, 0.806226, 0.72111, 0.640312, 0.565685, 0.5, 0.447214, 0.412311, 0.4, 0.412311, 0.447214, 0.5, 0.565685, 0.640312, 0.72111, 0.806226, 0.894427, 0.984886, 0, 0, 0, 0.943398, 0.860233, 0.781025, 0.707107, 0.640312, 0.583095, 0.538516, 0.509902, 0.5, 0.509902, 0.538516, 0.583095, 0.640312, 0.707107, 0.781025, 0.860233, 0.943398, 0, 0, 0, 0, 1., 0.921954, 0.848528, 0.781025, 0.72111, 0.67082, 0.632456, 0.608276, 0.6, 0.608276, 0.632456, 0.67082, 0.72111, 0.781025, 0.848528, 0.921954, 1., 0, 0, 0, 0, 0, 0.989949, 0.921954, 0.860233, 0.806226, 0.761577, 0.728011, 0.707107, 0.7, 0.707107, 0.728011, 0.761577, 0.806226, 0.860233, 0.921954, 0.989949, 0, 0, 0, 0, 0, 0, 0, 1., 0.943398, 0.894427, 0.8544, 0.824621, 0.806226, 0.8, 0.806226, 0.824621, 0.8544, 0.894427, 0.943398, 1., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.984886, 0.948683, 0.921954, 0.905539, 0.9, 0.905539, 0.921954, 0.948683, 0.984886, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.