I have recently asked a question on math.stackexchange concerning how to compute volumes of intersecting hypercubes and hyperspheres to which I got an extremely helpful answer.
Now, I'm trying to utilize sage
to generate some analytic solution for the lowest dimensionalities. With my very naive understanding of sage, the help of google and some trial & error, I came up with the following solution:
from sage.symbolic.integration.integral import definite_integral
R = var("R")
assume(R>0)
x = var("x")
V0(R) = 1
V = [V0]
for i in range(1,4):
vlast = V[i-1]
vnew(R) = definite_integral( vlast(R=sqrt(R**2 - x**2)),x,-min(R,1),min(R,1))
V.append(vnew)
print(V)
However, the output is not quite what I expected:
[R |--> 1, R |--> 2*R, R |--> pi*R^2, R |--> 4/3*pi*R^3]
Sage calculates the volumes of the n-dimesional spheres very nicely, but it somehow doesn't pick up the fact that the resulting function will be defined piecewise in R.
Is there something wrong about how I use the min
function? Is there something wrong with how I define (or name) my variables? Where am I messing up?