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

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

问题描述

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



我的代码如下:

  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对应于质量为150个单位的2点质量。我还需要使用这些点执行后续计算,所以我也想知道如何使用更一般的格式,而不是使用'a','b','c'来表示n点群众。



感谢您的帮助。

解决方案

假设我理解你的问题欢迎您发表评论):

创建任何给定分布的方法是在分布CDF的逆矩阵上进行插值。这是我的功能:

  import numpy as np 
import matplotlib.pyplot as plt

def randdist(PDF,x,n):
在PDF(x)之后创建一个分布PDF和x
必须具有相同的长度n是样本数。
fp = np.random.rand(n,)
CDF = np.cumsum(PDF)
返回np.interp(fp,CDF,x)

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

  num = 1000#点数
r = np.linspace(-50,50,100)
PDF = np.abs(1 / r)
PDF = PDF / np.sum(PDF)#PDF应标准化
R = randdist(PDF,r,num)
Theta = 2 * np.pi * np.random.rand(num,)

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

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

绘图

 绘图(x,y,'。')



请注意,在我的答案中,在r = 50时存在硬切断。有很多方法可以解决这个问题,但现在我保持原样。

现在你似乎也想在2D网格中嵌入点,就像直方图。您可以使用

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


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?

My code is below:

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 

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.

Thanks for your help.

解决方案

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

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)

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,)

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)]

To plot

plot(x,y,'.')

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.

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天全站免登陆