Skip to content

Commit 96abe29

Browse files
committed
feat: change functions to references to avoid cloning any underlying function data
1 parent ce41291 commit 96abe29

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

benches/rosenbrock_benchmark.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ fn rosenbrock_benchmark(c: &mut Criterion) {
99
let x0 = vec![5.0; *ndim];
1010
b.iter(|| {
1111
let rb = Rosenbrock { n };
12-
let mut m = NelderMead::new(rb, &x0, Some(NelderMeadOptions::builder().build()));
12+
let mut m = NelderMead::new(&rb, &x0, Some(NelderMeadOptions::builder().build()));
1313
minimize!(m, 1000000).unwrap();
1414
});
1515
});
1616
group.bench_with_input(BenchmarkId::new("Adaptive", n), &n, |b, ndim| {
1717
let x0 = vec![5.0; *ndim];
1818
b.iter(|| {
1919
let rb = Rosenbrock { n };
20-
let mut m = NelderMead::new(rb, &x0, Some(NelderMeadOptions::adaptive(n).build()));
20+
let mut m = NelderMead::new(&rb, &x0, Some(NelderMeadOptions::adaptive(n).build()));
2121
minimize!(m, 1000000).unwrap();
2222
});
2323
});

src/algorithms/gradient_descent.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ pub struct GradientDescentOptions<F: Field> {
2727
///
2828
/// This method will terminate if $`|f(\vec{x}_{i}) - f(\vec{x}_{i-1})|`$ is smaller than
2929
/// [`GradientDescentOptions::tolerance`].
30-
pub struct GradientDescent<F, A, E>
30+
pub struct GradientDescent<'f, F, A, E>
3131
where
3232
F: Field,
3333
{
34-
function: Box<dyn Function<F, A, E>>,
34+
function: &'f dyn Function<F, A, E>,
3535
options: GradientDescentOptions<F>,
3636
x: Vec<F>,
3737
fx: F,
@@ -40,19 +40,19 @@ where
4040
fx_best: F,
4141
current_step: usize,
4242
}
43-
impl<F, A, E> GradientDescent<F, A, E>
43+
impl<'f, F, A, E> GradientDescent<'f, F, A, E>
4444
where
4545
F: Field,
4646
{
4747
/// Create a new Gradient Descent optimizer from a struct which implements [`Function`], an initial
4848
/// starting point `x0`, and some options.
4949
pub fn new<Func: Function<F, A, E> + 'static>(
50-
function: Func,
50+
function: &'f Func,
5151
x0: &[F],
5252
options: Option<GradientDescentOptions<F>>,
5353
) -> Self {
5454
Self {
55-
function: Box::new(function),
55+
function,
5656
options: options.unwrap_or_else(|| GradientDescentOptions::builder().build()),
5757
x: x0.to_vec(),
5858
fx: F::NAN,
@@ -64,7 +64,7 @@ where
6464
}
6565
}
6666

67-
impl<F, A, E> Minimizer<F, A, E> for GradientDescent<F, A, E>
67+
impl<'f, F, A, E> Minimizer<F, A, E> for GradientDescent<'f, F, A, E>
6868
where
6969
F: Field,
7070
{

src/algorithms/nelder_mead.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ impl<F: Field> NelderMeadOptions<F> {
100100
/// See [`NelderMeadOptions`] to set the values of $`\alpha`$, $`\gamma`$, $`\rho_i`$, $`\rho_o`$,
101101
/// and $`\sigma`$.
102102
///
103-
pub struct NelderMead<F, A, E>
103+
pub struct NelderMead<'f, F, A, E>
104104
where
105105
F: Field,
106106
{
107-
function: Box<dyn Function<F, A, E>>,
107+
function: &'f dyn Function<F, A, E>,
108108
options: NelderMeadOptions<F>,
109109
simplex_x: Vec<Vec<F>>,
110110
simplex_fx: Vec<F>,
@@ -116,22 +116,22 @@ where
116116
n_simplex: usize,
117117
current_step: usize,
118118
}
119-
impl<F, A, E> NelderMead<F, A, E>
119+
impl<'f, F, A, E> NelderMead<'f, F, A, E>
120120
where
121121
F: Field,
122122
{
123123
/// Create a new Nelder-Mead optimizer from a struct which implements [`Function`], an initial
124124
/// starting point `x0`, and some options.
125125
pub fn new<Func: Function<F, A, E> + 'static>(
126-
function: Func,
126+
function: &'f Func,
127127
x0: &[F],
128128
options: Option<NelderMeadOptions<F>>,
129129
) -> Self {
130130
let n_simplex = x0.len() + 1;
131131
let options = options.unwrap_or_else(|| NelderMeadOptions::builder().build());
132132
let simplex_size = options.simplex_size;
133133
Self {
134-
function: Box::new(function),
134+
function,
135135
options,
136136
simplex_x: Self::construct_simplex(x0, n_simplex, simplex_size),
137137
simplex_fx: vec![F::NAN; n_simplex],
@@ -267,7 +267,7 @@ pub struct NelderMeadMessage<F> {
267267
pub sstd: F,
268268
}
269269

270-
impl<F, A, E> Minimizer<F, A, E> for NelderMead<F, A, E>
270+
impl<'f, F, A, E> Minimizer<F, A, E> for NelderMead<'f, F, A, E>
271271
where
272272
F: Field,
273273
{

src/algorithms/newton.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ pub struct NewtonOptions<F: Field> {
3131
///
3232
/// This method will terminate if either [`NewtonOptions::max_iters`] steps are performed or if
3333
/// $`|f(\vec{x}_{i}) - f(\vec{x}_{i-1})|`$ is smaller than [`NewtonOptions::tolerance`].
34-
pub struct Newton<F, A, E>
34+
pub struct Newton<'f, F, A, E>
3535
where
3636
F: Field,
3737
{
38-
function: Box<dyn Function<F, A, E>>,
38+
function: &'f dyn Function<F, A, E>,
3939
options: NewtonOptions<F>,
4040
x: Vec<F>,
4141
fx: F,
@@ -45,19 +45,19 @@ where
4545
current_step: usize,
4646
singular_hessian: bool,
4747
}
48-
impl<F, A, E> Newton<F, A, E>
48+
impl<'f, F, A, E> Newton<'f, F, A, E>
4949
where
5050
F: Field,
5151
{
5252
/// Create a new Newton optimizer from a struct which implements [`Function`], an initial
5353
/// starting point `x0`, and some options.
5454
pub fn new<Func: Function<F, A, E> + 'static>(
55-
function: Func,
55+
function: &'f Func,
5656
x0: &[F],
5757
options: Option<NewtonOptions<F>>,
5858
) -> Self {
5959
Self {
60-
function: Box::new(function),
60+
function,
6161
options: options.unwrap_or_else(|| NewtonOptions::builder().build()),
6262
x: x0.to_vec(),
6363
fx: F::NAN,
@@ -70,7 +70,7 @@ where
7070
}
7171
}
7272

73-
impl<F, A, E> Minimizer<F, A, E> for Newton<F, A, E>
73+
impl<'f, F, A, E> Minimizer<F, A, E> for Newton<'f, F, A, E>
7474
where
7575
F: Field,
7676
{

0 commit comments

Comments
 (0)