SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ShiftInvariantKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2016 Soumyajit De
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
31 #include <shogun/lib/common.h>
34 
35 using namespace shogun;
36 
38 {
39  register_params();
40 }
41 
43 {
44  register_params();
45  init(l, r);
46 }
47 
49 {
50  cleanup();
52 }
53 
54 bool CShiftInvariantKernel::init(CFeatures* l, CFeatures* r)
55 {
56  REQUIRE(m_distance, "The distance instance cannot be NULL!\n");
57  CKernel::init(l,r);
58  m_distance->init(l, r);
59  return init_normalizer();
60 }
61 
63 {
64  REQUIRE(m_distance, "The distance instance cannot be NULL!\n");
65  REQUIRE(m_distance->init(lhs, rhs), "Could not initialize the distance instance!\n");
66 
68  if (m_precomputed_distance==NULL)
69  {
70  m_precomputed_distance=new CCustomDistance();
71  SG_REF(m_precomputed_distance);
72  }
73 
74  if (lhs==rhs)
75  m_precomputed_distance->set_triangle_distance_matrix_from_full(dist_mat.data(), dist_mat.num_rows, dist_mat.num_cols);
76  else
77  m_precomputed_distance->set_full_distance_matrix_from_full(dist_mat.data(), dist_mat.num_rows, dist_mat.num_cols);
78 }
79 
81 {
82  SG_UNREF(m_precomputed_distance);
83  m_precomputed_distance=NULL;
86 }
87 
89 {
90  REQUIRE(m_distance, "The distance instance cannot be NULL!\n");
91  return m_distance->get_distance_type();
92 }
93 
94 float64_t CShiftInvariantKernel::distance(int32_t a, int32_t b) const
95 {
96  REQUIRE(m_distance, "The distance instance cannot be NULL!\n");
97  if (m_precomputed_distance!=NULL)
98  return m_precomputed_distance->distance(a, b);
99  else
100  return m_distance->distance(a, b);
101 }
102 
103 void CShiftInvariantKernel::register_params()
104 {
105  SG_ADD((CSGObject**) &m_distance, "m_distance", "Distance to be used.", MS_NOT_AVAILABLE);
106  SG_ADD((CSGObject**) &m_precomputed_distance, "m_precomputed_distance", "Precomputed istance to be used.", MS_NOT_AVAILABLE);
107 
108  m_distance=NULL;
109  m_precomputed_distance=NULL;
110 }
111 
112 void CShiftInvariantKernel::set_precomputed_distance(CCustomDistance* precomputed_distance)
113 {
114  REQUIRE(precomputed_distance, "The precomputed distance instance cannot be NULL!\n");
115  SG_REF(precomputed_distance);
116  SG_UNREF(m_precomputed_distance);
117  m_precomputed_distance=precomputed_distance;
118 }
119 
120 CCustomDistance* CShiftInvariantKernel::get_precomputed_distance() const
121 {
122  REQUIRE(m_precomputed_distance, "The precomputed distance instance cannot be NULL!\n");
123  SG_REF(m_precomputed_distance);
124  return m_precomputed_distance;
125 }
bool set_triangle_distance_matrix_from_full(const float64_t *dm, int32_t rows, int32_t cols)
virtual void cleanup()
Definition: Kernel.cpp:173
virtual void cleanup()=0
virtual EDistanceType get_distance_type()=0
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual EDistanceType get_distance_type() const
#define SG_REF(x)
Definition: SGObject.h:54
EDistanceType
Definition: Distance.h:32
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
double float64_t
Definition: common.h:50
bool set_full_distance_matrix_from_full(const float64_t *dm, int32_t rows, int32_t cols)
virtual bool init_normalizer()
Definition: Kernel.cpp:168
float float32_t
Definition: common.h:49
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1062
virtual float64_t distance(int32_t idx_a, int32_t idx_b)
Definition: Distance.cpp:206
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1060
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual float64_t distance(int32_t idx_a, int32_t idx_b) const
The Kernel base class.
Definition: Kernel.h:159
SGMatrix< float64_t > get_distance_matrix()
Definition: Distance.h:156
The Custom Distance allows for custom user provided distance matrices.
virtual bool init(CFeatures *l, CFeatures *r)
#define SG_ADD(...)
Definition: SGObject.h:84
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Distance.cpp:78

SHOGUN Machine Learning Toolbox - Documentation