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
//! Implementation of ?trsm - Solving triangular systems of linear equations.
use blas::{ctrsm, dtrsm, strsm, ztrsm};
use crate::dense::linalg::lapack::interface::lapack_return;
use crate::base_types::LapackResult;
use crate::base_types::{c32, c64};
/// Left: Solve AX = B. Right: Solve XA = B.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum TrsmSide {
/// Solve AX = B
Left = b'L',
/// Solve XA = B
Right = b'R',
}
/// Uplo: Specifies whether the upper or lower triangular part of the matrix is stored.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum TrsmUplo {
/// Upper triangular part of the matrix is stored.
Upper = b'U',
/// Lower triangular part of the matrix is stored.
Lower = b'L',
}
/// Transpose: Specifies whether the matrix is transposed or not.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum TrsmTransA {
/// No transpose: A is used as is.
NoTrans = b'N',
/// Transpose
Transpose = b'T',
/// Conjugate transpose
ConjugateTranspose = b'C',
}
/// Diagonal: Specifies whether the matrix is unit diagonal or not.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum TrsmDiag {
/// Non-unit diagonal
NonUnit = b'N',
/// Unit diagonal
Unit = b'U',
}
/// ?trsm - Solve triangular systems of linear equations.
pub trait Trsm {
/// Solve a triangular system of linear equations.
///
/// **Arguments:**
/// - `side`: Specifies whether to solve AX = B (Left) or XA = B (Right).
/// - `uplo`: Specifies whether the upper or lower triangular part of the matrix is stored.
/// - `transa`: Specifies whether the matrix is transposed or not.
/// - `diag`: Specifies whether the matrix is unit diagonal or not.
/// - `m`: The number of rows of the matrix B.
/// - `n`: The number of columns of the matrix B.
/// - `alpha`: Scalar multiplier for B.
/// - `a`: The triangular matrix A.
/// - `lda`: The leading dimension of the matrix A.
/// - `b`: The right-hand side matrix B. On exit contains the solution.
/// - `ldb`: The leading dimension of the matrix B.
#[allow(clippy::too_many_arguments)]
fn trsm(
side: TrsmSide,
uplo: TrsmUplo,
transa: TrsmTransA,
diag: TrsmDiag,
m: usize,
n: usize,
alpha: Self,
a: &mut [Self],
lda: usize,
b: &mut [Self],
ldb: usize,
) -> LapackResult<()>
where
Self: Sized;
}
macro_rules! implement_trsm {
($scalar:ty, $trsm:expr) => {
impl Trsm for $scalar {
fn trsm(
side: TrsmSide,
uplo: TrsmUplo,
transa: TrsmTransA,
diag: TrsmDiag,
m: usize,
n: usize,
alpha: Self,
a: &mut [Self],
lda: usize,
b: &mut [Self],
ldb: usize,
) -> LapackResult<()> {
assert_eq!(
a.len(),
match side {
TrsmSide::Left => lda * m,
TrsmSide::Right => lda * n,
},
"Require `a.len()` == `lda * m` or `lda * n` depending on the side.",
);
assert!(
lda >= std::cmp::max(
1,
match side {
TrsmSide::Left => m,
TrsmSide::Right => n,
}
),
"Require `lda` {} >= `max(1, {})` {}.",
lda,
match side {
TrsmSide::Left => m,
TrsmSide::Right => n,
},
std::cmp::max(
1,
match side {
TrsmSide::Left => m,
TrsmSide::Right => n,
}
)
);
assert_eq!(
b.len(),
ldb * n,
"Require `b.len()` {} == `ldb * n` {}.",
b.len(),
ldb * n
);
assert!(
ldb >= std::cmp::max(1, m),
"Require `ldb` {} >= `max(1, m)` {}.",
ldb,
std::cmp::max(1, m)
);
unsafe {
$trsm(
side as u8,
uplo as u8,
transa as u8,
diag as u8,
m as i32,
n as i32,
alpha,
a,
lda as i32,
b,
ldb as i32,
);
}
lapack_return(0, ())
}
}
};
}
implement_trsm!(f32, strsm);
implement_trsm!(f64, dtrsm);
implement_trsm!(c32, ctrsm);
implement_trsm!(c64, ztrsm);