nalgebra/src/linalg/householder.rs

139 lines
4.7 KiB
Rust
Raw Normal View History

//! Construction of householder elementary reflections.
2021-07-17 12:17:56 +08:00
use std::mem::MaybeUninit;
2019-03-23 21:29:07 +08:00
use crate::allocator::Allocator;
2021-04-11 17:00:38 +08:00
use crate::base::{DefaultAllocator, OMatrix, OVector, Unit, Vector};
2019-03-23 21:29:07 +08:00
use crate::dimension::Dim;
use crate::storage::{Storage, StorageMut};
2020-03-21 19:16:46 +08:00
use num::Zero;
use simba::scalar::ComplexField;
2019-03-23 21:29:07 +08:00
use crate::geometry::Reflection;
/// Replaces `column` by the axis of the householder reflection that transforms `column` into
/// `(+/-|column|, 0, ..., 0)`.
///
/// The unit-length axis is output to `column`. Returns what would be the first component of
/// `column` after reflection and `false` if no reflection was necessary.
#[doc(hidden)]
#[inline(always)]
2021-04-11 17:00:38 +08:00
pub fn reflection_axis_mut<T: ComplexField, D: Dim, S: StorageMut<T, D>>(
column: &mut Vector<T, D, S>,
) -> (T, bool) {
let reflection_sq_norm = column.norm_squared();
let reflection_norm = reflection_sq_norm.sqrt();
let factor;
2019-03-18 18:23:19 +08:00
let signed_norm;
unsafe {
2019-03-18 18:23:19 +08:00
let (modulus, sign) = column.vget_unchecked(0).to_exp();
signed_norm = sign.scale(reflection_norm);
2019-03-23 21:29:07 +08:00
factor = (reflection_sq_norm + modulus * reflection_norm) * crate::convert(2.0);
2019-03-18 18:23:19 +08:00
*column.vget_unchecked_mut(0) += signed_norm;
};
if !factor.is_zero() {
column.unscale_mut(factor.sqrt());
2019-03-18 18:23:19 +08:00
(-signed_norm, true)
2018-02-02 19:26:35 +08:00
} else {
2020-11-15 23:57:49 +08:00
// TODO: not sure why we don't have a - sign here.
2019-03-20 05:52:57 +08:00
(signed_norm, false)
}
}
/// Uses an householder reflection to zero out the `icol`-th column, starting with the `shift + 1`-th
/// subdiagonal element.
#[doc(hidden)]
2021-04-11 17:00:38 +08:00
pub fn clear_column_unchecked<T: ComplexField, R: Dim, C: Dim>(
matrix: &mut OMatrix<T, R, C>,
2021-07-17 12:17:56 +08:00
diag_elt: *mut T,
2018-02-02 19:26:35 +08:00
icol: usize,
shift: usize,
2021-07-17 15:52:57 +08:00
bilateral: Option<&mut OVector<MaybeUninit<T>, R>>,
2018-02-02 19:26:35 +08:00
) where
2021-04-11 17:00:38 +08:00
DefaultAllocator: Allocator<T, R, C> + Allocator<T, R>,
2018-02-02 19:26:35 +08:00
{
let (mut left, mut right) = matrix.columns_range_pair_mut(icol, icol + 1..);
let mut axis = left.rows_range_mut(icol + shift..);
let (reflection_norm, not_zero) = reflection_axis_mut(&mut axis);
2021-07-17 12:17:56 +08:00
unsafe {
*diag_elt = reflection_norm;
}
if not_zero {
2021-04-11 17:00:38 +08:00
let refl = Reflection::new(Unit::new_unchecked(axis), T::zero());
2019-03-19 19:00:10 +08:00
let sign = reflection_norm.signum();
if let Some(mut work) = bilateral {
2019-03-19 19:00:10 +08:00
refl.reflect_rows_with_sign(&mut right, &mut work, sign);
}
2019-03-19 19:00:10 +08:00
refl.reflect_with_sign(&mut right.rows_range_mut(icol + shift..), sign.conjugate());
}
}
2019-03-18 18:23:19 +08:00
/// Uses an householder reflection to zero out the `irow`-th row, ending before the `shift + 1`-th
/// superdiagonal element.
#[doc(hidden)]
2021-04-11 17:00:38 +08:00
pub fn clear_row_unchecked<T: ComplexField, R: Dim, C: Dim>(
matrix: &mut OMatrix<T, R, C>,
2021-07-17 12:17:56 +08:00
diag_elt: *mut T,
axis_packed: &mut OVector<MaybeUninit<T>, C>,
work: &mut OVector<MaybeUninit<T>, R>,
2018-02-02 19:26:35 +08:00
irow: usize,
shift: usize,
) where
2021-04-11 17:00:38 +08:00
DefaultAllocator: Allocator<T, R, C> + Allocator<T, R> + Allocator<T, C>,
2018-02-02 19:26:35 +08:00
{
let (mut top, mut bottom) = matrix.rows_range_pair_mut(irow, irow + 1..);
let mut axis = axis_packed.rows_range_mut(irow + shift..);
2021-07-17 15:52:57 +08:00
axis.tr_copy_init_from(&top.columns_range(irow + shift..));
let mut axis = unsafe { axis.assume_init_mut() };
let (reflection_norm, not_zero) = reflection_axis_mut(&mut axis);
2019-03-18 18:23:19 +08:00
axis.conjugate_mut(); // So that reflect_rows actually cancels the first row.
2021-07-17 15:52:57 +08:00
unsafe {
*diag_elt = reflection_norm;
}
if not_zero {
2021-04-11 17:00:38 +08:00
let refl = Reflection::new(Unit::new_unchecked(axis), T::zero());
2019-03-19 19:00:10 +08:00
refl.reflect_rows_with_sign(
2018-02-02 19:26:35 +08:00
&mut bottom.columns_range_mut(irow + shift..),
&mut work.rows_range_mut(irow + 1..),
2019-03-19 19:00:10 +08:00
reflection_norm.signum().conjugate(),
2018-02-02 19:26:35 +08:00
);
top.columns_range_mut(irow + shift..)
2021-07-04 11:19:07 +08:00
.tr_copy_from(refl.axis());
2018-02-02 19:26:35 +08:00
} else {
top.columns_range_mut(irow + shift..).tr_copy_from(&axis);
}
}
2019-03-19 19:00:10 +08:00
/// Computes the orthogonal transformation described by the elementary reflector axii stored on
/// the lower-diagonal element of the given matrix.
/// matrices.
#[doc(hidden)]
2021-04-11 17:00:38 +08:00
pub fn assemble_q<T: ComplexField, D: Dim>(m: &OMatrix<T, D, D>, signs: &[T]) -> OMatrix<T, D, D>
2020-04-06 00:49:48 +08:00
where
2021-04-11 17:00:38 +08:00
DefaultAllocator: Allocator<T, D, D>,
2020-04-06 00:49:48 +08:00
{
assert!(m.is_square());
let dim = m.data.shape().0;
// NOTE: we could build the identity matrix and call p_mult on it.
2018-09-24 12:48:42 +08:00
// Instead we don't so that we take in account the matrix sparseness.
2021-04-11 17:00:38 +08:00
let mut res = OMatrix::identity_generic(dim, dim);
2018-02-02 19:26:35 +08:00
for i in (0..dim.value() - 1).rev() {
let axis = m.slice_range(i + 1.., i);
2021-04-11 17:00:38 +08:00
let refl = Reflection::new(Unit::new_unchecked(axis), T::zero());
2018-02-02 19:26:35 +08:00
let mut res_rows = res.slice_range_mut(i + 1.., i..);
2019-03-19 19:00:10 +08:00
refl.reflect_with_sign(&mut res_rows, signs[i].signum());
}
res
}