4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
7 * Sparse matrix storage with linked lists for rows and cols.
8 * I did not need floats, so this is all integer.
16 typedef struct _matrix_elem_t {
20 typedef struct _sp_matrix_t sp_matrix_t;
23 * Allocate a new matrix and init internal data for a matrix of size
24 * row_init X col_init. Matrix cannot grow beyond these init values.
25 * All elements are initially (implicit) set to 0.
27 sp_matrix_t *new_matrix(int rows, int cols);
30 * Free space used by matrix m
32 void del_matrix(sp_matrix_t *m);
35 * Sets m[row, col] to val
37 void matrix_set(sp_matrix_t *m, int row, int col, int val);
40 * Returns the value stored in m[row, col].
42 int matrix_get(const sp_matrix_t *m, int row, int col);
45 * Returns the number of (not-0-)entries.
47 int matrix_get_entries(const sp_matrix_t *m);
50 * Returns the number of rows in this matrix; the height; the first dimension
52 int matrix_get_rowcount(const sp_matrix_t *m);
55 * Returns the number of cols in this matrix; the width; the second dimension
57 int matrix_get_rowcount(const sp_matrix_t *m);
60 * Start iteration over all matrix elements. Row by row, from top to bottom.
61 * @return NULL if the matrix is empty, else the first element.
63 const matrix_elem_t *matrix_first(sp_matrix_t *m);
66 * Start iteratation over a row. Elements are returned from left to right.
67 * @return NULL if row is empty, else the first element.
69 const matrix_elem_t *matrix_row_first(sp_matrix_t *m, int row);
72 * Start iteratation over a column. Elements are returned from top to bottom.
73 * @return NULL if column is empty, else the first element.
75 const matrix_elem_t *matrix_col_first(sp_matrix_t *m, int row);
78 * @return the next element in iteration order or NULL if iteration is done.
80 const matrix_elem_t *matrix_next(sp_matrix_t *m);
84 * curr The variable to assign all elements to during iteration
85 * Save against removal of curr
87 #define matrix_foreach(m,curr) \
88 for (curr = matrix_first(m); curr; curr = matrix_next(m))
93 * curr The variable to assign all elements to during iteration
94 * Save against removal of curr
96 #define matrix_foreach_in_row(m,r,curr) \
97 for (curr = matrix_row_first(m, r); curr; curr = matrix_next(m))
102 * curr The variable to assign all elements to during iteration
103 * Save against removal of curr
105 #define matrix_foreach_in_col(m,c,curr) \
106 for (curr = matrix_col_first(m, c); curr; curr = matrix_next(m))
109 * Changes the matrix into an equivalent one with maximal number zero-rows.
110 * The only equivalence transformation is:
111 * Adding a constant to Qij and substracting it from Qji
113 void matrix_optimize(sp_matrix_t *m);
116 * Dumps the matrix factor*m to the stream @p out.
117 * Remark: I dont need spaces between the elements. So feel free to add
118 * char *seperator to the arguments.
120 void matrix_dump(sp_matrix_t *m, FILE *out, int factor);
123 * Perform a self test with a sqare matrix of dimensions d.
125 void matrix_self_test(int d);