-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSR_Matrix.h
More file actions
61 lines (50 loc) · 2.08 KB
/
CSR_Matrix.h
File metadata and controls
61 lines (50 loc) · 2.08 KB
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
#ifndef CSR_MATRIX_H
#define CSR_MATRIX_H
#include <stdbool.h>
#include <stddef.h>
/* CSR (Compressed Sparse Row) Matrix Format
*
* For an m x n matrix with nnz nonzeros:
* - p: array of size (m + 1) indicating start of each row
* - i: array of size nnz containing column indices
* - x: array of size nnz containing values
* - m: number of rows
* - n: number of columns
* - nnz: number of nonzero entries
*/
typedef struct CSR_Matrix
{
int *p;
int *i;
double *x;
int m;
int n;
int nnz;
} CSR_Matrix;
/* constructors and destructors.
If mem is non-NULL, *mem is incremented by the bytes allocated. */
CSR_Matrix *new_csr_matrix(int m, int n, int nnz, size_t *mem);
CSR_Matrix *new_csr(const CSR_Matrix *A, size_t *mem);
CSR_Matrix *new_csr_copy_sparsity(const CSR_Matrix *A, size_t *mem);
void free_csr_matrix(CSR_Matrix *matrix);
void copy_csr_matrix(const CSR_Matrix *A, CSR_Matrix *C);
/* transpose functionality (iwork must be of size A->n) */
CSR_Matrix *transpose(const CSR_Matrix *A, int *iwork);
CSR_Matrix *AT_alloc(const CSR_Matrix *A, int *iwork, size_t *mem);
void AT_fill_values(const CSR_Matrix *A, CSR_Matrix *AT, int *iwork);
/* computes dense y = Ax */
void Ax_csr(const CSR_Matrix *A, const double *x, double *y, int col_offset);
/* fills values of C = diag(d) @ A */
void DA_fill_values(const double *d, const CSR_Matrix *A, CSR_Matrix *C);
/* Count number of columns with nonzero entries in A and marks them in col_nz */
int count_nonzero_cols(const CSR_Matrix *A, bool *col_nz);
/* inserts 'idx' into array 'arr' in sorted order, and moves the other elements */
void insert_idx(int idx, int *arr, int len);
/* get value at position (row, col) in A */
double csr_get_value(const CSR_Matrix *A, int row, int col);
/* Returns total bytes used by p, i, x arrays (0 if A is NULL) */
size_t csr_bytes(const CSR_Matrix *A);
/* Expand symmetric CSR matrix A to full matrix C. A is assumed to store
only upper triangle. C must be pre-allocated with sufficient nnz */
void symmetrize_csr(const int *Ap, const int *Ai, int m, CSR_Matrix *C);
#endif /* CSR_MATRIX_H */