7 #include "gaussseidel.h"
11 * The number of newly allocated rows (realloc)
12 * when there is no more room. Must be >= 1.
14 #define ROW_INCREASE_FACTOR 1.2
17 * The number of newly allocated cols (realloc)
18 * when there is no more room. Must be >= 1.
20 #define COL_INCREASE 2
22 typedef struct col_val_t {
27 typedef struct row_col_t {
35 int initial_col_increase;
37 int n_zero_entries; ///< Upper bound on number of entries equal to 0.0
41 static inline void alloc_cols(row_col_t *row, int c_cols)
43 assert(c_cols > row->c_cols);
45 row->cols = XREALLOC(row->cols, col_val_t, c_cols);
48 static inline void alloc_rows(gs_matrix_t *m, int c_rows, int c_cols, int begin_init)
51 assert(c_rows > m->c_rows);
54 m->rows = XREALLOC(m->rows, row_col_t, c_rows);
56 for (i = begin_init; i < c_rows; ++i) {
57 m->rows[i].c_cols = 0;
58 m->rows[i].n_cols = 0;
59 m->rows[i].diag = 0.0;
60 m->rows[i].cols = NULL;
62 alloc_cols(&m->rows[i], c_cols);
66 gs_matrix_t *gs_new_matrix(int n_init_rows, int n_init_cols)
68 gs_matrix_t *res = XMALLOCZ(gs_matrix_t);
71 res->initial_col_increase = n_init_cols;
72 alloc_rows(res, n_init_rows, n_init_cols, 0);
76 void gs_delete_matrix(gs_matrix_t *m)
79 for (i = 0; i < m->c_rows; ++i) {
80 if (m->rows[i].c_cols)
81 xfree(m->rows[i].cols);
88 unsigned gs_matrix_get_n_entries(const gs_matrix_t *m)
91 unsigned n_entries = 0;
93 for (i = 0; i < m->c_rows; ++i) {
94 n_entries += m->rows[i].n_cols;
95 n_entries += (m->rows[i].diag != 0.0) ? 1 : 0;
98 return n_entries - m->n_zero_entries;
101 int gs_matrix_get_sizeof_allocated_memory(const gs_matrix_t *m)
103 int i, n_col_val_ts = 0;
104 for (i = 0; i < m->c_rows; ++i)
105 n_col_val_ts += m->rows[i].c_cols;
107 return n_col_val_ts * sizeof(col_val_t) + m->c_rows * sizeof(row_col_t) + sizeof(gs_matrix_t);
110 void gs_matrix_assure_row_capacity(gs_matrix_t *m, int row, int min_capacity)
112 row_col_t *the_row = &m->rows[row];
113 if (the_row->c_cols < min_capacity)
114 alloc_cols(the_row, min_capacity);
117 void gs_matrix_trim_row_capacities(gs_matrix_t *m)
120 for (i = 0; i < m->c_rows; ++i) {
121 row_col_t *the_row = &m->rows[i];
122 if (the_row->c_cols) {
123 the_row->c_cols = the_row->n_cols;
125 the_row->cols = XREALLOC(the_row->cols, col_val_t, the_row->c_cols);
127 xfree(the_row->cols);
132 void gs_matrix_delete_zero_entries(gs_matrix_t *m)
135 for (i = 0; i < m->c_rows; ++i) {
136 row_col_t *the_row = &m->rows[i];
139 for (read_pos = 0; read_pos < the_row->n_cols; ++read_pos)
140 if (the_row->cols[read_pos].v != 0.0 && read_pos != write_pos)
141 the_row->cols[write_pos++] = the_row->cols[read_pos];
143 the_row->n_cols = write_pos;
145 m->n_zero_entries = 0;
148 void gs_matrix_set(gs_matrix_t *m, int row, int col, double val)
154 if (row >= m->c_rows) {
155 int new_c_rows = (int)(ROW_INCREASE_FACTOR * row);
156 alloc_rows(m, new_c_rows, m->initial_col_increase, m->c_rows);
159 the_row = &m->rows[row];
162 /* Note that we store the diagonal inverted to turn divisions to mults in
163 * matrix_gauss_seidel(). */
165 the_row->diag = 1.0 / val;
169 // Search for correct column
170 cols = the_row->cols;
172 max = the_row->n_cols;
175 int idx = cols[c].col_idx;
185 // Have we found the entry?
186 if (c < the_row->n_cols && the_row->cols[c].col_idx == col) {
187 the_row->cols[c].v = val;
193 // We haven't found the entry, so we must create a new one.
194 // Is there enough space?
195 if (the_row->c_cols == the_row->n_cols)
196 alloc_cols(the_row, the_row->c_cols + COL_INCREASE);
198 // Shift right-most entries to the right by one
199 for (i = the_row->n_cols; i > c; --i)
200 the_row->cols[i] = the_row->cols[i-1];
202 // Finally insert the new entry
204 the_row->cols[c].col_idx = col;
205 the_row->cols[c].v = val;
207 // Check that the entries are sorted
208 assert(c==0 || the_row->cols[c-1].col_idx < the_row->cols[c].col_idx);
209 assert(c>=the_row->n_cols-1 || the_row->cols[c].col_idx < the_row->cols[c+1].col_idx);
212 double gs_matrix_get(const gs_matrix_t *m, int row, int col)
217 if (row >= m->c_rows)
220 the_row = &m->rows[row];
223 return the_row->diag != 0.0 ? 1.0 / the_row->diag : 0.0;
225 // Search for correct column
226 for (c = 0; c < the_row->n_cols && the_row->cols[c].col_idx < col; ++c) {
229 if (c >= the_row->n_cols || the_row->cols[c].col_idx > col)
232 assert(the_row->cols[c].col_idx == col);
233 return the_row->cols[c].v;
236 /* NOTE: You can slice out miss_rate and weights.
237 * This does ONE step of gauss_seidel. Termination must be checked outside!
238 * This solves m*x=0. You must add stuff for m*x=b. See wikipedia german and english article. Should be simple.
239 * param a is the number of rows in the matrix that should be considered.
241 * Note that the diagonal element is stored separately in this matrix implementation.
243 double gs_matrix_gauss_seidel(const gs_matrix_t *m, double *x, int n)
248 assert(n <= m->c_rows);
250 for (r = 0; r < n; ++r) {
251 row_col_t *row = &m->rows[r];
252 col_val_t *cols = row->cols;
257 for (c = 0; c < row->n_cols; ++c) {
258 int col_idx = cols[c].col_idx;
259 sum += cols[c].v * x[col_idx];
263 nw = - sum * row->diag;
264 // nw = old - overdrive * (old + sum * row->diag);
265 res += fabs(old - nw);
272 void gs_matrix_export(const gs_matrix_t *m, double *nw, int size)
274 int effective_rows = MIN(size, m->c_rows);
277 memset(nw, 0, size * size * sizeof(*nw));
278 for (r=0; r < effective_rows; ++r) {
279 row_col_t *row = &m->rows[r];
282 assert(row->diag != 0.0);
283 nw[base + r] = 1.0 / row->diag;
284 for (c = 0; c < row->n_cols; ++c) {
285 int col_idx = row->cols[c].col_idx;
286 nw[base + col_idx] = row->cols[c].v;
291 void gs_matrix_dump(const gs_matrix_t *m, int a, int b, FILE *out)
293 int effective_rows = MIN(a, m->c_rows);
295 double *elems = XMALLOCN(double, b);
297 // The rows which have some content
298 for (r=0; r < effective_rows; ++r) {
299 row_col_t *row = &m->rows[r];
301 memset(elems, 0, b * sizeof(*elems));
303 for (c = 0; c < row->n_cols; ++c) {
304 int col_idx = row->cols[c].col_idx;
305 elems[col_idx] = row->cols[c].v;
307 elems[r] = row->diag != 0.0 ? 1.0 / row->diag : 0.0;
309 for (i = 0; i < b; ++i)
311 fprintf(out, "%+4.4f ", elems[i]);
317 // Append 0-rows to fit height of matrix
318 for (r=effective_rows; r < a; ++r) {
319 for (c=0; c < b; ++c)