clarabel/algebra/dense/blas/
symv.rs

1#![allow(non_snake_case)]
2
3use crate::algebra::{
4    DenseMatrix, FloatT, Matrix, MultiplySYMV, ShapedMatrix, Symmetric,
5};
6
7impl<T> MultiplySYMV for Symmetric<'_, Matrix<T>>
8where
9    T: FloatT,
10{
11    type T = T;
12    // implements y = αA*x + βy
13    fn symv(&self, x: &[Self::T], y: &mut [Self::T], α: Self::T, β: Self::T) {
14        let (m, n) = self.size();
15        assert!(m == n);
16
17        // standard BLAS ?symv arguments for computing matrix-vector product
18        let uplo = self.uplo.as_blas_char();
19        let n = n.try_into().unwrap();
20        let a = self.src.data();
21        let lda = n;
22        let incx = 1;
23        let incy = 1;
24        T::xsymv(uplo, n, α, a, lda, x, incx, β, y, incy);
25    }
26}
27
28macro_rules! generate_test_gsymv {
29    ($fxx:ty, $test_name:ident) => {
30        #[test]
31        fn $test_name() {
32            #[rustfmt::skip]
33            let A = Matrix::<$fxx>::from(&[
34                [ 1.,  2.,   4.], 
35                [ 0.,  3.,   5.], 
36                [ 0.,  0.,   6.],
37            ]);
38
39            let x = vec![1., -2., 3.];
40            let mut y = vec![-4., -1., 3.];
41            A.sym_up().symv(&x, &mut y, 2.0, 3.0);
42            assert_eq!(y, [6.0, 19.0, 33.0]);
43
44            #[rustfmt::skip]
45            let A = Matrix::<$fxx>::from(&[
46                [ 1.,  0.,   0.], 
47                [ 2.,  3.,   0.], 
48                [ 4.,  5.,   6.],
49            ]);
50
51            let x = vec![1., -2., 3.];
52            let mut y = vec![-4., -1., 3.];
53            A.sym_lo().symv(&x, &mut y, 2.0, 3.0);
54            assert_eq!(y, [6.0, 19.0, 33.0]);
55        } 
56    };
57}
58
59generate_test_gsymv!(f32, test_gsymv_f32);
60generate_test_gsymv!(f64, test_gsymv_f64);