3 /* xbt_matrix_t management functions */
5 /* Copyright (c) 2006 Martin Quinson. All rights reserved. */
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. */
10 #include "xbt/sysdep.h"
12 #include "xbt/matrix.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_matrix,xbt,"2D data storage");
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 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) {
35 xbt_matrix_t res=xbt_matrix_new(lsize,rsize,
36 from->elmsize, from->free_f);
37 xbt_matrix_copy_values(res,from,lsize,rsize,0,0,lpos,rpos,cpy_f);
41 /** \brief destructor */
42 void xbt_matrix_free(xbt_matrix_t mat) {
46 for (i=0; i < (mat->lines * mat->rows) ; i++) {
47 (*(mat->free_f))( (void*)& (mat->data[i*mat->elmsize]) );
55 /** \brief Freeing function for containers of xbt_matrix_t */
56 void xbt_matrix_free_voidp(void *d) {
57 xbt_matrix_free( (xbt_matrix_t) *(void**)d );
61 /** \brief Display the content of a matrix (debugging purpose)
62 * \param coords: boolean indicating whether we should add the coords of each cell to the output*/
63 void xbt_matrix_dump(xbt_matrix_t matrix, const char*name, int coords,
64 void_f_pvoid_t display_fun) {
67 fprintf(stderr,">>> Matrix %s dump (%d x %d)\n",name,matrix->lines,matrix->rows);
68 for (i=0; i<matrix->lines; i++) {
70 for (j=0; j<matrix->rows; j++) {
72 fprintf(stderr," (%d,%d)=",i,j);
75 display_fun(xbt_matrix_get_ptr(matrix,i,j));
79 fprintf(stderr,"<<< end_of_matrix %s dump\n",name);
82 void xbt_matrix_dump_display_double(void*d) {
83 fprintf(stderr,"%.2f",*(double*)d);
85 /** \brief Copy the values from the matrix src into the matrix dst
86 * \param dest: destination
88 * \param lsize: number of lines to copy
89 * \param rsize: number of rows to copy
90 * \param lpos_dst: line offset on destination matrix
91 * \param rpos_dst: row offset on destination matrix
92 * \param lpos_src: line offset on destination matrix
93 * \param rpos_src: row offset on destination matrix
95 void xbt_matrix_copy_values(xbt_matrix_t dst, xbt_matrix_t src,
97 int lpos_dst,int rpos_dst,
98 int lpos_src,int rpos_src,
99 pvoid_f_pvoid_t *const cpy_f) {
102 DEBUG10("Copy a %dx%d submatrix from %dx%d(of %dx%d) to %dx%d (of %dx%d)",
104 lpos_src,rpos_src,src->lines,src->rows,
105 lpos_dst,rpos_dst,dst->lines,dst->rows);
107 /* everybody knows that issue is between the chair and the screen (particulary in my office) */
108 xbt_assert(src->elmsize == dst->elmsize);
109 /* don't check free_f since the user may play weird games with this */
111 xbt_assert(lpos_src+lsize <= src->lines);
112 xbt_assert(rpos_src+rsize <= src->rows);
114 xbt_assert(lpos_dst+lsize <= dst->lines);
115 xbt_assert(rpos_dst+rsize <= dst->rows);
117 /* Lets get serious here */
118 for (i=0;i<rsize;i++) {
120 for (j=0;j<lsize;j++)
121 xbt_matrix_get_as(dst,j+lpos_dst,i+rpos_dst,void*) = cpy_f(xbt_matrix_get_ptr(src,j+rpos_src,i+lpos_src));
123 memcpy(xbt_matrix_get_ptr(dst,lpos_dst,i+rpos_dst),
124 xbt_matrix_get_ptr(src,lpos_src,i+rpos_src),
131 /** \brief Creates a new matrix of double filled with zeros */
132 xbt_matrix_t xbt_matrix_double_new_zeros(int lines, int rows) {
133 xbt_matrix_t res = xbt_matrix_new(lines, rows,sizeof(double),NULL);
135 memset(res->data,0, res->elmsize * res->lines * res->rows);
139 /** \brief Creates a new matrix of double being the identity matrix */
140 xbt_matrix_t xbt_matrix_double_new_id(int lines, int rows) {
141 xbt_matrix_t res = xbt_matrix_double_new_zeros(lines, rows);
144 for (i=0; i<lines; i++)
145 xbt_matrix_get_as(res,i,i,double) = 1;
149 /** \brief Creates a new matrix of double randomly filled */
150 xbt_matrix_t xbt_matrix_double_new_rand(int lines, int rows) {
151 xbt_matrix_t res = xbt_matrix_new(lines, rows,sizeof(double),NULL);
154 for (i=0; i<lines; i++)
155 for (j=0; j<rows; j++)
156 xbt_matrix_get_as(res,i,j,double) = (double)rand();
159 /** \brief Creates a new matrix of double randomly by subsequent numbers */
160 xbt_matrix_t xbt_matrix_double_new_seq(int lines, int rows) {
161 xbt_matrix_t res = xbt_matrix_new(lines, rows,sizeof(double),NULL);
164 for (i=0; i<lines*rows; i++)
165 *(double*)&res->data[i*res->elmsize] = i;
170 /** \brief Creates a new matrix being the multiplication of two others */
171 xbt_matrix_t xbt_matrix_double_new_mult(xbt_matrix_t A,xbt_matrix_t B) {
172 xbt_matrix_t result = xbt_matrix_double_new_zeros(A->lines, B->rows);
174 xbt_matrix_double_addmult(A,B,result);
178 /** \brief add to C the result of A*B */
179 void xbt_matrix_double_addmult(xbt_matrix_t A,xbt_matrix_t B,
180 /*OUT*/ xbt_matrix_t C) {
183 xbt_assert2(A->lines == C->lines,
184 "A->lines != C->lines (%d vs %d)",A->lines,C->lines);
185 xbt_assert(B->rows == C->rows);
187 for (i=0; i<C->lines; i++)
188 for (j=0; j<C->rows; j++)
189 for (k=0; k<B->lines; k++)
190 xbt_matrix_get_as(C,i,j,double) +=
191 xbt_matrix_get_as(A,i,k,double) * xbt_matrix_get_as(B,k,j,double);