Algorithm about conical frustum from "Introduction to Arithmetic(1299)"
by
gg582 · 2026-11-05 12:23:52 · 29 views · 7 min read
Table of contents
A Problem Concerning a Circular Frustum
今有圓臺一所 計積五千四十尺
There is a circular frustum whose volume is 5,040 cubic chi.
cone_volume = 5040
只云上下周相和得一百八尺
It is given that the sum of the upper and lower circumferences is 108 chi.
circ_total = 108
高不及上周一十六尺
The height is 16 chi less than the upper circumference.
# cone_height = upper_circ - 16
問上下周及高幾何
What are the upper circumference, lower circumference, and height?
術曰 立天元一爲臺高
The procedure says: Set up the Heavenly Element One (x) as the height of the frustum.
from sympy import Eq, solve, symbols
cone_height = symbols('x')
加一十六尺爲上周
Add 16 chi to obtain the upper circumference.
upper_circ = cone_height + 16
以減於相和數爲下周
Subtract this from the given sum of the circumferences to obtain the lower circumference.
lower_circ = circ_total - upper_circ
自乘 又上周自乘 又上下周相乘得
Square the lower circumference, then square the upper circumference, and also multiply the upper and lower circumferences together.
lower_sq = lower_circ**2
upper_sq = upper_circ**2
upper_lower_mul = lower_circ * upper_circ
三位併之 又以高乘之 爲三十六段圓臺積 寄左列積
Add these three quantities together and multiply the result by the height. This gives thirty-six times the volume of the circular frustum. Set this expression aside on the left.
left_expr = (lower_sq + upper_sq + upper_lower_mul) * cone_height
以三十六乘之 與寄左相消得開方數式
Multiply the given volume by 36 and equate it with the expression set aside on the left to obtain an equation to be solved.
eq = Eq(left_expr, 36 * cone_volume)
飜法開之 得臺高
Solve the resulting equation by the method of inversion to obtain the height of the frustum.
roots = solve(eq, cone_height)
cone_height_val = [r for r in roots if r.is_real and 0 < r < (circ_total - 16)][0]
加不及卽上周
Add the deficient amount to obtain the upper circumference.
upper_circ_val = cone_height_val + 16
又上周減相和數 得下周也
Subtract the upper circumference from the given sum to obtain the lower circumference.
lower_circ_val = circ_total - upper_circ_val
Answer
from sympy import Eq, solve, symbols
# 今有圓臺一所 計積五千四十尺
cone_volume = 5040
# 只云上下周相和得一百八尺
circ_total = 108
# 術曰 立天元一爲臺高
cone_height = symbols('x')
# 加一十六尺爲上周
upper_circ = cone_height + 16
# 以減於相和數爲下周
lower_circ = circ_total - upper_circ
# 自乘 又上周自乘 又上下周相乘得
lower_sq = lower_circ**2
upper_sq = upper_circ**2
upper_lower_mul = lower_circ * upper_circ
# 三位併之 又以高乘之 爲三十六段圓臺積 寄左列積
left_expr = (lower_sq + upper_sq + upper_lower_mul) * cone_height
# 以三十六乘之 與寄左相消得開方數式
eq = Eq(left_expr, 36 * cone_volume)
# 飜法開之 得臺高
roots = solve(eq, cone_height)
cone_height_val = [r for r in roots if r.is_real and 0 < r < (circ_total - 16)][0]
# 加不及卽上周
upper_circ_val = cone_height_val + 16
# 又上周減相和數 得下周也
lower_circ_val = circ_total - upper_circ_val
print(cone_height_val, upper_circ_val, lower_circ_val)
Generalization with User-Defined Inputs
from sympy import Eq, solve, symbols
def solve_cone(cone_volume, circ_total, height_diff):
# 術曰 立天元一爲臺高
x = symbols('x')
# 加一十六尺爲上周
upper_circ = x + height_diff
# 以減於相和數爲下周
lower_circ = circ_total - upper_circ
# 自乘 又上周自乘 又上下周相乘得
lower_sq = lower_circ**2
upper_sq = upper_circ**2
upper_lower_mul = lower_circ * upper_circ
# 三位併之 又以高乘之 爲三十六段圓臺積 寄左列積
left_expr = (lower_sq + upper_sq + upper_lower_mul) * x
# 以三十六乘之 與寄左相消得開方數式
eq = Eq(left_expr, 36 * cone_volume)
# 飜法開之 得臺高
roots = solve(eq, x)
cone_height_val = [r for r in roots if r.is_real and 0 < r < (circ_total - height_diff)][0]
# 加不及卽上周
upper_circ_val = cone_height_val + height_diff
# 又上周減相和數 得下周也
lower_circ_val = circ_total - upper_circ_val
return cone_height_val, upper_circ_val, lower_circ_val
def main():
cone_volume = float(input())
circ_total = float(input())
height_diff = float(input())
height, upper, lower = solve_cone(cone_volume, circ_total, height_diff)
print(height, upper, lower)
if __name__ == "__main__":
main()
Improving Precision with a Japanese Value of Pi
From this point onward, the method goes beyond the scope of the original Suanxue Qimeng.
The value of pi has been modified using a Japanese mathematical constant, so this is not part of the original text.
We use the value of pi compiled by the Kanryu school. It provides greater precision than math.pi.
from sympy import Eq, Rational, solve, symbols
# 關流 松永良弼 方圓算術 零約術 密率
# Numerator: 428,224,593,349,304
# Denominator: 136,308,121,570,117
MATSUNAGA_PI_RATIONAL = Rational(428224593349304, 136308121570117)
def solve_cone(cone_volume, circ_total, height_diff):
# 術曰 立天元一爲臺高
x = symbols('x')
# 加一十六尺爲上周
upper_circ = x + height_diff
# 以減於相和數爲下周
lower_circ = circ_total - upper_circ
# 自乘 又上周自乘 又上下周相乘得
lower_sq = lower_circ**2
upper_sq = upper_circ**2
upper_lower_mul = lower_circ * upper_circ
# 三位併之 又以高乘之 爲三十六段圓臺積 寄左列積
left_expr = (lower_sq + upper_sq + upper_lower_mul) * x
# 昔用古率徑一周三 爲三十六段積 今易關流零分密率 爲一十二圓率段積
# Cancel against the expression set aside on the left to obtain the equation.
# V = (h / (12 * pi)) * (C1^2 + C2^2 + C1 * C2)
# Therefore:
# (C1^2 + C2^2 + C1 * C2) * h = 12 * pi * V
eq = Eq(left_expr, 12 * MATSUNAGA_PI_RATIONAL * cone_volume)
# 飜法開之 得臺高
roots = solve(eq, x)
cone_height_val = [r for r in roots if r.is_real and 0 < r < (circ_total - height_diff)][0]
# 加不及卽上周
upper_circ_val = cone_height_val + height_diff
# 又上周減相和數 得下周也
lower_circ_val = circ_total - upper_circ_val
return cone_height_val, upper_circ_val, lower_circ_val
def main():
cone_volume = Rational(input())
circ_total = Rational(input())
height_diff = Rational(input())
height, upper, lower = solve_cone(cone_volume, circ_total, height_diff)
print(height.evalf(30))
print(upper.evalf(30))
print(lower.evalf(30))
if __name__ == "__main__":
main()
Visualization (Using the Japanese Value of Pi)
import math
import os
import sys
os.environ.setdefault("PYOPENGL_PLATFORM", "glx")
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from sympy import Eq, Rational, solve, symbols
# 關流 松永良弼 方圓算術 零約術 密率
# Numerator: 428,224,593,349,304
# Denominator: 136,308,121,570,117
MATSUNAGA_PI_RATIONAL = Rational(428224593349304, 136308121570117)
def solve_cone(cone_volume, circ_total, height_diff):
# 術曰 立天元一爲臺高
x = symbols('x')
# 加一十六尺爲上周
upper_circ = x + height_diff
# 以減於相和數爲下周
lower_circ = circ_total - upper_circ
# 自乘 又上周自乘 又上下周相乘得
lower_sq = lower_circ**2
upper_sq = upper_circ**2
upper_lower_mul = lower_circ * upper_circ
# 三位併之 又以高乘之 爲三十六段圓臺積 寄左列積
left_expr = (lower_sq + upper_sq + upper_lower_mul) * x
# 昔用古率徑一周三 爲三十六段積 今易關流零分密率 爲一十二圓率段積
# 與寄左相消得開方數式
eq = Eq(left_expr, 12 * MATSUNAGA_PI_RATIONAL * cone_volume)
# 飜法開之 得臺高
roots = solve(eq, x)
cone_height_val = [r for r in roots if r.is_real and 0 < r < (circ_total - height_diff)][0]
# 加不及卽上周
upper_circ_val = cone_height_val + height_diff
# 又上周減相和數 得下周也
lower_circ_val = circ_total - upper_circ_val
return cone_height_val, upper_circ_val, lower_circ_val
rot_x, rot_y = 25.0, -45.0
last_mouse_x, last_mouse_y = 0, 0
mouse_down = False
g_h, g_r_up, g_r_low = 1.0, 1.0, 1.0
def draw_frustum(r_top, r_bottom, height, segments=64):
y_top = height / 2.0
y_bottom = -height / 2.0
# Lateral surface
dr = r_bottom - r_top
length = math.sqrt(height * height + dr * dr)
nx_ratio = height / length
ny_val = dr / length
glBegin(GL_QUAD_STRIP)
for i in range(segments + 1):
theta = 2.0 * math.pi * i / segments
cos_t = math.cos(theta)
sin_t = math.sin(theta)
glNormal3f(cos_t * nx_ratio, ny_val, sin_t * nx_ratio)
glVertex3f(r_top * cos_t, y_top, r_top * sin_t)
glVertex3f(r_bottom * cos_t, y_bottom, r_bottom * sin_t)
glEnd()
# Top face
glNormal3f(0.0, 1.0, 0.0)
glBegin(GL_TRIANGLE_FAN)
glVertex3f(0.0, y_top, 0.0)
for i in range(segments + 1):
theta = 2.0 * math.pi * i / segments
glVertex3f(r_top * math.cos(theta), y_top, r_top * math.sin(theta))
glEnd()
# Bottom face
glNormal3f(0.0, -1.0, 0.0)
glBegin(GL_TRIANGLE_FAN)
glVertex3f(0.0, y_bottom, 0.0)
for i in range(segments, -1, -1):
theta = 2.0 * math.pi * i / segments
glVertex3f(r_bottom * math.cos(theta), y_bottom, r_bottom * math.sin(theta))
glEnd()
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
max_dim = max(g_h, g_r_low * 2.0)
gluLookAt(0.0, 0.0, max_dim * 2.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
glRotatef(rot_x, 1.0, 0.0, 0.0)
glRotatef(rot_y, 0.0, 1.0, 0.0)
# Frustum surface
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glColor4f(0.2, 0.6, 0.7, 0.75)
draw_frustum(g_r_up, g_r_low, g_h)
glDisable(GL_BLEND)
# Wireframe
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glColor3f(0.9, 0.9, 0.9)
draw_frustum(g_r_up, g_r_low, g_h, segments=32)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glutSwapBuffers()
def reshape(w, h):
if h == 0:
h = 1
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(w) / float(h), 0.1, 1000.0)
glMatrixMode(GL_MODELVIEW)
def mouse(button, state, x, y):
global mouse_down, last_mouse_x, last_mouse_y
if button == GLUT_LEFT_BUTTON:
if state == GLUT_DOWN:
mouse_down = True
last_mouse_x = x
last_mouse_y = y
elif state == GLUT_UP:
mouse_down = False
def motion(x, y):
global rot_x, rot_y, last_mouse_x, last_mouse_y
if mouse_down:
rot_y += (x - last_mouse_x) * 0.5
rot_x += (y - last_mouse_y) * 0.5
last_mouse_x = x
last_mouse_y = y
glutPostRedisplay()
def init_gl():
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
glLightfv(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
glLightfv(GL_LIGHT0, GL_AMBIENT, [0.3, 0.3, 0.3, 1.0])
glClearColor(0.1, 0.1, 0.12, 1.0)
def main():
global g_h, g_r_up, g_r_low
cone_volume = Rational(input())
circ_total = Rational(input())
height_diff = Rational(input())
height, upper, lower = solve_cone(cone_volume, circ_total, height_diff)
print(height.evalf(30))
print(upper.evalf(30))
print(lower.evalf(30))
pi_val = float(MATSUNAGA_PI_RATIONAL)
g_h = float(height)
g_r_up = float(upper) / (2.0 * pi_val)
g_r_low = float(lower) / (2.0 * pi_val)
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(800, 600)
glutCreateWindow(b"Conical Frustum Display")
init_gl()
glutDisplayFunc(display)
glutReshapeFunc(reshape)
glutMouseFunc(mouse)
glutMotionFunc(motion)
# Render the frustum
glutMainLoop()
if __name__ == "__main__":
main()
Comments
No comments yet.