/usr/include/dlib/svm/kernel_matrix.h is in libdlib-dev 18.18-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | // Copyright (C) 2009 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SVm_KERNEL_MATRIX_
#define DLIB_SVm_KERNEL_MATRIX_
#include <vector>
#include "kernel_matrix_abstract.h"
#include "../matrix.h"
#include "../algs.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
namespace impl
{
template <typename kernel_type, typename T>
inline const typename T::type& access ( const matrix_exp<T>& m, long i)
{
return m(i);
}
// bind to anything that looks like an array and isn't a matrix
template <typename kernel_type, typename T>
inline const typename disable_if<is_matrix<T>,typename T::type>::type& access ( const T& m, long i)
{
return m[i];
}
// Only use this function if T isn't a std::pair because in that case the entire vector is
// probably itself a sparse sample.
template <typename kernel_type, typename T, typename alloc>
inline typename disable_if<is_pair<T>,const T&>::type access ( const std::vector<T,alloc>& m, long i)
{
return m[i];
}
// Only use this function if T isn't a std::pair because in that case the entire vector is
// probably a sparse sample.
template <typename kernel_type, typename T, typename alloc>
inline typename disable_if<is_pair<T>,const T&>::type access ( const std_vector_c<T,alloc>& m, long i)
{
return m[i];
}
template <typename kernel_type>
inline const typename kernel_type::sample_type& access (
const typename kernel_type::sample_type& samp,
long
)
{
return samp;
}
// --------------------------------------------
template <typename kernel_type, typename T>
inline typename disable_if<is_same_type<T,typename kernel_type::sample_type>,unsigned long>::type
size ( const T& m)
{
return m.size();
}
template <typename kernel_type>
inline unsigned long size (
const typename kernel_type::sample_type&
)
{
return 1;
}
// --------------------------------------------
template <typename T>
typename disable_if<is_matrix<T> >::type assert_is_vector(const T&)
{}
template <typename T>
// This funny #ifdef thing is here because gcc sometimes gives a warning
// about v being unused otherwise.
#ifdef ENABLE_ASSERTS
void assert_is_vector(const matrix_exp<T>& v)
#else
void assert_is_vector(const matrix_exp<T>& )
#endif
{
// make sure requires clause is not broken
DLIB_ASSERT(is_vector(v) == true,
"\tconst matrix_exp kernel_matrix()"
<< "\n\t You have to supply this function with row or column vectors"
<< "\n\t v.nr(): " << v.nr()
<< "\n\t v.nc(): " << v.nc()
);
}
}
template <typename K, typename vect_type1, typename vect_type2>
struct op_kern_mat
{
op_kern_mat(
const K& kern_,
const vect_type1& vect1_,
const vect_type2& vect2_
) :
kern(kern_),
vect1(vect1_),
vect2(vect2_)
{
// make sure the requires clauses get checked eventually
impl::assert_is_vector(vect1);
impl::assert_is_vector(vect2);
}
const K& kern;
const vect_type1& vect1;
const vect_type2& vect2;
typedef typename K::scalar_type type;
const static long cost = 100;
const static long NR = (is_same_type<vect_type1,typename K::sample_type>::value) ? 1 : 0;
const static long NC = (is_same_type<vect_type2,typename K::sample_type>::value) ? 1 : 0;
typedef const type const_ret_type;
typedef typename K::mem_manager_type mem_manager_type;
typedef row_major_layout layout_type;
const_ret_type apply (long r, long c ) const
{
return kern(impl::access<K>(vect1,r), impl::access<K>(vect2,c));
}
long nr () const { return impl::size<K>(vect1); }
long nc () const { return impl::size<K>(vect2); }
template <typename U> bool aliases ( const matrix_exp<U>& item ) const { return alias_helper(item.ref()); }
template <typename U> bool destructively_aliases ( const matrix_exp<U>& item ) const { return alias_helper(item.ref()); }
template <typename U> bool alias_helper ( const U& ) const { return false; }
typedef typename K::sample_type samp_type;
// Say we destructively alias if one of the vect* objects is actually item.
bool alias_helper (const samp_type& item ) const { return are_same(item, vect1) || are_same(item, vect2); }
template <typename U> bool are_same (const samp_type& , const U& ) const { return false; }
bool are_same (const samp_type& a, const samp_type& b) const { return (&a == &b); }
};
// ----------------------------------------------------------------------------------------
template <
typename K,
typename V1,
typename V2
>
const matrix_op<op_kern_mat<K,V1,V2> > kernel_matrix (
const K& kern,
const V1& v1,
const V2& v2
)
{
typedef op_kern_mat<K,V1,V2> op;
return matrix_op<op>(op(kern,v1,v2));
}
// ----------------------------------------------------------------------------------------
/*
It is possible to implement the kernel_matrix() operator with just one operator
class but treating the version that takes only a single vector separately
leads to more efficient output by gcc in certain instances.
*/
template <typename K, typename vect_type1>
struct op_kern_mat_single
{
op_kern_mat_single(
const K& kern_,
const vect_type1& vect1_
) :
kern(kern_),
vect1(vect1_)
{
// make sure the requires clauses get checked eventually
impl::assert_is_vector(vect1);
}
const K& kern;
const vect_type1& vect1;
typedef typename K::scalar_type type;
const static long cost = 100;
const static long NR = (is_same_type<vect_type1,typename K::sample_type>::value) ? 1 : 0;
const static long NC = (is_same_type<vect_type1,typename K::sample_type>::value) ? 1 : 0;
typedef const type const_ret_type;
typedef typename K::mem_manager_type mem_manager_type;
typedef row_major_layout layout_type;
const_ret_type apply (long r, long c ) const
{
return kern(impl::access<K>(vect1,r), impl::access<K>(vect1,c));
}
long nr () const { return impl::size<K>(vect1); }
long nc () const { return impl::size<K>(vect1); }
template <typename U> bool aliases ( const matrix_exp<U>& item ) const { return alias_helper(item.ref()); }
template <typename U> bool destructively_aliases ( const matrix_exp<U>& item ) const { return alias_helper(item.ref()); }
template <typename U> bool alias_helper ( const U& ) const { return false; }
typedef typename K::sample_type samp_type;
// Say we destructively alias if vect1 is actually item.
bool alias_helper (const samp_type& item ) const { return are_same(item, vect1); }
template <typename U> bool are_same (const samp_type& , const U& ) const { return false; }
bool are_same (const samp_type& a, const samp_type& b) const { return (&a == &b); }
};
template <
typename K,
typename V
>
const matrix_op<op_kern_mat_single<K,V> > kernel_matrix (
const K& kern,
const V& v
)
{
typedef op_kern_mat_single<K,V> op;
return matrix_op<op>(op(kern,v));
}
// ----------------------------------------------------------------------------------------
template <
typename matrix_dest_type,
typename K,
typename V
>
inline void matrix_assign (
matrix_dest_type& dest,
const matrix_exp<matrix_op<op_kern_mat_single<K,V> > >& src
)
/*!
Overload matrix assignment so that when a kernel_matrix expression
gets assigned it only evaluates half the kernel matrix (since it is symmetric)
!*/
{
for (long r = 0; r < src.nr(); ++r)
{
for (long c = r; c < src.nc(); ++c)
{
dest(r,c) = dest(c,r) = src(r,c);
}
}
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_SVm_KERNEL_MATRIX_
|