/usr/include/shogun/lib/SGSparseMatrix.h is in libshogun-dev 3.2.0-7.3build4.
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 | /*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Fernando José Iglesias García
* Written (W) 2010,2012 Soeren Sonnenburg
* Copyright (C) 2010 Berlin Institute of Technology
* Copyright (C) 2012 Soeren Sonnenburg
*/
#ifndef __SGSPARSEMATRIX_H__
#define __SGSPARSEMATRIX_H__
#include <shogun/lib/common.h>
#include <shogun/lib/DataType.h>
#include <shogun/lib/SGSparseVector.h>
#include <shogun/lib/SGReferencedData.h>
#include <shogun/io/LibSVMFile.h>
namespace shogun
{
template <class T> class SGSparseVector;
template<class T> class SGMatrix;
class CFile;
class CLibSVMFile;
class CRegressionLabels;
/** @brief template class SGSparseMatrix */
template <class T> class SGSparseMatrix : public SGReferencedData
{
public:
/** default constructor */
SGSparseMatrix();
/** constructor for setting params */
SGSparseMatrix(SGSparseVector<T>* vecs, index_t num_feat,
index_t num_vec, bool ref_counting=true);
/** constructor to create new matrix in memory */
SGSparseMatrix(index_t num_feat, index_t num_vec, bool ref_counting=true);
/** constructor to create new sparse matrix from a dense one
*
* @param dense dense matrix to be converted
*/
SGSparseMatrix(SGMatrix<T> dense);
/** copy constructor */
SGSparseMatrix(const SGSparseMatrix &orig);
/** destructor */
virtual ~SGSparseMatrix();
/** index access operator */
inline const SGSparseVector<T>& operator[](index_t index) const
{
return sparse_matrix[index];
}
/** index access operator */
inline SGSparseVector<T>& operator[](index_t index)
{
return sparse_matrix[index];
}
/**
* get the sparse matrix (no copying is done here)
*
* @return the refcount increased matrix
*/
inline SGSparseMatrix<T> get()
{
return *this;
}
/** compute sparse-matrix dense-vector multiplication
* @param v the dense-vector to be multiplied with
* @return the result vector \f$Q*v\f$, Q being this sparse matrix
*/
const SGVector<T> operator*(SGVector<T> v) const
{
SGVector<T> result(num_vectors);
REQUIRE(v.vlen==num_features,
"Dimension mismatch! %d vs %d\n",
v.vlen, num_features);
for (index_t i=0; i<num_vectors; ++i)
result[i]=sparse_matrix[i].dense_dot(1.0, v.vector, v.vlen, 0.0);
return result;
}
/** compute sparse-matrix dense-vector multiplication
* @param v the dense-vector to be multiplied with
* @return the result vector \f$Q*v\f$, Q being this sparse matrix
*/
template<class ST> const SGVector<T> operator*(SGVector<ST> v) const;
/** operator overload for sparse-matrix read only access
* @param i_row
* @param i_col
*/
inline const T operator()(index_t i_row, index_t i_col) const
{
REQUIRE(i_row>=0, "index %d negative!\n", i_row);
REQUIRE(i_col>=0, "index %d negative!\n", i_col);
REQUIRE(i_row<num_vectors, "index should be less than %d, %d provided!\n",
num_vectors, i_row);
REQUIRE(i_col<num_features, "index should be less than %d, %d provided!\n",
num_features, i_col);
for (index_t i=0; i<sparse_matrix[i_row].num_feat_entries; ++i)
{
if (i_col==sparse_matrix[i_row].features[i].feat_index)
return sparse_matrix[i_row].features[i].entry;
}
return 0;
}
/** operator overload for sparse-matrix r/w access
* @param i_row
* @param i_col
*/
inline T& operator()(index_t i_row, index_t i_col)
{
REQUIRE(i_row>=0, "index %d negative!\n", i_row);
REQUIRE(i_col>=0, "index %d negative!\n", i_col);
REQUIRE(i_row<num_vectors, "index should be less than %d, %d provided!\n",
num_vectors, i_row);
REQUIRE(i_col<num_features, "index should be less than %d, %d provided!\n",
num_features, i_col);
for (index_t i=0; i<sparse_matrix[i_row].num_feat_entries; ++i)
{
if (i_col==sparse_matrix[i_row].features[i].feat_index)
return sparse_matrix[i_row].features[i].entry;
}
index_t j=sparse_matrix[i_row].num_feat_entries;
sparse_matrix[i_row].num_feat_entries=j+1;
sparse_matrix[i_row].features=SG_REALLOC(SGSparseVectorEntry<T>,
sparse_matrix[i_row].features, j, j+1);
sparse_matrix[i_row].features[j].feat_index=i_col;
sparse_matrix[i_row].features[j].entry=static_cast<T>(0);
return sparse_matrix[i_row].features[j].entry;
}
/** load sparse matrix from file
*
* @param loader File object via which to load data
*/
void load(CFile* loader);
/** load sparse matrix from libsvm file together with labels
*
* @param libsvm_file the libsvm file
* @param do_sort_features whether to sort the vector indices (such that they are in
* ascending order) after loading
* @return label vector
*/
SGVector<float64_t> load_with_labels(CLibSVMFile* libsvm_file, bool do_sort_features=true);
/** save sparse matrix to file
*
* @param saver File object via which to save data
*/
void save(CFile* saver);
/** save sparse matrix together with labels to file
*
* @param saver File object via which to save data
* @param labels label vector
*/
void save_with_labels(CLibSVMFile* saver, SGVector<float64_t> labels);
/** return the transposed of the sparse matrix */
SGSparseMatrix<T> get_transposed();
/** create a sparse matrix from a dense one
*
* @param full the dense matrix to create the sparse one from
*/
void from_dense(SGMatrix<T> full);
/** sort the indices of the sparse matrix such that they are in ascending order */
void sort_features();
protected:
/** copy data */
virtual void copy_data(const SGReferencedData& orig);
/** init data */
virtual void init_data();
/** free data */
virtual void free_data();
public:
/// total number of vectors
index_t num_vectors;
/// total number of features
index_t num_features;
/// array of sparse vectors of size num_vectors
SGSparseVector<T>* sparse_matrix;
};
}
#endif // __SGSPARSEMATRIX_H__
|