在 XY 网格中遵循 1/R 密度分布的点? [英] Points that follow a 1/R density distribution in an XY grid?

查看:23
本文介绍了在 XY 网格中遵循 1/R 密度分布的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XY 网格,其中一些网格点具有分配给它们的某些值,在这种情况下,每个值都表示一定的质量,所以基本上是网格中的点质量.我现在想要获得一组遵循 1/R 密度分布的点,其中 R 是到中心的距离,所以 R = sqrt(x^2 + y^2).通过密度分布,我的意思是点的数量必须下降为 1/R.我将如何进行编码?

I have a XY grid with some gridpoints having certain values assigned to them, in this case, each value means a certain mass, so basically point masses in a grid. I now want to obtain a set of points which follow a density distribution of 1/R, where R is the distance from the center, so R = sqrt(x^2 + y^2). By density distribution, I mean the number of points has to fall off as 1/R. How would I go about coding this?

我的代码如下:

import numpy as np
x = np.linspace(-50,50,100)
y = np.linspace(-50,50,100)
X, Y = np.meshgrid(x,y)
zeta_a = (25,25)
zeta_b = (-10,5) 
M_a = 150
M_b = 150 

zeta_a 和 zeta_b 对应于 2 个质量为 150 单位的点质量.我还需要使用这些点进行后续计算,所以我也想知道如何使用更通用的格式,而不是使用 'a'、'b'、'c' 来表示 n 点质量.

The zeta_a and zeta_b correspond to 2 point masses having masses of 150 units. I also need to perform follow up calculations using these points, so i'd also like to know how to use a more general format rather than using 'a','b','c' for n-point masses.

感谢您的帮助.

推荐答案

假设我理解你的问题(如果没有,欢迎评论):

Assuming I understood your question (if not comments are welcomed):

创建任何给定分布的方法是对分布 CDF 的逆进行插值.这是我的功能:

The way to create any given distribution is by interpolating over the inverse of the distribution CDF. This is my function to do it:

import numpy as np
import matplotlib.pyplot as plt

def randdist(PDF, x, n):
    """Create a distribution following PDF(x). PDF and x
    must be of the same length. n is the number of samples."""
    fp = np.random.rand(n,)
    CDF = np.cumsum(PDF)
    return np.interp(fp, CDF, x)

现在,在您的情况下,我们将在极坐标中工作,R 分布为 1/r,Theta 均匀分布:

Now, in your case we're going to work in polar coordinates with R distributed as 1/r and Theta uniformly distributed:

num = 1000   # The number of points
r = np.linspace(-50, 50, 100)
PDF = np.abs(1/r)
PDF = PDF/np.sum(PDF)    # PDF should be normalized
R = randdist(PDF, r, num)
Theta = 2*np.pi*np.random.rand(num,)

现在让我们创建点 x 和 y 向量

Now let's create the points x and y vectors

x = [R[k]*np.cos(Theta[k]) for k in range(num)]
y = [R[k]*np.sin(Theta[k]) for k in range(num)]

绘制

plot(x,y,'.')

请注意,在我的回答中,在 r=50 处有一个硬截止.有办法克服这个问题,但现在我保持原样.

Note that in my answer there is a hard cutoff at r=50. There are ways to overcome this but for now I leave it as it is.

现在您似乎还想将点嵌入到 2D 网格中,就像直方图一样.您可以使用

Now you seem to also want to embed the points inside a 2D grid, much like a histogram. You can do that using

z, _, _ = np.histogram2d(x, y, [100, 100])

这篇关于在 XY 网格中遵循 1/R 密度分布的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆