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
use crate::ffi::{self, blas_int, c_char};
use crate::util::*;
use derive_builder::Builder;
use ndarray::prelude::*;

/* #region BLAS func */

pub trait SPR2Func<F>
where
    F: BLASFloat,
{
    unsafe fn spr2(
        uplo: *const c_char,
        n: *const blas_int,
        alpha: *const F,
        x: *const F,
        incx: *const blas_int,
        y: *const F,
        incy: *const blas_int,
        ap: *mut F,
    );
}

macro_rules! impl_func {
    ($type: ty, $func: ident) => {
        impl SPR2Func<$type> for BLASFunc
        where
            $type: BLASFloat,
        {
            unsafe fn spr2(
                uplo: *const c_char,
                n: *const blas_int,
                alpha: *const $type,
                x: *const $type,
                incx: *const blas_int,
                y: *const $type,
                incy: *const blas_int,
                ap: *mut $type,
            ) {
                ffi::$func(uplo, n, alpha, x, incx, y, incy, ap);
            }
        }
    };
}

impl_func!(f32, sspr2_);
impl_func!(f64, dspr2_);
impl_func!(c32, chpr2_);
impl_func!(c64, zhpr2_);

/* #endregion */

/* #region BLAS driver */

pub struct SPR2_Driver<'x, 'y, 'a, F>
where
    F: BLASFloat,
{
    uplo: c_char,
    n: blas_int,
    alpha: F,
    x: ArrayView1<'x, F>,
    incx: blas_int,
    y: ArrayView1<'y, F>,
    incy: blas_int,
    ap: ArrayOut1<'a, F>,
}

impl<'x, 'y, 'a, F> BLASDriver<'a, F, Ix1> for SPR2_Driver<'x, 'y, 'a, F>
where
    F: BLASFloat,
    BLASFunc: SPR2Func<F>,
{
    fn run_blas(self) -> Result<ArrayOut1<'a, F>, BLASError> {
        let Self { uplo, n, alpha, x, incx, y, incy, mut ap } = self;
        let x_ptr = x.as_ptr();
        let y_ptr = y.as_ptr();
        let ap_ptr = ap.get_data_mut_ptr();

        // assuming dimension checks has been performed
        // unconditionally return Ok if output does not contain anything
        if n == 0 {
            return Ok(ap.clone_to_view_mut());
        }

        unsafe {
            BLASFunc::spr2(&uplo, &n, &alpha, x_ptr, &incx, y_ptr, &incy, ap_ptr);
        }
        return Ok(ap.clone_to_view_mut());
    }
}

/* #endregion */

/* #region BLAS builder */

#[derive(Builder)]
#[builder(pattern = "owned", build_fn(error = "BLASError"), no_std)]
pub struct SPR2_<'x, 'y, 'a, F>
where
    F: BLASFloat,
{
    pub x: ArrayView1<'x, F>,
    pub y: ArrayView1<'y, F>,

    #[builder(setter(into, strip_option), default = "None")]
    pub ap: Option<ArrayViewMut1<'a, F>>,
    #[builder(setter(into), default = "F::one()")]
    pub alpha: F,
    #[builder(setter(into), default = "BLASUpper")]
    pub uplo: BLASUpLo,
    #[builder(setter(into, strip_option), default = "None")]
    pub layout: Option<BLASLayout>,
}

impl<'x, 'y, 'a, F> BLASBuilder_<'a, F, Ix1> for SPR2_<'x, 'y, 'a, F>
where
    F: BLASFloat,
    BLASFunc: SPR2Func<F>,
{
    fn driver(self) -> Result<SPR2_Driver<'x, 'y, 'a, F>, BLASError> {
        let Self { x, y, ap, alpha, uplo, layout, .. } = self;

        // initialize intent(hide)
        let incx = x.stride_of(Axis(0));
        let incy = y.stride_of(Axis(0));
        let n = x.len_of(Axis(0));

        // only fortran-preferred (col-major) is accepted in inner wrapper
        assert_eq!(layout, Some(BLASColMajor));

        // check optional
        blas_assert_eq!(y.len_of(Axis(0)), n, InvalidDim)?;

        // prepare output
        let ap = match ap {
            Some(ap) => {
                blas_assert_eq!(ap.len_of(Axis(0)), n * (n + 1) / 2, InvalidDim)?;
                if ap.is_standard_layout() {
                    ArrayOut1::ViewMut(ap)
                } else {
                    let ap_buffer = ap.view().to_seq_layout()?.into_owned();
                    ArrayOut1::ToBeCloned(ap, ap_buffer)
                }
            },
            None => ArrayOut1::Owned(Array1::zeros(n * (n + 1) / 2)),
        };

        // finalize
        let driver = SPR2_Driver {
            uplo: uplo.into(),
            n: n.try_into()?,
            alpha,
            x,
            incx: incx.try_into()?,
            y,
            incy: incy.try_into()?,
            ap,
        };
        return Ok(driver);
    }
}

/* #endregion */

/* #region BLAS wrapper */

pub type SPR2<'x, 'y, 'a, F> = SPR2_Builder<'x, 'y, 'a, F>;
pub type SSPR2<'x, 'y, 'a> = SPR2<'x, 'y, 'a, f32>;
pub type DSPR2<'x, 'y, 'a> = SPR2<'x, 'y, 'a, f64>;

pub type HPR2<'x, 'y, 'a, F> = SPR2_Builder<'x, 'y, 'a, F>;
pub type CHPR2<'x, 'y, 'a> = HPR2<'x, 'y, 'a, c32>;
pub type ZHPR2<'x, 'y, 'a> = HPR2<'x, 'y, 'a, c64>;

impl<'x, 'y, 'a, F> BLASBuilder<'a, F, Ix1> for SPR2_Builder<'x, 'y, 'a, F>
where
    F: BLASFloat,
    BLASFunc: SPR2Func<F>,
{
    fn run(self) -> Result<ArrayOut1<'a, F>, BLASError> {
        // initialize
        let obj = self.build()?;

        if obj.layout == Some(BLASColMajor) {
            // F-contiguous
            return obj.driver()?.run_blas();
        } else {
            // C-contiguous
            let uplo = obj.uplo.flip();
            if F::is_complex() {
                let x = obj.x.mapv(F::conj);
                let y = obj.y.mapv(F::conj);
                let obj = SPR2_ { y: x.view(), x: y.view(), uplo, layout: Some(BLASColMajor), ..obj };
                return obj.driver()?.run_blas();
            } else {
                let obj = SPR2_ { uplo, x: obj.y, y: obj.x, layout: Some(BLASColMajor), ..obj };
                return obj.driver()?.run_blas();
            };
        }
    }
}

/* #endregion */