GenSVM
Functions
gensvm_sparse.c File Reference

Functions for dealing with sparse data matrices. More...

#include "gensvm_sparse.h"
Include dependency graph for gensvm_sparse.c:

Go to the source code of this file.

Functions

struct GenSparsegensvm_init_sparse (void)
 Initialize a GenSparse structure. More...
 
void gensvm_free_sparse (struct GenSparse *sp)
 Free an allocated GenSparse structure. More...
 
long gensvm_count_nnz (double *A, long rows, long cols)
 Count the number of nonzeros in a matrix. More...
 
bool gensvm_nnz_comparison (long nnz, long rows, long cols)
 Compare the number of nonzeros is such that sparsity if worth it. More...
 
bool gensvm_could_sparse (double *A, long rows, long cols)
 Check if it is worthwile to convert to a sparse matrix. More...
 
struct GenSparsegensvm_dense_to_sparse (double *A, long rows, long cols)
 Convert a dense matrix to a GenSparse structure if advantageous. More...
 
double * gensvm_sparse_to_dense (struct GenSparse *A)
 Convert a GenSparse structure to a dense matrix. More...
 

Detailed Description

Functions for dealing with sparse data matrices.

Author
G.J.J. van den Burg
Date
2016-10-11

This file is part of GenSVM.

GenSVM 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.

GenSVM 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with GenSVM. If not, see http://www.gnu.org/licenses/.

Definition in file gensvm_sparse.c.

Function Documentation

◆ gensvm_could_sparse()

bool gensvm_could_sparse ( double *  A,
long  rows,
long  cols 
)

Check if it is worthwile to convert to a sparse matrix.

It is only worth to convert to a sparse matrix if the amount of sparsity is sufficient. For this to be the case, the number of nonzeros must be smaller than $(n_{row}(n_{col} - 1) - 1)/2$. This is tested here. If the amount of nonzero entries is small enough, the function returns the number of nonzeros. If it is too big, it returns -1.

See also
gensvm_nnz_comparison()
Parameters
[in]Amatrix in dense format (RowMajor order)
[in]rowsnumber of rows of A
[in]colsnumber of columns of A
Returns
whether or not sparsity is worth it

Definition at line 129 of file gensvm_sparse.c.

Here is the call graph for this function:

◆ gensvm_count_nnz()

long gensvm_count_nnz ( double *  A,
long  rows,
long  cols 
)

Count the number of nonzeros in a matrix.

This is a utility function to count the number of nonzeros in a dense matrix. This is simply done by comparing with 0.0.

Parameters
[in]Aa dense matrix (RowMajor order)
[in]rowsthe number of rows of A
[in]colsthe number of columns of A
Returns
the number of nonzeros in A

Definition at line 84 of file gensvm_sparse.c.

◆ gensvm_dense_to_sparse()

struct GenSparse* gensvm_dense_to_sparse ( double *  A,
long  rows,
long  cols 
)

Convert a dense matrix to a GenSparse structure if advantageous.

This utility function can be used to convert a dense matrix to a sparse matrix in the form of a GenSparse struture. Note that the allocated memory must be freed by the caller. The user should first check whether using a sparse matrix is worth it by calling gensvm_could_sparse().

Parameters
[in]Aa dense matrix in RowMajor order
[in]rowsnumber of rows of the matrix A
[in]colsnumber of columns of the matrix A
Returns
a GenSparse struct

Definition at line 150 of file gensvm_sparse.c.

Here is the call graph for this function:

◆ gensvm_free_sparse()

void gensvm_free_sparse ( struct GenSparse sp)

Free an allocated GenSparse structure.

Simply free a previously allocated GenSparse structure by freeing all of its components. Finally, the structure itself is freed, and the pointer is set to NULL for safety.

Parameters
[in]spGenSparse structure to free

Definition at line 62 of file gensvm_sparse.c.

◆ gensvm_init_sparse()

struct GenSparse* gensvm_init_sparse ( void  )

Initialize a GenSparse structure.

A GenSparse structure is used to hold a sparse data matrix. We work with Compressed Row Storage (CSR) storage, also known as old Yale format.

Returns
initialized GenSparse instance

Definition at line 38 of file gensvm_sparse.c.

◆ gensvm_nnz_comparison()

bool gensvm_nnz_comparison ( long  nnz,
long  rows,
long  cols 
)

Compare the number of nonzeros is such that sparsity if worth it.

This is a utility function, see gensvm_could_sparse() for more info.

Parameters
[in]nnznumber of nonzero elements
[in]rowsnumber of rows
[in]colsnumber of columns
Returns
whether or not sparsity is worth it

Definition at line 105 of file gensvm_sparse.c.

◆ gensvm_sparse_to_dense()

double* gensvm_sparse_to_dense ( struct GenSparse A)

Convert a GenSparse structure to a dense matrix.

This function converts a GenSparse structure back to a normal dense matrix in RowMajor order. Note that the allocated memory must be freed by the caller.

Parameters
[in]Aa GenSparse structure
Returns
a dense matrix

Definition at line 199 of file gensvm_sparse.c.