Skip to content

Commit

Permalink
sphere: fix bug
Browse files Browse the repository at this point in the history
scaling properly by radius
more tests
  • Loading branch information
alphaville committed Oct 27, 2023
1 parent 757d72e commit 125444d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/constraints/sphere2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ impl<'a> Constraint for Sphere2<'a> {
fn project(&self, x: &mut [f64]) {
if let Some(center) = &self.center {
let mut norm_difference = 0.0;
x.iter().zip(center.iter()).for_each(|(a, b)| {
let diff_ = *a - *b;
x.iter().zip(center.iter()).for_each(|(xi, ci)| {
let diff_ = *xi - *ci;
norm_difference += diff_ * diff_
});
norm_difference = norm_difference.sqrt();
x.iter_mut().zip(center.iter()).for_each(|(x, c)| {
*x = *c + (*x - *c) / norm_difference;
*x = *c + self.radius * (*x - *c) / norm_difference;
});
} else {
let norm_x = crate::matrix_operations::norm2(x);
Expand All @@ -37,6 +37,6 @@ impl<'a> Constraint for Sphere2<'a> {
}

fn is_convex(&self) -> bool {
true
false
}
}
6 changes: 3 additions & 3 deletions src/constraints/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,9 @@ fn t_ball1_random_optimality_conditions_centered() {

#[test]
fn t_sphere2_no_center() {
let radius = 1.0;
let radius = 0.9;
let mut x_out = [1.0, 1.0];
let mut x_in = [1.0, 1.0];
let mut x_in = [-0.3, -0.2];
let unit_sphere = Sphere2::new(None, radius);
unit_sphere.project(&mut x_out);
unit_sphere.project(&mut x_in);
Expand All @@ -779,7 +779,7 @@ fn t_sphere2_no_center() {

#[test]
fn t_sphere2_center() {
let radius = 1.0;
let radius = 1.3;
let center = [-3.0, 5.0];
let mut x = [1.0, 1.0];
let unit_sphere = Sphere2::new(Some(&center), radius);
Expand Down

0 comments on commit 125444d

Please sign in to comment.