from sympy import *
from sympy import Integral, S, cos, sqrt
from IPython.display import display
init_printing()
x, h, r, t = symbols("x h r t")
# "volume" of a 0 dimensional sphere is 1, i.e. r^0
f0 = r ** 0
def next(f):
x = r * sin(t)
i = Integral(f.subs(r, x), (h, 0, r))
# Rearranging h = r * cos(t) gives t = acos(h / r)
# [transform] does u-substitution for integrals
return 2 * i.transform(acos(h / r), t).doit()
def pretty_print(f, i):
print(f'"Volume" of a {i}-dimensional sphere:')
display(f)
f = f0
for i in range(10):
pretty_print(f, i)
f = next(f)