I am trying to make the multiplication of matrices for Vinograd, but somehow it does not work out very well. I ask for your help. What is:

#include <iostream> #include <vector> int main() { const int n = 5; const int max = 10; const int min =1; int arr1[n][n]; int arr2[n][n]; int arr3[n][n]; std::vector <int> rowFactor; std::vector <int> columnFactor; int d = n/2; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { arr1[i][j] = rand()%(max-min+1) + min; arr2[i][j] = rand()%(max-min+1) + min; } } for(int i = 0 ;i<n;++i) { rowFactor.insert(rowFactor.end(), arr1[i][1]*arr1[i][2]); for(int j =0;j<d;++j) { rowFactor.insert(rowFactor.end(),rowFactor[i] + arr1[i][2*j - 1] * arr1[i][2*j]); } } for(int i = 0 ;i<n;++i) { columnFactor.insert(columnFactor.end(), arr2[i][1]*arr2[i][2]); for(int j =0;j<d;++j) { columnFactor.insert(columnFactor.end(), columnFactor[i] + arr2[2*j - 1][ i] * arr2[2*j][ i]); } } for(int i =0; i<n;++i) { for(int j =0;j<n;++j) { arr3[i][j] = -rowFactor[i] - columnFactor[j]; for(int k = 0; k< d;k++) { arr3[i][j]=arr3[i][j]+(arr1[i][ 2*k-1]+arr2[2*k][j])*(arr1[i][ 2*k] + arr2[2*k-1][j]); } } } std::cout<<"\nresult\n"<<std::endl; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) std::cout << " " << arr3[i][j]; std::cout << std::endl; } } 

Even something counts, but not correctly. I think this line error

 arr3[i][j]=arr3[i][j]+(arr1[i][ 2*k-1]+arr2[2*k][j])*(arr1[i][ 2*k] + arr2[2*k-1][j]); 
  • one
    I think you're doing a little wrong. Judging by algolib.narod.ru/Math/Matrix.html , you need to do not insert (rowFactor.insert (rowFactor.end (), rowFactor [i] + arr1 [i] [2 * j - 1] * arr1 [i ] [2 * j]);), and the addition: rowFactor [i] = rowFactor [i] + arr1 [i] [2 * j - 1] * arr1 [i] [2 * j]; . This is the first thing that caught my eye. (similarly for columnFactor columns) - zcorvid
  • This moment has been corrected, but now at least there is a varning "array subscript is below array bounds" in the row arr3 [i] [j] = arr3 [i] [j] + (arr1 [i] [2 * k-1] + arr2 [ 2 * k] [j]) * (arr1 [i] [2 * k] + arr2 [2 * k-1] [j]); in the places arr2 [2 * k-1] [j] and arr1 [i] [2 * k-1] - Luntic
  • one
    In this formula, you have an exact error, with k == 0 you refer to the index -1, which is not good (this is what warning curses). - zcorvid
  • And you can tell how to fix it? I understood the error myself, but how to fix something does not reach me in any way. He relied on the algorithm algolib.narod.ru/Math/Matrix.html - Luntic
  • one
    In this article, the numbering goes from 1 to N, and with such data 2k - 1 at the beginning is 1, which is correct. In our case, the numbering goes from 0 to N - 1, considering this, I think the following line will be correct: arr3 [i] [j] = arr3 [i] [j] + (arr1 [i] [2 * k] + arr2 [ 2 * k + 1] [j]) * (arr1 [i] [2 * k + 1] + arr2 [2 * k] [j]); Ps. Transferred correction from the comment in reply. - zcorvid

1 answer 1

In the article on the Vinograd method, the numbering goes from 1 to N (there the code is written in Pascal), and with such data 2k - 1 at the beginning is 1, which is correct. In your case, the numbering goes from 0 to N - 1, considering this, I think the following line will be correct:

 arr3[i][j] = arr3[i][j] + (arr1[i][2*k] + arr2[2*k + 1][j]) * (arr1[i][2*k + 1] + arr2[2*k][j]); 
  • Damn, so ashamed. I thought about it, but did not. Thank! =) And this is another question, if you multiply the matrix by the matrix in the usual way and data, then the answer should be the same, I understand correctly? - Luntic
  • one
    I was glad to help :) Please mark the answer as correct if it helped you. It would also be nice if you edit the question by correcting an insert error in the code so that later those who will read the question are not confused about them. - zcorvid