Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int multiplyExpMatrix(sparse_m *m1, sparse_m *m2, sparse_m* m3)
- {
- if(!m1 || !m2) return BAD_PTR;
- int rows1, cols1, rows2, cols2, nz_count = 0;
- sparse_m *result = NULL;
- if(m1->m == 0 || m2->m == 0) return EMPTY_DATA;
- cols1 = m1->n - 1;
- cols2 = m2->n - 1;
- rows1 = m1->Ai[0];
- rows2 = m2->Ai[0];
- for(int i = 0; i < m2->Aj[cols2]; i++)
- if(m2->Ai[i] > rows2)
- rows2 = m2->Ai[i];
- rows2++;
- for(int i = 0; i < m1->Aj[cols1]; i++)
- if(m1->Ai[i] > rows1)
- rows1 = m1->Ai[i];
- rows1++;
- // printf("r1 = %d, c1 = %d, r2 = %d, c2 = %d\n", rows1, cols1, rows2, cols2);
- if(cols1 != rows1) return WRONG_DATA;
- result = malloc(sizeof(sparse_m));
- result->A = calloc(rows2, sizeof(data_t));
- result->Ai = malloc(rows2*sizeof(int));
- result->Aj = malloc(2*sizeof(int));
- result->m = rows2;
- result->n = 2;
- result->Aj[0] = 0;
- data_t sum;
- // int ai = 0;
- for(int i = 0; i < result->m; i++) // заполняем все элементы результирующего вектора
- {
- sum = 0;
- for(int j = 0, s, e, ai = 0; j < m1->n-1; j++, ai++) // для столбцов с индексом начала s, конца e...
- {
- s = m1->Aj[j]; // индекс начала
- e = m1->Aj[j+1]; // индекс конца
- if(m1->Ai[s] > i) continue; // если в данном столбце нет нужных элементов берем следующий столбец
- for(int k = s; k < e; k++) // идем по столбцу
- {
- if(m1->Ai[k] == i) // если элемент из столбца находится в строке i
- sum += m1->A[k] * m2->A[ai]; // собсно перемножаем операнды
- }
- }
- result->A[i] = sum;
- }
- result->Aj[1] = nz_count;
- *m3 = *result;
- return OK;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement