1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include <stdlib.h>
#include <string.h>
#include "linear.h"
#include "mex.h"
#ifdef MX_API_VER
#if MX_API_VER < 0x07030000
typedef int mwIndex;
#endif
#endif
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
#define NUM_OF_RETURN_FIELD 7
static const char *field_names[] = {
"Parameters",
"nr_class",
"nr_feature",
"bias",
"Label",
"w",
"rho",
};
const char *model_to_matlab_structure(mxArray *plhs[], struct model *model_)
{
int i;
int nr_w;
double *ptr;
mxArray *return_model, **rhs;
int out_id = 0;
int n, w_size;
rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD);
// Parameters
// for now, only solver_type is needed
rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
ptr = mxGetPr(rhs[out_id]);
ptr[0] = model_->param.solver_type;
out_id++;
// nr_class
rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
ptr = mxGetPr(rhs[out_id]);
ptr[0] = model_->nr_class;
out_id++;
if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS)
nr_w=1;
else
nr_w=model_->nr_class;
// nr_feature
rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
ptr = mxGetPr(rhs[out_id]);
ptr[0] = model_->nr_feature;
out_id++;
// bias
rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
ptr = mxGetPr(rhs[out_id]);
ptr[0] = model_->bias;
out_id++;
if(model_->bias>=0)
n=model_->nr_feature+1;
else
n=model_->nr_feature;
w_size = n;
// Label
if(model_->label)
{
rhs[out_id] = mxCreateDoubleMatrix(model_->nr_class, 1, mxREAL);
ptr = mxGetPr(rhs[out_id]);
for(i = 0; i < model_->nr_class; i++)
ptr[i] = model_->label[i];
}
else
rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
out_id++;
// w
rhs[out_id] = mxCreateDoubleMatrix(nr_w, w_size, mxREAL);
ptr = mxGetPr(rhs[out_id]);
for(i = 0; i < w_size*nr_w; i++)
ptr[i]=model_->w[i];
out_id++;
// rho
rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
ptr = mxGetPr(rhs[out_id]);
ptr[0] = model_->rho;
out_id++;
/* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */
return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names);
/* Fill struct matrix with input arguments */
for(i = 0; i < NUM_OF_RETURN_FIELD; i++)
mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i]));
/* return */
plhs[0] = return_model;
mxFree(rhs);
return NULL;
}
const char *matlab_matrix_to_model(struct model *model_, const mxArray *matlab_struct)
{
int i, num_of_fields;
int nr_w;
double *ptr;
int id = 0;
int n, w_size;
mxArray **rhs;
num_of_fields = mxGetNumberOfFields(matlab_struct);
rhs = (mxArray **) mxMalloc(sizeof(mxArray *)*num_of_fields);
for(i=0;i<num_of_fields;i++)
rhs[i] = mxGetFieldByNumber(matlab_struct, 0, i);
model_->nr_class=0;
nr_w=0;
model_->nr_feature=0;
model_->w=NULL;
model_->label=NULL;
// Parameters
ptr = mxGetPr(rhs[id]);
model_->param.solver_type = (int)ptr[0];
id++;
// nr_class
ptr = mxGetPr(rhs[id]);
model_->nr_class = (int)ptr[0];
id++;
if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS)
nr_w=1;
else
nr_w=model_->nr_class;
// nr_feature
ptr = mxGetPr(rhs[id]);
model_->nr_feature = (int)ptr[0];
id++;
// bias
ptr = mxGetPr(rhs[id]);
model_->bias = ptr[0];
id++;
if(model_->bias>=0)
n=model_->nr_feature+1;
else
n=model_->nr_feature;
w_size = n;
// Label
if(mxIsEmpty(rhs[id]) == 0)
{
model_->label = Malloc(int, model_->nr_class);
ptr = mxGetPr(rhs[id]);
for(i=0;i<model_->nr_class;i++)
model_->label[i] = (int)ptr[i];
}
id++;
// w
ptr = mxGetPr(rhs[id]);
model_->w=Malloc(double, w_size*nr_w);
for(i = 0; i < w_size*nr_w; i++)
model_->w[i]=ptr[i];
id++;
// rho
ptr = mxGetPr(rhs[id]);
model_->rho = ptr[0];
id++;
mxFree(rhs);
return NULL;
}
|