简体   繁体   English

如何在 python 中生成统一的随机复数

[英]how to generate uniform random complex numbers in python

For my research, I need to generate uniform random complex numbers.对于我的研究,我需要生成统一的随机复数。 How to do this in python because there is no such module to generate the complex numbers.如何在 python 中执行此操作,因为没有这样的模块来生成复数。

Your question is underspecified, you need to say from what region of the complex plane you want to draw your uniformly distributed numbers.您的问题未详细说明,您需要说明要从复平面的哪个区域绘制均匀分布的数字。 For uniformly sampling real numbers, this is true as well.对于均匀采样实数,这也是正确的。 However, in the real case there is a very obvious choice, namely the interval [0, 1).但是,在实际情况中,有一个非常明显的选择,即区间 [0, 1)。 You can see, for example that numpy.random.uniform per default samples from this interval.例如,您可以看到numpy.random.uniform来自此间隔的每个默认样本。

I will present some solution for regions of the complex plane that could be useful, but ultimately, the choice that is right for you will depend on your application.我将为复平面的区域提出一些可能有用的解决方案,但最终,适合您的选择将取决于您的应用程序。

Assume np is numpy and that we want to genereate an array of many such random numbers with shape shape .假设npnumpy并且我们想要生成一个包含许多形状为shape的随机数的数组。

A square centered at the origin以原点为中心的正方形

Ie sampling uniformly from all complex numbers z such that both real and imaginary part are in [-1,1].即从所有复数 z 中均匀采样,使得实部和虚部都在 [-1,1] 中。 You can generate such complex numbers eg via您可以生成这样的复数,例如通过

np.random.uniform(-1, 1, shape) + 1.j * np.random.uniform(-1, 1, shape)

A disc centered at the origin以原点为中心的圆盘

Ie sampling uniformly from all complex numbers with absolute value in [0,1].即从所有复数中均匀采样,绝对值在[0,1]。 You can generate them eg as您可以生成它们,例如

np.sqrt(np.random.uniform(0, 1, shape)) * np.exp(1.j * np.random.uniform(0, 2 * np.pi, shape))

Explanation: We can parametrize points in the disc as z = r * exp(ia).解释:我们可以将圆盘中的点参数化为 z = r * exp(ia)。 Uniform distribution of z in the disc means that the angle a is uniform in [0, 2pi] but the radius is non-uniform (intuition: in the disc there are more points with larger radius than with a small one). z 在圆盘中的均匀分布意味着角度 a 在 [0, 2pi] 内是均匀的,但半径是不均匀的(直觉:圆盘中半径较大的点比半径小的点多)。 the radius has a probability density of p(r) = 2r on the interval [0, 1], and a CDF (integral of p(r)) of F(r) = r^2, inverse CDF sampling then allows us to sample such radii as X = F^{-1}(Y) = sqrt(Y) where Y is uniformly distributed.半径在区间 [0, 1] 上具有 p(r) = 2r 的概率密度,以及 F(r) = r^2 的 CDF(p(r) 的整数),然后逆 CDF 采样允许我们采样半径为 X = F^{-1}(Y) = sqrt(Y) 其中 Y 是均匀分布的。

Is it not enough to do:这样做还不够:

a = np.random.uniform(1,10,10)
b = a + a * <some constant>j

I think this stays uniform.我认为这保持统一。

array([7.51553061 +9.01863673j, 1.53844779 +1.84613735j,
       2.33666459 +2.80399751j, 9.44081138+11.32897366j,
       7.47316887 +8.96780264j, 6.96193206 +8.35431847j,
       9.13933486+10.96720183j, 2.10023098 +2.52027718j,
       4.70705458 +5.6484655j , 8.02055689 +9.62466827j])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM