34 #include "SparseMatrix.hpp"
35 #include "Triplet.hpp"
50 template<
typename T >
61 std::vector< std::vector< Triplet< T > > >
ds;
67 if ( one.
j() < two.
j() )
69 if ( one.
j() > two.
j() )
78 if ( one.
i() < two.
i() )
80 if ( one.
i() > two.
i() )
94 std::vector< Triplet< double > >::iterator it =
ds.at( col_index ).begin();
96 while( it!=
ds.get( col_index ).end() ) {
98 if( cur.
i() == row_index ) {
104 std::vector< Triplet< double > >::iterator it =
ds.get( row_index ).begin();
106 while( it!=
ds.get( row_index ).end() ) {
108 if( cur.
j() == col_index ) {
126 SVM( std::string file, T zero = 0 ) {
137 SVM(
const ULI number_of_nonzeros,
const ULI number_of_rows,
const ULI number_of_cols,
const T zero ):
138 SparseMatrix< T, ULI >( number_of_nonzeros, number_of_rows, number_of_cols, zero ) {}
149 for( ULI i=0; i<(
major?this->
noc:this->
nor); i++ ) {
150 std::vector< Triplet< double > > toAdd;
151 std::vector< Triplet< double > >::iterator it = toCopy.
ds.get( i ).begin();
152 while( it!=toCopy.
ds.get( i ).end() ) toAdd.push_back( *(it++) );
153 ds.push_back( toAdd );
168 SVM(
const std::vector<
Triplet< T > >& input,
const ULI
m,
const ULI
n,
const T zero,
const char direction = 0 ) {
169 load( input, m, n, zero, direction );
177 load( input, m, n, zero, 0 );
191 void load(
const std::vector<
Triplet< T > >& input,
const ULI
m,
const ULI
n,
const T zero,
const char direction ) {
193 this->
nnz = input.size();
200 typename std::vector< Triplet< T > >::const_iterator in_it = input.begin();
201 for( ; in_it != input.end(); in_it++ ) {
203 const ULI currow =
major ? cur.
j() : cur.
i();
204 const T value = cur.
value;
206 ds.at( currow ).push_back( cur );
210 for( ULI currow = 0; currow < this->
nor; currow++ ) {
211 if(
ds.at( currow ).size() == 0 )
continue;
212 qsort( &(
ds.at( currow )[ 0 ] ),
ds.at( currow ).size(),
sizeof(
Triplet< T > ),
220 std::vector< std::vector< Triplet< double > > >&
getData() {
232 if (
find( i, j, ret ) ) {
240 row =
ds[0].at(0).i();
241 col =
ds[0].at(0).j();
250 virtual void zxa(
const T*__restrict__ x, T*__restrict__ z ) {
253 for( col = 0; col < this->
noc; col++, z++ ) {
254 std::vector< Triplet< double > >::iterator it =
ds.at( col ).begin();
255 while( it!=
ds.at(col).end() ) {
257 *z += cur.
value * x[ cur.
i() ];
262 for( row = 0; row < this->
nor; row++, x++ ) {
263 std::vector< Triplet< double > >::iterator it =
ds.at( row ).begin();
264 while( it!=
ds.at( row ).end() ) {
266 z[ cur.
j() ] += cur.
value * *x;
278 virtual void zax(
const T*__restrict__ x, T*__restrict__ z ) {
281 for( col = 0; col < this->
noc; col++, x++ ) {
282 std::vector< Triplet< double > >::iterator it =
ds.at( col ).begin();
283 while( it!=
ds.at(col).end() ) {
285 z[ cur.
i() ] = cur.
value * *x;
290 for( row = 0; row < this->
nor; row++, z++ ) {
291 std::vector< Triplet< double > >::iterator it =
ds.at( row ).begin();
292 while( it!=
ds.at( row ).end() ) {
294 *z += cur.
value * x[ cur.
j() ];
302 return sizeof( ULI ) * 2 * this->
nnz +
sizeof( T ) * this->
nnz +
sizeof(
void* ) * this->
nor;
ULI nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
SVM(const std::vector< Triplet< T > > &input, const ULI m, const ULI n, const T zero, const char direction=0)
Constructor which transforms a collection of input triplets to SVM format.
Definition: SVM.hpp:168
~SVM()
Base deconstructor.
Definition: SVM.hpp:306
SVM(const ULI number_of_nonzeros, const ULI number_of_rows, const ULI number_of_cols, const T zero)
Base constructor which only initialises the internal arrays.
Definition: SVM.hpp:137
static int compareTripletsR(const void *left, const void *right)
Sorts 1D columnwise.
Definition: SVM.hpp:64
ULI i() const
Definition: Triplet.hpp:70
virtual unsigned long int m()
Queries the number of rows this matrix contains.
Definition: SparseMatrix.hpp:107
std::vector< std::vector< Triplet< double > > > & getData()
Definition: SVM.hpp:220
static int compareTripletsC(const void *left, const void *right)
Sorts 1D rowwise.
Definition: SVM.hpp:75
virtual void load(std::vector< Triplet< T > > &input, const ULI m, const ULI n, const T zero)
This will default to row-major SVM format.
Definition: SVM.hpp:176
void loadFromFile(const std::string file, const T zero=0)
Function which loads a matrix from a matrix market file.
Definition: SparseMatrix.hpp:89
Interface common to all sparse matrix storage schemes.
Definition: SparseMatrix.hpp:46
SVM(SVM< T > &toCopy)
Copy constructor.
Definition: SVM.hpp:143
std::vector< std::vector< Triplet< T > > > ds
SVM structure.
Definition: SVM.hpp:61
SVM(std::string file, T zero=0)
Base constructor.
Definition: SVM.hpp:126
bool find(const ULI col_index, const ULI row_index, Triplet< double > &ret)
Helper function which finds a value with a given index.
Definition: SVM.hpp:92
ULI noc
Number of columns.
Definition: SparseMatrix.hpp:55
virtual void zax(const T *__restrict__ x, T *__restrict__ z)
In-place z=Ax function.
Definition: SVM.hpp:278
virtual void zxa(const T *__restrict__ x, T *__restrict__ z)
In-place z=xA function.
Definition: SVM.hpp:250
SVM()
Base constructor.
Definition: SVM.hpp:120
virtual void getFirstIndexPair(ULI &row, ULI &col)
Returns the first nonzero index, per reference.
Definition: SVM.hpp:239
The sparse vector matrix format.
Definition: SVM.hpp:51
void load(const std::vector< Triplet< T > > &input, const ULI m, const ULI n, const T zero, const char direction)
Will load a collection of triplets into SVM format.
Definition: SVM.hpp:191
virtual size_t bytesUsed()
Function to query the amount of storage required by this sparse matrix.
Definition: SVM.hpp:300
T value
Value stored at this triplet.
Definition: Triplet.hpp:95
char major
Row major (0), column major (1)
Definition: SVM.hpp:58
T & random_access(ULI i, ULI j)
Method which provides random matrix access to the stored sparse matrix.
Definition: SVM.hpp:230
ULI nor
Number of rows.
Definition: SparseMatrix.hpp:52
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
ULI j() const
Definition: Triplet.hpp:73
virtual unsigned long int n()
Queries the number of columns this matrix contains.
Definition: SparseMatrix.hpp:115
A single triplet value.
Definition: Triplet.hpp:52