1 /*
2   This code was extracted from libsvm 3.2.3 in Feb 2019 and
3   modified for the use with Octave and Matlab
4 
5 
6 Copyright (c) 2000-2019 Chih-Chung Chang and Chih-Jen Lin
7 All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12 
13 1. Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15 
16 2. Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
19 
20 3. Neither name of copyright holders nor the names of its contributors
21 may be used to endorse or promote products derived from this software
22 without specific prior written permission.
23 
24 
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
29 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37 
38 #ifndef _LIBSVM_H
39 #define _LIBSVM_H
40 
41 #define LIBSVM_VERSION 323
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 extern int libsvm_version;
48 
49 struct svm_node
50 {
51 	int index;
52 	double value;
53 };
54 
55 struct svm_problem
56 {
57 	int l;
58 	double *y;
59 	struct svm_node **x;
60 };
61 
62 enum { C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR };	/* svm_type */
63 enum { LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED }; /* kernel_type */
64 
65 struct svm_parameter
66 {
67 	int svm_type;
68 	int kernel_type;
69 	int degree;	/* for poly */
70 	double gamma;	/* for poly/rbf/sigmoid */
71 	double coef0;	/* for poly/sigmoid */
72 
73 	/* these are for training only */
74 	double cache_size; /* in MB */
75 	double eps;	/* stopping criteria */
76 	double C;	/* for C_SVC, EPSILON_SVR and NU_SVR */
77 	int nr_weight;		/* for C_SVC */
78 	int *weight_label;	/* for C_SVC */
79 	double* weight;		/* for C_SVC */
80 	double nu;	/* for NU_SVC, ONE_CLASS, and NU_SVR */
81 	double p;	/* for EPSILON_SVR */
82 	int shrinking;	/* use the shrinking heuristics */
83 	int probability; /* do probability estimates */
84 };
85 
86 //
87 // svm_model
88 //
89 struct svm_model
90 {
91 	struct svm_parameter param;	/* parameter */
92 	int nr_class;		/* number of classes, = 2 in regression/one class svm */
93 	int l;			/* total #SV */
94 	struct svm_node **SV;		/* SVs (SV[l]) */
95 	double **sv_coef;	/* coefficients for SVs in decision functions (sv_coef[k-1][l]) */
96 	double *rho;		/* constants in decision functions (rho[k*(k-1)/2]) */
97 	double *probA;		/* pariwise probability information */
98 	double *probB;
99 	int *sv_indices;        /* sv_indices[0,...,nSV-1] are values in [1,...,num_traning_data] to indicate SVs in the training set */
100 
101 	/* for classification only */
102 
103 	int *label;		/* label of each class (label[k]) */
104 	int *nSV;		/* number of SVs for each class (nSV[k]) */
105 				/* nSV[0] + nSV[1] + ... + nSV[k-1] = l */
106 	/* XXX */
107 	int free_sv;		/* 1 if svm_model is created by svm_load_model*/
108 				/* 0 if svm_model is created by svm_train */
109 };
110 
111 struct svm_model *svm_train(const struct svm_problem *prob, const struct svm_parameter *param);
112 void svm_cross_validation(const struct svm_problem *prob, const struct svm_parameter *param, int nr_fold, double *target);
113 
114 int svm_save_model(const char *model_file_name, const struct svm_model *model);
115 struct svm_model *svm_load_model(const char *model_file_name);
116 
117 int svm_get_svm_type(const struct svm_model *model);
118 int svm_get_nr_class(const struct svm_model *model);
119 void svm_get_labels(const struct svm_model *model, int *label);
120 void svm_get_sv_indices(const struct svm_model *model, int *sv_indices);
121 int svm_get_nr_sv(const struct svm_model *model);
122 double svm_get_svr_probability(const struct svm_model *model);
123 
124 double svm_predict_values(const struct svm_model *model, const struct svm_node *x, double* dec_values);
125 double svm_predict(const struct svm_model *model, const struct svm_node *x);
126 double svm_predict_probability(const struct svm_model *model, const struct svm_node *x, double* prob_estimates);
127 
128 void svm_free_model_content(struct svm_model *model_ptr);
129 void svm_free_and_destroy_model(struct svm_model **model_ptr_ptr);
130 void svm_destroy_param(struct svm_parameter *param);
131 
132 const char *svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param);
133 int svm_check_probability_model(const struct svm_model *model);
134 
135 void svm_set_print_string_function(void (*print_func)(const char *));
136 
137 #ifdef __cplusplus
138 }
139 #endif
140 
141 #endif /* _LIBSVM_H */
142