gohanlonllc-libsvm-ruby 2.8.4

Sign up to get free protection for your applications and to get access to all the features.
data/ext/install-sh ADDED
@@ -0,0 +1,325 @@
1
+ #!/bin/sh
2
+ # install - install a program, script, or datafile
3
+
4
+ scriptversion=2004-04-01.17
5
+
6
+ # This originates from X11R5 (mit/util/scripts/install.sh), which was
7
+ # later released in X11R6 (xc/config/util/install.sh) with the
8
+ # following copyright and license.
9
+ #
10
+ # Copyright (C) 1994 X Consortium
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ # of this software and associated documentation files (the "Software"), to
14
+ # deal in the Software without restriction, including without limitation the
15
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16
+ # sell copies of the Software, and to permit persons to whom the Software is
17
+ # furnished to do so, subject to the following conditions:
18
+ #
19
+ # The above copyright notice and this permission notice shall be included in
20
+ # all copies or substantial portions of the Software.
21
+ #
22
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26
+ # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27
+ # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
+ #
29
+ # Except as contained in this notice, the name of the X Consortium shall not
30
+ # be used in advertising or otherwise to promote the sale, use or other deal-
31
+ # ings in this Software without prior written authorization from the X Consor-
32
+ # tium.
33
+ #
34
+ #
35
+ # FSF changes to this file are in the public domain.
36
+ #
37
+ # Calling this script install-sh is preferred over install.sh, to prevent
38
+ # `make' implicit rules from creating a file called install from it
39
+ # when there is no Makefile.
40
+ #
41
+ # This script is compatible with the BSD install script, but was written
42
+ # from scratch. It can only install one file at a time, a restriction
43
+ # shared with many OS's install programs.
44
+
45
+ # set DOITPROG to echo to test this script
46
+
47
+ # Don't use :- since 4.3BSD and earlier shells don't like it.
48
+ doit="${DOITPROG-}"
49
+
50
+ # put in absolute paths if you don't have them in your path; or use env. vars.
51
+
52
+ mvprog="${MVPROG-mv}"
53
+ cpprog="${CPPROG-cp}"
54
+ chmodprog="${CHMODPROG-chmod}"
55
+ chownprog="${CHOWNPROG-chown}"
56
+ chgrpprog="${CHGRPPROG-chgrp}"
57
+ stripprog="${STRIPPROG-strip}"
58
+ rmprog="${RMPROG-rm}"
59
+ mkdirprog="${MKDIRPROG-mkdir}"
60
+
61
+ transformbasename=
62
+ transform_arg=
63
+ instcmd="$mvprog"
64
+ chmodcmd="$chmodprog 0755"
65
+ chowncmd=
66
+ chgrpcmd=
67
+ stripcmd=
68
+ rmcmd="$rmprog -f"
69
+ mvcmd="$mvprog"
70
+ src=
71
+ dst=
72
+ dir_arg=
73
+
74
+ usage="Usage: $0 [OPTION]... SRCFILE DSTFILE
75
+ or: $0 [OPTION]... SRCFILES... DIRECTORY
76
+ or: $0 -d DIRECTORIES...
77
+
78
+ In the first form, install SRCFILE to DSTFILE, removing SRCFILE by default.
79
+ In the second, create the directory path DIR.
80
+
81
+ Options:
82
+ -b=TRANSFORMBASENAME
83
+ -c copy source (using $cpprog) instead of moving (using $mvprog).
84
+ -d create directories instead of installing files.
85
+ -g GROUP $chgrp installed files to GROUP.
86
+ -m MODE $chmod installed files to MODE.
87
+ -o USER $chown installed files to USER.
88
+ -s strip installed files (using $stripprog).
89
+ -t=TRANSFORM
90
+ --help display this help and exit.
91
+ --version display version info and exit.
92
+
93
+ Environment variables override the default commands:
94
+ CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
95
+ "
96
+
97
+ while test -n "$1"; do
98
+ case $1 in
99
+ -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
100
+ shift
101
+ continue;;
102
+
103
+ -c) instcmd=$cpprog
104
+ shift
105
+ continue;;
106
+
107
+ -d) dir_arg=true
108
+ shift
109
+ continue;;
110
+
111
+ -g) chgrpcmd="$chgrpprog $2"
112
+ shift
113
+ shift
114
+ continue;;
115
+
116
+ --help) echo "$usage"; exit 0;;
117
+
118
+ -m) chmodcmd="$chmodprog $2"
119
+ shift
120
+ shift
121
+ continue;;
122
+
123
+ -o) chowncmd="$chownprog $2"
124
+ shift
125
+ shift
126
+ continue;;
127
+
128
+ -s) stripcmd=$stripprog
129
+ shift
130
+ continue;;
131
+
132
+ -t=*) transformarg=`echo $1 | sed 's/-t=//'`
133
+ shift
134
+ continue;;
135
+
136
+ --version) echo "$0 $scriptversion"; exit 0;;
137
+
138
+ *) # When -d is used, all remaining arguments are directories to create.
139
+ test -n "$dir_arg" && break
140
+ # Otherwise, the last argument is the destination. Remove it from $@.
141
+ for arg
142
+ do
143
+ if test -n "$dstarg"; then
144
+ # $@ is not empty: it contains at least $arg.
145
+ set fnord "$@" "$dstarg"
146
+ shift # fnord
147
+ fi
148
+ shift # arg
149
+ dstarg=$arg
150
+ done
151
+ break;;
152
+ esac
153
+ done
154
+
155
+ if test -z "$1"; then
156
+ if test -z "$dir_arg"; then
157
+ echo "$0: no input file specified." >&2
158
+ exit 1
159
+ fi
160
+ # It's OK to call `install-sh -d' without argument.
161
+ # This can happen when creating conditional directories.
162
+ exit 0
163
+ fi
164
+
165
+ for src
166
+ do
167
+ # Protect names starting with `-'.
168
+ case $src in
169
+ -*) src=./$src ;;
170
+ esac
171
+
172
+ if test -n "$dir_arg"; then
173
+ dst=$src
174
+ src=
175
+
176
+ if test -d "$dst"; then
177
+ instcmd=:
178
+ chmodcmd=
179
+ else
180
+ instcmd=$mkdirprog
181
+ fi
182
+ else
183
+ # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
184
+ # might cause directories to be created, which would be especially bad
185
+ # if $src (and thus $dsttmp) contains '*'.
186
+ if test ! -f "$src" && test ! -d "$src"; then
187
+ echo "$0: $src does not exist." >&2
188
+ exit 1
189
+ fi
190
+
191
+ if test -z "$dstarg"; then
192
+ echo "$0: no destination specified." >&2
193
+ exit 1
194
+ fi
195
+
196
+ dst=$dstarg
197
+ # Protect names starting with `-'.
198
+ case $dst in
199
+ -*) dst=./$dst ;;
200
+ esac
201
+
202
+ # If destination is a directory, append the input filename; won't work
203
+ # if double slashes aren't ignored.
204
+ if test -d "$dst"; then
205
+ dst=$dst/`basename "$src"`
206
+ fi
207
+ fi
208
+
209
+ # This sed command emulates the dirname command.
210
+ dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
211
+
212
+ # Make sure that the destination directory exists.
213
+
214
+ # Skip lots of stat calls in the usual case.
215
+ if test ! -d "$dstdir"; then
216
+ defaultIFS='
217
+ '
218
+ IFS="${IFS-$defaultIFS}"
219
+
220
+ oIFS=$IFS
221
+ # Some sh's can't handle IFS=/ for some reason.
222
+ IFS='%'
223
+ set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
224
+ IFS=$oIFS
225
+
226
+ pathcomp=
227
+
228
+ while test $# -ne 0 ; do
229
+ pathcomp=$pathcomp$1
230
+ shift
231
+ if test ! -d "$pathcomp"; then
232
+ $mkdirprog "$pathcomp" || lasterr=$?
233
+ # mkdir can fail with a `File exist' error in case several
234
+ # install-sh are creating the directory concurrently. This
235
+ # is OK.
236
+ test ! -d "$pathcomp" && { (exit ${lasterr-1}); exit; }
237
+ fi
238
+ pathcomp=$pathcomp/
239
+ done
240
+ fi
241
+
242
+ if test -n "$dir_arg"; then
243
+ $doit $instcmd "$dst" \
244
+ && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
245
+ && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
246
+ && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
247
+ && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
248
+
249
+ else
250
+ # If we're going to rename the final executable, determine the name now.
251
+ if test -z "$transformarg"; then
252
+ dstfile=`basename "$dst"`
253
+ else
254
+ dstfile=`basename "$dst" $transformbasename \
255
+ | sed $transformarg`$transformbasename
256
+ fi
257
+
258
+ # don't allow the sed command to completely eliminate the filename.
259
+ test -z "$dstfile" && dstfile=`basename "$dst"`
260
+
261
+ # Make a couple of temp file names in the proper directory.
262
+ dsttmp=$dstdir/_inst.$$_
263
+ rmtmp=$dstdir/_rm.$$_
264
+
265
+ # Trap to clean up those temp files at exit.
266
+ trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
267
+ trap '(exit $?); exit' 1 2 13 15
268
+
269
+ # Move or copy the file name to the temp name
270
+ $doit $instcmd "$src" "$dsttmp" &&
271
+
272
+ # and set any options; do chmod last to preserve setuid bits.
273
+ #
274
+ # If any of these fail, we abort the whole thing. If we want to
275
+ # ignore errors from any of these, just make sure not to ignore
276
+ # errors from the above "$doit $instcmd $src $dsttmp" command.
277
+ #
278
+ { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
279
+ && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
280
+ && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
281
+ && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
282
+
283
+ # Now rename the file to the real destination.
284
+ { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
285
+ || {
286
+ # The rename failed, perhaps because mv can't rename something else
287
+ # to itself, or perhaps because mv is so ancient that it does not
288
+ # support -f.
289
+
290
+ # Now remove or move aside any old file at destination location.
291
+ # We try this two ways since rm can't unlink itself on some
292
+ # systems and the destination file might be busy for other
293
+ # reasons. In this case, the final cleanup might fail but the new
294
+ # file should still install successfully.
295
+ {
296
+ if test -f "$dstdir/$dstfile"; then
297
+ $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
298
+ || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
299
+ || {
300
+ echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
301
+ (exit 1); exit
302
+ }
303
+ else
304
+ :
305
+ fi
306
+ } &&
307
+
308
+ # Now rename the file to the real destination.
309
+ $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
310
+ }
311
+ }
312
+ fi || { (exit 1); exit; }
313
+ done
314
+
315
+ # The final little trick to "correctly" pass the exit status to the exit trap.
316
+ {
317
+ (exit 0); exit
318
+ }
319
+
320
+ # Local variables:
321
+ # eval: (add-hook 'write-file-hooks 'time-stamp)
322
+ # time-stamp-start: "scriptversion="
323
+ # time-stamp-format: "%:y-%02m-%02d.%02H"
324
+ # time-stamp-end: "$"
325
+ # End:
data/ext/main.cpp ADDED
@@ -0,0 +1,685 @@
1
+ /* RubySVM 1.0 by Rudi Cilibrasi (cilibrar@ofb.net)
2
+ * Released under the GPL
3
+ * Mon May 12 11:20:48 CEST 2003,
4
+ * based on libsvm-2.4
5
+ */
6
+
7
+ #define obstack_chunk_alloc xmalloc
8
+ #define obstack_chunk_free free
9
+
10
+ #define HAVE_DEFINE_ALLOC_FUNCTION 1
11
+
12
+ #include "ruby.h"
13
+ #include "node.h"
14
+ #include <string.h>
15
+ #include <obstack.h>
16
+ #include <stdio.h>
17
+ #include <malloc.h>
18
+ #include <libsvm/svm.h>
19
+ #include <stdlib.h>
20
+
21
+ VALUE mSVM, cSVMProblem, cSVMParameter, cSVMModel;
22
+ static VALUE cMarshal;
23
+
24
+ static int getSVCount(struct svm_model *m);
25
+
26
+ struct RSVM_Problem {
27
+ struct svm_problem prob;
28
+ struct obstack xs, ys;
29
+ int k;
30
+ };
31
+
32
+ struct RSVM_Model {
33
+ struct svm_model *m;
34
+ };
35
+
36
+ struct RSVM_Parameter {
37
+ struct svm_parameter p;
38
+ };
39
+
40
+ VALUE svmpa_new(VALUE cl);
41
+
42
+ /*
43
+ * Converts a Ruby array of consecutive values into a list of
44
+ * value-index svm_node's.
45
+ */
46
+ struct svm_node *rubyArrayToNodelist(VALUE xs)
47
+ {
48
+ //struct obstack xso;
49
+ struct svm_node *n;
50
+ int i;
51
+ int len = RARRAY(xs)->len;
52
+ n = (struct svm_node *) calloc(sizeof(struct svm_node), len+1);
53
+ for (i = 0; i < len; ++i) {
54
+ n[i].value = NUM2DBL(rb_ary_entry(xs, i));
55
+ n[i].index = i;
56
+ }
57
+ n[i].value = 0;
58
+ n[i].index = -1;
59
+
60
+ return n;
61
+ }
62
+
63
+ /*
64
+ * Serializes an SVMParameter object
65
+ */
66
+ VALUE svmpa_svm_dump(VALUE self, VALUE limit)
67
+ {
68
+ struct RSVM_Parameter *rp;
69
+ VALUE obj = rb_ary_new();
70
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
71
+ rb_ary_push(obj, INT2NUM(rp->p.svm_type));
72
+ rb_ary_push(obj, INT2NUM(rp->p.kernel_type));
73
+ rb_ary_push(obj, rb_float_new(rp->p.degree));
74
+ rb_ary_push(obj, rb_float_new(rp->p.gamma));
75
+ rb_ary_push(obj, rb_float_new(rp->p.coef0));
76
+ rb_ary_push(obj, rb_float_new(rp->p.cache_size));
77
+ rb_ary_push(obj, rb_float_new(rp->p.eps));
78
+ rb_ary_push(obj, rb_float_new(rp->p.C));
79
+ rb_ary_push(obj, rb_float_new(rp->p.nu));
80
+ rb_ary_push(obj, rb_float_new(rp->p.p));
81
+ rb_ary_push(obj, INT2NUM(rp->p.shrinking));
82
+ return rb_funcall(cMarshal, rb_intern("dump"), 1, obj);
83
+ }
84
+
85
+ /*
86
+ * Deserializes an SVMParameter object
87
+ */
88
+
89
+ VALUE svmpa_svm_load(VALUE kl, VALUE obj)
90
+ {
91
+ struct RSVM_Parameter *rp;
92
+ printf("In load!\n");
93
+ VALUE self = svmpa_new(cSVMParameter);
94
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
95
+ printf("RP is %p\n", rp);
96
+ obj = rb_funcall(cMarshal, rb_intern("load"), 1, obj);
97
+ rp->p.svm_type = NUM2INT(rb_ary_entry(obj, 0));
98
+ printf("first!\n");
99
+ rp->p.kernel_type = NUM2INT(rb_ary_entry(obj, 1));
100
+ rp->p.degree = (int) NUM2DBL(rb_ary_entry(obj, 2));
101
+ rp->p.gamma = NUM2DBL(rb_ary_entry(obj, 3));
102
+ rp->p.coef0 = NUM2DBL(rb_ary_entry(obj, 4));
103
+ rp->p.cache_size = NUM2DBL(rb_ary_entry(obj, 5));
104
+ printf("midway!\n");
105
+ rp->p.eps = NUM2DBL(rb_ary_entry(obj, 6));
106
+ rp->p.C = NUM2DBL(rb_ary_entry(obj, 7));
107
+ rp->p.nu = NUM2DBL(rb_ary_entry(obj, 8));
108
+ rp->p.p = NUM2DBL(rb_ary_entry(obj, 9));
109
+ rp->p.shrinking = NUM2INT(rb_ary_entry(obj, 10));
110
+ printf("Never returned!\n");
111
+ return self;
112
+ }
113
+
114
+ /*
115
+ * Gets gamma value, the exponent used in the kernel function
116
+ */
117
+ VALUE svmpa_gamma(VALUE self) {
118
+ struct RSVM_Parameter *rp;
119
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
120
+ return rb_float_new(rp->p.gamma);
121
+ }
122
+
123
+ /*
124
+ * Sets gamma value, the exponent used in the kernel function
125
+ */
126
+ VALUE svmpa_gammaeq(VALUE self, VALUE eq) {
127
+ struct RSVM_Parameter *rp;
128
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
129
+ rp->p.gamma = NUM2DBL(eq);
130
+ return Qnil;
131
+ }
132
+ /*
133
+ * Gets coef0, the constant added in the polynomial kernel
134
+ */
135
+ VALUE svmpa_coef0(VALUE self) {
136
+ struct RSVM_Parameter *rp;
137
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
138
+ return rb_float_new(rp->p.coef0);
139
+ }
140
+
141
+ /*
142
+ * Sets coef0, the constant added in the polynomial kernel
143
+ */
144
+ VALUE svmpa_coef0eq(VALUE self, VALUE eq) {
145
+ struct RSVM_Parameter *rp;
146
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
147
+ rp->p.coef0 = NUM2DBL(eq);
148
+ return Qnil;
149
+ }
150
+
151
+ /*
152
+ * Gets cachesize, the number of megabytes of memory to use for the cache
153
+ */
154
+ VALUE svmpa_cache_size(VALUE self) {
155
+ struct RSVM_Parameter *rp;
156
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
157
+ return rb_float_new(rp->p.cache_size);
158
+ }
159
+
160
+ /*
161
+ * Sets cachesize, the number of megabytes of memory to use for the cache
162
+ */
163
+ VALUE svmpa_cache_sizeeq(VALUE self, VALUE eq) {
164
+ struct RSVM_Parameter *rp;
165
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
166
+ rp->p.cache_size = NUM2DBL(eq);
167
+ return Qnil;
168
+ }
169
+ /*
170
+ * Gets eps, the tolerance of termination criterion
171
+ */
172
+ VALUE svmpa_eps(VALUE self) {
173
+ struct RSVM_Parameter *rp;
174
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
175
+ return rb_float_new(rp->p.eps);
176
+ }
177
+
178
+ /*
179
+ * Sets eps, the tolerance of termination criterion
180
+ */
181
+ VALUE svmpa_epseq(VALUE self, VALUE eq) {
182
+ struct RSVM_Parameter *rp;
183
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
184
+ rp->p.eps = NUM2DBL(eq);
185
+ return Qnil;
186
+ }
187
+ /*
188
+ * Gets C, the cost parameter of C-SVC, epsilon-SVR, and nu-SVR
189
+ */
190
+ VALUE svmpa_C(VALUE self) {
191
+ struct RSVM_Parameter *rp;
192
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
193
+ return rb_float_new(rp->p.C);
194
+ }
195
+
196
+ /*
197
+ * Sets C, the cost parameter of C-SVC, epsilon-SVR, and nu-SVR
198
+ */
199
+ VALUE svmpa_Ceq(VALUE self, VALUE eq) {
200
+ struct RSVM_Parameter *rp;
201
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
202
+ rp->p.C = NUM2DBL(eq);
203
+ return Qnil;
204
+ }
205
+
206
+ /*
207
+ * Gets nu, the SV-ratio parameter of nu-SVC, one-class SVM, and nu-SVR
208
+ */
209
+ VALUE svmpa_nu(VALUE self) {
210
+ struct RSVM_Parameter *rp;
211
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
212
+ return rb_float_new(rp->p.nu);
213
+ }
214
+
215
+ /*
216
+ * Sets nu, the SV-ratio parameter of nu-SVC, one-class SVM, and nu-SVR
217
+ */
218
+ VALUE svmpa_nueq(VALUE self, VALUE eq) {
219
+ struct RSVM_Parameter *rp;
220
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
221
+ rp->p.nu = NUM2DBL(eq);
222
+ return Qnil;
223
+ }
224
+
225
+ /*
226
+ * Gets p, the zero-loss width zone in epsilon-insensitive SVR
227
+ */
228
+ VALUE svmpa_p(VALUE self) {
229
+ struct RSVM_Parameter *rp;
230
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
231
+ return rb_float_new(rp->p.p);
232
+ }
233
+
234
+ /*
235
+ * Sets p, the zero-loss width zone in epsilon-insensitive SVR
236
+ */
237
+ VALUE svmpa_peq(VALUE self, VALUE eq) {
238
+ struct RSVM_Parameter *rp;
239
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
240
+ rp->p.p = NUM2DBL(eq);
241
+ return Qnil;
242
+ }
243
+
244
+ /*
245
+ * Gets degree, the degree of the kernel function
246
+ */
247
+ VALUE svmpa_degree(VALUE self) {
248
+ struct RSVM_Parameter *rp;
249
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
250
+ return rb_float_new(rp->p.degree);
251
+ }
252
+
253
+ /*
254
+ * Sets degree, the degree of the kernel function
255
+ */
256
+ VALUE svmpa_degreeeq(VALUE self, VALUE eq) {
257
+ struct RSVM_Parameter *rp;
258
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
259
+ rp->p.degree = (int) NUM2DBL(eq);
260
+ return Qnil;
261
+ }
262
+
263
+ /*
264
+ * Gets kernel_type, which is one of:
265
+ * * LINEAR
266
+ * * POLY
267
+ * * RBF
268
+ * * SIGMOID
269
+ */
270
+ VALUE svmpa_kernel_type(VALUE self) {
271
+ struct RSVM_Parameter *rp;
272
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
273
+ return INT2FIX(rp->p.kernel_type);
274
+ }
275
+
276
+ /*
277
+ * Sets kernel_type, which is one of:
278
+ * * LINEAR
279
+ * * POLY
280
+ * * RBF
281
+ * * SIGMOID
282
+ */
283
+ VALUE svmpa_kernel_typeeq(VALUE self, VALUE eq) {
284
+ struct RSVM_Parameter *rp;
285
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
286
+ rp->p.kernel_type = FIX2INT(eq);
287
+ return Qnil;
288
+ }
289
+
290
+ /*
291
+ * Gets svm_type, which is one of:
292
+ * * C_SVC
293
+ * * NU_SVC
294
+ * * ONE_CLASS
295
+ * * EPSILON_SVR
296
+ * * NU_SVR
297
+ */
298
+ VALUE svmpa_svm_type(VALUE self) {
299
+ struct RSVM_Parameter *rp;
300
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
301
+ return INT2FIX(rp->p.svm_type);
302
+ }
303
+
304
+ /*
305
+ * Sets svm_type, which is one of:
306
+ * * C_SVC
307
+ * * NU_SVC
308
+ * * ONE_CLASS
309
+ * * EPSILON_SVR
310
+ * * NU_SVR
311
+ */
312
+ VALUE svmpa_svm_typeeq(VALUE self, VALUE eq) {
313
+ struct RSVM_Parameter *rp;
314
+ Data_Get_Struct(self, struct RSVM_Parameter, rp);
315
+ rp->p.svm_type = FIX2INT(eq);
316
+ return Qnil;
317
+ }
318
+
319
+ struct RSVM_Parameter *newParameter()
320
+ {
321
+ struct RSVM_Parameter *rp = (struct RSVM_Parameter *)
322
+ calloc(sizeof(struct RSVM_Parameter), 1);
323
+ rp->p.svm_type = C_SVC;
324
+ rp->p.kernel_type = RBF;
325
+ rp->p.degree = 3;
326
+ rp->p.gamma = 0;
327
+ rp->p.coef0 = 0;
328
+ rp->p.nu = 0.5;
329
+ rp->p.cache_size = 40;
330
+ rp->p.C = 1;
331
+ rp->p.eps = 1e-3;
332
+ rp->p.p = 0.1;
333
+ rp->p.shrinking = 1;
334
+ rp->p.nr_weight = 0;
335
+ return rp;
336
+ }
337
+
338
+ /*
339
+ * Creates a new, empty SVMProblem object.
340
+ */
341
+ struct RSVM_Problem *newProblem()
342
+ {
343
+ struct RSVM_Problem *rprob = (struct RSVM_Problem *) calloc(sizeof(struct RSVM_Problem), 1);
344
+ rprob->prob.l = 0;
345
+ rprob->prob.x = NULL;
346
+ rprob->prob.y = NULL;
347
+ obstack_init(&rprob->xs);
348
+ obstack_init(&rprob->ys);
349
+ return rprob;
350
+ }
351
+
352
+ /*
353
+ * Adds an example to an SVMProblem given a target value and an input vector.
354
+ */
355
+ void addExample(struct RSVM_Problem *rp, double y, struct svm_node *x)
356
+ {
357
+ obstack_grow(&rp->ys, &y, sizeof(double));
358
+ obstack_grow(&rp->xs, &x, sizeof(struct svm_node *));
359
+ }
360
+
361
+ void syncProblem(struct RSVM_Problem *rp)
362
+ {
363
+ rp->prob.l = obstack_object_size(&rp->ys) / sizeof(double);
364
+ rp->prob.y = (double *) obstack_base(&rp->ys);
365
+ rp->prob.x = (struct svm_node **) obstack_base(&rp->xs);
366
+ }
367
+
368
+ /*
369
+ * Frees an SVMModel
370
+ */
371
+ static void svmpm_free(void *ptr)
372
+ {
373
+ struct RSVM_Model *rp = (struct RSVM_Model *) ptr;
374
+ svm_destroy_model(rp->m);
375
+ free(rp);
376
+ }
377
+
378
+ /*
379
+ * Frees an SVMParameter
380
+ */
381
+ static void svmpa_free(void *ptr)
382
+ {
383
+ struct RSVM_Parameter *rp = (struct RSVM_Parameter *) ptr;
384
+ free(rp);
385
+ }
386
+
387
+ /*
388
+ * Frees an SVMProblem
389
+ */
390
+ static void svmpr_free(void *ptr)
391
+ {
392
+ struct RSVM_Problem *rp = (struct RSVM_Problem *) ptr;
393
+ int i;
394
+ syncProblem(rp);
395
+ for (i = 0; i < rp->prob.l; ++i)
396
+ free(rp->prob.x[i]);
397
+ obstack_free(&(rp->xs),NULL);
398
+ obstack_free(&(rp->ys),NULL);
399
+ free(rp);
400
+ }
401
+
402
+ /*
403
+ * Creates a new SVMParameter object.
404
+ * Uses the following default values:
405
+ * * svm_type = C_SVC
406
+ * * kernel_type = RBF
407
+ * * degree = 3
408
+ * * gamma = 1 / k (0 means this also)
409
+ * * coef0 = 0
410
+ * * nu = 0.5
411
+ * * cache_size = 40
412
+ * * C = 1
413
+ * * eps = 1e-3
414
+ * * p = 0.1
415
+ * * shrinking = 1
416
+ * * nr_weight = 0
417
+ */
418
+ VALUE svmpa_new(VALUE cl)
419
+ {
420
+ struct RSVM_Parameter *rp = newParameter();
421
+ VALUE tdata = Data_Wrap_Struct(cl, 0, svmpa_free, rp);
422
+ printf("In the new!!\n");
423
+ rb_obj_call_init(tdata, 0, NULL);
424
+ return tdata;
425
+ }
426
+
427
+ #ifdef HAVE_DEFINE_ALLOC_FUNCTION
428
+ static VALUE svmpa_allocate(VALUE kl) {
429
+ return svmpa_new(kl);
430
+ }
431
+ #endif
432
+
433
+ /*
434
+ * Creates a new, empty SVMProblem object.
435
+ */
436
+ VALUE svmpr_new(VALUE cl)
437
+ {
438
+ struct RSVM_Problem *rp = newProblem();
439
+ VALUE tdata = Data_Wrap_Struct(cl, 0, svmpr_free, rp);
440
+ rb_obj_call_init(tdata, 0, NULL);
441
+ return tdata;
442
+ }
443
+
444
+ /*
445
+ * Trains an SVM according to a given problem set and parameter specification
446
+ */
447
+ VALUE svmpm_new(VALUE cl, VALUE prob, VALUE par)
448
+ {
449
+ struct RSVM_Model *rp = (struct RSVM_Model *) calloc(sizeof(struct RSVM_Model), 1);
450
+ struct RSVM_Problem *cpro;
451
+ struct RSVM_Parameter *cpa;
452
+ bool defgamma = false;
453
+ VALUE tdata = Data_Wrap_Struct(cl, 0, svmpm_free, rp);
454
+ Data_Get_Struct(prob, struct RSVM_Problem, cpro);
455
+ Data_Get_Struct(par, struct RSVM_Parameter, cpa);
456
+ syncProblem(cpro);
457
+ if (cpa->p.gamma == 0)
458
+ defgamma = true;
459
+ if (defgamma)
460
+ cpa->p.gamma = 1.0 / (double) cpro->k;
461
+ cpa->p.probability = 1;
462
+ rp->m = svm_train(&cpro->prob, &cpa->p);
463
+ if (defgamma)
464
+ cpa->p.gamma = 0;
465
+ rb_obj_call_init(tdata, 0, NULL);
466
+ return tdata;
467
+ }
468
+
469
+ static VALUE svmpm_predict_values(VALUE self, VALUE xs)
470
+ {
471
+ struct RSVM_Model *rp;
472
+ double *pe;
473
+ struct svm_node *x = rubyArrayToNodelist(xs);
474
+ int i, nr_class, numvals;
475
+ VALUE decvals;
476
+ Data_Get_Struct(self, struct RSVM_Model, rp);
477
+ nr_class = svm_get_nr_class(rp->m);
478
+ decvals = rb_ary_new();
479
+ numvals = (nr_class * (nr_class - 1))/2;
480
+ pe = (double *) calloc(numvals, sizeof(double));
481
+ svm_predict_values(rp->m, x, pe);
482
+ for (i = 0; i < numvals; i += 1)
483
+ rb_ary_push(decvals, rb_float_new(pe[i]));
484
+ free(pe);
485
+ return decvals;
486
+ }
487
+
488
+ static VALUE svmpm_predict_probability(VALUE self, VALUE xs)
489
+ {
490
+ double result;
491
+ struct RSVM_Model *rp;
492
+ double *pe;
493
+ struct svm_node *x = rubyArrayToNodelist(xs);
494
+ int i;
495
+ VALUE probs, retval;
496
+ retval = rb_ary_new();
497
+ Data_Get_Struct(self, struct RSVM_Model, rp);
498
+ probs = rb_ary_new();
499
+ pe = (double *) calloc(svm_get_nr_class(rp->m), sizeof(double));
500
+ result = svm_predict_probability(rp->m, x, pe);
501
+ for (i = 0; i < svm_get_nr_class(rp->m); i += 1)
502
+ rb_ary_push(probs, rb_float_new(pe[i]));
503
+ free(pe);
504
+ rb_ary_push(retval, rb_float_new(result));
505
+ rb_ary_push(retval, probs);
506
+ return retval;
507
+ }
508
+
509
+ /*
510
+ * Predicts a value (regression or classification) based on an input vector
511
+ */
512
+ static VALUE svmpm_predict(VALUE self, VALUE xs)
513
+ {
514
+ double result;
515
+ struct RSVM_Model *rp;
516
+ Data_Get_Struct(self, struct RSVM_Model, rp);
517
+ struct svm_node *x = rubyArrayToNodelist(xs);
518
+ result = svm_predict(rp->m, x);
519
+ free(x);
520
+ return rb_float_new(result);
521
+ }
522
+
523
+ /*
524
+ * Initializes an SVMModel
525
+ */
526
+ static VALUE svmpm_init(VALUE self)
527
+ {
528
+ return self;
529
+ }
530
+
531
+ /*
532
+ * Initializes an SVMParameter
533
+ */
534
+ static VALUE svmpa_init(VALUE self)
535
+ {
536
+ return self;
537
+ }
538
+
539
+ /*
540
+ * Initializes an SVMProblem
541
+ */
542
+ static VALUE svmpr_init(VALUE self)
543
+ {
544
+ return self;
545
+ }
546
+
547
+ /*
548
+ * Returns the number of samples in an SVMProblem
549
+ */
550
+ static VALUE svmpr_size(VALUE self)
551
+ {
552
+ struct RSVM_Problem *rp;
553
+ Data_Get_Struct(self, struct RSVM_Problem, rp);
554
+ syncProblem(rp);
555
+ return INT2FIX(rp->prob.l);
556
+ }
557
+
558
+ /*
559
+ * Returns the number of Support Vectors in an SVMModel
560
+ */
561
+ static VALUE svmpm_svcount(VALUE self)
562
+ {
563
+ struct RSVM_Model *rp;
564
+ Data_Get_Struct(self, struct RSVM_Model, rp);
565
+ return INT2FIX(getSVCount(rp->m));
566
+ }
567
+
568
+ /*
569
+ * Adds a training example to an SVMProblem
570
+ */
571
+ static VALUE svmpr_addex(VALUE self, VALUE y, VALUE xs)
572
+ {
573
+ struct RSVM_Problem *rp;
574
+ struct svm_node *fini;
575
+ double yd;
576
+ Data_Get_Struct(self, struct RSVM_Problem, rp);
577
+ yd = NUM2DBL(y);
578
+ fini = rubyArrayToNodelist(xs);
579
+ addExample(rp, yd, fini);
580
+ if (rp->k == 0) rp->k = RARRAY(xs)->len;
581
+ return Qnil;
582
+ }
583
+
584
+ /* To be removed in next version */
585
+ struct svm_model
586
+ {
587
+ svm_parameter param; // parameter
588
+ int nr_class; // number of classes, = 2 in regression/one class svm
589
+ int l; // total #SV
590
+ svm_node **SV; // SVs (SV[l])
591
+ double **sv_coef; // coefficients for SVs in decision functions (sv_coef[n-1][l])
592
+ double *rho; // constants in decision functions (rho[n*(n-1)/2])
593
+
594
+ // for classification only
595
+
596
+ int *label; // label of each class (label[n])
597
+ int *nSV; // number of SVs for each class (nSV[n])
598
+ // nSV[0] + nSV[1] + ... + nSV[n-1] = l
599
+ // XXX
600
+ int free_sv; // 1 if svm_model is created by svm_load_model
601
+ // 0 if svm_model is created by svm_train
602
+ };
603
+
604
+ static int getSVCount(struct svm_model *m)
605
+ {
606
+ return m->l;
607
+ }
608
+
609
+ extern "C" {
610
+ void Init_SVM();
611
+ };
612
+
613
+ void Init_SVM()
614
+ {
615
+ #ifdef QUIETFUNC
616
+ svm_set_verbosity(0);
617
+ #endif
618
+ mSVM = rb_define_module("SVM");
619
+ cSVMProblem = rb_define_class_under(mSVM, "Problem", rb_cObject);
620
+ cSVMParameter = rb_define_class_under(mSVM, "Parameter", rb_cObject);
621
+ cSVMModel = rb_define_class_under(mSVM, "Model", rb_cObject);
622
+
623
+
624
+ rb_define_singleton_method(cSVMProblem, "new", (VALUE (*) (...))svmpr_new, 0);
625
+ rb_define_method(cSVMProblem, "initialize", (VALUE (*) (...))svmpr_init, 0);
626
+ rb_define_method(cSVMProblem, "size", (VALUE (*) (...))svmpr_size, 0);
627
+ rb_define_method(cSVMProblem, "addExample", (VALUE (*) (...))svmpr_addex, 2);
628
+
629
+ rb_define_const(mSVM, "C_SVC", INT2FIX(C_SVC));
630
+ rb_define_const(mSVM, "NU_SVC", INT2FIX(NU_SVC));
631
+ rb_define_const(mSVM, "ONE_CLASS", INT2FIX(ONE_CLASS));
632
+ rb_define_const(mSVM, "EPSILON_SVR", INT2FIX(EPSILON_SVR));
633
+ rb_define_const(mSVM, "NU_SVR", INT2FIX(NU_SVR));
634
+ rb_define_const(mSVM, "LINEAR", INT2FIX(LINEAR));
635
+ rb_define_const(mSVM, "POLY", INT2FIX(POLY));
636
+ rb_define_const(mSVM, "RBF", INT2FIX(RBF));
637
+ rb_define_const(mSVM, "SIGMOID", INT2FIX(SIGMOID));
638
+
639
+ rb_define_singleton_method(cSVMParameter, "new", (VALUE (*) (...))svmpa_new, 0);
640
+ rb_define_method(cSVMParameter, "degree", (VALUE (*) (...))svmpa_degree, 0);
641
+ rb_define_method(cSVMParameter, "degree=", (VALUE (*) (...))svmpa_degreeeq, 1);
642
+ rb_define_method(cSVMParameter, "gamma", (VALUE (*) (...))svmpa_gamma, 0);
643
+ rb_define_method(cSVMParameter, "gamma=", (VALUE (*) (...))svmpa_gammaeq, 1);
644
+ rb_define_method(cSVMParameter, "coef0", (VALUE (*) (...))svmpa_coef0, 0);
645
+ rb_define_method(cSVMParameter, "coef0=", (VALUE (*) (...))svmpa_coef0eq, 1);
646
+ rb_define_method(cSVMParameter, "cache_size", (VALUE (*) (...))svmpa_cache_size, 0);
647
+ rb_define_method(cSVMParameter, "cache_size=", (VALUE (*) (...))svmpa_cache_sizeeq, 1);
648
+ rb_define_method(cSVMParameter, "eps", (VALUE (*) (...))svmpa_eps, 0);
649
+ rb_define_method(cSVMParameter, "eps=", (VALUE (*) (...))svmpa_epseq, 1);
650
+ rb_define_method(cSVMParameter, "C", (VALUE (*) (...))svmpa_C, 0);
651
+ rb_define_method(cSVMParameter, "C=", (VALUE (*) (...))svmpa_Ceq, 1);
652
+ rb_define_method(cSVMParameter, "nu", (VALUE (*) (...))svmpa_nu, 0);
653
+ rb_define_method(cSVMParameter, "nu=", (VALUE (*) (...))svmpa_nueq, 1);
654
+ rb_define_method(cSVMParameter, "p", (VALUE (*) (...))svmpa_p, 0);
655
+ rb_define_method(cSVMParameter, "p=", (VALUE (*) (...))svmpa_peq, 1);
656
+ rb_define_method(cSVMParameter, "kernel_type", (VALUE (*) (...))svmpa_kernel_type, 0);
657
+ rb_define_method(cSVMParameter, "kernel_type=", (VALUE (*) (...))svmpa_kernel_typeeq, 1);
658
+ rb_define_method(cSVMParameter, "svm_type", (VALUE (*) (...))svmpa_svm_type, 0);
659
+ rb_define_method(cSVMParameter, "svm_type=", (VALUE (*) (...))svmpa_svm_typeeq, 1);
660
+ /*
661
+ rb_define_method(cSVMParameter, "_dump_data", (VALUE (*) (...))svmpa_svm_dump_data, 0);
662
+
663
+ rb_define_method(cSVMParameter, "_load_data", (VALUE (*) (...))svmpa_svm_load_data, 1);
664
+ */
665
+ rb_define_method(cSVMParameter, "_dump", (VALUE (*) (...))svmpa_svm_dump, 1);
666
+
667
+ rb_define_singleton_method(cSVMParameter, "_load", (VALUE (*) (...))svmpa_svm_load, 1);
668
+
669
+ #ifdef HAVE_DEFINE_ALLOC_FUNCTION
670
+ rb_define_alloc_func(cSVMModel, svmpa_allocate);
671
+ #endif
672
+ /*rb_undef_alloc_func(cSVMModel); */
673
+ /* rb_add_method(cSVMModel, ID_ALLOCATOR, NEW_CFUNC(svmpa_allocate, 0), NOEX_PRIVATE | NOEX_CFUNC); */
674
+ /* rb_define_singleton_method(cSVMModel, "allocate", (VALUE (*) (...))svmpa_allocate, 1);
675
+ rb_define_singleton_method(cSVMModel, "_alloc", (VALUE (*) (...))svmpa_allocate, 1);
676
+ */
677
+
678
+ rb_define_singleton_method(cSVMModel, "new", (VALUE (*) (...))svmpm_new, 2);
679
+ rb_define_method(cSVMModel, "predict", (VALUE (*) (...))svmpm_predict, 1);
680
+ rb_define_method(cSVMModel, "predict_probability", (VALUE (*) (...))svmpm_predict_probability, 1);
681
+ rb_define_method(cSVMModel, "predict_values", (VALUE (*) (...))svmpm_predict_values, 1);
682
+ rb_define_method(cSVMModel, "svcount", (VALUE (*) (...))svmpm_svcount, 0);
683
+ cMarshal = rb_const_get(rb_cObject, rb_intern("Marshal"));
684
+ }
685
+