1 /* matrix.c -- basic matrix operations
3 * author: hackbard@hackdaworld.dyndns.org
9 int matrix_init(t_matrix *matrix,int outfd) {
11 dprintf(outfd,"[matrix] initializing matrix api ...\n");
24 int matrix_alloc_data(t_matrix *matrix) {
26 /* there is always one matrix */
27 if((matrix->m==0)||(matrix->n==0)) {
28 dprintf(matrix->outfd,"[matrix] wrong dimensions ...\n");
32 matrix->m1=malloc(matrix->m*matrix->n*sizeof(double));
33 if(matrix->m1==NULL) {
34 dprintf(matrix->outfd,"[matrix] malloc failed\n");
38 /* but often there are more ... */
39 if(matrix->type&MULT) {
41 dprintf(matrix->outfd,"[matrix] wrong dimensions ...\n");
45 matrix->m2=malloc(matrix->n*matrix->z*sizeof(double));
46 if(matrix->m2==NULL) {
47 dprintf(matrix->outfd,"[matrix] malloc failed\n");
50 matrix->res=malloc(matrix->m*matrix->z*sizeof(double));
51 if(matrix->res==NULL) {
52 dprintf(matrix->outfd,"[matrix] malloc failed\n");
56 dprintf(matrix->outfd,"[matrix] type of operation not supported by now\n");
57 return M_NOT_SUPPORTED;
63 int matrix_shutdown(t_matrix *matrix) {
65 dprintf(matrix->outfd,"[matrix] shutdown\n");
67 if(matrix->m1!=NULL) free(matrix->m1);
68 if(matrix->m2!=NULL) free(matrix->m2);
69 if(matrix->res!=NULL) free(matrix->res);
74 /* matrix operations */
76 int matrix_calc(t_matrix *matrix) {
78 unsigned int i,j,k,off,off1,off2;
80 if(!(matrix->type&MULT)) {
81 dprintf(matrix->outfd,"[matrix] only MULT supported by now ...\n");
82 return M_NOT_SUPPORTED;
85 /* mult operation, an O(N^3) problem
87 * maybe speedup by different order of mult?
90 for(i=0;i<matrix->z;i++) {
93 for(j=0;j<matrix->n;j++) {
97 for(k=0;k<matrix->m;k++) {
98 matrix->res[off]+=(matrix->m1[off1]*matrix->m2[off2]);