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.
15 typedef double matrix_type;
17 typedef struct _matrix_elem_t {
22 typedef struct _sp_matrix_t sp_matrix_t;
25 * Allocate a new matrix and init internal data for a matrix of size
26 * row_init X col_init. Matrix cannot grow beyond these init values.
27 * All elements are initially (implicit) set to 0.
29 sp_matrix_t *new_matrix(int rows, int cols);
32 * Free space used by matrix m
34 void del_matrix(sp_matrix_t *m);
37 * Sets m[row, col] to val
39 void matrix_set(sp_matrix_t *m, int row, int col, matrix_type val);
42 * Returns the value stored in m[row, col].
44 matrix_type matrix_get(const sp_matrix_t *m, int row, int col);
47 * Returns the number of (not-0-)entries.
49 int matrix_get_entries(const sp_matrix_t *m);
52 * Returns the number of rows in this matrix; the height; the first dimension
54 int matrix_get_rowcount(const sp_matrix_t *m);
57 * Returns the number of cols in this matrix; the width; the second dimension
59 int matrix_get_rowcount(const sp_matrix_t *m);
62 * Start iteration over all matrix elements. Row by row, from top to bottom.
63 * @return NULL if the matrix is empty, else the first element.
65 const matrix_elem_t *matrix_first(sp_matrix_t *m);
68 * Start iteratation over a row. Elements are returned from left to right.
69 * @return NULL if row is empty, else the first element.
71 const matrix_elem_t *matrix_row_first(sp_matrix_t *m, int row);
74 * Start iteratation over a column. Elements are returned from top to bottom.
75 * @return NULL if column is empty, else the first element.
77 const matrix_elem_t *matrix_col_first(sp_matrix_t *m, int row);
80 * @return the next element in iteration order or NULL if iteration is done.
82 const matrix_elem_t *matrix_next(sp_matrix_t *m);
86 * curr The variable to assign all elements to during iteration
87 * Save against removal of curr
89 #define matrix_foreach(m,curr) \
90 for (curr = matrix_first(m); curr; curr = matrix_next(m))
95 * curr The variable to assign all elements to during iteration
96 * Save against removal of curr
98 #define matrix_foreach_in_row(m,r,curr) \
99 for (curr = matrix_row_first(m, r); curr; curr = matrix_next(m))
104 * curr The variable to assign all elements to during iteration
105 * Save against removal of curr
107 #define matrix_foreach_in_col(m,c,curr) \
108 for (curr = matrix_col_first(m, c); curr; curr = matrix_next(m))
111 * Changes the matrix into an equivalent one with maximal number zero-rows.
112 * The only equivalence transformation is:
113 * Adding a constant to Qij and substracting it from Qji
115 void matrix_optimize(sp_matrix_t *m);
118 * Dumps the matrix factor*m to the stream @p out.
119 * Remark: I dont need spaces between the elements. So feel free to add
120 * char *seperator to the arguments.
122 void matrix_dump(sp_matrix_t *m, FILE *out, int factor);
125 * Perform a self test with a sqare matrix of dimensions d.
127 void matrix_self_test(int d);