Thread: Math Quiz 9
View Single Post
  #92   Spotlight this post!  
Unread 11-08-2016, 14:49
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,038
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Math Quiz 9

Quote:
Originally Posted by Ether View Post
Using symmetry for the [full-circle] chords problem, you can reduce the length computation to:
Code:
sum += sin(pi*abs(random-random));
... and when the iterations are complete, multiply the sum by 2.
In case there are any readers wondering where the above code came from, here's a brief explanation:

Code:


// naive implementation
q1 := 2*pi*random; q2 := 2*pi*random;
theta := abs(q2-q1);
len := 2*sin(theta/2.0);
sum := sum + len; 

// get rid of q1 and q2 
theta := 2*pi*abs(random-random);
len := 2*sin(theta/2.0);
sum := sum + len; 

// get rid of theta
len := 2*sin(pi*abs(random-random));
sum := sum + len; 

// get rid of len
sum := sum + 2*sin(pi*abs(random-random));

// move factor of 2 outside the loop
sum := sum + sin(pi*abs(random-random));
Gus pointed out that sin(pi*abs(random-random)) could then be optimized to sin(pi*random).

Some readers may be wondering why. It is due to the interaction between the symmetry of sin(x) around x=pi/2 and the shape of the pi*abs(random-random) distribution.

The distribution of pi*abs(random-random) has the shape f(x):=1-x/pi. That function is symmetric about the point [pi/2,0.5] and has the property that, for any value "a" between -pi/2 and pi/2, f(pi/2-a)+f(pi/2+a) is always equal to 1. This interacts with the symmetry of sin(pi/2ħa) to make the distribution sin(pi*abs(random-random)) be the same as sin(pi*random).


Attached Thumbnails
Click image for larger version

Name:	one minus x over pi.png
Views:	6
Size:	4.5 KB
ID:	20962  Click image for larger version

Name:	pi.abs(rand-rand).png
Views:	5
Size:	7.2 KB
ID:	20963  Click image for larger version

Name:	pi.rand.png
Views:	3
Size:	7.6 KB
ID:	20964  Click image for larger version

Name:	sin(pi.abs(rand-rand)).png
Views:	4
Size:	7.4 KB
ID:	20965  Click image for larger version

Name:	sin(pi.rand).png
Views:	3
Size:	7.3 KB
ID:	20966  


Last edited by Ether : 11-08-2016 at 15:36.
Reply With Quote