ndarray_linalg/
generate.rs

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! Generator functions for matrices

use ndarray::*;
use rand::prelude::*;

use super::convert::*;
use super::error::*;
use super::qr::*;
use super::types::*;

/// Hermite conjugate matrix
pub fn conjugate<A, Si, So>(a: &ArrayBase<Si, Ix2>) -> ArrayBase<So, Ix2>
where
    A: Scalar,
    Si: Data<Elem = A>,
    So: DataOwned<Elem = A> + DataMut,
{
    let mut a: ArrayBase<So, Ix2> = replicate(&a.t());
    for val in a.iter_mut() {
        *val = val.conj();
    }
    a
}

/// Generate random array with given shape
///
/// - This function uses [rand::thread_rng].
///   See [random_using] for using another RNG
pub fn random<A, S, Sh, D>(sh: Sh) -> ArrayBase<S, D>
where
    A: Scalar,
    S: DataOwned<Elem = A>,
    D: Dimension,
    Sh: ShapeBuilder<Dim = D>,
{
    let mut rng = thread_rng();
    random_using(sh, &mut rng)
}

/// Generate random array with given RNG
///
/// - See [random] for using default RNG
pub fn random_using<A, S, Sh, D, R>(sh: Sh, rng: &mut R) -> ArrayBase<S, D>
where
    A: Scalar,
    S: DataOwned<Elem = A>,
    D: Dimension,
    Sh: ShapeBuilder<Dim = D>,
    R: Rng,
{
    ArrayBase::from_shape_fn(sh, |_| A::rand(rng))
}

/// Generate random unitary matrix using QR decomposition
///
/// - Be sure that this it **NOT** a uniform distribution.
///   Use it only for test purpose.
/// - This function uses [rand::thread_rng].
///   See [random_unitary_using] for using another RNG.
pub fn random_unitary<A>(n: usize) -> Array2<A>
where
    A: Scalar + Lapack,
{
    let mut rng = thread_rng();
    random_unitary_using(n, &mut rng)
}

/// Generate random unitary matrix using QR decomposition with given RNG
///
/// - Be sure that this it **NOT** a uniform distribution.
///   Use it only for test purpose.
/// - See [random_unitary] for using default RNG.
pub fn random_unitary_using<A, R>(n: usize, rng: &mut R) -> Array2<A>
where
    A: Scalar + Lapack,
    R: Rng,
{
    let a: Array2<A> = random_using((n, n), rng);
    let (q, _r) = a.qr_into().unwrap();
    q
}

/// Generate random regular matrix
///
/// - Be sure that this it **NOT** a uniform distribution.
///   Use it only for test purpose.
/// - This function uses [rand::thread_rng].
///   See [random_regular_using] for using another RNG.
pub fn random_regular<A>(n: usize) -> Array2<A>
where
    A: Scalar + Lapack,
{
    let mut rng = rand::thread_rng();
    random_regular_using(n, &mut rng)
}

/// Generate random regular matrix with given RNG
///
/// - Be sure that this it **NOT** a uniform distribution.
///   Use it only for test purpose.
/// - See [random_regular] for using default RNG.
pub fn random_regular_using<A, R>(n: usize, rng: &mut R) -> Array2<A>
where
    A: Scalar + Lapack,
    R: Rng,
{
    let a: Array2<A> = random_using((n, n), rng);
    let (q, mut r) = a.qr_into().unwrap();
    for i in 0..n {
        r[(i, i)] = A::one() + A::from_real(r[(i, i)].abs());
    }
    q.dot(&r)
}

/// Random Hermite matrix
///
/// - This function uses [rand::thread_rng].
///   See [random_hermite_using] for using another RNG.
pub fn random_hermite<A, S>(n: usize) -> ArrayBase<S, Ix2>
where
    A: Scalar,
    S: DataOwned<Elem = A> + DataMut,
{
    let mut rng = rand::thread_rng();
    random_hermite_using(n, &mut rng)
}

/// Random Hermite matrix with given RNG
///
/// - See [random_hermite] for using default RNG.
pub fn random_hermite_using<A, S, R>(n: usize, rng: &mut R) -> ArrayBase<S, Ix2>
where
    A: Scalar,
    S: DataOwned<Elem = A> + DataMut,
    R: Rng,
{
    let mut a: ArrayBase<S, Ix2> = random_using((n, n), rng);
    for i in 0..n {
        a[(i, i)] = a[(i, i)] + a[(i, i)].conj();
        for j in (i + 1)..n {
            a[(i, j)] = a[(j, i)].conj();
        }
    }
    a
}

/// Random Hermite Positive-definite matrix
///
/// - Eigenvalue of matrix must be larger than 1 (thus non-singular)
/// - This function uses [rand::thread_rng].
///   See [random_hpd_using] for using another RNG.
///
pub fn random_hpd<A, S>(n: usize) -> ArrayBase<S, Ix2>
where
    A: Scalar,
    S: DataOwned<Elem = A> + DataMut,
{
    let mut rng = rand::thread_rng();
    random_hpd_using(n, &mut rng)
}

/// Random Hermite Positive-definite matrix with given RNG
///
/// - Eigenvalue of matrix must be larger than 1 (thus non-singular)
/// - See [random_hpd] for using default RNG.
///
pub fn random_hpd_using<A, S, R>(n: usize, rng: &mut R) -> ArrayBase<S, Ix2>
where
    A: Scalar,
    S: DataOwned<Elem = A> + DataMut,
    R: Rng,
{
    let a: Array2<A> = random_using((n, n), rng);
    let ah: Array2<A> = conjugate(&a);
    ArrayBase::eye(n) + &ah.dot(&a)
}

/// construct matrix from diag
pub fn from_diag<A>(d: &[A]) -> Array2<A>
where
    A: Scalar,
{
    let n = d.len();
    let mut e = Array::zeros((n, n));
    for i in 0..n {
        e[(i, i)] = d[i];
    }
    e
}

/// stack vectors into matrix horizontally
pub fn hstack<A, S>(xs: &[ArrayBase<S, Ix1>]) -> Result<Array<A, Ix2>>
where
    A: Scalar,
    S: Data<Elem = A>,
{
    let views: Vec<_> = xs.iter().map(|x| x.view()).collect();
    stack(Axis(1), &views).map_err(Into::into)
}

/// stack vectors into matrix vertically
pub fn vstack<A, S>(xs: &[ArrayBase<S, Ix1>]) -> Result<Array<A, Ix2>>
where
    A: Scalar,
    S: Data<Elem = A>,
{
    let views: Vec<_> = xs.iter().map(|x| x.view()).collect();
    stack(Axis(0), &views).map_err(Into::into)
}