## The correct version of Richard Gill's incorrect calculations of the four
## 'Bell test' correlations predicted by my 3-sphere model for the EPR-Bohm
## correlations. All lines except those generating the set of directions 'u'
## and/or 'v' have been ignored from the code in http://rpubs.com/jjc/16415.

## For the exact terms of the challenge put forward by Richard Gill please
## see http://www.sciphysicsforums.com/spfbb1/viewtopic.php?f=6&t=52#p1898.

set.seed(9875)

N <- 10^5

r <- runif(N, 0, 2 * pi)
s <- runif(N, 0, pi)
t <- runif(N, -1, +1)

x <- cos(r)
y <- 1.199 * (-1 + (2/(sqrt(1 + (3 * s/pi))))) * sign(t)

u <- rbind(x, y)
v <- rbind(y, x)

##'u' and 'v' are 2 x N matrices. The N columns of 'u' represent the x and y
## coordinates of points on a circle of radius sqrt(x^2+y^2) in the xy plane.

## Note that, despite appearances, u and v specify just one set of directions
## for the angular momentum (and likewise the vectors -u and -v). It is very
## easy to check that there is one-to-one map between the set of directions u
## and the set of directions v. In other words, for every u_j there is a v_k
## such that u_j = v_k, and for every v_j there is a u_k such that v_j = u_k.
## It is easy to see this by noting that the vector v_j = y e_x + x e_y is
## nothing but the vector u_k = x e_x + y e_y reflected about the line y = x.
## Consequently, the set of vectors u(x, y) and the set of vectors v(y, x)
## represent exactly the same set of spin direcitons in the physical space,
## but correspond to two alternate (or hidden) orientations of the 3-sphere.

## With this in mind, we now calculate the four correlations as follows:

alpha <- 0 * pi/180
beta <- 45 * pi/180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
(E_0_45 <- mean(sign(colSums(a * u)) * -sign(colSums(b * u))))
## [1] -0.7072

alpha <- 0 * pi/180
beta <- 135 * pi/180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
(E_0_135 <- mean(sign(colSums(a * u)) * -sign(colSums(b * u))))
## [1] 0.707

## Next, we calculate two 'mirror images' of the above correlations. One can
## think of these 'mirror images' as correlations observed (and calculated)
## from a clockwise perspective rather than a counter-clockwise perspective:

alpha <- 90 * pi/180
beta <- 45 * pi/180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
(E_90_45 <- mean(sign(colSums(a * v)) * -sign(colSums(b * v))))
## [1] -0.7072

alpha <- 90 * pi/180
beta <- 135 * pi/180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
(E_90_135 <- mean(sign(colSums(a * v)) * -sign(colSums(b * v))))
## [1] -0.707

## The Bell-CHSH inequality is violated:

-E_0_45 + E_0_135 - E_90_45 - E_90_135
## [1] 2.828

## The data frame of Alice's N spin directions:

write.table(t(u), "AliceDu.txt", sep = "   ")
write.table(t(v), "AliceDv.txt", sep = "   ")

## The data frame of Bob's N spin directions:

write.table(t(-u), "BobDu.txt", sep = "   ")
write.table(t(-v), "BobDv.txt", sep = "   ")