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