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

Fix multifit_solver FFI #83

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/multifit_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn main() {
}
}

rgsl::multifit::covar(&s.J(), 0f64, &mut covar);
rgsl::multifit::covar(&s.J().unwrap(), 0f64, &mut covar);

{
let chi = rgsl::blas::level1::dnrm2(&s.f());
Expand Down
36 changes: 26 additions & 10 deletions src/ffi/solvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ pub struct gsl_multifit_function_fdf {
pub n: size_t,
pub p: size_t,
pub params: *mut c_void,
pub nevalf: size_t,
pub nevaldf: size_t,
}

#[repr(C)]
Expand All @@ -175,23 +177,35 @@ pub struct gsl_multifit_fdfsolver_type {
pub set: Option<
extern "C" fn(
state: *mut c_void,
wts: *const gsl_vector,
fdf: *mut gsl_multifit_function_fdf,
x: *mut gsl_vector,
f: *mut gsl_vector,
J: *mut gsl_matrix,
dx: *mut gsl_vector,
) -> c_int,
>,
pub iterate: Option<
extern "C" fn(
state: *mut c_void,
wts: *const gsl_vector,
fdf: *mut gsl_multifit_function_fdf,
x: *mut gsl_vector,
f: *mut gsl_vector,
J: *mut gsl_matrix,
dx: *mut gsl_vector,
) -> c_int,
>,
pub gradient: ::std::option::Option<
extern "C" fn(
state: *mut c_void,
g: *mut gsl_vector,
) -> c_int,
>,
pub jac: ::std::option::Option<
extern "C" fn(
state: *mut c_void,
J: *mut gsl_matrix,
) -> c_int,
>,
pub free: Option<extern "C" fn(state: *mut c_void)>,
}

Expand All @@ -201,8 +215,10 @@ pub struct gsl_multifit_fdfsolver {
pub fdf: *mut gsl_multifit_function_fdf,
pub x: *mut gsl_vector,
pub f: *mut gsl_vector,
pub J: *mut gsl_matrix,
pub dx: *mut gsl_vector,
pub g: *mut gsl_vector,
pub sqrt_wts: *mut gsl_vector,
pub niter: size_t,
pub state: *mut c_void,
}

Expand All @@ -222,7 +238,7 @@ pub struct gsl_root_fsolver_type {
extern "C" fn(
state: *mut c_void,
f: *mut gsl_function,
root: c_double,
root: *mut c_double,
x_lower: c_double,
x_upper: c_double,
) -> c_int,
Expand All @@ -231,9 +247,9 @@ pub struct gsl_root_fsolver_type {
extern "C" fn(
state: *mut c_void,
f: *mut gsl_function,
root: c_double,
x_lower: c_double,
x_upper: c_double,
root: *mut c_double,
x_lower: *mut c_double,
x_upper: *mut c_double,
) -> c_int,
>,
}
Expand All @@ -260,13 +276,13 @@ pub struct gsl_function_fdf {

#[repr(C)]
pub struct gsl_root_fdfsolver_type {
pub name: c_char,
pub name: *const c_char,
pub size: size_t,
pub set: Option<
extern "C" fn(state: *mut c_void, f: *mut gsl_function_fdf, root: c_double) -> c_int,
extern "C" fn(state: *mut c_void, f: *mut gsl_function_fdf, root: *mut c_double) -> c_int,
>,
pub iterate: Option<
extern "C" fn(state: *mut c_void, f: *mut gsl_function_fdf, root: c_double) -> c_int,
extern "C" fn(state: *mut c_void, f: *mut gsl_function_fdf, root: *mut c_double) -> c_int,
>,
}

Expand Down
20 changes: 17 additions & 3 deletions src/types/multifit_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ pub struct MultiFitFunction {

pub struct MultiFitFdfSolver {
intern: *mut ffi::gsl_multifit_fdfsolver,
n: usize,
p: usize,
}

impl MultiFitFdfSolver {
Expand All @@ -343,7 +345,7 @@ impl MultiFitFdfSolver {
if s.is_null() {
None
} else {
Some(MultiFitFdfSolver { intern: s })
Some(MultiFitFdfSolver { intern: s, n, p, })
}
}

Expand All @@ -363,8 +365,10 @@ impl MultiFitFdfSolver {
unsafe { ffi::FFI::soft_wrap((*self.intern).f) }
}

pub fn J(&self) -> ::MatrixF64 {
unsafe { ffi::FFI::soft_wrap((*self.intern).J) }
pub fn J(&self) -> Option<::MatrixF64> {
let mut matrix = ::MatrixF64::new(self.n, self.p)?;
unsafe{ (*(*self.intern).type_).jac?((*self.intern).state, ffi::FFI::unwrap_unique(&mut matrix)); }
Some(matrix)
}

pub fn dx(&self) -> ::VectorF64 {
Expand Down Expand Up @@ -481,6 +485,8 @@ impl MultiFitFunctionFdf {
n: n,
p: p,
params: ::std::ptr::null_mut(),
nevalf: 0,
nevaldf: 0,
},
}
}
Expand All @@ -491,6 +497,14 @@ impl MultiFitFunctionFdf {
self.intern.params = self as *mut MultiFitFunctionFdf as *mut c_void;
&mut self.intern
}

pub fn nevalf(&self) -> usize {
self.intern.nevalf
}

pub fn nevaldf(&self) -> usize {
self.intern.nevaldf
}
}

extern "C" fn f(
Expand Down