Logo AND Algorithmique Numérique Distribuée

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