This file is indexed.

/usr/include/shark/LinAlg/KernelMatrix.h is in libshark-dev 3.1.3+ds1-2.

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
268
269
270
271
272
273
274
275
276
277
278
279
//===========================================================================
/*!
 * 
 *
 * \brief       Kernel Gram matrix
 * 
 * 
 * \par
 * 
 * 
 *
 * \author      T. Glasmachers
 * \date        2007-2012
 *
 *
 * \par Copyright 1995-2015 Shark Development Team
 * 
 * <BR><HR>
 * This file is part of Shark.
 * <http://image.diku.dk/shark/>
 * 
 * Shark is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Shark is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Shark.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
//===========================================================================


#ifndef SHARK_LINALG_KERNELMATRIX_H
#define SHARK_LINALG_KERNELMATRIX_H


#include <shark/Data/Dataset.h>
#include <shark/LinAlg/Base.h>
#include <shark/Models/Kernels/KernelHelpers.h>

#include <vector>
#include <cmath>


namespace shark {

///
/// \brief Kernel Gram matrix
///
/// \par
/// The KernelMatrix is the most prominent type of matrix
/// for quadratic programming. It provides the Gram matrix
/// of a fixed data set with respect to an inner product
/// implicitly defined by a kernel function.
///
/// \par
/// NOTE: The KernelMatrix class stores pointers to the
/// data, instead of maintaining a copy of the data. Thus,
/// it implicitly assumes that the dataset is not altered
/// during the lifetime of the KernelMatrix object. This
/// condition is ensured as long as the class is used via
/// the various SVM-trainers.
///
template <class InputType, class CacheType>
class KernelMatrix
{
public:
    typedef CacheType QpFloatType;

    /// Constructor
    /// \param kernelfunction   kernel function defining the Gram matrix
    /// \param data             data to evaluate the kernel function
    KernelMatrix(AbstractKernelFunction<InputType> const& kernelfunction,
            Data<InputType> const& data)
    : kernel(kernelfunction)
    , m_data(data)
    , m_accessCounter( 0 )
    {
        std::size_t elements = m_data.numberOfElements();
        x.resize(elements);
        typename Data<InputType>::const_element_range::iterator iter=m_data.elements().begin();
        for(std::size_t i = 0; i != elements; ++i,++iter){
            x[i]=iter.getInnerIterator();
        }
    }

    /// return a single matrix entry
    QpFloatType operator () (std::size_t i, std::size_t j) const
    { return entry(i, j); }

    /// return a single matrix entry
    QpFloatType entry(std::size_t i, std::size_t j) const
    {
        ++m_accessCounter;
        return (QpFloatType)kernel.eval(*x[i], *x[j]);
    }
    
    /// \brief Computes the i-th row of the kernel matrix.
    ///
    ///The entries start,...,end of the i-th row are computed and stored in storage.
    ///There must be enough room for this operation preallocated.
    void row(std::size_t i, std::size_t start,std::size_t end, QpFloatType* storage) const{
        m_accessCounter += end-start;
        
        typename AbstractKernelFunction<InputType>::ConstInputReference xi = *x[i];
        SHARK_PARALLEL_FOR(int j = (int)start; j < (int) end; j++)
        {
            storage[j-start] = QpFloatType(kernel.eval(xi, *x[j]));
        }
    }
    
    /// \brief Computes the kernel-matrix
    template<class M>
    void matrix(
        blas::matrix_expression<M> & storage
    ) const{
        calculateRegularizedKernelMatrix(kernel,m_data,storage);
    }

    /// swap two variables
    void flipColumnsAndRows(std::size_t i, std::size_t j){
        using std::swap;
        swap(x[i],x[j]);
    }

    /// return the size of the quadratic matrix
    std::size_t size() const
    { return x.size(); }

    /// query the kernel access counter
    unsigned long long getAccessCount() const
    { return m_accessCounter; }

    /// reset the kernel access counter
    void resetAccessCount()
    { m_accessCounter = 0; }

protected:
    /// Kernel function defining the kernel Gram matrix
    const AbstractKernelFunction<InputType>& kernel;

    Data<InputType> m_data;

    typedef typename Batch<InputType>::const_iterator PointerType;
    /// Array of data pointers for kernel evaluations
    std::vector<PointerType> x;

    /// counter for the kernel accesses
    mutable unsigned long long m_accessCounter;
};

//~ ///\brief Specialization for dense vectors which often can be computed much faster
//~ template <class T, class CacheType>
//~ class KernelMatrix<blas::vector<T>, CacheType>
//~ {
//~ public:

    //~ //////////////////////////////////////////////////////////////////
    //~ // The types below define the type used for caching kernel values. The default is float,
    //~ // since this type offers sufficient accuracy in the vast majority of cases, at a memory
    //~ // cost of only four bytes. However, the type definition makes it easy to use double instead
    //~ // (e.g., in case high accuracy training is needed).
    //~ typedef CacheType QpFloatType;
    //~ typedef blas::vector<T> InputType;

    //~ /// Constructor
    //~ /// \param kernelfunction   kernel function defining the Gram matrix
    //~ /// \param data             data to evaluate the kernel function
    //~ KernelMatrix(
        //~ AbstractKernelFunction<InputType> const& kernelfunction,
        //~ Data<InputType> const& data)
    //~ : kernel(kernelfunction)
    //~ , m_data(data)
    //~ , m_batchStart(data.numberOfBatches())
    //~ , m_accessCounter( 0 )
    //~ {
        //~ m_data.makeIndependent();
        //~ std::size_t elements = m_data.numberOfElements();
        //~ x.resize(elements);
        //~ typename Data<InputType>::element_range::iterator iter=m_data.elements().begin();
        //~ for(std::size_t i = 0; i != elements; ++i,++iter){
            //~ x[i]=iter.getInnerIterator();
        //~ }
        
        //~ for(std::size_t i = 0,start = 0; i != m_data.numberOfBatches(); ++i){
            //~ m_batchStart[i] = start;
            //~ start+= m_data.batch(i).size1();
        //~ }
    //~ }

    //~ /// return a single matrix entry
    //~ QpFloatType operator () (std::size_t i, std::size_t j) const
    //~ { return entry(i, j); }

    //~ /// return a single matrix entry
    //~ QpFloatType entry(std::size_t i, std::size_t j) const
    //~ {
        //~ ++m_accessCounter;
        //~ return (QpFloatType)kernel.eval(*x[i], *x[j]);
    //~ }
    
    //~ /// \brief Computes the i-th row of the kernel matrix.
    //~ ///
    //~ ///The entries start,...,end of the i-th row are computed and stored in storage.
    //~ ///There must be enough room for this operation preallocated.
    //~ void row(std::size_t k, std::size_t start,std::size_t end, QpFloatType* storage) const
    //~ {
        //~ m_accessCounter +=end-start;
        
        //~ typename AbstractKernelFunction<InputType>::ConstInputReference xi = *x[k];     
        //~ typename blas::matrix<T> mx(1,xi.size());
        //~ noalias(blas::row(mx,0))=xi;
        
        //~ int numBatches = (int)m_data.numberOfBatches();
        //~ SHARK_PARALLEL_FOR(int i = 0; i < numBatches; i++)
        //~ {
            //~ std::size_t pos = m_batchStart[i];
            //~ std::size_t batchSize = m_data.batch(i).size1();
            //~ if(!(pos+batchSize < start || pos > end)){
                //~ RealMatrix rowpart(1,batchSize);
                //~ kernel.eval(mx,m_data.batch(i),rowpart);
                //~ std::size_t batchStart = (start <=pos) ? 0: start-pos;
                //~ std::size_t batchEnd = (pos+batchSize > end) ? end-pos: batchSize;
                //~ for(std::size_t j =  batchStart;  j !=  batchEnd;++j){
                    //~ storage[pos+j-start] = static_cast<QpFloatType>(rowpart(0,j));
                //~ }
            //~ }
        //~ }
    //~ }
    
    //~ /// \brief Computes the kernel-matrix
    //~ template<class M>
    //~ void matrix(
        //~ blas::matrix_expression<M> & storage
    //~ ) const{
        //~ calculateRegularizedKernelMatrix(kernel,m_data,storage);
    //~ }

    //~ /// swap two variables
    //~ void flipColumnsAndRows(std::size_t i, std::size_t j){
        //~ if( i == j ) return;
        //~ swap(*x[i],*x[j]);
    //~ }

    //~ /// return the size of the quadratic matrix
    //~ std::size_t size() const
    //~ { return x.size(); }
    
    //~ /// query the kernel access counter
    //~ unsigned long long getAccessCount() const
    //~ { return m_accessCounter; }

    //~ /// reset the kernel access counter
    //~ void resetAccessCount()
    //~ { m_accessCounter = 0; }

//~ protected:
    //~ /// Kernel function defining the kernel Gram matrix
    //~ const AbstractKernelFunction<InputType>& kernel;

    //~ Data<InputType> m_data;

    //~ typedef typename Batch<InputType>::iterator PointerType;
    //~ /// Array of data pointers for kernel evaluations
    //~ std::vector<PointerType> x;

    //~ std::vector<std::size_t> m_batchStart;

    //~ mutable unsigned long long m_accessCounter;
//~ };

}
#endif