受这段视频无穷级数的启发。
Pi定义为圆周与圆直径之比。但是如何定义一个圆呢?通常,圆被定义为与中心点有恒定距离的点(假设中心位于(0,0)
)。下一个问题是:我们如何定义距离?在以下中,我们考虑了不同的距离概念(由Lp
-norms诱导):
给定一个范数(=度量长度的东西),我们可以很容易地构造一个距离(=两点之间的距离)如下:
dist(A,B) := norm (A-B)
欧几里得范数是由下列人士提供的:
norm((x,y)) = (x^2 + y^2)^(1/2)
这也被称为L2-范数。另一个Lp-范数是通过将上述公式中的2
替换为1到无穷之间的其他值来构造的:
norm_p((x,y)) = (|x|^p + |y|^p)^(1/p)
这些不同规范的单位圆圈有相当不同的形状:
给定一个p >= 1
,计算一个Lp-圆相对于Lp
-norm的圆周与直径的比值,其精度为四个显着性数字。
对于p,q
和1 = 1/p + 1/q
,我们可以使用它,得到Lp
和Lq
范数的相同比率。此外,对于p = q = 2
,这个比率是最小的,对于p = 1, q = infinity
,我们得到了一个4的比率,所以这个比率总是在pi
和4
之间。
p or q ratio
1 infinity 4
2 2 3.141592
1.623 2.60513 3.200
1.5 3 3.25976
4 1.33333 3.39693
发布于 2017-01-07 23:09:13
from scipy.integrate import*
lambda p:2/p*quad(lambda x:(x/x**p+(1-x)**(1-p))**(1/p),0,1)[0]
公式来自这个math.SE问题。
发布于 2017-01-08 03:31:20
0:1e-3:1lyG^-lG/^v!d|G^!slG/^sE
这产生在1001点取样的单位圆的四分之一的x,y坐标,在x中的步骤0.001。圆的四分之一的长度近似于通过这些点的多边形线的长度,即1000个分段的长度之和。长度当然是根据p
-norm计算的。将结果乘以2,得到半圆的近似长度,即π。
0:1e-3:1 % Push [0 0.001 0.002 ... 0.999 1]. These are the x coordinates of
% the vertices of the polygonal line that will approximate a quarter
% of the unit circle
l % Push 1
y % Duplicate [0 0.001 0.002 ... 0.999 1] onto the top of the stack.
G % Push input, p
^ % Element-wise power: gives [0^p 0.001^p ... 1^p]
- % Element-wise subtract from 1: gives [1-0^p 1-0.001^p ... 1-1^p]
lG/ % Push 1, push p, divide: gives 1/p
^ % Element-wise power: gives [(1-0^p)^(1/p) (1-0.001^p)^(1/p) ...
% ... (1-1^p)^(1/p)]. These are the y coordinates of the vertices
% of the polygonal line
v % Concatenate vertically into a 2×1001 matrix. The first row contains
% the x coordinates and the second row contains the y coordinates
! % Transpose
d| % Compute consecutive differences down each column. This gives a
% 1000×2 matrix with the x and y increments of each segment. These
% increments will be referred to as Δx, Δy
G % Push p
^ % Element-wise power
! % Transpose
s % Sum of each column. This gives a 1×1000 vector containing
% (Δx)^p+(Δy)^p for each segment
lG/ % Push 1/p
^ % Element-wise power. This gives a 1×1000 vector containing
% ((Δx)^p+(Δy)^p)^(1/p) for each segment, that is, the length of
% each segment according to p-norm
s % Sum the lenghts of all segments. This approximates the length of
% a quarter of the unit circle
E % Multiply by 2. This gives the length of half unit circle, that is,
% pi. Implicitly display
发布于 2022-05-05 01:27:11
https://codegolf.stackexchange.com/questions/106035
复制相似问题