计算半径 R 和维度 D 的球体内的整数点
Counting integer points inside a sphere of radius R and dimension D
我正在尝试编写一种有效的算法来计算半径 R 和维度 D 的球体内的点数。球体始终位于原点。假设我们有一个半径为 5 的 2 维球体(圆)。
我的策略是在第一象限内生成所有可能的点,所以对于上面的例子,我们知道 (1,2) 在圆内,所以必须是该点的所有 + / - 组合,这只是维度平方。因此,对于在 n 维球体的单个象限中找到的每个点,我们将 2 ^ 维度添加到总计数中。
我不确定这个问题是否有更有效的解决方案,但这是我目前在实施方面所拥有的。
int count_lattice_points(const double radius, const int dimension) {
int R = static_cast<int>(radius);
int count = 0;
std::vector<int> points;
std::vector<int> point;
for(int i = 0; i <= R; i++)
points.push_back(i);
do {
for(int i = 0; i < dimension - 1; i++)
point.push_back(points.at(i));
if(isPointWithinSphere(point, radius)) count += std::pow(2,dimension);
point.clear();
}while(std::next_permutation(points.begin(), points.end()));
return count + 3;
}
在这种情况下我可以解决或改进什么?
对于 2D 情况,这是 Gauss's circle problem. 一种可能的公式:
N(r) = 1 + 4 * r + 4 * Sum[i=1..r]{Floor(Sqrt(r^2-i^2))}
(中心点+四个象限,4*r为轴上点,其他为象限内区域)。
请注意,二维情况下没有已知的简单闭合数学表达式。
总的来说,你对象限、八分圆等的想法是正确的,但检查所有的点太昂贵了。
人们可能会找到从 1..D 组成从 0 到 r^2 的所有正方形的方法的数量
整数平方((4)公式的扩展).
请注意,组合学有助于加快计算速度。例如,找到方法的数量就足够了
从 D 个自然方块中生成 X^2,并乘以 2^D(不同的符号组合);找出从 D-1 个自然方块中生成 X^2 的方法数,并乘以 D*2^(D-1)(不同的符号组合 + D 位为零加数)等
D=2、R=3 的例子
addends: 0,1,4,9
possible sum compositions number of variants
0 0+0 1
1 0+1,1+0 2*2=4
2 1+1 4
4 0+4,4+0 2*2=4
5 1+4,4+1 2*4=8
8 4+4 4
9 0+9,9+0 2*2=4
-------------------------------------
29
类似于 的方法,包括源代码,可以在以下位置找到
https://monsiterdex.wordpress.com/2013/04/05/integer-lattice-in-n-dimensional-sphere-count-of-points-with-integer-coordinates-using-parallel-programming-part-i/.
该方法包括找到半径的分区,然后为球体中的每个分区计算它可以通过置换坐标和翻转非零坐标的符号在球体中表示的方式的数量。
我在这里展示了我的 2D 算法(带有一些源代码和一个丑陋但方便的插图):
它比 计算原点和其中一个圆圈边缘之间的点快 3.4 倍。
你只要想象一个内切的正方形,然后只计算那个圆圈内那个正方形外面的八分之一。
public static int gaussCircleProblem(int radius) {
int allPoints=0; //holds the sum of points
double y=0; //will hold the precise y coordinate of a point on the circle edge for a given x coordinate.
long inscribedSquare=(long) Math.sqrt(radius*radius/2); //the length of the side of an inscribed square in the upper right quarter of the circle
int x=(int)inscribedSquare; //will hold x coordinate - starts on the edge of the inscribed square
while(x<=radius){
allPoints+=(long) y; //returns floor of y, which is initially 0
x++; //because we need to start behind the inscribed square and move outwards from there
y=Math.sqrt(radius*radius-x*x); // Pythagorean equation - returns how many points there are vertically between the X axis and the edge of the circle for given x
}
allPoints*=8; //because we were counting points in the right half of the upper right corner of that circle, so we had just one-eightth
allPoints+=(4*inscribedSquare*inscribedSquare); //how many points there are in the inscribed square
allPoints+=(4*radius+1); //the loop and the inscribed square calculations did not touch the points on the axis and in the center
return allPoints;
}
我正在尝试编写一种有效的算法来计算半径 R 和维度 D 的球体内的点数。球体始终位于原点。假设我们有一个半径为 5 的 2 维球体(圆)。
我的策略是在第一象限内生成所有可能的点,所以对于上面的例子,我们知道 (1,2) 在圆内,所以必须是该点的所有 + / - 组合,这只是维度平方。因此,对于在 n 维球体的单个象限中找到的每个点,我们将 2 ^ 维度添加到总计数中。
我不确定这个问题是否有更有效的解决方案,但这是我目前在实施方面所拥有的。
int count_lattice_points(const double radius, const int dimension) {
int R = static_cast<int>(radius);
int count = 0;
std::vector<int> points;
std::vector<int> point;
for(int i = 0; i <= R; i++)
points.push_back(i);
do {
for(int i = 0; i < dimension - 1; i++)
point.push_back(points.at(i));
if(isPointWithinSphere(point, radius)) count += std::pow(2,dimension);
point.clear();
}while(std::next_permutation(points.begin(), points.end()));
return count + 3;
}
在这种情况下我可以解决或改进什么?
对于 2D 情况,这是 Gauss's circle problem. 一种可能的公式:
N(r) = 1 + 4 * r + 4 * Sum[i=1..r]{Floor(Sqrt(r^2-i^2))}
(中心点+四个象限,4*r为轴上点,其他为象限内区域)。
请注意,二维情况下没有已知的简单闭合数学表达式。
总的来说,你对象限、八分圆等的想法是正确的,但检查所有的点太昂贵了。
人们可能会找到从 1..D 组成从 0 到 r^2 的所有正方形的方法的数量 整数平方((4)公式的扩展).
请注意,组合学有助于加快计算速度。例如,找到方法的数量就足够了 从 D 个自然方块中生成 X^2,并乘以 2^D(不同的符号组合);找出从 D-1 个自然方块中生成 X^2 的方法数,并乘以 D*2^(D-1)(不同的符号组合 + D 位为零加数)等
D=2、R=3 的例子
addends: 0,1,4,9
possible sum compositions number of variants
0 0+0 1
1 0+1,1+0 2*2=4
2 1+1 4
4 0+4,4+0 2*2=4
5 1+4,4+1 2*4=8
8 4+4 4
9 0+9,9+0 2*2=4
-------------------------------------
29
类似于
该方法包括找到半径的分区,然后为球体中的每个分区计算它可以通过置换坐标和翻转非零坐标的符号在球体中表示的方式的数量。
我在这里展示了我的 2D 算法(带有一些源代码和一个丑陋但方便的插图):
它比
你只要想象一个内切的正方形,然后只计算那个圆圈内那个正方形外面的八分之一。
public static int gaussCircleProblem(int radius) {
int allPoints=0; //holds the sum of points
double y=0; //will hold the precise y coordinate of a point on the circle edge for a given x coordinate.
long inscribedSquare=(long) Math.sqrt(radius*radius/2); //the length of the side of an inscribed square in the upper right quarter of the circle
int x=(int)inscribedSquare; //will hold x coordinate - starts on the edge of the inscribed square
while(x<=radius){
allPoints+=(long) y; //returns floor of y, which is initially 0
x++; //because we need to start behind the inscribed square and move outwards from there
y=Math.sqrt(radius*radius-x*x); // Pythagorean equation - returns how many points there are vertically between the X axis and the edge of the circle for given x
}
allPoints*=8; //because we were counting points in the right half of the upper right corner of that circle, so we had just one-eightth
allPoints+=(4*inscribedSquare*inscribedSquare); //how many points there are in the inscribed square
allPoints+=(4*radius+1); //the loop and the inscribed square calculations did not touch the points on the axis and in the center
return allPoints;
}