#[test]
fn rank_one_update_matches_the_source_contract() {
for (m, n) in [(0, 0), (1, 1), (2, 3), (3, 2), (5, 4), (8, 8)] {
for beta in [Accum::Replace, Accum::Add] {
for alpha in [z(0.0), z(1.0), z(-2.0), z(0.5)] {
let x = Mat::<Scalar>::from_fn(m, 1, |i, _| integer(i + 1, 0));
let y = Mat::<Scalar>::from_fn(1, n, |_, j| integer(0, j + 2));
let x_before = x.clone();
let y_before = y.clone();
let a = Mat::<Scalar>::from_fn(m, n, |i, j| integer(i + 3, j));
let mut dst = a.clone();
matmul(&mut dst, beta, x.as_ref(), y.as_ref(), alpha, Par::Seq);
check_blend(
"ger",
&a,
&dst,
BlockStructure::Rectangular,
x_before.as_ref(),
BlockStructure::Rectangular,
Conj::No,
y_before.as_ref(),
BlockStructure::Rectangular,
Conj::No,
beta,
alpha,
);
let mut storage = a.transpose().to_owned();
matmul(
storage.transpose_mut(),
beta,
x.as_ref(),
y.as_ref(),
alpha,
Par::Seq,
);
check_blend(
"ger row major",
&a,
&storage.transpose().to_owned(),
BlockStructure::Rectangular,
x_before.as_ref(),
BlockStructure::Rectangular,
Conj::No,
y_before.as_ref(),
BlockStructure::Rectangular,
Conj::No,
beta,
alpha,
);
crate::assert!(
exact(x.as_ref(), x_before.as_ref())
&& exact(y.as_ref(), y_before.as_ref()),
"ger modified an operand: m={m} n={n} beta={beta:?} \
alpha={:?}",
real(&alpha)
);
}
}
}
}
#[test]
fn symmetric_rank_k_update_writes_only_the_stored_triangle() {
for structure in [
BlockStructure::TriangularLower,
BlockStructure::TriangularUpper,
] {
for trans in 0..4 {
for (n, k) in [(0, 0), (3, 0), (1, 1), (3, 1), (2, 3), (3, 2), (4, 4), (5, 3)]
{
let (ma, na) =
if trans == 0 || trans == 3 { (n, k) } else { (k, n) };
let a =
Mat::<Scalar>::from_fn(ma, na, |i, j| integer(i, j + 1));
let a_before = a.clone();
let (a_view, a_structure, a_conj, rhs, rhs_structure, conj_rhs) =
match trans {
0 => (
a.as_ref(),
BlockStructure::Rectangular,
Conj::No,
a.as_ref().transpose(),
BlockStructure::Rectangular,
Conj::No,
),
1 => (
a.as_ref().transpose(),
BlockStructure::Rectangular,
Conj::No,
a.as_ref(),
BlockStructure::Rectangular,
Conj::No,
),
2 => (
a.as_ref().transpose(),
BlockStructure::Rectangular,
Conj::Yes,
a.as_ref(),
BlockStructure::Rectangular,
Conj::No,
),
_ => (
a.as_ref(),
BlockStructure::Rectangular,
Conj::No,
a.as_ref().transpose(),
BlockStructure::Rectangular,
Conj::Yes,
),
};
for beta in [Accum::Replace, Accum::Add] {
for alpha in [z(0.0), z(1.0), z(-1.0), z(0.5)] {
let c = Mat::<Scalar>::from_fn(n, n, |i, j| {
if accessed(structure, i, j) {
integer(i + 2, j + 5)
} else {
nan()
}
});
let mut dst = c.clone();
matmul_with_conj(
&mut dst,
structure,
beta,
a_view,
a_structure,
a_conj,
rhs,
rhs_structure,
conj_rhs,
alpha,
Par::Seq,
);
let want = blended(
beta,
c.as_ref(),
structure,
a_view,
a_structure,
a_conj,
rhs,
rhs_structure,
conj_rhs,
alpha,
);
crate::assert!(
exact(dst.as_ref(), want.as_ref()),
"rank-k structure={structure:?} trans={trans} \
n={n} k={k} beta={beta:?} \
alpha={:?}\ngot={:?}\nwant={:?}",
real(&alpha),
dst.as_ref(),
want.as_ref()
);
crate::assert!(
exact(a.as_ref(), a_before.as_ref()),
"rank-k modified A: structure={structure:?} \
trans={trans}"
);
}
}
}
}
}
}
#[test]
fn rank_one_update_conjugates_the_row_operand() {
if Scalar::IS_REAL {
return;
}
for (m, n) in [(0, 0), (1, 1), (2, 3), (3, 2), (4, 5)] {
for beta in [Accum::Replace, Accum::Add] {
for alpha in [z(0.0), z(1.0), z(-2.0), z(0.5)] {
let x = Mat::<Scalar>::from_fn(m, 1, |i, _| integer(i + 1, 0));
let y = Mat::<Scalar>::from_fn(n, 1, |j, _| integer(j + 2, 1));
let x_before = x.clone();
let y_before = y.clone();
let a = Mat::<Scalar>::from_fn(m, n, |i, j| integer(i + 3, j));
let mut dst = a.clone();
matmul_with_conj(
dst.as_mut(),
BlockStructure::Rectangular,
beta,
x.as_ref(),
BlockStructure::Rectangular,
Conj::No,
y.as_ref().transpose(),
BlockStructure::Rectangular,
Conj::Yes,
alpha,
Par::Seq,
);
check_blend(
"gerc",
&a,
&dst,
BlockStructure::Rectangular,
x_before.as_ref(),
BlockStructure::Rectangular,
Conj::No,
y_before.as_ref().transpose(),
BlockStructure::Rectangular,
Conj::Yes,
beta,
alpha,
);
crate::assert!(
exact(x.as_ref(), x_before.as_ref())
&& exact(y.as_ref(), y_before.as_ref()),
"gerc modified an operand: m={m} n={n} beta={beta:?} \
alpha={:?}",
real(&alpha)
);
}
}
}
}
fn check_blend(
name: &str,
dst_before: &Mat<Scalar>,
dst: &Mat<Scalar>,
dst_structure: BlockStructure,
lhs: MatRef<'_, Scalar>,
lhs_structure: BlockStructure,
conj_lhs: Conj,
rhs: MatRef<'_, Scalar>,
rhs_structure: BlockStructure,
conj_rhs: Conj,
beta: Accum,
alpha: Scalar,
) {
let alpha_dbg = real(&alpha);
let want = blended(
beta,
dst_before.as_ref(),
dst_structure,
lhs,
lhs_structure,
conj_lhs,
rhs,
rhs_structure,
conj_rhs,
alpha,
);
crate::assert!(
exact(dst.as_ref(), want.as_ref()),
"{name} beta={beta:?} alpha={:?}\ngot={:?}\nwant={:?}",
alpha_dbg,
dst.as_ref(),
want.as_ref()
);
}