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