Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various refactorings #1470

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
10 changes: 4 additions & 6 deletions nalgebra-glm/src/ext/quaternion_trigonometric.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use na::{Unit, UnitQuaternion};

use crate::aliases::{Qua, TVec3};
use crate::RealNumber;

/// The rotation angle of this quaternion assumed to be normalized.
pub fn quat_angle<T: RealNumber>(x: &Qua<T>) -> T {
Expand All @@ -15,9 +14,8 @@ pub fn quat_angle_axis<T: RealNumber>(angle: T, axis: &TVec3<T>) -> Qua<T> {

/// The rotation axis of a quaternion assumed to be normalized.
pub fn quat_axis<T: RealNumber>(x: &Qua<T>) -> TVec3<T> {
if let Some(a) = UnitQuaternion::from_quaternion(*x).axis() {
a.into_inner()
} else {
TVec3::zeros()
}
UnitQuaternion::from_quaternion(*x)
.axis()
.map(|a| a.into_inner())
.unwrap_or_else(|| TVec3::zeros())
}
58 changes: 28 additions & 30 deletions nalgebra-sparse/src/cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,19 @@ where
let lane = self.pattern.get_lane(self.current_lane_idx);
let minor_dim = self.pattern.minor_dim();

if let Some(minor_indices) = lane {
let count = minor_indices.len();
let values_in_lane = &self.remaining_values[..count];
self.remaining_values = &self.remaining_values[count..];
self.current_lane_idx += 1;

Some(CsLane {
minor_dim,
minor_indices,
values: values_in_lane,
})
} else {
None
}
let Some(minor_indices) = lane else {
return None;
};
let count = minor_indices.len();
let values_in_lane = &self.remaining_values[..count];
self.remaining_values = &self.remaining_values[count..];
self.current_lane_idx += 1;

Some(CsLane {
minor_dim,
minor_indices,
values: values_in_lane,
})
}
}

Expand Down Expand Up @@ -365,22 +364,21 @@ where
let lane = self.pattern.get_lane(self.current_lane_idx);
let minor_dim = self.pattern.minor_dim();

if let Some(minor_indices) = lane {
let count = minor_indices.len();

let remaining = std::mem::take(&mut self.remaining_values);
let (values_in_lane, remaining) = remaining.split_at_mut(count);
self.remaining_values = remaining;
self.current_lane_idx += 1;

Some(CsLaneMut {
minor_dim,
minor_indices,
values: values_in_lane,
})
} else {
None
}
let Some(minor_indices) = lane else {
return None;
};
let count = minor_indices.len();

let remaining = std::mem::take(&mut self.remaining_values);
let (values_in_lane, remaining) = remaining.split_at_mut(count);
self.remaining_values = remaining;
self.current_lane_idx += 1;

Some(CsLaneMut {
minor_dim,
minor_indices,
values: values_in_lane,
})
}
}

Expand Down
46 changes: 22 additions & 24 deletions nalgebra-sparse/src/ops/serial/csc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,32 +268,30 @@ fn spsolve_csc_lower_triangular_transpose<T: RealField>(
// Skip entries above the diagonal
// TODO: Can use exponential search here to quickly skip entries
let diag_csc_index = l_col_i.row_indices().iter().position(|&k| i == k);
if let Some(diag_csc_index) = diag_csc_index {
let l_ii = l_col_i.values()[diag_csc_index].clone();

if l_ii != T::zero() {
// // Update entry associated with diagonal
// x_col_j[k] /= a_kk;

// Copy value after updating (so we don't run into the borrow checker)
let mut x_ii = x_col_j[i].clone();

let row_indices = &l_col_i.row_indices()[(diag_csc_index + 1)..];
let a_values = &l_col_i.values()[(diag_csc_index + 1)..];

// Note: The remaining entries are below the diagonal
for (k, l_ki) in row_indices.iter().zip(a_values) {
let x_kj = x_col_j[*k].clone();
x_ii -= l_ki.clone() * x_kj;
}

x_col_j[i] = x_ii / l_ii;
} else {
return spsolve_encountered_zero_diagonal();
}
} else {
let Some(diag_csc_index) = diag_csc_index else {
return spsolve_encountered_zero_diagonal();
};
let l_ii = l_col_i.values()[diag_csc_index].clone();

if l_ii.is_zero() {
return spsolve_encountered_zero_diagonal();
}
// // Update entry associated with diagonal
// x_col_j[k] /= a_kk;

// Copy value after updating (so we don't run into the borrow checker)
let mut x_ii = x_col_j[i].clone();

let row_indices = &l_col_i.row_indices()[(diag_csc_index + 1)..];
let a_values = &l_col_i.values()[(diag_csc_index + 1)..];

// Note: The remaining entries are below the diagonal
for (k, l_ki) in row_indices.iter().zip(a_values) {
let x_kj = x_col_j[*k].clone();
x_ii -= l_ki.clone() * x_kj;
}

x_col_j[i] = x_ii / l_ii;
}
}

Expand Down
28 changes: 4 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,27 +339,13 @@ pub fn partial_ge<T: PartialOrd>(a: &T, b: &T) -> bool {
/// Return the minimum of `a` and `b` if they are comparable.
#[inline]
pub fn partial_min<'a, T: PartialOrd>(a: &'a T, b: &'a T) -> Option<&'a T> {
if let Some(ord) = a.partial_cmp(b) {
match ord {
Ordering::Greater => Some(b),
_ => Some(a),
}
} else {
None
}
a.partial_cmp(b).map(|ord| if ord.is_ge() { b } else { a })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid a change in behavior, this should be is_gt (and similar below)

Copy link
Contributor Author

@adamnemecek adamnemecek Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the correct behavior, is_ge == matches!(self, Greater) which is what was there before.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's incorrect. The "e" stands for "equals", as in "greater or equals". This is illustrated in the documentation.

}

/// Return the maximum of `a` and `b` if they are comparable.
#[inline]
pub fn partial_max<'a, T: PartialOrd>(a: &'a T, b: &'a T) -> Option<&'a T> {
if let Some(ord) = a.partial_cmp(b) {
match ord {
Ordering::Less => Some(b),
_ => Some(a),
}
} else {
None
}
a.partial_cmp(b).map(|ord| if ord.is_le() { b } else { a })
}

/// Clamp `value` between `min` and `max`. Returns `None` if `value` is not comparable to
Expand All @@ -382,14 +368,8 @@ pub fn partial_clamp<'a, T: PartialOrd>(value: &'a T, min: &'a T, max: &'a T) ->
/// Sorts two values in increasing order using a partial ordering.
#[inline]
pub fn partial_sort2<'a, T: PartialOrd>(a: &'a T, b: &'a T) -> Option<(&'a T, &'a T)> {
if let Some(ord) = a.partial_cmp(b) {
match ord {
Ordering::Less => Some((a, b)),
_ => Some((b, a)),
}
} else {
None
}
a.partial_cmp(b)
.map(|ord| if ord.is_le() { (a, b) } else { (b, a) })
}

/*
Expand Down