-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrandom.py
More file actions
99 lines (81 loc) · 3.95 KB
/
crandom.py
File metadata and controls
99 lines (81 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import numpy as np
def crandomu(size=None, dtype=np.complex128, out=None):
"""
crandomu(size=None, dtype=np.complex128, out=None)
Returns uniformly distributed random complex numbers on the complex unit circle.
Parameters
----------
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
single value is returned.
dtype : dtype, optional
Desired dtype of the result, only `complex128` and `complex64` are supported.
Byteorder must be native. The default value is np.complex128.
out : ndarray, optional
Alternative output array in which to place the result. If size is not None,
it must have the same shape as the provided size and must match the type of
the output values.
Returns
-------
out : complex number or ndarray of complex numbers
Array of random complex numbers of shape `size` (unless ``size=None``, in which
case a single complex number is returned).
"""
return np.exp(2j*np.pi * np.random.default_rng().random(size=size), dtype=dtype, out=out)
def crandom(size=None, dtype=np.complex128, out=None):
"""
crandom(size=None, dtype=np.complex128, out=None)
Returns uniformly distributed random complex numbers within the open complex unit circle.
Parameters
----------
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
single value is returned.
dtype : dtype, optional
Desired dtype of the result, only `complex128` and `complex64` are supported.
Byteorder must be native. The default value is np.complex128.
out : ndarray, optional
Alternative output array in which to place the result. If size is not None,
it must have the same shape as the provided size and must match the type of
the output values.
Returns
-------
out : complex number or ndarray of complex numbers
Array of random complex numbers of shape `size` (unless ``size=None``, in which
case a single complex number is returned).
"""
rng = np.random.default_rng()
return np.multiply(np.sqrt(rng.random(size=size)), np.exp(2j*np.pi * rng.random(size=size)), dtype=dtype, out=out)
def crandomn(loc=0j, scale=((1, 0), (0, 1)), size=None, dtype=np.complex128, out=None):
"""
crandomn(loc=0j, scale=((1, 0), (0, 1)), size=None, dtype=np.complex128, out=None)
Returns normal distributed random complex numbers.
Parameters
----------
loc : complex number, optional
Mean (“centre”) of the distribution. Default is 0.
scale: 2-D array_like of shape (2, 2), optional
Covariance matrix of the distribution.
It must be symmetric and positive-semidefinite for proper sampling. Default is ``((1, 0), (0, 1))``.
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
single value is returned.
dtype : dtype, optional
Desired dtype of the result, only `complex128` and `complex64` are supported.
Byteorder must be native. The default value is np.complex128.
out : ndarray, optional
Alternative output array in which to place the result. If size is not None,
it must have the same shape as the provided size and must match the type of
the output values.
Returns
-------
out : complex number or ndarray of complex numbers
Array of random complex numbers of shape `size` (unless ``size=None``, in which
case a single complex number is returned).
"""
rng = np.random.default_rng()
M = rng.multivariate_normal((loc.real, loc.imag), scale, size=size)
return M[..., 0] + 1j*M[..., 1]