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
use super::ComplexMatrix;
use crate::{to_i32, Complex64};
extern "C" {
// Scales a vector by a constant
// <https://www.netlib.org/lapack/explore-html/d2/d74/zscal_8f.html>
fn cblas_zscal(n: i32, alpha: *const Complex64, x: *mut Complex64, incx: i32);
}
/// (zscal) Scales matrix
///
/// ```text
/// a := alpha * a
/// ```
///
/// See also <https://www.netlib.org/lapack/explore-html/d2/d74/zscal_8f.html>
///
/// # Examples
///
/// ```
/// use russell_lab::*;
///
/// fn main() -> Result<(), StrError> {
/// #[rustfmt::skip]
/// let mut a = ComplexMatrix::from(&[
/// [cpx!( 6.0, 3.0), cpx!( 9.0, -3.0)],
/// [cpx!(-6.0, 3.0), cpx!(-9.0, -3.0)],
/// ]);
///
/// complex_mat_scale(&mut a, cpx!(1.0 / 3.0, 0.0));
///
/// #[rustfmt::skip]
/// let a_correct = ComplexMatrix::from(&[
/// [cpx!( 2.0, 1.0), cpx!( 3.0, -1.0)],
/// [cpx!(-2.0, 1.0), cpx!(-3.0, -1.0)],
/// ]);
///
/// complex_mat_approx_eq(&a, &a_correct, 1e-15);
/// Ok(())
/// }
/// ```
pub fn complex_mat_scale(a: &mut ComplexMatrix, alpha: Complex64) {
let (m, n) = a.dims();
let mn_i32 = to_i32(m * n);
unsafe {
cblas_zscal(mn_i32, &alpha, a.as_mut_data().as_mut_ptr(), 1);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use super::{complex_mat_scale, ComplexMatrix};
use crate::{complex_mat_approx_eq, cpx, Complex64};
#[test]
fn complex_mat_scale_works() {
#[rustfmt::skip]
let mut a = ComplexMatrix::from(&[
[cpx!( 6.0, 1.0), cpx!( 9.0, -1.0), cpx!( 12.0, 1.0)],
[cpx!(-6.0, 1.0), cpx!(-9.0, -1.0), cpx!(-12.0, 2.0)],
]);
complex_mat_scale(&mut a, cpx!(2.0, 3.0));
#[rustfmt::skip]
let correct = ComplexMatrix::from(&[
[cpx!( 9.0, 20.0), cpx!( 21.0, 25.0), cpx!( 21.0, 38.0)],
[cpx!(-15.0, -16.0), cpx!(-15.0, -29.0), cpx!(-30.0, -32.0)],
]);
complex_mat_approx_eq(&a, &correct, 1e-15);
}
}