6560
|
1 /* |
|
2 |
|
3 $Id$ |
|
4 Copyright (c) 2007-2009 The LIBLINEAR Project. |
|
5 Copyright (c) 2010 Alois Schloegl <a.schloegl@ieee.org> |
|
6 This function is part of the NaN-toolbox |
|
7 http://hci.tugraz.at/~schloegl/matlab/NaN/ |
|
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 |
|
53 const char *model_to_matlab_structure(mxArray *plhs[], struct model *model_) |
|
54 { |
|
55 int i; |
|
56 int nr_w; |
|
57 double *ptr; |
|
58 mxArray *return_model, **rhs; |
|
59 int out_id = 0; |
|
60 int n, w_size; |
|
61 |
|
62 rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD); |
|
63 |
6585
|
64 /* Parameters */ |
|
65 /* for now, only solver_type is needed */ |
6560
|
66 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); |
|
67 ptr = mxGetPr(rhs[out_id]); |
|
68 ptr[0] = model_->param.solver_type; |
|
69 out_id++; |
|
70 |
6585
|
71 /* nr_class */ |
6560
|
72 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); |
|
73 ptr = mxGetPr(rhs[out_id]); |
|
74 ptr[0] = model_->nr_class; |
|
75 out_id++; |
|
76 |
|
77 if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS) |
|
78 nr_w=1; |
|
79 else |
|
80 nr_w=model_->nr_class; |
|
81 |
6585
|
82 /* nr_feature */ |
6560
|
83 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); |
|
84 ptr = mxGetPr(rhs[out_id]); |
|
85 ptr[0] = model_->nr_feature; |
|
86 out_id++; |
|
87 |
6585
|
88 /* bias */ |
6560
|
89 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); |
|
90 ptr = mxGetPr(rhs[out_id]); |
|
91 ptr[0] = model_->bias; |
|
92 out_id++; |
|
93 |
|
94 if(model_->bias>=0) |
|
95 n=model_->nr_feature+1; |
|
96 else |
|
97 n=model_->nr_feature; |
|
98 |
|
99 w_size = n; |
6585
|
100 /* Label */ |
6560
|
101 if(model_->label) |
|
102 { |
|
103 rhs[out_id] = mxCreateDoubleMatrix(model_->nr_class, 1, mxREAL); |
|
104 ptr = mxGetPr(rhs[out_id]); |
|
105 for(i = 0; i < model_->nr_class; i++) |
|
106 ptr[i] = model_->label[i]; |
|
107 } |
|
108 else |
|
109 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); |
|
110 out_id++; |
|
111 |
6585
|
112 /* w */ |
6560
|
113 rhs[out_id] = mxCreateDoubleMatrix(nr_w, w_size, mxREAL); |
|
114 ptr = mxGetPr(rhs[out_id]); |
|
115 for(i = 0; i < w_size*nr_w; i++) |
|
116 ptr[i]=model_->w[i]; |
|
117 out_id++; |
|
118 |
|
119 /* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */ |
|
120 return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names); |
|
121 |
|
122 /* Fill struct matrix with input arguments */ |
|
123 for(i = 0; i < NUM_OF_RETURN_FIELD; i++) |
|
124 mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i])); |
|
125 /* return */ |
|
126 plhs[0] = return_model; |
|
127 mxFree(rhs); |
|
128 |
|
129 return NULL; |
|
130 } |
|
131 |
|
132 const char *matlab_matrix_to_model(struct model *model_, const mxArray *matlab_struct) |
|
133 { |
|
134 int i, num_of_fields; |
|
135 int nr_w; |
|
136 double *ptr; |
|
137 int id = 0; |
|
138 int n, w_size; |
|
139 mxArray **rhs; |
|
140 |
|
141 num_of_fields = mxGetNumberOfFields(matlab_struct); |
|
142 rhs = (mxArray **) mxMalloc(sizeof(mxArray *)*num_of_fields); |
|
143 |
|
144 for(i=0;i<num_of_fields;i++) |
|
145 rhs[i] = mxGetFieldByNumber(matlab_struct, 0, i); |
|
146 |
|
147 model_->nr_class=0; |
|
148 nr_w=0; |
|
149 model_->nr_feature=0; |
|
150 model_->w=NULL; |
|
151 model_->label=NULL; |
|
152 |
6585
|
153 /* Parameters */ |
6560
|
154 ptr = mxGetPr(rhs[id]); |
|
155 model_->param.solver_type = (int)ptr[0]; |
|
156 id++; |
|
157 |
6585
|
158 /* nr_class */ |
6560
|
159 ptr = mxGetPr(rhs[id]); |
|
160 model_->nr_class = (int)ptr[0]; |
|
161 id++; |
|
162 |
|
163 if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS) |
|
164 nr_w=1; |
|
165 else |
|
166 nr_w=model_->nr_class; |
|
167 |
6585
|
168 /* nr_feature */ |
6560
|
169 ptr = mxGetPr(rhs[id]); |
|
170 model_->nr_feature = (int)ptr[0]; |
|
171 id++; |
|
172 |
6585
|
173 /* bias */ |
6560
|
174 ptr = mxGetPr(rhs[id]); |
|
175 model_->bias = (int)ptr[0]; |
|
176 id++; |
|
177 |
|
178 if(model_->bias>=0) |
|
179 n=model_->nr_feature+1; |
|
180 else |
|
181 n=model_->nr_feature; |
|
182 w_size = n; |
|
183 |
|
184 ptr = mxGetPr(rhs[id]); |
|
185 model_->label=Malloc(int, model_->nr_class); |
|
186 for(i=0; i<model_->nr_class; i++) |
|
187 model_->label[i]=(int)ptr[i]; |
|
188 id++; |
|
189 |
|
190 ptr = mxGetPr(rhs[id]); |
|
191 model_->w=Malloc(double, w_size*nr_w); |
|
192 for(i = 0; i < w_size*nr_w; i++) |
|
193 model_->w[i]=ptr[i]; |
|
194 id++; |
|
195 mxFree(rhs); |
|
196 |
|
197 return NULL; |
|
198 } |
|
199 |