SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LinearKernel.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2010 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  * Copyright (C) 2010 Berlin Institute of Technology
10  */
11 
12 #include <shogun/lib/common.h>
13 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
21 : CDotKernel(0), normal(NULL), normal_length(0)
22 {
24 }
25 
27 : CDotKernel(0), normal(NULL), normal_length(0)
28 {
30  init(l,r);
31 }
32 
34 {
35  cleanup();
36 }
37 
38 bool CLinearKernel::init(CFeatures* l, CFeatures* r)
39 {
40  CDotKernel::init(l, r);
41 
42  return init_normalizer();
43 }
44 
46 {
48 
50 }
51 
53 {
54  int32_t num = ((CDotFeatures*) lhs)->get_dim_feature_space();
55  if (normal==NULL)
56  {
57  normal = SG_MALLOC(float64_t, num);
58  normal_length=num;
59  }
60 
61  memset(normal, 0, sizeof(float64_t)*normal_length);
62 
63  set_is_initialized(true);
64 }
65 
66 void CLinearKernel::add_to_normal(int32_t idx, float64_t weight)
67 {
68  ((CDotFeatures*) lhs)->add_to_dense_vec(
69  normalizer->normalize_lhs(weight, idx), idx, normal, normal_length);
70  set_is_initialized(true);
71 }
72 
74  int32_t num_suppvec, int32_t* sv_idx, float64_t* alphas)
75 {
76  clear_normal();
77 
78  for (int32_t i=0; i<num_suppvec; i++)
79  add_to_normal(sv_idx[i], alphas[i]);
80 
81  set_is_initialized(true);
82  return true;
83 }
84 
86 {
87  clear_normal();
88 
89  int32_t num_suppvec=km->get_num_support_vectors();
90 
91  for (int32_t i=0; i<num_suppvec; i++)
93 
94  set_is_initialized(true);
95  return true;
96 }
97 
99 {
100  SG_FREE(normal);
101  normal_length=0;
102  normal=NULL;
103  set_is_initialized(false);
104 
105  return true;
106 }
107 
109 {
111  float64_t result = ((CDotFeatures*) rhs)->
112  dense_dot(idx, normal, normal_length);
113  return normalizer->normalize_rhs(result, idx);
114 }

SHOGUN Machine Learning Toolbox - Documentation