I’ve been talking with AI so much lately that I feel like my own thinking has dulled, so I’ve gone back to working through university entrance-exam problems for fun.
One of the volume integrals was over almost immediately, so I also tried computing it in code.
The problem
In coordinate space, let \(V\) be the cone obtained by rotating the triangle \(OAB\), with vertices
\[ O(0,0,0),\quad A(1,0,0),\quad B(1,1,0), \]once around the \(x\)-axis. Find the volume of the solid obtained by rotating the cone \(V\) once around the \(y\)-axis. (Osaka University, 2013, science mathematics, problem 4)
Approach and answer
The plan is simple: take the cross-sectional area of the final solid at each \(y\), and integrate along the \(y\)-axis.

Rotate that around the \(y\)-axis:

It looks roughly like this. A slice at one \(y\) is:

Then integrate.
The area of the shaded annulus is
\[ \pi\left((2-y^2)-y^2\right)=2\pi(1-y^2). \]So the volume is
\[ \int_{-1}^{1} 2\pi(1-y^2)\,dy = 4\pi\int_{0}^{1}(1-y^2)\,dy = 4\pi\left[y-\frac{y^3}{3}\right]_{0}^{1} = 4\pi\left(1-\frac{1}{3}\right) = \frac{8\pi}{3}. \]Doing it in Go
Python is the usual choice for numerical work, but Go has gonum, a set of numerical libraries.
The approach is the Monte Carlo method. It is a family of methods that draw random numbers many times, run a probabilistic simulation or a numerical calculation, and arrive at an approximate answer. Here, that answer is the volume.
Throw points into a region that contains the solid, and multiply the fraction that land inside by the volume of that region. There are \(1{,}000{,}000\) points. A point is inside when it lies in the annulus above. With \(r^2=x^2+z^2\),
\[ |y|\le 1,\quad y^2\le r^2\le 2-y^2. \]The regions get tighter, starting from one that contains the solid. First, sample the box uniformly. Its four corners are always outside, so next sample the cylinder with those corners removed. Last, drop the random numbers and use the centers of a uniform grid on the same box.
Box
This is a box that contains the solid.
\[ x,z\in[-\sqrt{2},\sqrt{2}],\quad y\in[-1,1] \]Its volume is \(16\), so multiply the hit fraction by \(16\).
Cylinder
Do not sample the corners where \(x^2+z^2>2\). The base is a disk of radius \(\sqrt{2}\), the height is \(2\), and the volume is \(4\pi\). The angle \(\theta\) is uniform, and the radius is \(r=\sqrt{2U}\), where \(U\) is uniform on \([0,1]\).
Grid
Divide the same box into \(100^3\) cells and take the center of each. The factor is \(16\) again.
package main
import (
"fmt"
"math"
"gonum.org/v1/gonum/stat/distuv"
)
func inside(x, y, z float64) bool {
r2 := x*x + z*z
return -1 <= y && y <= 1 && y*y <= r2 && r2 <= 2-y*y
}
func center(i, n int, min, width float64) float64 {
return min + (float64(i)+0.5)*width/float64(n)
}
func estimateCube(n int) float64 {
xz := distuv.Uniform{Min: -math.Sqrt2, Max: math.Sqrt2}
yDist := distuv.Uniform{Min: -1, Max: 1}
hit := 0
for range n {
if inside(xz.Rand(), yDist.Rand(), xz.Rand()) {
hit++
}
}
return 16 * float64(hit) / float64(n)
}
func estimateCylinder(n int) float64 {
yDist := distuv.Uniform{Min: -1, Max: 1}
unit := distuv.Uniform{Min: 0, Max: 1}
hit := 0
for range n {
y := yDist.Rand()
theta := 2 * math.Pi * unit.Rand()
r := math.Sqrt(2 * unit.Rand())
x := r * math.Cos(theta)
z := r * math.Sin(theta)
if inside(x, y, z) {
hit++
}
}
return 4 * math.Pi * float64(hit) / float64(n)
}
func estimateGrid(n int) float64 {
R := math.Sqrt2
hit := 0
for i := range n {
x := center(i, n, -R, 2*R)
for j := range n {
y := center(j, n, -1, 2)
for k := range n {
z := center(k, n, -R, 2*R)
if inside(x, y, z) {
hit++
}
}
}
}
return 16 * float64(hit) / float64(n*n*n)
}
func main() {
const samples = 1_000_000
const gridN = 100 // 100³ = 1,000,000
exact := 8 * math.Pi / 3
for range 10 {
cube := estimateCube(samples)
cylinder := estimateCylinder(samples)
grid := estimateGrid(gridN)
fmt.Printf("Cube: %.8f (error: %.6f)\n", cube, math.Abs(cube-exact))
fmt.Printf("Cylinder: %.8f (error: %.6f)\n", cylinder, math.Abs(cylinder-exact))
fmt.Printf("Grid: %.8f (error: %.6f)\n", grid, math.Abs(grid-exact))
fmt.Println("---")
}
}
Ten runs
The exact value is \(8\pi/3 \approx 8.377580\).
| Placement | Range of estimates | Mean absolute error |
|---|---|---|
| Box | 8.365040 to 8.386800 | 0.0046 |
| Cylinder | 8.369215 to 8.390252 | 0.0069 |
| Grid | 8.379392 (the same all ten times) | 0.001812 |
Output of the ten runs
Cube: 8.36504000 (error: 0.012540)
Cylinder: 8.38886920 (error: 0.011289)
Grid: 8.37939200 (error: 0.001812)
---
Cube: 8.38680000 (error: 0.009220)
Cylinder: 8.36921540 (error: 0.008365)
Grid: 8.37939200 (error: 0.001812)
---
Cube: 8.36980800 (error: 0.007772)
Cylinder: 8.38885663 (error: 0.011276)
Grid: 8.37939200 (error: 0.001812)
---
Cube: 8.37550400 (error: 0.002076)
Cylinder: 8.37307327 (error: 0.004507)
Grid: 8.37939200 (error: 0.001812)
---
Cube: 8.37764800 (error: 0.000068)
Cylinder: 8.39025150 (error: 0.012671)
Grid: 8.37939200 (error: 0.001812)
---
Cube: 8.37918400 (error: 0.001604)
Cylinder: 8.37086159 (error: 0.006719)
Grid: 8.37939200 (error: 0.001812)
---
Cube: 8.37438400 (error: 0.003196)
Cylinder: 8.37918053 (error: 0.001600)
Grid: 8.37939200 (error: 0.001812)
---
Cube: 8.37297600 (error: 0.004604)
Cylinder: 8.38518725 (error: 0.007607)
Grid: 8.37939200 (error: 0.001812)
---
Cube: 8.38142400 (error: 0.003844)
Cylinder: 8.37635309 (error: 0.001227)
Grid: 8.37939200 (error: 0.001812)
---
Cube: 8.37859200 (error: 0.001012)
Cylinder: 8.38129168 (error: 0.003711)
Grid: 8.37939200 (error: 0.001812)
---
Parallelism
Timed, a million points took 38ms in the box, 43ms in the cylinder, and 1.2ms on the grid. The grid is still \(100^3\).
| Estimate | Time | Error | |
|---|---|---|---|
| Box | 8.37467200 | 38.063ms | 0.00290841 |
| Cylinder | 8.38012301 | 43.17675ms | 0.00254260 |
| Grid | 8.37939200 | 1.1615ms | 0.00181159 |
At a billion points the box takes 18 seconds and the cylinder 35 seconds. The grid is refined to 1000 divisions per side as well. \(1000^3\) is a billion points.
| Estimate | Time | Error | |
|---|---|---|---|
| Box | 8.37736758 | 18.486891083s | 0.00021283 |
| Cylinder | 8.37741300 | 35.008779542s | 0.00016741 |
| Grid | 8.37757005 | 839.644333ms | 0.00001036 |
That was slow, so the loops are split across GOMAXPROCS goroutines. This run used 10 workers. Each worker keeps its own math/rand/v2 generator.
| Estimate | Time | Error | |
|---|---|---|---|
| Box | 8.37699379 | 2.594924125s | 0.00058662 |
| Cylinder | 8.37727354 | 4.979571291s | 0.00030687 |
| Grid | 8.37757005 | 140.203833ms | 0.00001036 |
The box came down to 2.6 seconds, the cylinder to 5.0 seconds, and the grid to 140ms.
Parallel code
package main
import (
"fmt"
"math"
"math/rand/v2"
"runtime"
"sync"
"time"
)
func inside(x, y, z float64) bool {
r2 := x*x + z*z
return -1 <= y && y <= 1 &&
y*y <= r2 &&
r2 <= 2-y*y
}
func center(i, n int, min, width float64) float64 {
return min + (float64(i)+0.5)*width/float64(n)
}
func newRNG(worker int) *rand.Rand {
return rand.New(rand.NewPCG(
uint64(worker+1),
uint64(worker+10001),
))
}
func estimateCube(n int) float64 {
workers := min(runtime.GOMAXPROCS(0), n)
results := make([]int, workers)
var wg sync.WaitGroup
for worker := range workers {
wg.Add(1)
go func(worker int) {
defer wg.Done()
rng := newRNG(worker)
hit := 0
start := worker * n / workers
end := (worker + 1) * n / workers
for i := start; i < end; i++ {
x := (2*rng.Float64() - 1) * math.Sqrt2
y := 2*rng.Float64() - 1
z := (2*rng.Float64() - 1) * math.Sqrt2
if inside(x, y, z) {
hit++
}
}
results[worker] = hit
}(worker)
}
wg.Wait()
total := 0
for _, hit := range results {
total += hit
}
return 16 * float64(total) / float64(n)
}
func estimateCylinder(n int) float64 {
workers := min(runtime.GOMAXPROCS(0), n)
results := make([]int, workers)
var wg sync.WaitGroup
for worker := range workers {
wg.Add(1)
go func(worker int) {
defer wg.Done()
rng := newRNG(worker)
hit := 0
start := worker * n / workers
end := (worker + 1) * n / workers
for i := start; i < end; i++ {
y := 2*rng.Float64() - 1
theta := 2 * math.Pi * rng.Float64()
r := math.Sqrt(2 * rng.Float64())
x := r * math.Cos(theta)
z := r * math.Sin(theta)
if inside(x, y, z) {
hit++
}
}
results[worker] = hit
}(worker)
}
wg.Wait()
total := 0
for _, hit := range results {
total += hit
}
return 4 * math.Pi * float64(total) / float64(n)
}
func estimateGrid(n int) float64 {
workers := min(runtime.GOMAXPROCS(0), n)
results := make([]int, workers)
var wg sync.WaitGroup
R := math.Sqrt2
for worker := range workers {
wg.Add(1)
go func(worker int) {
defer wg.Done()
hit := 0
start := worker * n / workers
end := (worker + 1) * n / workers
for i := start; i < end; i++ {
x := center(i, n, -R, 2*R)
for j := range n {
y := center(j, n, -1, 2)
for k := range n {
z := center(k, n, -R, 2*R)
if inside(x, y, z) {
hit++
}
}
}
}
results[worker] = hit
}(worker)
}
wg.Wait()
total := 0
for _, hit := range results {
total += hit
}
return 16 * float64(total) / float64(n*n*n)
}
func benchmark(name string, f func() float64, exact float64) {
start := time.Now()
estimate := f()
elapsed := time.Since(start)
errorValue := math.Abs(estimate - exact)
fmt.Printf("Method: %s\n", name)
fmt.Printf("Estimate: %.8f\n", estimate)
fmt.Printf("Time: %s\n", elapsed)
fmt.Printf("Error: %.8f\n\n", errorValue)
}
func main() {
const N = 1_000_000_000
const gridN = 1000
exact := 8 * math.Pi / 3
fmt.Printf("Workers: %d\n\n", runtime.GOMAXPROCS(0))
benchmark("Cube Parallel", func() float64 {
return estimateCube(N)
}, exact)
benchmark("Cylinder Parallel", func() float64 {
return estimateCylinder(N)
}, exact)
benchmark("Grid Parallel", func() float64 {
return estimateGrid(gridN)
}, exact)
}