rgsl/types/
multifit_linear.rs

1//
2// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
3//
4
5use crate::{MatrixF64, Value, VectorF64};
6use ffi::FFI;
7
8ffi_wrapper!(
9    MultifitLinearWorkspace,
10    *mut sys::gsl_multifit_linear_workspace,
11    gsl_multifit_linear_free
12);
13
14impl MultifitLinearWorkspace {
15    #[doc(alias = "gsl_multifit_linear_alloc")]
16    pub fn new(n: usize, p: usize) -> Option<Self> {
17        let s = unsafe { sys::gsl_multifit_linear_alloc(n, p) };
18        if s.is_null() {
19            None
20        } else {
21            Some(Self::wrap(s))
22        }
23    }
24
25    /// Returns `chisq`.
26    #[doc(alias = "gsl_multifit_linear")]
27    pub fn linear(
28        &mut self,
29        x: &MatrixF64,
30        y: &VectorF64,
31        c: &mut VectorF64,
32        cov: &mut MatrixF64,
33    ) -> Result<f64, Value> {
34        let mut chisq = 0.;
35        let ret = unsafe {
36            sys::gsl_multifit_linear(
37                x.unwrap_shared(),
38                y.unwrap_shared(),
39                c.unwrap_unique(),
40                cov.unwrap_unique(),
41                &mut chisq,
42                self.unwrap_unique(),
43            )
44        };
45        result_handler!(ret, chisq)
46    }
47
48    /// Returns `(chisq, rank)`.
49    #[cfg(feature = "v2_3")]
50    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_3")))]
51    #[doc(alias = "gsl_multifit_linear_tsvd")]
52    pub fn linear_tsvd(
53        &mut self,
54        x: &MatrixF64,
55        y: &VectorF64,
56        tol: f64,
57        c: &mut VectorF64,
58        cov: &mut MatrixF64,
59    ) -> Result<(f64, usize), Value> {
60        let mut chisq = 0.;
61        let mut rank = 0;
62        let ret = unsafe {
63            sys::gsl_multifit_linear_tsvd(
64                x.unwrap_shared(),
65                y.unwrap_shared(),
66                tol,
67                c.unwrap_unique(),
68                cov.unwrap_unique(),
69                &mut chisq,
70                &mut rank,
71                self.unwrap_unique(),
72            )
73        };
74        result_handler!(ret, (chisq, rank))
75    }
76
77    #[doc(alias = "gsl_multifit_linear_svd")]
78    pub fn linear_svd(&mut self, x: &mut MatrixF64) -> Result<(), Value> {
79        let ret = unsafe { sys::gsl_multifit_linear_svd(x.unwrap_unique(), self.unwrap_unique()) };
80        result_handler!(ret, ())
81    }
82
83    #[doc(alias = "gsl_multifit_linear_bsvd")]
84    pub fn linear_bsvd(&mut self, x: &mut MatrixF64) -> Result<(), Value> {
85        let ret = unsafe { sys::gsl_multifit_linear_bsvd(x.unwrap_unique(), self.unwrap_unique()) };
86        result_handler!(ret, ())
87    }
88
89    #[cfg(feature = "v2_3")]
90    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_3")))]
91    #[doc(alias = "gsl_multifit_linear_rank")]
92    pub fn linear_rank(&self, tol: f64) -> usize {
93        unsafe { sys::gsl_multifit_linear_rank(tol, self.unwrap_shared()) }
94    }
95
96    /// Returns `(rnorm, snorm)`.
97    #[doc(alias = "gsl_multifit_linear_solve")]
98    pub fn linear_solve(
99        &mut self,
100        lambda: f64,
101        x: &MatrixF64,
102        y: &VectorF64,
103        c: &mut VectorF64,
104    ) -> Result<(f64, f64), Value> {
105        let mut rnorm = 0.;
106        let mut snorm = 0.;
107        let ret = unsafe {
108            sys::gsl_multifit_linear_solve(
109                lambda,
110                x.unwrap_shared(),
111                y.unwrap_shared(),
112                c.unwrap_unique(),
113                &mut rnorm,
114                &mut snorm,
115                self.unwrap_unique(),
116            )
117        };
118        result_handler!(ret, (rnorm, snorm))
119    }
120
121    #[doc(alias = "gsl_multifit_linear_stdform1")]
122    pub fn linear_stdform1(
123        &mut self,
124        l: &VectorF64,
125        x: &MatrixF64,
126        y: &VectorF64,
127        xs: &mut MatrixF64,
128        ys: &mut VectorF64,
129    ) -> Result<(), Value> {
130        let ret = unsafe {
131            sys::gsl_multifit_linear_stdform1(
132                l.unwrap_shared(),
133                x.unwrap_shared(),
134                y.unwrap_shared(),
135                xs.unwrap_unique(),
136                ys.unwrap_unique(),
137                self.unwrap_unique(),
138            )
139        };
140        result_handler!(ret, ())
141    }
142
143    #[doc(alias = "gsl_multifit_linear_wstdform1")]
144    pub fn linear_wstdform1(
145        &mut self,
146        l: &VectorF64,
147        x: &MatrixF64,
148        w: &VectorF64,
149        y: &VectorF64,
150        xs: &mut MatrixF64,
151        ys: &mut VectorF64,
152    ) -> Result<(), Value> {
153        let ret = unsafe {
154            sys::gsl_multifit_linear_wstdform1(
155                l.unwrap_shared(),
156                x.unwrap_shared(),
157                w.unwrap_shared(),
158                y.unwrap_shared(),
159                xs.unwrap_unique(),
160                ys.unwrap_unique(),
161                self.unwrap_unique(),
162            )
163        };
164        result_handler!(ret, ())
165    }
166
167    #[doc(alias = "gsl_multifit_linear_stdform2")]
168    pub fn linear_stdform2(
169        &mut self,
170        lqr: &MatrixF64,
171        ltau: &VectorF64,
172        x: &MatrixF64,
173        y: &VectorF64,
174        xs: &mut MatrixF64,
175        ys: &mut VectorF64,
176        m: &mut MatrixF64,
177    ) -> Result<(), Value> {
178        let ret = unsafe {
179            sys::gsl_multifit_linear_stdform2(
180                lqr.unwrap_shared(),
181                ltau.unwrap_shared(),
182                x.unwrap_shared(),
183                y.unwrap_shared(),
184                xs.unwrap_unique(),
185                ys.unwrap_unique(),
186                m.unwrap_unique(),
187                self.unwrap_unique(),
188            )
189        };
190        result_handler!(ret, ())
191    }
192
193    #[doc(alias = "gsl_multifit_linear_wstdform2")]
194    pub fn linear_wstdform2(
195        &mut self,
196        lqr: &MatrixF64,
197        ltau: &VectorF64,
198        x: &MatrixF64,
199        w: &VectorF64,
200        y: &VectorF64,
201        xs: &mut MatrixF64,
202        ys: &mut VectorF64,
203        m: &mut MatrixF64,
204    ) -> Result<(), Value> {
205        let ret = unsafe {
206            sys::gsl_multifit_linear_wstdform2(
207                lqr.unwrap_shared(),
208                ltau.unwrap_shared(),
209                x.unwrap_shared(),
210                w.unwrap_shared(),
211                y.unwrap_shared(),
212                xs.unwrap_unique(),
213                ys.unwrap_unique(),
214                m.unwrap_unique(),
215                self.unwrap_unique(),
216            )
217        };
218        result_handler!(ret, ())
219    }
220
221    #[doc(alias = "gsl_multifit_linear_genform1")]
222    pub fn linear_genform1(
223        &mut self,
224        l: &VectorF64,
225        cs: &VectorF64,
226        c: &mut VectorF64,
227    ) -> Result<(), Value> {
228        let ret = unsafe {
229            sys::gsl_multifit_linear_genform1(
230                l.unwrap_shared(),
231                cs.unwrap_shared(),
232                c.unwrap_unique(),
233                self.unwrap_unique(),
234            )
235        };
236        result_handler!(ret, ())
237    }
238
239    #[doc(alias = "gsl_multifit_linear_genform2")]
240    pub fn linear_genform2(
241        &mut self,
242        lqr: &MatrixF64,
243        ltau: &VectorF64,
244        x: &MatrixF64,
245        y: &VectorF64,
246        cs: &VectorF64,
247        m: &MatrixF64,
248        c: &mut VectorF64,
249    ) -> Result<(), Value> {
250        let ret = unsafe {
251            sys::gsl_multifit_linear_genform2(
252                lqr.unwrap_shared(),
253                ltau.unwrap_shared(),
254                x.unwrap_shared(),
255                y.unwrap_shared(),
256                cs.unwrap_shared(),
257                m.unwrap_shared(),
258                c.unwrap_unique(),
259                self.unwrap_unique(),
260            )
261        };
262        result_handler!(ret, ())
263    }
264
265    #[doc(alias = "gsl_multifit_linear_wgenform2")]
266    pub fn linear_wgenform2(
267        &mut self,
268        lqr: &MatrixF64,
269        ltau: &VectorF64,
270        x: &MatrixF64,
271        w: &VectorF64,
272        y: &VectorF64,
273        cs: &VectorF64,
274        m: &MatrixF64,
275        c: &mut VectorF64,
276    ) -> Result<(), Value> {
277        let ret = unsafe {
278            sys::gsl_multifit_linear_wgenform2(
279                lqr.unwrap_shared(),
280                ltau.unwrap_shared(),
281                x.unwrap_shared(),
282                w.unwrap_shared(),
283                y.unwrap_shared(),
284                cs.unwrap_shared(),
285                m.unwrap_shared(),
286                c.unwrap_unique(),
287                self.unwrap_unique(),
288            )
289        };
290        result_handler!(ret, ())
291    }
292
293    #[doc(alias = "gsl_multifit_linear_lcurve")]
294    pub fn linear_lcurve(
295        &mut self,
296        y: &VectorF64,
297        reg_param: &mut VectorF64,
298        rho: &mut VectorF64,
299        eta: &mut VectorF64,
300    ) -> Result<(), Value> {
301        let ret = unsafe {
302            sys::gsl_multifit_linear_lcurve(
303                y.unwrap_shared(),
304                reg_param.unwrap_unique(),
305                rho.unwrap_unique(),
306                eta.unwrap_unique(),
307                self.unwrap_unique(),
308            )
309        };
310        result_handler!(ret, ())
311    }
312
313    #[doc(alias = "gsl_multifit_linear_Lsobolev")]
314    pub fn linear_Lsobolev(
315        &mut self,
316        p: usize,
317        kmax: usize,
318        alpha: &VectorF64,
319        l: &mut MatrixF64,
320    ) -> Result<(), Value> {
321        let ret = unsafe {
322            sys::gsl_multifit_linear_Lsobolev(
323                p,
324                kmax,
325                alpha.unwrap_shared(),
326                l.unwrap_unique(),
327                self.unwrap_unique(),
328            )
329        };
330        result_handler!(ret, ())
331    }
332
333    /// Returns `chisq`.
334    #[doc(alias = "gsl_multifit_wlinear")]
335    pub fn wlinear(
336        &mut self,
337        x: &MatrixF64,
338        w: &VectorF64,
339        y: &VectorF64,
340        c: &mut VectorF64,
341        cov: &mut MatrixF64,
342    ) -> Result<f64, Value> {
343        let mut chisq = 0.;
344        let ret = unsafe {
345            sys::gsl_multifit_wlinear(
346                x.unwrap_shared(),
347                w.unwrap_shared(),
348                y.unwrap_shared(),
349                c.unwrap_unique(),
350                cov.unwrap_unique(),
351                &mut chisq,
352                self.unwrap_unique(),
353            )
354        };
355        result_handler!(ret, chisq)
356    }
357
358    /// Returns `(chisq, rank)`.
359    #[cfg(feature = "v2_3")]
360    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_3")))]
361    #[doc(alias = "gsl_multifit_wlinear_tsvd")]
362    pub fn wlinear_tsvd(
363        &mut self,
364        x: &MatrixF64,
365        w: &VectorF64,
366        y: &VectorF64,
367        tol: f64,
368        c: &mut VectorF64,
369        cov: &mut MatrixF64,
370    ) -> Result<(f64, usize), Value> {
371        let mut chisq = 0.;
372        let mut rank = 0;
373        let ret = unsafe {
374            sys::gsl_multifit_wlinear_tsvd(
375                x.unwrap_shared(),
376                w.unwrap_shared(),
377                y.unwrap_shared(),
378                tol,
379                c.unwrap_unique(),
380                cov.unwrap_unique(),
381                &mut chisq,
382                &mut rank,
383                self.unwrap_unique(),
384            )
385        };
386        result_handler!(ret, (chisq, rank))
387    }
388
389    /// Returns `(rank, chisq)`.
390    #[doc(alias = "gsl_multifit_wlinear_svd")]
391    pub fn wlinear_svd(
392        &mut self,
393        x: &MatrixF64,
394        w: &VectorF64,
395        y: &VectorF64,
396        tol: f64,
397        c: &mut VectorF64,
398        cov: &mut MatrixF64,
399    ) -> Result<(usize, f64), Value> {
400        let mut rank = 0;
401        let mut chisq = 0.;
402        let ret = unsafe {
403            sys::gsl_multifit_wlinear_svd(
404                x.unwrap_shared(),
405                w.unwrap_shared(),
406                y.unwrap_shared(),
407                tol,
408                &mut rank,
409                c.unwrap_unique(),
410                cov.unwrap_unique(),
411                &mut chisq,
412                self.unwrap_unique(),
413            )
414        };
415        result_handler!(ret, (rank, chisq))
416    }
417
418    /// Returns `(rank, chisq)`.
419    #[doc(alias = "gsl_multifit_wlinear_usvd")]
420    pub fn wlinear_usvd(
421        &mut self,
422        x: &MatrixF64,
423        w: &VectorF64,
424        y: &VectorF64,
425        tol: f64,
426        c: &mut VectorF64,
427        cov: &mut MatrixF64,
428    ) -> Result<(usize, f64), Value> {
429        let mut rank = 0;
430        let mut chisq = 0.;
431        let ret = unsafe {
432            sys::gsl_multifit_wlinear_usvd(
433                x.unwrap_shared(),
434                w.unwrap_shared(),
435                y.unwrap_shared(),
436                tol,
437                &mut rank,
438                c.unwrap_unique(),
439                cov.unwrap_unique(),
440                &mut chisq,
441                self.unwrap_unique(),
442            )
443        };
444        result_handler!(ret, (rank, chisq))
445    }
446
447    #[cfg(feature = "v2_1")]
448    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_1")))]
449    #[doc(alias = "gsl_multifit_linear_rcond")]
450    pub fn linear_rcond(&mut self) -> f64 {
451        unsafe { sys::gsl_multifit_linear_rcond(self.unwrap_unique()) }
452    }
453
454    /// Returns `delta0`.
455    #[doc(alias = "gsl_multifit_linear_gcv_init")]
456    pub fn linear_gcv_init(
457        &mut self,
458        y: &VectorF64,
459        reg_param: &mut VectorF64,
460        UTy: &mut VectorF64,
461    ) -> Result<f64, Value> {
462        let mut delta0 = 0.;
463        let ret = unsafe {
464            sys::gsl_multifit_linear_gcv_init(
465                y.unwrap_shared(),
466                reg_param.unwrap_unique(),
467                UTy.unwrap_unique(),
468                &mut delta0,
469                self.unwrap_unique(),
470            )
471        };
472        result_handler!(ret, delta0)
473    }
474
475    #[doc(alias = "gsl_multifit_linear_gcv_curve")]
476    pub fn linear_gcv_curve(
477        &mut self,
478        reg_param: &VectorF64,
479        UTy: &VectorF64,
480        delta0: f64,
481        g: &mut VectorF64,
482    ) -> Result<(), Value> {
483        let ret = unsafe {
484            sys::gsl_multifit_linear_gcv_curve(
485                reg_param.unwrap_shared(),
486                UTy.unwrap_shared(),
487                delta0,
488                g.unwrap_unique(),
489                self.unwrap_unique(),
490            )
491        };
492        result_handler!(ret, ())
493    }
494
495    /// Returns `lambda`.
496    #[doc(alias = "gsl_multifit_linear_gcv_min")]
497    pub fn linear_gcv_min(
498        &mut self,
499        reg_param: &VectorF64,
500        UTy: &VectorF64,
501        g: &VectorF64,
502        delta0: f64,
503    ) -> Result<f64, Value> {
504        let mut lambda = 0.;
505        let ret = unsafe {
506            sys::gsl_multifit_linear_gcv_min(
507                reg_param.unwrap_shared(),
508                UTy.unwrap_shared(),
509                g.unwrap_shared(),
510                delta0,
511                &mut lambda,
512                self.unwrap_unique(),
513            )
514        };
515        result_handler!(ret, lambda)
516    }
517
518    #[doc(alias = "gsl_multifit_linear_gcv_calc")]
519    pub fn linear_gcv_calc(&mut self, lambda: f64, UTy: &VectorF64, delta0: f64) -> f64 {
520        unsafe {
521            sys::gsl_multifit_linear_gcv_calc(
522                lambda,
523                UTy.unwrap_shared(),
524                delta0,
525                self.unwrap_unique(),
526            )
527        }
528    }
529
530    /// Returns `(lambda, g_lambda)`.
531    #[doc(alias = "gsl_multifit_linear_gcv")]
532    pub fn linear_gcv(
533        &mut self,
534        y: &VectorF64,
535        reg_param: &mut VectorF64,
536        g: &mut VectorF64,
537    ) -> Result<(f64, f64), Value> {
538        let mut lambda = 0.;
539        let mut g_lambda = 0.;
540        let ret = unsafe {
541            sys::gsl_multifit_linear_gcv(
542                y.unwrap_shared(),
543                reg_param.unwrap_unique(),
544                g.unwrap_unique(),
545                &mut lambda,
546                &mut g_lambda,
547                self.unwrap_unique(),
548            )
549        };
550        result_handler!(ret, (lambda, g_lambda))
551    }
552}