annotate extra/NaN/src/linear_model_matlab.c @ 6585:ae521dec5b54 octave-forge

partial support of lcc-win compiler
author schloegl
date Thu, 14 Jan 2010 17:50:16 +0000
parents 1170c9d6d1c4
children c101c486d80a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
1 /*
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
2
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
3 $Id$
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
4 Copyright (c) 2007-2009 The LIBLINEAR Project.
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
5 Copyright (c) 2010 Alois Schloegl <a.schloegl@ieee.org>
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
6 This function is part of the NaN-toolbox
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
7 http://hci.tugraz.at/~schloegl/matlab/NaN/
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
8
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
9 This code was extracted from liblinear-1.51 in Jan 2010 and
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
10 modified for the use with Octave
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
11
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
12 This program is free software; you can redistribute it and/or modify
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
13 it under the terms of the GNU General Public License as published by
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
14 the Free Software Foundation; either version 3 of the License, or
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
15 (at your option) any later version.
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
16
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
17 This program is distributed in the hope that it will be useful,
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
20 GNU General Public License for more details.
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
21
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
22 You should have received a copy of the GNU General Public License
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
23 along with this program; if not, see <http://www.gnu.org/licenses/>.
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
24
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
25 */
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
26
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
27
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
28 #include <stdlib.h>
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
29 #include <string.h>
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
30 #include "linear.h"
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
31
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
32 #include "mex.h"
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
33
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
34 #ifdef tmwtypes_h
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
35 #if (MX_API_VER<=0x07020000)
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
36 typedef int mwSize;
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
37 #endif
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
38 #endif
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
39
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
40 #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
41
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
42 #define NUM_OF_RETURN_FIELD 6
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
43
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
44 static const char *field_names[] = {
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
45 "Parameters",
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
46 "nr_class",
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
47 "nr_feature",
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
48 "bias",
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
49 "Label",
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
50 "w",
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
51 };
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
52
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
53 const char *model_to_matlab_structure(mxArray *plhs[], struct model *model_)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
54 {
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
55 int i;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
56 int nr_w;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
57 double *ptr;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
58 mxArray *return_model, **rhs;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
59 int out_id = 0;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
60 int n, w_size;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
61
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
62 rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
63
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
64 /* Parameters */
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
65 /* for now, only solver_type is needed */
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
66 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
67 ptr = mxGetPr(rhs[out_id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
68 ptr[0] = model_->param.solver_type;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
69 out_id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
70
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
71 /* nr_class */
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
72 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
73 ptr = mxGetPr(rhs[out_id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
74 ptr[0] = model_->nr_class;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
75 out_id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
76
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
77 if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
78 nr_w=1;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
79 else
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
80 nr_w=model_->nr_class;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
81
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
82 /* nr_feature */
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
83 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
84 ptr = mxGetPr(rhs[out_id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
85 ptr[0] = model_->nr_feature;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
86 out_id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
87
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
88 /* bias */
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
89 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
90 ptr = mxGetPr(rhs[out_id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
91 ptr[0] = model_->bias;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
92 out_id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
93
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
94 if(model_->bias>=0)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
95 n=model_->nr_feature+1;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
96 else
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
97 n=model_->nr_feature;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
98
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
99 w_size = n;
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
100 /* Label */
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
101 if(model_->label)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
102 {
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
103 rhs[out_id] = mxCreateDoubleMatrix(model_->nr_class, 1, mxREAL);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
104 ptr = mxGetPr(rhs[out_id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
105 for(i = 0; i < model_->nr_class; i++)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
106 ptr[i] = model_->label[i];
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
107 }
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
108 else
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
109 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
110 out_id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
111
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
112 /* w */
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
113 rhs[out_id] = mxCreateDoubleMatrix(nr_w, w_size, mxREAL);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
114 ptr = mxGetPr(rhs[out_id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
115 for(i = 0; i < w_size*nr_w; i++)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
116 ptr[i]=model_->w[i];
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
117 out_id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
118
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
119 /* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
120 return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
121
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
122 /* Fill struct matrix with input arguments */
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
123 for(i = 0; i < NUM_OF_RETURN_FIELD; i++)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
124 mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i]));
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
125 /* return */
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
126 plhs[0] = return_model;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
127 mxFree(rhs);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
128
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
129 return NULL;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
130 }
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
131
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
132 const char *matlab_matrix_to_model(struct model *model_, const mxArray *matlab_struct)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
133 {
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
134 int i, num_of_fields;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
135 int nr_w;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
136 double *ptr;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
137 int id = 0;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
138 int n, w_size;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
139 mxArray **rhs;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
140
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
141 num_of_fields = mxGetNumberOfFields(matlab_struct);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
142 rhs = (mxArray **) mxMalloc(sizeof(mxArray *)*num_of_fields);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
143
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
144 for(i=0;i<num_of_fields;i++)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
145 rhs[i] = mxGetFieldByNumber(matlab_struct, 0, i);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
146
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
147 model_->nr_class=0;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
148 nr_w=0;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
149 model_->nr_feature=0;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
150 model_->w=NULL;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
151 model_->label=NULL;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
152
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
153 /* Parameters */
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
154 ptr = mxGetPr(rhs[id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
155 model_->param.solver_type = (int)ptr[0];
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
156 id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
157
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
158 /* nr_class */
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
159 ptr = mxGetPr(rhs[id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
160 model_->nr_class = (int)ptr[0];
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
161 id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
162
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
163 if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
164 nr_w=1;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
165 else
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
166 nr_w=model_->nr_class;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
167
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
168 /* nr_feature */
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
169 ptr = mxGetPr(rhs[id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
170 model_->nr_feature = (int)ptr[0];
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
171 id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
172
6585
ae521dec5b54 partial support of lcc-win compiler
schloegl
parents: 6560
diff changeset
173 /* bias */
6560
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
174 ptr = mxGetPr(rhs[id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
175 model_->bias = (int)ptr[0];
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
176 id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
177
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
178 if(model_->bias>=0)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
179 n=model_->nr_feature+1;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
180 else
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
181 n=model_->nr_feature;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
182 w_size = n;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
183
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
184 ptr = mxGetPr(rhs[id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
185 model_->label=Malloc(int, model_->nr_class);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
186 for(i=0; i<model_->nr_class; i++)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
187 model_->label[i]=(int)ptr[i];
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
188 id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
189
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
190 ptr = mxGetPr(rhs[id]);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
191 model_->w=Malloc(double, w_size*nr_w);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
192 for(i = 0; i < w_size*nr_w; i++)
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
193 model_->w[i]=ptr[i];
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
194 id++;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
195 mxFree(rhs);
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
196
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
197 return NULL;
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
198 }
1170c9d6d1c4 method LibLinear is included
schloegl
parents:
diff changeset
199