fn operand(n: usize, structure: BlockStructure) -> Mat<Scalar> {
Mat::from_fn(n, n, |i, j| {
if accessed(structure, i, j) {
integer(i, j)
} else {
nan()
}
})
}
const STRUCTURES: [BlockStructure; 6] = [
BlockStructure::TriangularLower,
BlockStructure::StrictTriangularLower,
BlockStructure::UnitTriangularLower,
BlockStructure::TriangularUpper,
BlockStructure::StrictTriangularUpper,
BlockStructure::UnitTriangularUpper,
];
#[test]
fn trmm_matches_an_independent_product() {
for structure in STRUCTURES {
for trans in 0..3 {
for (m, n) in
[(0, 0), (1, 1), (2, 1), (3, 2), (4, 4), (5, 3), (8, 2)]
{
for left in [true, false] {
let order = if left { m } else { n };
let a = operand(order, structure);
let a_before = a.clone();
let b =
Mat::<Scalar>::from_fn(m, n, |i, j| integer(i + 1, j));
let b_before = b.clone();
let (a_view, a_structure, a_conj) = match trans {
0 => (a.as_ref(), structure, Conj::No),
1 => (
a.as_ref().transpose(),
structure.transpose(),
Conj::No,
),
_ => (
a.as_ref().transpose(),
structure.transpose(),
Conj::Yes,
),
};
let (
lhs,
lhs_structure,
conj_lhs,
rhs,
rhs_structure,
conj_rhs,
) = if left {
(
a_view,
a_structure,
a_conj,
b.as_ref(),
BlockStructure::Rectangular,
Conj::No,
)
} else {
(
b.as_ref(),
BlockStructure::Rectangular,
Conj::No,
a_view,
a_structure,
a_conj,
)
};
for alpha in [z(0.0), z(1.0), z(2.0), z(0.5)] {
let mut dst = Mat::<Scalar>::full(m, n, rogue());
matmul_with_conj(
&mut dst,
BlockStructure::Rectangular,
Accum::Replace,
lhs,
lhs_structure,
conj_lhs,
rhs,
rhs_structure,
conj_rhs,
alpha,
Par::Seq,
);
let (lhs_ref, rhs_ref) = if left {
(lhs, b_before.as_ref())
} else {
(b_before.as_ref(), rhs)
};
let want = blended(
Accum::Replace,
Mat::<Scalar>::zeros(m, n).as_ref(),
BlockStructure::Rectangular,
lhs_ref,
lhs_structure,
conj_lhs,
rhs_ref,
rhs_structure,
conj_rhs,
alpha,
);
crate::assert!(
exact(dst.as_ref(), want.as_ref()),
"trmm structure={structure:?} trans={trans} m={m} \
n={n} left={left} alpha={:?}\ngot={:?}\nwant={:?}",
real(&alpha),
dst.as_ref(),
want.as_ref()
);
}
crate::assert!(
exact(a.as_ref(), a_before.as_ref()),
"trmm modified its triangular operand: \
structure={structure:?} trans={trans} left={left}"
);
crate::assert!(
exact(b.as_ref(), b_before.as_ref()),
"trmm modified its right-hand-side operand: \
structure={structure:?} trans={trans} left={left}"
);
}
}
}
}
}
#[test]
fn trmm_reads_and_writes_nothing_outside_its_operands() {
use BlockStructure::*;
for (structure, left) in [
(TriangularLower, true),
(UnitTriangularUpper, true),
(StrictTriangularLower, false),
(TriangularUpper, false),
] {
let (m, n) = (5, 4);
let order = if left { m } else { n };
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 b = Mat::<Scalar>::from_fn(m, n, |i, j| integer(i, j));
let b_before = b.clone();
let mut dst_parent = Mat::<Scalar>::full(m + 2, n + 2, rogue());
let dst_before = dst_parent.clone();
{
let a = a_parent.as_ref().submatrix(1, 1, order, order);
let mut dst = dst_parent.as_mut().submatrix_mut(1, 1, m, n);
if left {
matmul_with_conj(
&mut dst,
Rectangular,
Accum::Replace,
a,
structure,
Conj::No,
b.as_ref(),
Rectangular,
Conj::No,
one(),
Par::Seq,
);
} else {
matmul_with_conj(
&mut dst,
Rectangular,
Accum::Replace,
b.as_ref(),
Rectangular,
Conj::No,
a,
structure,
Conj::No,
one(),
Par::Seq,
);
}
}
let ctx = format!(
"trmm structure={structure:?} left={left} m={m} n={n}"
);
let want = if left {
blended(
Accum::Replace,
Mat::<Scalar>::zeros(m, n).as_ref(),
Rectangular,
a_before.as_ref().submatrix(1, 1, order, order),
structure,
Conj::No,
b_before.as_ref(),
Rectangular,
Conj::No,
one(),
)
} else {
blended(
Accum::Replace,
Mat::<Scalar>::zeros(m, n).as_ref(),
Rectangular,
b_before.as_ref(),
Rectangular,
Conj::No,
a_before.as_ref().submatrix(1, 1, order, order),
structure,
Conj::No,
one(),
)
};
let got = dst_parent.as_ref().submatrix(1, 1, m, n);
crate::assert!(
exact(got, want.as_ref()),
"{ctx}\ngot={:?}\nwant={:?}",
got,
want.as_ref()
);
crate::assert!(
exact(a_before.as_ref(), a_parent.as_ref()),
"{ctx}: the triangular operand or its parent was modified"
);
crate::assert!(
untouched_outside(
dst_before.as_ref(),
dst_parent.as_ref(),
1,
1,
m,
n
),
"{ctx}: the destination's parent was modified outside the product"
);
crate::assert!(
exact(b.as_ref(), b_before.as_ref()),
"{ctx}: the right-hand-side operand was modified"
);
}
}