1 /* xbt_matrix_t management functions */
3 /* Copyright (c) 2006 Martin Quinson. All rights reserved. */
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. */
9 #include "xbt/sysdep.h"
11 #include "xbt/matrix.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_matrix, xbt, "2D data storage");
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)
20 xbt_matrix_t res = xbt_new(s_xbt_matrix_t, 1);
23 res->elmsize = elmsize;
25 res->data = xbt_malloc(elmsize * lines * rows);
29 /** \brief Creates a matrix being a submatrix of another one */
30 xbt_matrix_t xbt_matrix_new_sub(xbt_matrix_t from,
33 pvoid_f_pvoid_t const cpy_f)
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);
42 /** \brief destructor */
43 void xbt_matrix_free(xbt_matrix_t mat)
48 for (i = 0; i < (mat->lines * mat->rows); i++) {
49 (*(mat->free_f)) ((void *) &(mat->data[i * mat->elmsize]));
57 /** \brief Freeing function for containers of xbt_matrix_t */
58 void xbt_matrix_free_voidp(void *d)
60 xbt_matrix_free((xbt_matrix_t) * (void **) d);
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)
71 fprintf(stderr, ">>> Matrix %s dump (%d x %d)\n", name, matrix->lines,
73 for (i = 0; i < matrix->lines; i++) {
75 for (j = 0; j < matrix->rows; j++) {
77 fprintf(stderr, " (%d,%d)=", i, j);
80 (*display_fun) (xbt_matrix_get_ptr(matrix, i, j));
82 fprintf(stderr, "\n");
84 fprintf(stderr, "<<< end_of_matrix %s dump\n", name);
87 void xbt_matrix_dump_display_double(void *d)
89 fprintf(stderr, "%.2f", *(double *) d);
92 /** \brief Copy the values from the matrix src into the matrix dst
93 * \param dest: destination
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
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)
110 DEBUG10("Copy a %dx%d submatrix from %dx%d(of %dx%d) to %dx%d (of %dx%d)",
112 lpos_src, rpos_src, src->lines, src->rows,
113 lpos_dst, rpos_dst, dst->lines, dst->rows);
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 */
119 xbt_assert(lpos_src + lsize <= src->lines);
120 xbt_assert(rpos_src + rsize <= src->rows);
122 xbt_assert(lpos_dst + lsize <= dst->lines);
123 xbt_assert(rpos_dst + rsize <= dst->rows);
125 /* Lets get serious here */
126 for (i = 0; i < rsize; i++) {
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));
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);
140 /** \brief Creates a new matrix of double filled with zeros */
141 xbt_matrix_t xbt_matrix_double_new_zeros(int lines, int rows)
143 xbt_matrix_t res = xbt_matrix_new(lines, rows, sizeof(double), NULL);
145 memset(res->data, 0, res->elmsize * res->lines * res->rows);
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)
152 xbt_matrix_t res = xbt_matrix_double_new_zeros(lines, rows);
155 for (i = 0; i < lines; i++)
156 xbt_matrix_get_as(res, i, i, double) = 1;
160 /** \brief Creates a new matrix of double randomly filled */
161 xbt_matrix_t xbt_matrix_double_new_rand(int lines, int rows)
163 xbt_matrix_t res = xbt_matrix_new(lines, rows, sizeof(double), NULL);
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();
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)
175 xbt_matrix_t res = xbt_matrix_new(lines, rows, sizeof(double), NULL);
178 for (i = 0; i < lines * rows; i++)
179 *(double *) &res->data[i * res->elmsize] = i;
183 /** \brief Checks whether the matrix contains the sequence of numbers */
184 int xbt_matrix_double_is_seq(xbt_matrix_t mat) {
187 for (i = 0; i < mat->lines * mat->rows; i++) {
188 double val = xbt_matrix_get_as(mat,i,0,double);
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)
199 xbt_matrix_t result = xbt_matrix_double_new_zeros(A->lines, B->rows);
201 xbt_matrix_double_addmult(A, B, result);
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)
209 unsigned int i, j, k;
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);
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,