Apostolos Fanakis
6 years ago
10 changed files with 229 additions and 203 deletions
@ -0,0 +1,93 @@ |
|||||
|
#include "csr_sparse_matrix.h" |
||||
|
|
||||
|
CsrSparseMatrix initCsrSparseMatrix() { |
||||
|
CsrSparseMatrix sparseMatrix; |
||||
|
sparseMatrix.size = 0; |
||||
|
sparseMatrix.numberOfNonZeroElements = 0; |
||||
|
|
||||
|
sparseMatrix.values = NULL; |
||||
|
sparseMatrix.columnIndexes = NULL; |
||||
|
sparseMatrix.rowCumulativeIndexes = NULL; |
||||
|
return sparseMatrix; |
||||
|
} |
||||
|
|
||||
|
void allocMemoryForCsr(CsrSparseMatrix *sparseMatrix, int numberOfElements) { |
||||
|
sparseMatrix->values = (double *) malloc(numberOfElements * sizeof(double)); |
||||
|
sparseMatrix->columnIndexes = (int *) malloc( |
||||
|
numberOfElements * sizeof(int)); |
||||
|
sparseMatrix->rowCumulativeIndexes = (int *) malloc( |
||||
|
(numberOfElements + 1) * sizeof(int)); |
||||
|
|
||||
|
for (int i=0; i<numberOfElements+1; ++i) { |
||||
|
sparseMatrix->rowCumulativeIndexes[i] = 0; |
||||
|
} |
||||
|
sparseMatrix->size = numberOfElements; |
||||
|
} |
||||
|
|
||||
|
// Row indexes start from 0!
|
||||
|
void zeroOutRow(CsrSparseMatrix *sparseMatrix, int row) { |
||||
|
int startIndex = sparseMatrix->rowCumulativeIndexes[row], |
||||
|
endIndex = sparseMatrix->rowCumulativeIndexes[row+1]; |
||||
|
for (int i=startIndex; i<endIndex; ++i) { |
||||
|
sparseMatrix->values[i] = 0; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void zeroOutColumn(CsrSparseMatrix *sparseMatrix, int column) { |
||||
|
for (int i=0; i<sparseMatrix->numberOfNonZeroElements; ++i){ |
||||
|
if(sparseMatrix->columnIndexes[i] == column){ |
||||
|
// Zeros out this element
|
||||
|
sparseMatrix->values[i] = 0; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void csrSparseMatrixVectorMultiplication(CsrSparseMatrix sparseMatrix, |
||||
|
double *vector, double **product, int vectorSize) { |
||||
|
// Initializes the elements of the product vector to zero
|
||||
|
for (int i=0; i<vectorSize; ++i) { |
||||
|
(*product)[i] = 0; |
||||
|
} |
||||
|
|
||||
|
for (int i=0; i<sparseMatrix.size; ++i) { |
||||
|
// Gets start and end indexes of this row's elements
|
||||
|
int startIndex = sparseMatrix.rowCumulativeIndexes[i], |
||||
|
endIndex = sparseMatrix.rowCumulativeIndexes[i+1]; |
||||
|
|
||||
|
if (startIndex == endIndex) { |
||||
|
// This row has no elements
|
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
double sum = 0; |
||||
|
for(int j=startIndex; j<endIndex; ++j){ |
||||
|
int elementColumn = sparseMatrix.columnIndexes[j]; |
||||
|
sum += sparseMatrix.values[j] * vector[elementColumn]; |
||||
|
} |
||||
|
|
||||
|
(*product)[i] = sum; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void destroyCsrSparseMatrix(CsrSparseMatrix *sparseMatrix) { |
||||
|
free(sparseMatrix->values); |
||||
|
free(sparseMatrix->rowCumulativeIndexes); |
||||
|
free(sparseMatrix->columnIndexes); |
||||
|
} |
||||
|
|
||||
|
void printCsrSparseMatrix(CsrSparseMatrix sparseMatrix) { |
||||
|
if (sparseMatrix.size == 0) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
for (int i=0; i<sparseMatrix.size; ++i){ |
||||
|
int startIndex = sparseMatrix.rowCumulativeIndexes[i], |
||||
|
endIndex = sparseMatrix.rowCumulativeIndexes[i+1]; |
||||
|
for(int j=startIndex; j<endIndex; ++j){ |
||||
|
printf("Row [%d] has [%d] nz elements: \n at column[%d] is value = %f \n", |
||||
|
i, endIndex-startIndex, |
||||
|
sparseMatrix.columnIndexes[j], |
||||
|
sparseMatrix.values[j]); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
#ifndef CSR_SPARSE_MATRIX_H /* Include guard */ |
||||
|
#define CSR_SPARSE_MATRIX_H |
||||
|
|
||||
|
#include <stdbool.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
|
||||
|
typedef struct csrSparseMatrix { |
||||
|
int size, numberOfNonZeroElements; |
||||
|
int *rowCumulativeIndexes, *columnIndexes; |
||||
|
double *values; |
||||
|
} CsrSparseMatrix; |
||||
|
|
||||
|
CsrSparseMatrix initCsrSparseMatrix(); |
||||
|
void allocMemoryForCsr(CsrSparseMatrix *sparseMatrix, int numberOfElements); |
||||
|
void zeroOutRow(CsrSparseMatrix *sparseMatrix, int row); |
||||
|
void zeroOutColumn(CsrSparseMatrix *sparseMatrix, int column); |
||||
|
void csrSparseMatrixVectorMultiplication(CsrSparseMatrix sparseMatrix, |
||||
|
double *vector, double **product, int vectorSize); |
||||
|
void destroyCsrSparseMatrix(CsrSparseMatrix *sparseMatrix); |
||||
|
void printCsrSparseMatrix(CsrSparseMatrix sparseMatrix); |
||||
|
|
||||
|
#endif // CSR_SPARSE_MATRIX_H
|
@ -1,76 +0,0 @@ |
|||||
#include "lil_sparse_matrix.h" |
|
||||
|
|
||||
LilSparseMatrix createLilSparseMatrix() { |
|
||||
LilSparseMatrix sparseMatrix; |
|
||||
sparseMatrix.elements = 0; |
|
||||
sparseMatrix.firstElement = NULL; |
|
||||
sparseMatrix.lastElement = NULL; |
|
||||
return sparseMatrix; |
|
||||
} |
|
||||
|
|
||||
void apendElement(LilSparseMatrix *sparseMatrix, double value, int row, int column) { |
|
||||
// Creates the new element
|
|
||||
LilSparseMatrixElement *newElement = (LilSparseMatrixElement *) malloc(sizeof(LilSparseMatrixElement)); |
|
||||
newElement->value = value; |
|
||||
newElement->rowIndex = row; |
|
||||
newElement->columnIndex = column; |
|
||||
newElement->nextElement = NULL; |
|
||||
|
|
||||
if (sparseMatrix->firstElement == NULL) { |
|
||||
// Sparse matrix is empty, this is the first element
|
|
||||
sparseMatrix->firstElement = newElement; |
|
||||
sparseMatrix->lastElement = newElement; |
|
||||
} else { |
|
||||
//Gets last element of the matrix
|
|
||||
LilSparseMatrixElement *lastElement = sparseMatrix->lastElement; |
|
||||
|
|
||||
lastElement->nextElement = newElement; |
|
||||
sparseMatrix->lastElement = newElement; |
|
||||
} |
|
||||
|
|
||||
sparseMatrix->elements = sparseMatrix->elements + 1; |
|
||||
} |
|
||||
|
|
||||
void lilSparseMatrixVectorMultiplication(LilSparseMatrix sparseMatrix, |
|
||||
double *vector, double **product, int vectorSize) { |
|
||||
// Initializes the elements of the product vector to zero
|
|
||||
for (int i=0; i<vectorSize; ++i) { |
|
||||
(*product)[i] = 0; |
|
||||
} |
|
||||
|
|
||||
LilSparseMatrixElement *element = sparseMatrix.firstElement; |
|
||||
for (int i=0; i<sparseMatrix.elements; ++i) { |
|
||||
int row = element->rowIndex, column = element->columnIndex; |
|
||||
|
|
||||
if (row >= vectorSize) { |
|
||||
printf("Error at sparseMatrixVectorMultiplication. Matrix has more rows than vector!\n"); |
|
||||
printf("row = %d\n", row); |
|
||||
exit(EXIT_FAILURE); |
|
||||
} |
|
||||
|
|
||||
(*product)[row] = (*product)[row] + element->value * vector[column]; |
|
||||
element = element->nextElement; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void destroyLilSparseMatrix(LilSparseMatrix *sparseMatrix) { |
|
||||
LilSparseMatrixElement *currentElement = sparseMatrix->firstElement; |
|
||||
while (currentElement != NULL) { |
|
||||
LilSparseMatrixElement *toDelete = currentElement; |
|
||||
currentElement = currentElement->nextElement; |
|
||||
free(toDelete); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void printLilSparseMatrix(LilSparseMatrix sparseMatrix) { |
|
||||
if (sparseMatrix.elements == 0) { |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
LilSparseMatrixElement *currentElement = sparseMatrix.firstElement; |
|
||||
for (int i=0; i<sparseMatrix.elements; ++i) { |
|
||||
printf("[%d,%d] = %f\n", currentElement->rowIndex, |
|
||||
currentElement->columnIndex, currentElement->value); |
|
||||
currentElement = currentElement->nextElement; |
|
||||
} |
|
||||
} |
|
@ -1,29 +0,0 @@ |
|||||
#ifndef LIL_SPARSE_MATRIX_H /* Include guard */ |
|
||||
#define LIL_SPARSE_MATRIX_H |
|
||||
|
|
||||
#include <stdbool.h> |
|
||||
#include <stdlib.h> |
|
||||
#include <stdio.h> |
|
||||
#include <stdlib.h> |
|
||||
|
|
||||
typedef struct lilSparseMatrixElement { |
|
||||
double value; |
|
||||
int rowIndex, columnIndex; |
|
||||
struct lilSparseMatrixElement *nextElement; |
|
||||
} LilSparseMatrixElement; |
|
||||
|
|
||||
typedef struct lilSparseMatrix { |
|
||||
int elements; |
|
||||
LilSparseMatrixElement *firstElement; |
|
||||
LilSparseMatrixElement *lastElement; |
|
||||
} LilSparseMatrix; |
|
||||
|
|
||||
LilSparseMatrix createLilSparseMatrix(); |
|
||||
void apendElement(LilSparseMatrix *sparseMatrix, double value, int row, |
|
||||
int column); |
|
||||
void lilSparseMatrixVectorMultiplication(LilSparseMatrix sparseMatrix, |
|
||||
double *vector, double **product, int vectorSize); |
|
||||
void destroyLilSparseMatrix(LilSparseMatrix *sparseMatrix); |
|
||||
void printLilSparseMatrix(LilSparseMatrix sparseMatrix); |
|
||||
|
|
||||
#endif // LIL_SPARSE_MATRIX_H
|
|
Loading…
Reference in new issue