Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: computational efficiency

by Lotus1 (Curate)
on Oct 04, 2011 at 18:51 UTC ( #929638=note: print w/replies, xml ) Need Help??

in reply to computational efficiency

What is the application? Do you need a uniform distribution of angles? of points on a circle? of points on a sphere?

Picking random sine values won't give you a uniform distribution of values for the angles. So for example, if you plot the points on a circle for the randomly chosen sine values then you end up with a higher density of points at certain points along the circumference.

Probably overkill for your application but I did something similar years ago for picking points on the surface of a sphere and this article was helpful.

Replies are listed 'Best First'.
Re^2: computational efficiency
by thelycaeum (Initiate) on Oct 10, 2011 at 19:00 UTC
    I've read the article a couple of times by now. Let me comment on that.

    I've been planning to randomize my rotation subroutine by doing three subsequent arbitrary rotations about each axis in cartesian space, i.e. arbitrary rotation with independent randomization for each axis x, y and z. These however have to be randomized by two randomization processes each, as the sign of the cosine corresponding to the randomized sine has to randomized, too (if I'm not mistaken here, cf. my reply to moritz comment on correctness). Hence, my (thanks to your comments) improved idea would involve six randomizations for each arbitrary rotation, while the method posted on wolfram would involve only two (cf. equations 9 to 11 in the article), correct?

    What I'm asking myself right now is the following: Moritz is absolutely correct, as a single rotation about one axis (which was my original question) results in omitting half of the angles, consequently incompleteness and bias towards rotation angles below 180. Would this still be true, if the routine is applied about each axis xyz? So, if Marsaglia (1972) uses two randomizations to produce an arbitrary point in three dimensional space in the polar coordinate system, would three randomizations in cartesian space still result in incompleteness? I think so, because equations 13 to 15 imply, that at least four randomizations are needed in cartesian space... ?

    P.S.: MidLifeXis: Thank you, I found  use Benchmark qw(:all) ;on

      The approach I like for this problem is what Perlbotics suggested. Make a table. I suggest a hash of arrays where the keys are angles and each value is an array of sin and cos values. Then you choose a random angle (0-360) and get both sin and cos from a fast hash with the negative signs included. This takes care of all four quadrands.

      Do this for each axis of rotation as you proposed. The article I posted was about picking uniformly distributed points on the surface of a sphere which I believe is more difficult than your problem. The only point I was making with my previous post is that if you pick random sine values the corresponding angles are not uniformly distributed. I'm not sure about proving this approach of three rotations but it seems good to me. I'll think about it more and maybe I'll come up with a proof.

      use warnings; use strict; my $ref_trig = make_trigtable(); print "\nangle sine cosine\n"; foreach (1..15) { my $angle = sprintf "%.1f", rand(3600)/10; printf "%5.1f %8.5f %8.5f\n", $angle, $ref_trig->{$angle}[0], $ref +_trig->{$angle}[0]; } sub make_trigtable { use constant PI => 4 * atan2(1, 1); use constant DEG_TO_RAD => PI / 180; my %trig; foreach (0..3600) { my $angle = sprintf "%.1f", $_/10; $trig{$angle}=[sin $angle * DEG_TO_RAD, cos $angle * DEG_TO_RA +D]; } return \%trig; } __END__ C:\perlmonks>perl angle sine cosine 216.2 -0.59061 -0.59061 268.6 -0.99970 -0.99970 166.8 0.22835 0.22835 276.8 -0.99297 -0.99297 81.6 0.98927 0.98927 355.4 -0.08020 -0.08020 11.9 0.20620 0.20620 82.3 0.99098 0.99098 70.1 0.94029 0.94029 173.1 0.12014 0.12014 14.5 0.25038 0.25038 251.3 -0.94721 -0.94721 161.6 0.31565 0.31565 117.7 0.88539 0.88539 319.7 -0.64679 -0.64679

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://929638]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-03-21 05:25 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (263 votes). Check out past polls.