Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. int multiplyExpMatrix(sparse_m *m1, sparse_m *m2, sparse_m* m3)
  2. {
  3.     if(!m1 || !m2) return BAD_PTR;
  4.     int rows1, cols1, rows2, cols2, nz_count = 0;
  5.     sparse_m *result = NULL;
  6.     if(m1->m == 0 || m2->m == 0) return EMPTY_DATA;
  7.     cols1 = m1->n - 1;
  8.     cols2 = m2->n - 1;
  9.     rows1 = m1->Ai[0];
  10.     rows2 = m2->Ai[0];
  11.  
  12.     for(int i = 0; i < m2->Aj[cols2]; i++)
  13.         if(m2->Ai[i] > rows2)
  14.             rows2 = m2->Ai[i];
  15.     rows2++;   
  16.  
  17.     for(int i = 0; i < m1->Aj[cols1]; i++)
  18.         if(m1->Ai[i] > rows1)
  19.             rows1 = m1->Ai[i];
  20.     rows1++;
  21.     // printf("r1 = %d, c1 = %d, r2 = %d, c2 = %d\n", rows1, cols1, rows2, cols2);
  22.     if(cols1 != rows1) return WRONG_DATA;
  23.     result = malloc(sizeof(sparse_m));
  24.  
  25.     result->A = calloc(rows2, sizeof(data_t));
  26.     result->Ai = malloc(rows2*sizeof(int));
  27.     result->Aj = malloc(2*sizeof(int));
  28.     result->m = rows2;
  29.     result->n = 2;
  30.  
  31.     result->Aj[0] = 0;
  32.     data_t sum;
  33.     // int ai = 0;
  34.     for(int i = 0; i < result->m; i++) // заполняем все элементы результирующего вектора
  35.     {
  36.         sum = 0;       
  37.         for(int j = 0, s, e, ai = 0; j < m1->n-1; j++, ai++) // для столбцов с индексом начала s, конца e...
  38.         {
  39.             s = m1->Aj[j];   // индекс начала
  40.             e = m1->Aj[j+1]; // индекс конца
  41.             if(m1->Ai[s] > i) continue; // если в данном столбце нет нужных элементов берем следующий столбец
  42.             for(int k = s; k < e; k++) // идем по столбцу
  43.             {
  44.                 if(m1->Ai[k] == i) // если элемент из столбца находится в строке i
  45.                     sum += m1->A[k] * m2->A[ai]; // собсно перемножаем операнды                   
  46.             }
  47.         }
  48.  
  49.         result->A[i] = sum;
  50.     }
  51.    
  52.    
  53.     result->Aj[1] = nz_count;
  54.  
  55.     *m3 = *result;
  56.     return OK;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement