Skip to content

Commit 6b5ad6e

Browse files
committed
feat!: Rename System::apply_mut to System::apply
The convention in Rust is to use `_mut` suffix for methods that can modify `self` (e.g., by returning mutable reference to itself or its field). There is not a "non-mut" alternative for applying a system in Gomez now, so using just `apply` for the method name seems fine.
1 parent 42db7f7 commit 6b5ad6e

File tree

12 files changed

+39
-39
lines changed

12 files changed

+39
-39
lines changed

benches/solvers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ where
249249
na::U1::name(),
250250
);
251251

252-
match self.f.apply_mut(&x, &mut fx) {
252+
match self.f.apply(&x, &mut fx) {
253253
Ok(_) => GslStatus::ok(),
254254
Err(_) => GslStatus::err(GslError::BadFunc),
255255
}

examples/rosenbrock.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl System for Rosenbrock {
1313
type Scalar = f64;
1414
type Dim = na::U2;
1515

16-
fn apply_mut<Sx, Sfx>(
16+
fn apply<Sx, Sfx>(
1717
&self,
1818
x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
1919
fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,

src/analysis/initial.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ where
5959
let scale = OVector::from_iterator_generic(f.dim(), U1::name(), scale_iter);
6060

6161
// Compute F'(x) in the initial point.
62-
f.apply_mut(x, fx)?;
62+
f.apply(x, fx)?;
6363
let jac1 = Jacobian::new(f, x, &scale, fx)?;
6464

6565
// Compute Newton step.
@@ -74,7 +74,7 @@ where
7474
*x += p;
7575

7676
// Compute F'(x) after one Newton step.
77-
f.apply_mut(x, fx)?;
77+
f.apply(x, fx)?;
7878
let jac2 = Jacobian::new(f, x, &scale, fx)?;
7979

8080
// Linear variables have no effect on the Jacobian matrix. They can be

src/core/solver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use super::system::System;
6161
/// });
6262
///
6363
/// // We must compute the residuals.
64-
/// f.apply_mut(x, fx)?;
64+
/// f.apply(x, fx)?;
6565
///
6666
/// Ok(())
6767
/// }
@@ -81,7 +81,7 @@ pub trait Solver<F: System> {
8181
/// The value of `x` is the current values of variables. After the method
8282
/// returns, `x` should hold the variable values of the performed step and
8383
/// `fx` *must* contain residuals of that step as computed by
84-
/// [`System::apply_mut`].
84+
/// [`System::apply`].
8585
///
8686
/// It is implementation error not to compute the residuals of the computed
8787
/// step.

src/core/system.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub enum SystemError {
2929
///
3030
/// A system is any type that implements [`System`] trait. There are two
3131
/// required associated types (scalar type and dimension type) and two required
32-
/// methods: [`apply_mut`](System::apply_mut) and [`dim`](System::dim).
32+
/// methods: [`apply`](System::apply) and [`dim`](System::dim).
3333
///
3434
/// ```rust
3535
/// use gomez::nalgebra as na;
@@ -49,7 +49,7 @@ pub enum SystemError {
4949
/// type Dim = na::U2;
5050
///
5151
/// // Apply trial values of variables to the system.
52-
/// fn apply_mut<Sx, Sfx>(
52+
/// fn apply<Sx, Sfx>(
5353
/// &self,
5454
/// x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
5555
/// fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -81,7 +81,7 @@ pub trait System {
8181
type Dim: Dim;
8282

8383
/// Calculate the residuals of the system given values of the variables.
84-
fn apply_mut<Sx, Sfx>(
84+
fn apply<Sx, Sfx>(
8585
&self,
8686
x: &Vector<Self::Scalar, Self::Dim, Sx>,
8787
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -104,7 +104,7 @@ pub trait System {
104104
/// Some extensions methods for the [`System`] that may be found useful.
105105
pub trait SystemExt: System {
106106
/// Calculate the residuals and return the squared norm of the residuals.
107-
fn apply_mut_norm_squared<Sx, Sfx>(
107+
fn apply_norm_squared<Sx, Sfx>(
108108
&self,
109109
x: &Vector<Self::Scalar, Self::Dim, Sx>,
110110
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -115,7 +115,7 @@ pub trait SystemExt: System {
115115
}
116116

117117
impl<F: System> SystemExt for F {
118-
fn apply_mut_norm_squared<Sx, Sfx>(
118+
fn apply_norm_squared<Sx, Sfx>(
119119
&self,
120120
x: &Vector<F::Scalar, F::Dim, Sx>,
121121
fx: &mut Vector<F::Scalar, F::Dim, Sfx>,
@@ -124,7 +124,7 @@ impl<F: System> SystemExt for F {
124124
Sx: Storage<Self::Scalar, Self::Dim>,
125125
Sfx: StorageMut<Self::Scalar, Self::Dim>,
126126
{
127-
self.apply_mut(x, fx)?;
127+
self.apply(x, fx)?;
128128
Ok(fx.norm_squared())
129129
}
130130
}
@@ -189,7 +189,7 @@ where
189189
type Scalar = F::Scalar;
190190
type Dim = F::Dim;
191191

192-
fn apply_mut<Sx, Sfx>(
192+
fn apply<Sx, Sfx>(
193193
&self,
194194
x: &Vector<Self::Scalar, Self::Dim, Sx>,
195195
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -201,7 +201,7 @@ where
201201
// TODO: RepulsiveSystem should adjust the residuals of the inner system
202202
// such that solvers tend to go away from the roots stored in the
203203
// archive.
204-
self.f.apply_mut(x, fx)
204+
self.f.apply(x, fx)
205205
}
206206

207207
fn dim(&self) -> Self::Dim {

src/derivatives.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ where
105105

106106
// Update the point.
107107
x[j] = xj + step;
108-
f.apply_mut(x, &mut col)?;
108+
f.apply(x, &mut col)?;
109109

110110
// Compute the derivative approximation: J[i, j] = (F(x + e_j * step_j) - F(x)) / step_j.
111111
col -= fx;
@@ -145,7 +145,7 @@ mod tests {
145145
let mut fx = dvector![0.0, 0.0];
146146

147147
let func = ExtendedRosenbrock::new(2);
148-
func.apply_mut(&x, &mut fx).unwrap();
148+
func.apply(&x, &mut fx).unwrap();
149149
let jac = Jacobian::new(&func, &mut x, &scale, &fx);
150150

151151
assert!(jac.is_ok());
@@ -162,7 +162,7 @@ mod tests {
162162
let mut fx = dvector![0.0, 0.0, 0.0, 0.0];
163163

164164
let func = ExtendedPowell::new(4);
165-
func.apply_mut(&x, &mut fx).unwrap();
165+
func.apply(&x, &mut fx).unwrap();
166166
let jac = Jacobian::new(&func, &mut x, &scale, &fx);
167167

168168
assert!(jac.is_ok());

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
//! type Dim = na::U2;
8484
//!
8585
//! // Apply trial values of variables to the system.
86-
//! fn apply_mut<Sx, Sfx>(
86+
//! fn apply<Sx, Sfx>(
8787
//! &self,
8888
//! x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
8989
//! fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -130,7 +130,7 @@
130130
//! # type Scalar = f64;
131131
//! # type Dim = na::U2;
132132
//! #
133-
//! # fn apply_mut<Sx, Sfx>(
133+
//! # fn apply<Sx, Sfx>(
134134
//! # &self,
135135
//! # x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
136136
//! # fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -183,7 +183,7 @@
183183
//! # type Scalar = f64;
184184
//! # type Dim = na::U2;
185185
//! #
186-
//! # fn apply_mut<Sx, Sfx>(
186+
//! # fn apply<Sx, Sfx>(
187187
//! # &self,
188188
//! # x: &na::Vector<Self::Scalar, Self::Dim, Sx>,
189189
//! # fx: &mut na::Vector<Self::Scalar, Self::Dim, Sfx>,

src/population.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ where
212212
Sx: Storage<F::Scalar, F::Dim>,
213213
Sfx: StorageMut<F::Scalar, F::Dim>,
214214
{
215-
f.apply_mut(x, fx)?;
215+
f.apply(x, fx)?;
216216
Ok(fx.norm())
217217
}
218218

src/solver/cuckoo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ where
314314

315315
// Assign te best individual.
316316
x.copy_from(&population.iter_sorted().next().unwrap());
317-
f.apply_mut(x, fx)?;
317+
f.apply(x, fx)?;
318318

319319
let report = population.report();
320320

src/solver/nelder_mead.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ where
219219
if simplex.is_empty() {
220220
// Simplex initialization.
221221
simplex.push(x.clone_owned());
222-
errors.push(f.apply_mut_norm_squared(x, fx)?);
222+
errors.push(f.apply_norm_squared(x, fx)?);
223223

224224
for j in 0..n {
225225
let mut xi = x.clone_owned();
226226
xi[j] = dom.vars()[j].clamp(xi[j] + scale[j]);
227227

228-
errors.push(f.apply_mut_norm_squared(&xi, fx)?);
228+
errors.push(f.apply_norm_squared(&xi, fx)?);
229229
simplex.push(xi);
230230
}
231231

@@ -268,7 +268,7 @@ where
268268
// Perform one of possible simplex transformations.
269269
reflection.on_line2_mut(centroid, &simplex[sort_perm[n]], reflection_coeff);
270270
let reflection_not_feasible = reflection.project(dom);
271-
let reflection_error = f.apply_mut_norm_squared(reflection, fx)?;
271+
let reflection_error = f.apply_norm_squared(reflection, fx)?;
272272

273273
#[allow(clippy::suspicious_else_formatting)]
274274
let (transformation, not_feasible) = if errors[sort_perm[0]] <= reflection_error
@@ -284,7 +284,7 @@ where
284284
// farther along this direction.
285285
expansion.on_line2_mut(centroid, &simplex[sort_perm[n]], expansion_coeff);
286286
let expansion_not_feasible = expansion.project(dom);
287-
let expansion_error = f.apply_mut_norm_squared(expansion, fx)?;
287+
let expansion_error = f.apply_norm_squared(expansion, fx)?;
288288

289289
if expansion_error < reflection_error {
290290
// Expansion indeed help, replace the worst point.
@@ -309,7 +309,7 @@ where
309309
// Try to perform outer contraction.
310310
contraction.on_line2_mut(centroid, &simplex[sort_perm[n]], outer_contraction_coeff);
311311
let contraction_not_feasible = contraction.project(dom);
312-
let contraction_error = f.apply_mut_norm_squared(contraction, fx)?;
312+
let contraction_error = f.apply_norm_squared(contraction, fx)?;
313313

314314
if contraction_error <= reflection_error {
315315
// Use the contracted point instead of the reflected point
@@ -327,7 +327,7 @@ where
327327
// Try to perform inner contraction.
328328
contraction.on_line2_mut(centroid, &simplex[sort_perm[n]], inner_contraction_coeff);
329329
let contraction_not_feasible = contraction.project(dom);
330-
let contraction_error = f.apply_mut_norm_squared(contraction, fx)?;
330+
let contraction_error = f.apply_norm_squared(contraction, fx)?;
331331

332332
if contraction_error <= errors[sort_perm[n]] {
333333
// The contracted point is better than the worst point.
@@ -352,7 +352,7 @@ where
352352
for i in 1..=n {
353353
let xi = &mut simplex[sort_perm[i]];
354354
xi.on_line_mut(contraction, shrink_coeff);
355-
errors[sort_perm[i]] = f.apply_mut_norm_squared(xi, fx)?;
355+
errors[sort_perm[i]] = f.apply_norm_squared(xi, fx)?;
356356
}
357357

358358
(Transformation::Shrinkage, false)
@@ -377,7 +377,7 @@ where
377377

378378
// Return the best simplex point.
379379
x.copy_from(&simplex[sort_perm[0]]);
380-
f.apply_mut(x, fx)?;
380+
f.apply(x, fx)?;
381381

382382
if transformation == Transformation::Shrinkage || not_feasible {
383383
// Check whether the simplex collapsed or not. It can happen only

src/solver/trust_region.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ where
241241
}
242242

243243
// Compute F(x) and F'(x).
244-
f.apply_mut(x, fx)?;
244+
f.apply(x, fx)?;
245245
jac.compute(f, x, scale, fx)?;
246246

247247
let fx_norm = fx.norm();
@@ -531,7 +531,7 @@ where
531531
}
532532

533533
// Compute F(x').
534-
let is_trial_valid = f.apply_mut(x_trial, fx_trial).is_ok();
534+
let is_trial_valid = f.apply(x_trial, fx_trial).is_ok();
535535
let fx_trial_norm = fx_trial.norm();
536536

537537
let gain_ratio = if is_trial_valid {

src/testing.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ where
5858
Sx: Storage<Self::Scalar, Self::Dim>,
5959
{
6060
let mut fx = x.clone_owned();
61-
if self.apply_mut(x, &mut fx).is_ok() {
61+
if self.apply(x, &mut fx).is_ok() {
6262
fx.iter().all(|fxi| fxi.abs() <= eps)
6363
} else {
6464
false
@@ -116,7 +116,7 @@ impl System for ExtendedRosenbrock {
116116
type Scalar = f64;
117117
type Dim = Dynamic;
118118

119-
fn apply_mut<Sx, Sfx>(
119+
fn apply<Sx, Sfx>(
120120
&self,
121121
x: &Vector<Self::Scalar, Self::Dim, Sx>,
122122
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -222,7 +222,7 @@ impl System for ExtendedPowell {
222222
type Scalar = f64;
223223
type Dim = Dynamic;
224224

225-
fn apply_mut<Sx, Sfx>(
225+
fn apply<Sx, Sfx>(
226226
&self,
227227
x: &Vector<Self::Scalar, Self::Dim, Sx>,
228228
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -301,7 +301,7 @@ impl System for BullardBiegler {
301301
type Scalar = f64;
302302
type Dim = U2;
303303

304-
fn apply_mut<Sx, Sfx>(
304+
fn apply<Sx, Sfx>(
305305
&self,
306306
x: &Vector<Self::Scalar, Self::Dim, Sx>,
307307
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -371,7 +371,7 @@ impl System for Sphere {
371371
type Scalar = f64;
372372
type Dim = Dynamic;
373373

374-
fn apply_mut<Sx, Sfx>(
374+
fn apply<Sx, Sfx>(
375375
&self,
376376
x: &Vector<Self::Scalar, Self::Dim, Sx>,
377377
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -440,7 +440,7 @@ impl System for Brown {
440440
type Scalar = f64;
441441
type Dim = Dynamic;
442442

443-
fn apply_mut<Sx, Sfx>(
443+
fn apply<Sx, Sfx>(
444444
&self,
445445
x: &Vector<Self::Scalar, Self::Dim, Sx>,
446446
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,
@@ -503,7 +503,7 @@ impl System for Exponential {
503503
type Scalar = f64;
504504
type Dim = Dynamic;
505505

506-
fn apply_mut<Sx, Sfx>(
506+
fn apply<Sx, Sfx>(
507507
&self,
508508
x: &Vector<Self::Scalar, Self::Dim, Sx>,
509509
fx: &mut Vector<Self::Scalar, Self::Dim, Sfx>,

0 commit comments

Comments
 (0)