fn op_entry(
a: MatRef<'_, Scalar>,
structure: BlockStructure,
trans: usize,
i: usize,
j: usize,
) -> Scalar {
match trans {
0 => entry(a, structure, i, j),
1 => entry(a, structure, j, i),
_ => entry(a, structure, j, i).conj(),
}
}
fn operand(order: usize, structure: BlockStructure) -> Mat<Scalar> {
Mat::from_fn(order, order, |i, j| {
if !accessed(structure, i, j) {
nan()
} else if i == j {
from_f64([1.0, -1.0, 2.0, -2.0][i % 4])
} else {
integer(i + 1, j)
}
})
}
fn solve(
a: MatRef<'_, Scalar>,
structure: BlockStructure,
conj: Conj,
rhs: MatMut<'_, Scalar>,
par: Par,
) {
let unit = matches!(
structure,
BlockStructure::UnitTriangularLower
| BlockStructure::UnitTriangularUpper
);
match (structure.is_lower(), unit) {
(true, false) => {
ts::solve_lower_triangular_in_place_with_conj(a, conj, rhs, par)
},
(true, true) => ts::solve_unit_lower_triangular_in_place_with_conj(
a, conj, rhs, par,
),
(false, false) => {
ts::solve_upper_triangular_in_place_with_conj(a, conj, rhs, par)
},
(false, true) => ts::solve_unit_upper_triangular_in_place_with_conj(
a, conj, rhs, par,
),
}
}
fn rhs_from_solution(
a: MatRef<'_, Scalar>,
structure: BlockStructure,
trans: usize,
left: bool,
x: MatRef<'_, Scalar>,
alpha: Scalar,
) -> Mat<Scalar> {
let order = if left { x.nrows() } else { x.ncols() };
Mat::<Scalar>::from_fn(x.nrows(), x.ncols(), |i, j| {
let mut acc = zero::<Scalar>();
for k in 0..order {
acc = acc
+ if left {
op_entry(a, structure, trans, i, k) * x[(k, j)].copy()
} else {
x[(i, k)].copy() * op_entry(a, structure, trans, k, j)
};
}
alpha.copy() * acc
})
}
fn check_solve(
rows: usize,
cols: usize,
structure: BlockStructure,
trans: usize,
left: bool,
alpha: Scalar,
) {
let order = if left { rows } else { cols };
let a = operand(order, structure);
let a_before = a.clone();
let x = Mat::<Scalar>::from_fn(rows, cols, |i, j| integer(i + j, j + 2));
let mut b =
rhs_from_solution(a.as_ref(), structure, trans, left, x.as_ref(), alpha);
let (a_view, a_structure, conj) = match (left, trans) {
(true, 0) | (false, 1) => (a.as_ref(), structure, Conj::No),
(true, 1) => (a.as_ref().transpose(), structure.transpose(), Conj::No),
(true, _) => (a.as_ref().transpose(), structure.transpose(), Conj::Yes),
(false, 0) => (a.as_ref().transpose(), structure.transpose(), Conj::No),
(false, _) => (a.as_ref(), structure, Conj::Yes),
};
if left {
solve(a_view, a_structure, conj, b.as_mut(), Par::Seq);
} else {
solve(
a_view,
a_structure,
conj,
b.as_mut().transpose_mut(),
Par::Seq,
);
}
let want =
Mat::<Scalar>::from_fn(rows, cols, |i, j| alpha * x[(i, j)].copy());
let ctx = format!(
"trsm structure={structure:?} trans={trans} left={left} rows={rows} \
cols={cols}"
);
crate::assert!(
exact(b.as_ref(), want.as_ref()),
"{ctx}\ngot={:?}\nwant={:?}",
b.as_ref(),
want.as_ref()
);
crate::assert!(exact(a.as_ref(), a_before.as_ref()), "{ctx}: A changed");
}
#[test]
fn trsm_recovers_the_exact_solution_with_an_implicit_diagonal() {
use BlockStructure::*;
for structure in [
TriangularLower,
TriangularUpper,
UnitTriangularLower,
UnitTriangularUpper,
] {
for trans in 0..3 {
for left in [true, false] {
for (rows, cols) in
[(0, 0), (1, 1), (2, 1), (3, 2), (4, 4), (5, 3), (8, 2)]
{
for alpha in [z(1.0), z(2.0)] {
check_solve(rows, cols, structure, trans, left, alpha);
}
}
}
}
}
}
#[test]
fn trsm_does_not_touch_memory_outside_its_operand() {
use BlockStructure::*;
for structure in [TriangularLower, UnitTriangularUpper] {
for (left, order) in [(true, 4usize), (false, 3usize)] {
let (rows, cols) = if left { (order, 2) } else { (2, order) };
let mut a_parent = Mat::<Scalar>::full(order + 2, order + 2, rogue());
a_parent
.as_mut()
.submatrix_mut(1, 1, order, order)
.copy_from(&operand(order, structure));
let a_before = a_parent.clone();
let x =
Mat::<Scalar>::from_fn(rows, cols, |i, j| integer(i + j, j + 2));
let rhs = rhs_from_solution(
a_before.as_ref().submatrix(1, 1, order, order),
structure,
0,
left,
x.as_ref(),
one(),
);
let mut parent = Mat::<Scalar>::full(rows + 2, cols + 2, rogue());
parent
.as_mut()
.submatrix_mut(1, 1, rows, cols)
.copy_from(&rhs);
let before = parent.clone();
{
let a = a_parent.as_ref().submatrix(1, 1, order, order);
let b = parent.as_mut().submatrix_mut(1, 1, rows, cols);
if left {
solve(a, structure, Conj::No, b, Par::Seq);
} else {
solve(
a.transpose(),
structure.transpose(),
Conj::No,
b.transpose_mut(),
Par::Seq,
);
}
}
let ctx = format!(
"trsm structure={structure:?} left={left} order={order}"
);
let got = parent.as_ref().submatrix(1, 1, rows, cols);
crate::assert!(
exact(got, x.as_ref()),
"{ctx}\ngot={:?}\nwant={:?}",
got,
x.as_ref()
);
crate::assert!(
untouched_outside(
before.as_ref(),
parent.as_ref(),
1,
1,
rows,
cols
),
"{ctx}: the solve wrote outside its right-hand side"
);
crate::assert!(
exact(a_before.as_ref(), a_parent.as_ref()),
"{ctx}: the triangular operand or its parent was modified"
);
}
}
}
#[test]
fn trsv_conjugates_an_imaginary_diagonal_exactly() {
use BlockStructure::*;
let pivot = scalar(0.0, 2.0);
if pivot == zero() {
return;
}
for structure in [TriangularLower, TriangularUpper] {
for (conj, want) in [(Conj::No, scalar(0.0, -0.5)), (Conj::Yes, scalar(0.0, 0.5))] {
let mut a = Mat::<Scalar>::full(1, 1, zero());
a[(0, 0)] = pivot;
let a_before = a.clone();
let mut b = Mat::<Scalar>::full(1, 1, one());
solve(a.as_ref(), structure, conj, b.as_mut(), Par::Seq);
let ctx = format!("1x1 structure={structure:?} conj={conj:?}");
crate::assert!(
same_number(b[(0, 0)].copy(), want),
"{ctx}: got {:?} want {:?}",
b[(0, 0)],
want
);
crate::assert!(
exact(a_before.as_ref(), a.as_ref()),
"{ctx}: the operand was modified"
);
}
}
}
#[test]
fn trsm_conjugates_wide_imaginary_diagonals_exactly() {
use BlockStructure::*;
let pivot = scalar(0.0, 2.0);
if pivot == zero() {
return;
}
for order in [2usize, 3, 4] {
for structure in [TriangularLower, TriangularUpper] {
let mut a = Mat::<Scalar>::full(order, order, zero());
for i in 0..order {
for j in 0..order {
let stored = match structure {
TriangularLower => i >= j,
TriangularUpper => i <= j,
_ => false,
};
if !stored {
continue;
}
a[(i, j)] = if i == j { pivot } else { integer(i + 1, j) };
}
}
let a_before = a.clone();
let conj_a =
Mat::<Scalar>::from_fn(order, order, |i, j| a[(i, j)].conj());
let x = Mat::<Scalar>::from_fn(order, 2, |i, j| integer(i + j, j + 2));
for conj in [Conj::No, Conj::Yes] {
let system = if matches!(conj, Conj::Yes) {
conj_a.as_ref()
} else {
a.as_ref()
};
let mut b =
rhs_from_solution(system, structure, 0, true, x.as_ref(), one());
solve(a.as_ref(), structure, conj, b.as_mut(), Par::Seq);
let ctx = format!("order={order} structure={structure:?} conj={conj:?}");
crate::assert!(
exact(b.as_ref(), x.as_ref()),
"{ctx}\\ngot={:?}\\nwant={:?}",
b.as_ref(),
x.as_ref()
);
crate::assert!(
exact(a.as_ref(), a_before.as_ref()),
"{ctx}: the operand was modified"
);
}
}
}
}