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
use crate::util::*;
use blas_sys::{c_double_complex, c_float_complex};
use libc::{c_double, c_float};
use ndarray::Dimension;
use num_complex::*;
use num_traits::*;

#[allow(non_camel_case_types)]
pub type c32 = Complex<f32>;
#[allow(non_camel_case_types)]
pub type c64 = Complex<f64>;

/// Trait for defining real part float types
pub trait BLASFloat:
    Num + NumAssignOps + Send + Sync + Copy + Clone + Default + std::fmt::Debug + std::fmt::Display
{
    type RealFloat: BLASFloat;
    type FFIFloat;
    const EPSILON: Self::RealFloat;
    fn is_complex() -> bool;
    fn conj(x: Self) -> Self;
    fn abs(x: Self) -> Self::RealFloat;
}

impl BLASFloat for f32 {
    type RealFloat = f32;
    type FFIFloat = c_float;
    const EPSILON: Self::RealFloat = f32::EPSILON;
    #[inline]
    fn is_complex() -> bool {
        false
    }
    #[inline]
    fn conj(x: Self) -> Self {
        x
    }
    #[inline]
    fn abs(x: Self) -> Self::RealFloat {
        x.abs()
    }
}

impl BLASFloat for f64 {
    type RealFloat = f64;
    type FFIFloat = c_double;
    const EPSILON: Self::RealFloat = f64::EPSILON;
    #[inline]
    fn is_complex() -> bool {
        false
    }
    #[inline]
    fn conj(x: Self) -> Self {
        x
    }
    #[inline]
    fn abs(x: Self) -> Self::RealFloat {
        x.abs()
    }
}

impl BLASFloat for c32 {
    type RealFloat = f32;
    type FFIFloat = c_float_complex;
    const EPSILON: Self::RealFloat = f32::EPSILON;
    #[inline]
    fn is_complex() -> bool {
        true
    }
    #[inline]
    fn conj(x: Self) -> Self {
        x.conj()
    }
    #[inline]
    fn abs(x: Self) -> Self::RealFloat {
        x.abs()
    }
}

impl BLASFloat for c64 {
    type RealFloat = f64;
    type FFIFloat = c_double_complex;
    const EPSILON: Self::RealFloat = f64::EPSILON;
    #[inline]
    fn is_complex() -> bool {
        true
    }
    #[inline]
    fn conj(x: Self) -> Self {
        x.conj()
    }
    #[inline]
    fn abs(x: Self) -> Self::RealFloat {
        x.abs()
    }
}

/// Trait marker for complex symmetry (whether it is symmetric or hermitian)
pub trait BLASSymmetric {
    type Float: BLASFloat;
    type HermitianFloat: BLASFloat;
    fn is_hermitian() -> bool;
}

/// Struct marker for symmetric matrix
pub struct BLASSymm<F>
where
    F: BLASFloat,
{
    _phantom: std::marker::PhantomData<F>,
}

impl<F> BLASSymmetric for BLASSymm<F>
where
    F: BLASFloat,
{
    type Float = F;
    type HermitianFloat = F;
    #[inline]
    fn is_hermitian() -> bool {
        false
    }
}

/// Struct marker for hermitian matrix
pub struct BLASHermi<F>
where
    F: BLASFloat,
{
    _phantom: std::marker::PhantomData<F>,
}

impl<F> BLASSymmetric for BLASHermi<F>
where
    F: BLASFloat,
{
    type Float = F;
    type HermitianFloat = <F as BLASFloat>::RealFloat;
    #[inline]
    fn is_hermitian() -> bool {
        true
    }
}

/// Marker struct of BLAS functions.
///
/// This struct will be implemented in modules of each function.
pub struct BLASFunc {}

/// Trait for BLAS drivers
pub trait BLASDriver<'c, F, D>
where
    D: Dimension,
{
    fn run_blas(self) -> Result<ArrayOut<'c, F, D>, AnyError>;
}

/// Trait for BLAS builder prototypes
pub trait BLASBuilder_<'c, F, D>
where
    D: Dimension,
{
    fn driver(self) -> Result<impl BLASDriver<'c, F, D>, AnyError>;
}

pub trait BLASBuilder<'c, F, D>
where
    D: Dimension,
{
    fn run(self) -> Result<ArrayOut<'c, F, D>, AnyError>;
}