@ -3,9 +3,9 @@ use crate::traits::XVar;
/// Trait that should be implemented for objects that will be minimzed
pub trait ObjectiveFun < T : XVar < E > + Clone , E > {
/// Return the objective function value at a specified coordinate
fn eval ( & self , xs : & [ T ] ) -> f64 ;
fn eval ( & self , xs : & T ) -> f64 ;
/// Return the gradients of the objective function value for specified coordinates
fn prime ( & self , xs : & [ T ] ) -> Vec < E > ;
fn prime ( & self , xs : & T ) -> E ;
}
/// Enum allowing for selection of style of numerical differentiation
@ -23,12 +23,12 @@ pub struct FunWithNumericalDiff {
style : DiffStyle ,
}
impl ObjectiveFun < f64 , f64 > for FunWithNumericalDiff {
fn eval ( & self , xs : & [ f64 ] ) -> f64 {
impl ObjectiveFun < Vec < f64 > , Vec < f64 > > for FunWithNumericalDiff {
fn eval ( & self , xs : & Vec < f64 > ) -> f64 {
( self . function ) ( xs )
}
fn prime ( & self , xs : & [ f64 ] ) -> Vec < f64 > {
fn prime ( & self , xs : & Vec < f64 > ) -> Vec < f64 > {
let mut xs_local = Vec ::new ( ) ;
xs_local . extend_from_slice ( xs ) ;
let f : Box < dyn FnMut ( ( usize , & f64 ) ) -> f64 > = match self . style {
@ -60,25 +60,25 @@ impl ObjectiveFun<f64, f64> for FunWithNumericalDiff {
/// Struct that wraps two lambda with one providing the objective function evaluation and the other
/// providing the gradient value
pub struct Fun < T : XVar < E > , E > {
function : Box < dyn Fn ( & [ T ] ) -> f64 > ,
prime : Box < dyn Fn ( & [ T ] ) -> Vec < E > > ,
function : Box < dyn Fn ( & T ) -> f64 > ,
prime : Box < dyn Fn ( & T ) -> E > ,
}
// Simple type to remove the generics
pub type F64Fun = Fun < f64 , f64 > ;
impl < T : XVar < E > , E > ObjectiveFun < T , E > for Fun < T , E > {
fn eval ( & self , xs : & [ T ] ) -> f64 {
fn eval ( & self , xs : & T ) -> f64 {
( self . function ) ( xs )
}
fn prime ( & self , xs : & [ T ] ) -> Vec < E > {
fn prime ( & self , xs : & T ) -> E {
( self . prime ) ( xs )
}
}
impl < T : XVar < E > , E > Fun < T , E > {
pub fn new ( function : Box < dyn Fn ( & [ T ] ) -> f64 > , prime : Box < dyn Fn ( & [ T ] ) -> Vec < E > > ) -> Self {
pub fn new ( function : Box < dyn Fn ( & T ) -> f64 > , prime : Box < dyn Fn ( & T ) -> E > ) -> Self {
Fun { function , prime }
}
}