sparsetools/csr/
ccsr.rs

1use crate::csc::CSC;
2use crate::csr::CSR;
3use crate::traits::{Complex, Float, Integer};
4
5pub trait CCSR<I, F, C> {
6    fn with_real(re: &CSR<I, F>) -> Self;
7    fn with_imag(im: &CSR<I, F>) -> Self;
8    fn conj(&self) -> Self;
9    fn real(&self) -> CSR<I, F>;
10    fn imag(&self) -> CSR<I, F>;
11    /// Returns the conjugate transpose of self.
12    fn h(&self) -> CSC<I, C>;
13}
14
15impl<I, F, C> CCSR<I, F, C> for CSR<I, C>
16where
17    I: Integer,
18    F: Float,
19    C: Complex<F>,
20{
21    fn with_real(re: &CSR<I, F>) -> Self {
22        Self::new(
23            re.rows(),
24            re.cols(),
25            re.rowptr.clone(),
26            re.colidx.clone(),
27            re.values.iter().map(|v| C::new(*v, F::zero())).collect(),
28        )
29        .unwrap()
30    }
31
32    fn with_imag(im: &CSR<I, F>) -> Self {
33        Self::new(
34            im.rows(),
35            im.cols(),
36            im.rowptr.clone(),
37            im.colidx.clone(),
38            im.values.iter().map(|v| C::new(F::zero(), *v)).collect(),
39        )
40        .unwrap()
41    }
42
43    fn conj(&self) -> CSR<I, C> {
44        CSR::new(
45            self.rows(),
46            self.cols(),
47            self.rowptr().to_vec(),
48            self.colidx().to_vec(),
49            self.values().iter().map(|d| d.conj()).collect(),
50        )
51        .unwrap()
52    }
53
54    fn real(&self) -> CSR<I, F> {
55        CSR::new(
56            self.rows(),
57            self.cols(),
58            self.rowptr().to_vec(),
59            self.colidx().to_vec(),
60            self.values().iter().map(|d| d.real()).collect(),
61        )
62        .unwrap()
63    }
64
65    fn imag(&self) -> CSR<I, F> {
66        CSR::new(
67            self.rows(),
68            self.cols(),
69            self.rowptr().to_vec(),
70            self.colidx().to_vec(),
71            self.values().iter().map(|d| d.imag()).collect(),
72        )
73        .unwrap()
74    }
75
76    fn h(&self) -> CSC<I, C> {
77        self.conj().transpose()
78    }
79}