2 * Sparse matrix storage with linked lists for rows and cols.
3 * I did not need floats, so this is all integer.
13 typedef struct _matrix_elem_t {
17 typedef struct _sp_matrix_t sp_matrix_t;
20 * Allocate a new matrix and init internal data for a matrix of size
21 * row_init X col_init. Matrix cannot grow beyond these init values.
22 * All elements are initially (implicit) set to 0.
24 sp_matrix_t *new_matrix(int rows, int cols);
27 * Free space used by matrix m
29 void del_matrix(sp_matrix_t *m);
32 * Sets m[row, col] to val
34 void matrix_set(sp_matrix_t *m, int row, int col, int val);
37 * Returns the value stored in m[row, col].
39 int matrix_get(const sp_matrix_t *m, int row, int col);
42 * Returns the number of (not-0-)entries.
44 int matrix_get_entries(const sp_matrix_t *m);
47 * Returns the number of rows in this matrix; the height; the first dimension
49 int matrix_get_rowcount(const sp_matrix_t *m);
52 * Returns the number of cols in this matrix; the width; the second dimension
54 int matrix_get_rowcount(const sp_matrix_t *m);
57 * Start iteration over all matrix elements. Row by row, from top to bottom.
58 * @return NULL if the matrix is empty, else the first element.
60 const matrix_elem_t *matrix_first(sp_matrix_t *m);
63 * Start iteratation over a row. Elements are returned from left to right.
64 * @return NULL if row is empty, else the first element.
66 const matrix_elem_t *matrix_row_first(sp_matrix_t *m, int row);
69 * Start iteratation over a column. Elements are returned from top to bottom.
70 * @return NULL if column is empty, else the first element.
72 const matrix_elem_t *matrix_col_first(sp_matrix_t *m, int row);
75 * @return the next element in iteration order or NULL if iteration is done.
77 const matrix_elem_t *matrix_next(sp_matrix_t *m);
81 * curr The variable to assign all elements to during iteration
82 * Save against removal of curr
84 #define matrix_foreach(m,curr) \
85 for (curr = matrix_first(m); curr; curr = matrix_next(m))
90 * curr The variable to assign all elements to during iteration
91 * Save against removal of curr
93 #define matrix_foreach_in_row(m,r,curr) \
94 for (curr = matrix_row_first(m, r); curr; curr = matrix_next(m))
99 * curr The variable to assign all elements to during iteration
100 * Save against removal of curr
102 #define matrix_foreach_in_col(m,c,curr) \
103 for (curr = matrix_col_first(m, c); curr; curr = matrix_next(m))
106 * Changes the matrix into an equivalent one with maximal number zero-rows.
107 * The only equivalence transformation is:
108 * Adding a constant to Qij and substracting it from Qji
110 void matrix_optimize(sp_matrix_t *m);
113 * Dumps the matrix factor*m to the stream @p out.
114 * Remark: I dont need spaces between the elements. So feel free to add
115 * char *seperator to the arguments.
117 void matrix_dump(sp_matrix_t *m, FILE *out, int factor);
120 * Perform a self test with a sqare matrix of dimensions d.
122 void matrix_self_test(int d);