www.pudn.com > ezw_davis.rar > matrix2d.c
/* File: MATRIX2D.C Basic 2D-matrix implementation in ANSI-C. C. Valens Created : 05/09/1999 Last update : 29/09/1999 */ #include "matrix2d.h" #include#include /* * Allocate memory for a two-dimensional RxC matrix. * Returns NULL on failure. */ matrix_2d *matrix_2d_create(int r, int c)//创建一个行为r,列为c的二维矩阵的指针 { int row; matrix_2d *m;//建立二维矩阵的指针 m = malloc(sizeof(matrix_2d)); if (m!=NULL) { m->row = r; m->col = c; m->m = malloc(r*sizeof(element_type*)); if (m->m==NULL) return NULL; for (row=0; row m[row] = malloc(c*sizeof(element_type)); if (m->m[row]==NULL) return NULL; } } return m; } /* * Destroy a matrix. */ void matrix_2d_destroy(matrix_2d *m)//删除二维矩阵 { int row; for (row=0; row row; row++) { free(m->m[row]); } free(m); } /* * Set all matrix elements to zero. */ void matrix_2d_clear(matrix_2d *m)//对矩阵清0 { int row, col; for (row=0; row row; row++) { for (col=0; col col; col++) { m->m[row][col] = 0; } } } /* * Display a matrix. */ void matrix_2d_write(matrix_2d *m)//打印出矩阵的值 { int row, col; for (row=0; row row; row++) { for (col=0; col col; col++) { printf("%3d",m->m[row][col]); } printf("\n"); } } /* * Returns max(abs(matrix)), the absolute maximum value of a matrix. */ element_type matrix_2d_abs_max(matrix_2d *m)//输出二维矩阵的绝对值的最大值 { int row, col; element_type max = 0; for (row=0; row row; row++) { for (col=0; col col; col++) { if (abs(m->m[row][col])>max) max = abs(m->m[row][col]); } } return max; }