Logo AND Algorithmique Numérique Distribuée

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