use nalgebra::{Const, DimName, OMatrix};
#[inline]
pub fn givens(a: f32, b: f32) -> (f32, f32) {
let h = libm::hypotf(a, b);
if h == 0.0 {
return (1.0, 0.0);
}
let inv_h = 1.0 / h;
(a * inv_h, -(b * inv_h))
}
#[inline]
pub fn givens_left_apply<const R: usize, const C: usize>(
r: &mut OMatrix<f32, Const<R>, Const<C>>,
c: f32,
s: f32,
row1: usize,
row2: usize,
n_cols: usize,
) where
Const<R>: DimName,
Const<C>: DimName,
{
for col in 0..n_cols {
let r1 = r[(row1, col)];
let r2 = r[(row2, col)];
r[(row1, col)] = c * r1 - s * r2;
r[(row2, col)] = s * r1 + c * r2;
}
}
#[inline]
pub fn givens_right_apply_t<const R: usize, const C: usize>(
q: &mut OMatrix<f32, Const<R>, Const<C>>,
c: f32,
s: f32,
col1: usize,
col2: usize,
n_rows: usize,
) where
Const<R>: DimName,
Const<C>: DimName,
{
for i in 0..n_rows {
let q1 = q[(i, col1)];
let q2 = q[(i, col2)];
q[(i, col1)] = c * q1 - s * q2;
q[(i, col2)] = s * q1 + c * q2;
}
}