comparison rDiff/src/locfit/Source/mexlf.c @ 0:0f80a5141704

version 0.3 uploaded
author vipints
date Thu, 14 Feb 2013 23:38:36 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0f80a5141704
1 /*=================================================================
2 * mexlocfit.c
3 *
4 * starting a locfit interface.
5 *
6 /* $Revision: 1.5 $ */
7 #include "mex.h"
8 #include "lfev.h"
9
10 design des;
11 lfit lf;
12 int lf_error;
13
14 extern void lfmxdata(), lfmxsp(), lfmxevs();
15
16 void
17 mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
18 {
19 int d, i, nvc[5], nvm, vc, nv, nc, mvc, lw[7];
20 double *y;
21 mxArray *iwkc, *temparray;
22 const char * fpcnames[] = {"evaluation_points","fitted_values","evaluation_vectors","fit_limits","family_link","kappa"};
23 const char * evsnames[] = {"cell","splitvar","lo","hi"};
24
25 mut_redirect(mexPrintf);
26 if (nrhs != 3) mexErrMsgTxt("mexlf requires 3 inputs.");
27 lf_error = 0;
28
29 lfit_alloc(&lf);
30 lfit_init(&lf);
31
32 lfmxdata(&lf.lfd,prhs[0]);
33 d = lf.lfd.d;
34
35 lfmxsp(&lf.sp,prhs[2],d);
36 lfmxevs(&lf,prhs[1]);
37
38 guessnv(&lf.evs,&lf.sp,&lf.mdl,lf.lfd.n,d,lw,nvc);
39 nvm = lf.fp.nvm = nvc[0];
40 vc = nvc[2];
41 if (ev(&lf.evs) != EPRES) lf.fp.xev = mxCalloc(nvm*d,sizeof(double));
42 lf.fp.lev = nvm*d;
43 lf.fp.wk = lf.fp.coef = mxCalloc(lw[1],sizeof(double));
44 lf.fp.lwk = lw[1];
45 lf.evs.iwk = mxCalloc(lw[2],sizeof(int));
46 lf.evs.liw = lw[2];
47 plhs[1] = mxCreateDoubleMatrix(1,lw[3],mxREAL);
48 lf.pc.wk = mxGetPr(plhs[1]);
49 lf.pc.lwk = lw[3];
50 lf.fp.kap = mxCalloc(lw[5],sizeof(double));
51 /* should also allocate design here */
52
53 if (!lf_error) startmodule(&lf,&des,NULL,NULL);
54
55 /* now, store the results:
56 plhs[0] stores informtion about fit points and evaluation structure.
57 it is now a matlab structure, not a cell
58 fit_points.evaluation_points - matrix of evaluation points.
59 fit_points.fitted_values - matrix fitted values etc.
60 fit_points.evaluation_vectors - structure of 'integer' vectors {cell,splitvar,lo,hi}
61 fit_points.fit_limits - fit limit (matrix with 2 cols).
62 fit_points.family_link - [familt link] numeric vector.
63 fit_points.kappa - kap vector.
64 */
65 plhs[0] = mxCreateStructMatrix(1,1,6,fpcnames);
66 if ( plhs[0] == NULL ) {
67 printf("Problem with CreateStructMatrix for plhs[0]\n");fflush(stdout);
68 }
69
70 mxSetField(plhs[0],0,"evaluation_points",mxCreateDoubleMatrix(d,lf.fp.nv,mxREAL));
71 memcpy(mxGetPr(mxGetField(plhs[0],0,"evaluation_points")), lf.fp.xev, d*lf.fp.nv*sizeof(double));
72
73 mxSetField(plhs[0],0,"fitted_values",mxCreateDoubleMatrix(lf.fp.nv,lf.mdl.keepv,mxREAL));
74 for (i=0; i<lf.mdl.keepv; i++)
75 memcpy(&mxGetPr(mxGetField(plhs[0],0,"fitted_values"))[i*lf.fp.nv], &lf.fp.coef[i*nvm], lf.fp.nv*sizeof(double));
76 /* another bit to save here? -- split values, kdtree */
77
78 temparray = mxCreateStructMatrix(1,1,4,evsnames);
79 if ( temparray == NULL ) {
80 printf("Problem with CreateStructMatrix for temparray\n");fflush(stdout);
81 }
82 mxSetField(plhs[0],0,"evaluation_vectors",temparray);
83 iwkc = mxGetField(plhs[0],0,"evaluation_vectors");
84 nv = lf.fp.nv;
85 nc = lf.evs.nce;
86 mvc = (nv>nc) ? nv : nc;
87 mxSetField(iwkc,0,"cell",mxCreateDoubleMatrix(vc,nc,mxREAL)); /* ce */
88 mxSetField(iwkc,0,"splitvar",mxCreateDoubleMatrix(1,mvc,mxREAL)); /* s */
89 mxSetField(iwkc,0,"lo",mxCreateDoubleMatrix(1,mvc,mxREAL)); /* lo */
90 mxSetField(iwkc,0,"hi",mxCreateDoubleMatrix(1,mvc,mxREAL)); /* hi */
91 y = mxGetPr(mxGetField(iwkc,0,"cell"));
92 for (i=0; i<vc*nc; i++) y[i] = lf.evs.ce[i];
93 y = mxGetPr(mxGetField(iwkc,0,"splitvar"));
94 for (i=0; i<mvc; i++) y[i] = lf.evs.s[i];
95 y = mxGetPr(mxGetField(iwkc,0,"lo"));
96 for (i=0; i<mvc; i++) y[i] = lf.evs.lo[i];
97 y = mxGetPr(mxGetField(iwkc,0,"hi"));
98 for (i=0; i<mvc; i++) y[i] = lf.evs.hi[i];
99
100 mxSetField(plhs[0],0,"fit_limits",mxCreateDoubleMatrix(d,2,mxREAL));
101 memcpy(mxGetPr(mxGetField(plhs[0],0,"fit_limits")), lf.evs.fl, 2*d*sizeof(double));
102
103 mxSetField(plhs[0],0,"family_link",mxCreateDoubleMatrix(1,2,mxREAL));
104 y = mxGetPr(mxGetField(plhs[0],0,"family_link"));
105 y[0] = fam(&lf.sp);
106 y[1] = link(&lf.sp);
107
108 mxSetField(plhs[0],0,"kappa",mxCreateDoubleMatrix(1,lf.mdl.keepc,mxREAL));
109 memcpy(mxGetPr(mxGetField(plhs[0],0,"kappa")),lf.fp.kap,lf.mdl.keepc*sizeof(double));
110 }