Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill old $Id$ command dating from CVS
[simgrid.git] / src / xbt / xbt_matrix.c
1 /* xbt_matrix_t management functions                                        */
2
3 /* Copyright (c) 2006 Martin Quinson. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdio.h>
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/matrix.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_matrix, xbt, "2D data storage");
14
15 /** \brief constructor */
16 xbt_matrix_t xbt_matrix_new(int lines, int rows,
17                             const unsigned long elmsize,
18                             void_f_pvoid_t const free_f)
19 {
20   xbt_matrix_t res = xbt_new(s_xbt_matrix_t, 1);
21   res->lines = lines;
22   res->rows = rows;
23   res->elmsize = elmsize;
24   res->free_f = free_f;
25   res->data = xbt_malloc(elmsize * lines * rows);
26   return res;
27 }
28
29 /** \brief Creates a matrix being a submatrix of another one */
30 xbt_matrix_t xbt_matrix_new_sub(xbt_matrix_t from,
31                                 int lsize, int rsize,
32                                 int lpos, int rpos,
33                                 pvoid_f_pvoid_t const cpy_f)
34 {
35
36   xbt_matrix_t res = xbt_matrix_new(lsize, rsize,
37                                     from->elmsize, from->free_f);
38   xbt_matrix_copy_values(res, from, lsize, rsize, 0, 0, lpos, rpos, cpy_f);
39   return res;
40 }
41
42 /** \brief destructor */
43 void xbt_matrix_free(xbt_matrix_t mat)
44 {
45   unsigned int i;
46   if (mat) {
47     if (mat->free_f) {
48       for (i = 0; i < (mat->lines * mat->rows); i++) {
49         (*(mat->free_f)) ((void *) &(mat->data[i * mat->elmsize]));
50       }
51     }
52     free(mat->data);
53     free(mat);
54   }
55 }
56
57 /** \brief Freeing function for containers of xbt_matrix_t */
58 void xbt_matrix_free_voidp(void *d)
59 {
60   xbt_matrix_free((xbt_matrix_t) * (void **) d);
61 }
62
63
64 /** \brief Display the content of a matrix (debugging purpose)
65  *  \param coords: boolean indicating whether we should add the coords of each cell to the output*/
66 void xbt_matrix_dump(xbt_matrix_t matrix, const char *name, int coords,
67                      void_f_pvoid_t display_fun)
68 {
69   unsigned int i, j;
70
71   fprintf(stderr, ">>> Matrix %s dump (%d x %d)\n", name, matrix->lines,
72           matrix->rows);
73   for (i = 0; i < matrix->lines; i++) {
74     fprintf(stderr, "  ");
75     for (j = 0; j < matrix->rows; j++) {
76       if (coords)
77         fprintf(stderr, " (%d,%d)=", i, j);
78       else
79         fprintf(stderr, " ");
80       (*display_fun) (xbt_matrix_get_ptr(matrix, i, j));
81     }
82     fprintf(stderr, "\n");
83   }
84   fprintf(stderr, "<<< end_of_matrix %s dump\n", name);
85 }
86
87 void xbt_matrix_dump_display_double(void *d)
88 {
89   fprintf(stderr, "%.2f", *(double *) d);
90 }
91
92 /** \brief Copy the values from the matrix src into the matrix dst
93  * \param dest: destination
94  * \param src: source
95  * \param lsize: number of lines to copy
96  * \param rsize: number of rows to copy
97  * \param lpos_dst: line offset on destination matrix
98  * \param rpos_dst: row offset on destination matrix
99  * \param lpos_src: line offset on destination matrix
100  * \param rpos_src: row offset on destination matrix
101  */
102 void xbt_matrix_copy_values(xbt_matrix_t dst, xbt_matrix_t src,
103                             unsigned int lsize, unsigned int rsize,
104                             unsigned int lpos_dst, unsigned int rpos_dst,
105                             unsigned int lpos_src, unsigned int rpos_src,
106                             pvoid_f_pvoid_t const cpy_f)
107 {
108   unsigned int i, j;
109
110   DEBUG10("Copy a %dx%d submatrix from %dx%d(of %dx%d) to %dx%d (of %dx%d)",
111           lsize, rsize,
112           lpos_src, rpos_src, src->lines, src->rows,
113           lpos_dst, rpos_dst, dst->lines, dst->rows);
114
115   /* everybody knows that issue is between the chair and the screen (particulary in my office) */
116   xbt_assert(src->elmsize == dst->elmsize);
117   /* don't check free_f since the user may play weird games with this */
118
119   xbt_assert(lpos_src + lsize <= src->lines);
120   xbt_assert(rpos_src + rsize <= src->rows);
121
122   xbt_assert(lpos_dst + lsize <= dst->lines);
123   xbt_assert(rpos_dst + rsize <= dst->rows);
124
125   /* Lets get serious here */
126   for (i = 0; i < rsize; i++) {
127     if (cpy_f) {
128       for (j = 0; j < lsize; j++)
129         xbt_matrix_get_as(dst, j + lpos_dst, i + rpos_dst, void *) =
130           (*cpy_f) (xbt_matrix_get_ptr(src, j + rpos_src, i + lpos_src));
131     } else {
132       memcpy(xbt_matrix_get_ptr(dst, lpos_dst, i + rpos_dst),
133              xbt_matrix_get_ptr(src, lpos_src, i + rpos_src),
134              dst->elmsize * lsize);
135     }
136   }
137
138 }
139
140 /** \brief Creates a new matrix of double filled with zeros */
141 xbt_matrix_t xbt_matrix_double_new_zeros(int lines, int rows)
142 {
143   xbt_matrix_t res = xbt_matrix_new(lines, rows, sizeof(double), NULL);
144
145   memset(res->data, 0, res->elmsize * res->lines * res->rows);
146   return res;
147 }
148
149 /** \brief Creates a new matrix of double being the identity matrix */
150 xbt_matrix_t xbt_matrix_double_new_id(int lines, int rows)
151 {
152   xbt_matrix_t res = xbt_matrix_double_new_zeros(lines, rows);
153   int i;
154
155   for (i = 0; i < lines; i++)
156     xbt_matrix_get_as(res, i, i, double) = 1;
157   return res;
158 }
159
160 /** \brief Creates a new matrix of double randomly filled */
161 xbt_matrix_t xbt_matrix_double_new_rand(int lines, int rows)
162 {
163   xbt_matrix_t res = xbt_matrix_new(lines, rows, sizeof(double), NULL);
164   int i, j;
165
166   for (i = 0; i < lines; i++)
167     for (j = 0; j < rows; j++)
168       xbt_matrix_get_as(res, i, j, double) = (double) rand();
169   return res;
170 }
171
172 /** \brief Creates a new matrix of double containing the sequence of numbers in order */
173 xbt_matrix_t xbt_matrix_double_new_seq(int lines, int rows)
174 {
175   xbt_matrix_t res = xbt_matrix_new(lines, rows, sizeof(double), NULL);
176   int i;
177
178   for (i = 0; i < lines * rows; i++)
179     *(double *) &res->data[i * res->elmsize] = i;
180
181   return res;
182 }
183 /** \brief Checks whether the matrix contains the sequence of numbers */
184 int xbt_matrix_double_is_seq(xbt_matrix_t mat) {
185   int i;
186
187   for (i = 0; i < mat->lines * mat->rows; i++) {
188     double val = xbt_matrix_get_as(mat,i,0,double);
189     if (val != i)
190       return 0;
191   }
192
193   return 1;
194 }
195
196 /** \brief Creates a new matrix being the multiplication of two others */
197 xbt_matrix_t xbt_matrix_double_new_mult(xbt_matrix_t A, xbt_matrix_t B)
198 {
199   xbt_matrix_t result = xbt_matrix_double_new_zeros(A->lines, B->rows);
200
201   xbt_matrix_double_addmult(A, B, result);
202   return result;
203 }
204
205 /** \brief add to C the result of A*B */
206 void xbt_matrix_double_addmult(xbt_matrix_t A, xbt_matrix_t B,
207                                /*OUT*/ xbt_matrix_t C)
208 {
209   unsigned int i, j, k;
210
211   xbt_assert2(A->lines == C->lines,
212               "A->lines != C->lines (%d vs %d)", A->lines, C->lines);
213   xbt_assert(B->rows == C->rows);
214
215   for (i = 0; i < C->lines; i++)
216     for (j = 0; j < C->rows; j++)
217       for (k = 0; k < B->lines; k++)
218         xbt_matrix_get_as(C, i, j, double) +=
219           xbt_matrix_get_as(A, i, k, double) * xbt_matrix_get_as(B, k, j,
220                                                                  double);
221 }