摘要 这是一篇可以运行的论文。文中的四幅图都不是图片,而是此刻正在你的浏览器里计算的程序:图 1 逐像素积分光子在黑洞附近的测地线;图 2 用外推法积分三体问题,并量出混沌的增长率;图 3 现场训练一个小神经网络,把高斯噪声“流”成你画的任意形状;图 4 把一段文字编成二维码,再从被涂坏的码里把它纠错读回来。四个实验守同一条规矩:从公式出发,一直写到像素为止,中间不调用任何库,文件也不发出任何网络请求。文末的表 1 是自检报告:页面打开时当场运行 24 项断言,每一项都拿正在运行的这份代码,去对照一个独立得到的结果。
关键词 零测地线;Bulirsch–Stoer 外推;流匹配;Reed–Solomon 码;可复现性
Abstract This is a paper that runs. Its four figures are programs executing in your browser right now: a per-pixel integration of null geodesics around a Schwarzschild black hole; an extrapolation integrator for the three-body problem that measures the growth rate of chaos; a small neural network trained on the spot to transport Gaussian noise onto any shape you draw; and a QR encoder whose Reed–Solomon decoder recovers the text from a symbol you have damaged. Everything from the governing equation down to the pixel is written here by hand, with no libraries and no network requests. Table 1 is a self-check: 24 assertions that run when the page opens, each comparing the running code against an independently obtained result.
Keywords null geodesics; Bulirsch–Stoer extrapolation; flow matching; Reed–Solomon codes; reproducibility
1光线在黑洞附近怎么走How light travels near a black hole
图 1 的每个像素都是一条光线。从相机出发、逆着光的传播方向追过去,它只有三种结局:落进视界,像素是黑的;撞上吸积盘,像素取盘在那一点的颜色;逃到无穷远,像素取那个方向的星空。全部物理都在“光线怎么弯”这一件事里。Every pixel of Figure 1 is a ray of light. Followed from the camera against its direction of travel, a ray can end in only three ways: it falls through the horizon and the pixel is black; it hits the accretion disk and the pixel takes the disk's colour at that point; or it escapes to infinity and the pixel shows the sky in that direction. All of the physics lies in one question: how does the ray bend?
对不带电、不自转的黑洞(史瓦西度规,取 ),光子的轨道落在一个过中心的平面内。用 作变量,轨道方程只有一行:For an uncharged, non-rotating black hole (the Schwarzschild metric, with ), a photon's orbit lies in a plane through the centre. In the variable the orbit equation takes one line:
左边等于零时它描述极坐标里的一条直线,右边的 是广义相对论的全部修正。这一项在 时可以忽略,在 处恰好能让光子绕着黑洞转圈,那里叫光子球。一条光线的命运由它的碰撞参数 决定: 小于临界值 的被俘获,略大于 的先绕黑洞若干圈再离开。能离开的光线,总偏折角有闭式[1]:With the left-hand side set to zero it describes a straight line in polar coordinates; the term on the right is the whole of the general-relativistic correction. It is negligible for , and at it is exactly strong enough to hold a photon on a circle, the photon sphere. The fate of a ray is fixed by its impact parameter : rays with below the critical value are captured, and rays slightly above it wind around the hole several times before leaving. For a ray that leaves, the total deflection has a closed form[1]:
其中 是近心点半径,由 给出, 与 是第一类完全、不完全椭圆积分。图 1(c) 画的就是这条曲线: 很大时它回到爱因斯坦的 , 时按 发散[2]。where is the radius of closest approach, given by , and and are the complete and incomplete elliptic integrals of the first kind. Figure 1(c) plots this curve: for large it reduces to Einstein's , and as it diverges like [2].
着色器并不直接用式 (2),因为它事先不知道光线半路会不会撞上盘,只能一步一步走。式 (1) 等价于平直空间里一个质点在有心力 作用下的轨迹, 沿途守恒。于是在三维里用蛙跳法推进位置和速度即可,每走一步顺便检查有没有穿过盘面、有没有掉进 。The shader does not use Eq. (2) directly: it cannot know in advance whether a ray will hit the disk on the way, so it has to march step by step. Eq. (1) is equivalent to the trajectory of a particle in flat space under the central force , with conserved along the path. Position and velocity are therefore advanced in three dimensions with a leapfrog scheme, and each step also checks whether the ray has crossed the disk plane or fallen inside .
代码 1 逐像素追踪的主循环(页面正在运行的源码,JavaScript 版;着色器里是逐行对应的 GLSL)Listing 1 The main loop of the per-pixel tracer (the source this page is running, with its comments translated; this is the JavaScript version, and the shader contains the same lines in GLSL)
有两处细节决定画面对不对。其一,相机是静止在 处的观察者,它眼里的角度和坐标系里的角度不是一回事:把视线方向换到坐标系时,径向分量要乘上 。漏掉这个因子, 处的阴影会被画大 2.6%;加上它,阴影的角半径就应该满足 。表 1 里有一项专门用二分法从渲染器中量出这个角来核对。Two details decide whether the picture is right. First, the camera is an observer at rest at , and the angles it sees are not coordinate angles: when a viewing direction is converted to the coordinate frame, its radial component must be multiplied by . Leave this factor out and the shadow at is drawn 2.6% too large; with it, the angular radius of the shadow should satisfy . One row of Table 1 measures this angle from the renderer by bisection and compares.
其二是颜色。盘取 Page 与 Thorne 的相对论薄盘模型[3],单位面积的辐射流量是The second detail is colour. The disk follows the relativistic thin-disk model of Page and Thorne[3], whose radiated flux per unit area is
它在最内稳定圆轨道 处降为零,在 处最大,有效温度 。盘上的气体以角速度 绕转,观察者收到的光子频率是发射时的 倍:It falls to zero at the innermost stable circular orbit and peaks near ; the effective temperature goes as . Gas in the disk orbits with angular velocity , and a photon reaches the observer with times the frequency it was emitted at:
是光子绕盘轴的角动量与能量之比。黑体辐射经过多普勒频移和引力红移之后仍是黑体,只是温度变成 。所以每个像素的颜色,就是温度为 的黑体的颜色:把普朗克谱乘上 CIE 1931 配色函数[4]积分,再转到 sRGB。朝我们转过来的一侧 ,又蓝又亮;转离的一侧又红又暗。关掉图 1 下面的“多普勒与红移”,盘立刻左右对称,这一项有多大一目了然。Here is the ratio of the photon's angular momentum about the disk axis to its energy. Black-body radiation that has been Doppler-shifted and gravitationally redshifted is still black-body radiation, only at temperature . The colour of each pixel is therefore the colour of a black body at : the Planck spectrum integrated against the CIE 1931 colour-matching functions[4] and converted to sRGB. The side rotating towards us has and is bluer and brighter; the receding side is redder and dimmer. Untick Doppler and redshift under Figure 1 and the disk becomes left–right symmetric at once, which shows how large this effect is.
至于画面的形状:盘在黑洞背后的那一半并没有被挡住。从它上表面出发的光被弯过黑洞上方,成了罩在阴影顶上的那道拱;从它下表面出发的光绕过下方,成了阴影底下那道细弧;紧贴阴影边缘还有一圈极细的亮环,是绕了黑洞半圈以上才出来的光。Luminet 在 1979 年第一次算出这幅图[5],再用墨水一点一点画在纸上;这里交给显卡实时重算。As for the shape of the image: the half of the disk behind the hole is not hidden. Light leaving its upper surface is bent over the top of the hole and becomes the arch above the shadow; light from its lower surface passes underneath and becomes the thin arc below; and hugging the edge of the shadow is a very thin bright ring, made of light that went more than half-way around the hole before escaping. Luminet first computed this picture in 1979[5] and then drew it on paper dot by dot in ink; here the graphics card recomputes it in real time.
背景的星空是程序生成的,但对待它的方式并不随便。恒星是点光源,被引力透镜放大以后仍然是一个点,只是更亮;如果把星空当成一张贴图去采样,黑洞周围的星星就会被拉成一段段弧线。着色器因此用屏幕空间的导数,把每颗星在天球上的偏移换算回屏幕上的像素,再画一个大小不变的圆点,亮度按放大率调整。The background sky is procedural, but it is not treated carelessly. A star is a point source, and after magnification by the gravitational lens it is still a point, only brighter; sampling the sky as if it were a texture would stretch the stars near the hole into arcs. The shader therefore uses screen-space derivatives to convert each star's offset on the celestial sphere back into pixels on the screen, and draws a dot of constant size whose brightness is scaled by the magnification.
2三体:同一组方程,两种性格Three bodies: one set of equations, two temperaments
三个质点两两以平方反比的引力相吸(取 )。直接积分三个位置矢量有一个隐患:两个天体贴得很近时,它们的间距是两个大数之差,有效数字丢得很快。这里改用三条边 、、 作状态量,运动方程仍然封闭(下标对 3 取模):Three point masses attract one another in pairs with inverse-square gravity (). Integrating the three position vectors directly has a weakness: when two bodies come very close, their separation is the difference of two large numbers and significant digits are lost quickly. The state used here is instead the three edges , , , for which the equations of motion still close (indices modulo 3):
时间推进用 Gragg–Bulirsch–Stoer 外推法[6]。同一个大步 ,用修正中点法分别走 个子步,得到一列越来越准的结果 ;把它们看作 的函数,逐列外推到零:Time is advanced with Gragg–Bulirsch–Stoer extrapolation[6]. One large step is taken with the modified midpoint method using substeps, giving a sequence of increasingly accurate results ; regarded as functions of , they are extrapolated to zero column by column:
相邻两列之差小于容差(这里取相对 )就接受这一步。整个积分器里没有一个需要查表的系数,循环里只用到加减乘除和开方,所以同一组初值在任何浏览器里都算出逐位相同的轨道。The step is accepted once two neighbouring columns differ by less than the tolerance (a relative here). The integrator contains no tabulated coefficients, and its loop uses only the four arithmetic operations and square roots, so the same initial conditions give a bit-for-bit identical orbit in every browser.
代码 2 修正中点法与外推表(页面正在运行的源码)Listing 2 Modified midpoint method and the extrapolation table (the source this page is running, with its comments translated)
(a) 轨道。实线是本体,细虚线是初值差了 的孪生系统Orbits. Solid: the system itself; thin dashed: a twin whose initial state differs by
(b) 两条轨道在相空间里的距离Phase-space distance between the two orbits
(c) 能量的相对误差Relative energy error
图 2 的几组初值各有来历。8 字:三个等质量的天体在同一条 8 字形曲线上首尾相追,彼此只差三分之一个周期[7, 8]。数值研究表明它是线性稳定的[9],所以图 2(b) 里那条孪生轨道只会慢慢地、近似线性地错开。Each set of initial conditions in Figure 2 has a history. Figure-eight: three equal masses chase one another along a single eight-shaped curve, a third of a period apart[7, 8]. Numerical studies show it to be linearly stable[9], which is why the twin orbit in Figure 2(b) drifts away only slowly and roughly linearly.
拉格朗日三角:三个等质量的天体占据等边三角形的三个顶点,整体刚性旋转。这是一个精确解,但不稳定。在转动参考系里把运动方程线性化,本征值满足[10]Lagrange triangle: three equal masses sit at the vertices of an equilateral triangle that rotates rigidly. This is an exact solution, but an unstable one. Linearising the equations of motion in the rotating frame gives eigenvalues that satisfy[10]
等质量时 ,解出 :偏离以 增长,同时以角频率 振荡。在对数坐标的图 2(b) 上,这是一条斜率确定的直线,实测斜率与它相差不到万分之一。更有意思的是没加任何扰动的本体:它从完美的三角形出发,转上七圈多之后照样解体。种子只是双精度浮点数的舍入误差,量级 ,被这个指数一路放大了十六个量级。For equal masses and : a deviation grows as while oscillating at angular frequency . On the logarithmic axis of Figure 2(b) this is a straight line of known slope, and the measured slope agrees with it to better than one part in ten thousand. More interesting is the unperturbed system itself: it starts from a perfect triangle and still falls apart after a little more than seven revolutions. The only seed is the rounding error of double-precision arithmetic, of order , which this exponential amplifies by sixteen orders of magnitude.
毕达哥拉斯问题[11]:质量为 3、4、5 的三个天体,静止地放在一个 3-4-5 直角三角形的顶点上,每个天体正对着与自己同名的那条边。它们先后经历十几次近距离交会,最近的一次是质量 4 和 5 的两个天体,相距只有 (); 的最后一次三体交会之后,最轻的那个被甩出去,另外两个结成双星[12]。这组初值对误差极其敏感,表 1 用两种容差各算一遍来确认结果已经收敛。The Pythagorean problem[11]: bodies of mass 3, 4 and 5 start at rest at the vertices of a 3-4-5 right triangle, each opposite the side of the same length. They pass through more than a dozen close encounters; the closest is between masses 4 and 5, at a distance of only (). After the last triple encounter at the lightest body is thrown out and the other two form a binary[12]. These initial conditions are extremely sensitive to error, so Table 1 runs them at two tolerances to confirm that the result has converged.
其余四条(蝴蝶、飞蛾、阴阳、蜻蜓)取自 Šuvakov 与 Dmitrašinović 在 2013 年用数值搜索找到的周期轨道[13]。原文的初值只有五位小数,积分几圈就散了;这里的初值是我用高斯–牛顿法迭代过的,直到“积分一个周期后回到原处”的残差降到 以下。The other four (butterfly, moth, yin-yang, dragonfly) are periodic orbits found by Šuvakov and Dmitrašinović in a numerical search in 2013[13]. The published initial conditions have five decimal places and come apart after a few periods; the values used here were refined by Gauss–Newton iteration until the residual of returning to the start after one period fell below .
3流匹配:让噪声流成一幅画Flow matching: letting noise flow into a picture
生成模型要做的事,是把一个容易采样的分布(高斯噪声)变成数据的分布。流匹配[14, 15]的办法是学一个速度场 ,让噪声点以它为速度,即按 运动,从 流到 。训练时并不需要解这个常微分方程。随机取一个噪声点 、一个数据点 和一个时刻 ,把两点连成直线,要求网络在途中的那一点报出这条线的方向:A generative model has to turn a distribution that is easy to sample (Gaussian noise) into the distribution of the data. Flow matching[14, 15] does this by learning a velocity field and moving noise points with it, , from to . Training does not require solving this differential equation. Draw a noise point , a data point and a time at random, join the two points by a straight line, and ask the network to report the direction of that line at the point part-way along it:
同一个点
图 3 一开始放的就是式 (9)。仔细看会发现,每个粒子最后都精确地落在某一个数据点上:损失函数的最优解只会背诵训练集。点“训练网络”换成神经网络以后,落点才开始铺满数据点之间的空隙。网络是一个光滑的函数,背不下每一个点,学到的反而是形状。Figure 3 starts out playing Eq. (9). Look closely and every particle ends exactly on one of the data points: the optimum of the loss can only recite the training set. Click Train network to switch to a neural network, and the end points begin to fill the gaps between the data. A network is a smooth function; it cannot memorise every point, so what it learns is the shape.
(a)
(b)
(c) 训练损失Training loss
- 粒子落点到最近数据点的平均距离Mean distance from each particle's end point to the nearest data point
- —
- 数据点到最近粒子的平均距离Mean distance from each data point to the nearest particle
- —
数据是单个高斯分布
式 (8) 里的
代码 3 一步训练:配对、插值、回归、反传(页面正在运行的源码)Listing 3 One training step: pairing, interpolation, regression, backpropagation (the source this page is running, with its comments translated)
4纠错码:涂掉一块还能读Error correction: readable with a patch painted out
二维码把数据切成字节,每个字节看作有限域
能被
图 4 可以亲手验证这个不等式。二维码的四个纠错等级 L、M、Q、H 大约分别能恢复 7%、15%、25%、30% 的码字;数据多的时候码字还会被分成若干块交织着摆放,让一片污渍的损失摊到每一块头上。Figure 4 lets you test this inequality by hand. The four error-correction levels of a QR code, L, M, Q and H, can restore roughly 7%, 15%, 25% and 30% of the codewords; when there is a lot of data the codewords are also split into several blocks and interleaved, so that the damage from one stain is shared among all the blocks.
代码 4 Reed–Solomon 译码:伴随式、Berlekamp–Massey、Chien 搜索、Forney 公式(页面正在运行的源码)Listing 4 Reed–Solomon decoding: syndromes, Berlekamp–Massey, Chien search, Forney's formula (the source this page is running, with its comments translated)
(a) 扫描器看到的样子What a scanner sees
(b) 码字地图Codeword map
数据码字data校验码字check功能图形function patterns读错的码字misread
(c) 译码器逐块的结论The decoder's verdict, block by block
5自检Self-check
前面每一节里都有“应该等于多少”这样的话。表 1 把它们写成断言,在你打开页面时用正在运行的这份代码当场核对。对照的依据有三类:解析结果(偏折角的弱场与强场极限、阴影的角半径、失稳的增长率、损失的最小值);另一种独立的算法(椭圆积分对直接积分常微分方程、反向传播对数值差分、两种容差的结果互相对照);以及穷举(匈牙利算法对全部 5040 种配对)。Every section above contains statements of the form this should equal that. Table 1 turns them into assertions and checks them, when you open the page, against the very code that is running. The references are of three kinds: analytic results (the weak- and strong-field limits of the deflection angle, the angular radius of the shadow, the growth rate of the instability, the minimum of the loss); a second, independent algorithm (elliptic integrals against direct integration of the differential equation, backpropagation against finite differences, two tolerances against each other); and exhaustive enumeration (the Hungarian algorithm against all 5040 pairings).
它不是摆设。写这份文件的过程中,“守恒量”一项最初是不通过的:8 字轨道跑完 100 圈,能量只漂了不到
表 1Table 1 自检报告。Self-check report.
| 图Fig. | 断言Assertion | 怎么核对How it is checked | 期望Expected | 实测Measured | 结论Result |
|---|
6没有做到的What this does not do
图 1 是史瓦西黑洞,不是带自转的克尔黑洞;盘是几何上无限薄、光学上不透明、各向同性发光的理想盘,没有算盘自己照亮自己。星空和盘面的纹理是程序生成的装饰,不是数据,纹理只是为了让转动看得见。图 2 只做了平面运动,也没有做碰撞正则化:两个天体贴到
参考文献References
- C. Darwin. The gravity field of a particle. Proceedings of the Royal Society A, 249: 180–194, 1959.
- V. Bozza. Gravitational lensing in the strong field limit. Physical Review D, 66: 103001, 2002.
- D. N. Page, K. S. Thorne. Disk-accretion onto a black hole. Time-averaged structure of accretion disk. The Astrophysical Journal, 191: 499–506, 1974.
- C. Wyman, P.-P. Sloan, P. Shirley. Simple analytic approximations to the CIE XYZ color matching functions. Journal of Computer Graphics Techniques, 2(2): 1–11, 2013.
- J.-P. Luminet. Image of a spherical black hole with thin accretion disk. Astronomy and Astrophysics, 75: 228–235, 1979.
- R. Bulirsch, J. Stoer. Numerical treatment of ordinary differential equations by extrapolation methods. Numerische Mathematik, 8: 1–13, 1966.
- C. Moore. Braids in classical dynamics. Physical Review Letters, 70: 3675–3679, 1993.
- A. Chenciner, R. Montgomery. A remarkable periodic solution of the three-body problem in the case of equal masses. Annals of Mathematics, 152: 881–901, 2000.
- C. Simó. Dynamical properties of the figure eight solution of the three-body problem. Contemporary Mathematics, 292: 209–228, 2002.
- E. J. Routh. On Laplace's three particles, with a supplement on the stability of steady motion. Proceedings of the London Mathematical Society, 6: 86–97, 1875.
- C. Burrau. Numerische Berechnung eines Spezialfalles des Dreikörperproblems. Astronomische Nachrichten, 195: 113–118, 1913.
- V. Szebehely, C. F. Peters. Complete solution of a general problem of three bodies. The Astronomical Journal, 72: 876–883, 1967.
- M. Šuvakov, V. Dmitrašinović. Three classes of Newtonian three-body planar periodic orbits. Physical Review Letters, 110: 114301, 2013.
- Y. Lipman, R. T. Q. Chen, H. Ben-Hamu, M. Nickel, M. Le. Flow matching for generative modeling. International Conference on Learning Representations, 2023.
- X. Liu, C. Gong, Q. Liu. Flow straight and fast: learning to generate and transfer data with rectified flow. International Conference on Learning Representations, 2023.
- A. Tong, K. Fatras, N. Malkin, G. Huguet, Y. Zhang, J. Rector-Brooks, G. Wolf, Y. Bengio. Improving and generalizing flow-based generative models with minibatch optimal transport. Transactions on Machine Learning Research, 2024.
- H. W. Kuhn. The Hungarian method for the assignment problem. Naval Research Logistics Quarterly, 2: 83–97, 1955.
- I. S. Reed, G. Solomon. Polynomial codes over certain finite fields. Journal of the Society for Industrial and Applied Mathematics, 8: 300–304, 1960.
- J. L. Massey. Shift-register synthesis and BCH decoding. IEEE Transactions on Information Theory, 15: 122–127, 1969.
- G. D. Forney. On decoding BCH codes. IEEE Transactions on Information Theory, 11: 549–557, 1965.