java - Why does this matrix-multiplication function produce incorrect outputs? -


my current implementation follows, taken this question , modified use exclusive or instead of addition per comments here. assumption made (and, goal of implementation, acceptable) input matrices 4x4.

/** * produces product of 2 matrices, treating first parameter "left" matrix. */ public static int[][] multiplymatrices(int[][] a, int[][] b) {     int[][] resultmatrix = new int[4][4];     (int = 0; < 4; i++)     {         for(int j = 0; j < 4; j++)         {             for(int k = 0; k < 4; k++)             {                 resultmatrix[i][j] = resultmatrix[i][j] ^ multiplypolynomials(a[i][k], b[k][j]);             }         }     }     return resultmatrix; } 

the multiplypolynomials function described here.

/** * multiplies 2 polynomials (mod 2) represented integers. * first converted binary string, used * select number of appropriate bit shifts of * second, added together. */ public static int multiplypolynomials(int n, int m) {     int result = 0x00000000;     string ns = tobitstring(n);      (int = 0; < ns.length(); i++)     {         int bitindex = (ns.length() - i) -1;         if (ns.charat(bitindex) == '1')         {             int temp = m;             temp = temp << i;             result = result ^ temp;         }     }     return result; } 

after running multiplymatrices, resultmatrix passed parameter of reducematrix, defined below, second parameter being given 0x011b. (tobitstring shortcut integer.tobinarystring.)

/* * reduces elements of matrix modulo given polynomial. * treats values bit-string representations of polynomials mod 2. * works xoring various left-shifts of modulo value value being reduced. * note: comment describes both reducematrix() , moduloreduce(). */ public static int[][] reducematrix(int[][] m, int p) {     int[][] reducedm = new int[4][4];      for(int = 0; < 4; i++)     {         for(int j = 0; j < 4; j++)         {             reducedm[i][j] = moduloreduce(m[i][j], p);         }     }      return reducedm; }  public static int moduloreduce(int f, int p) {     string fs = tobitstring(f);     string ps = tobitstring(p);     int temp = f;     int q = 0;     while (tobitstring(temp).length() >= ps.length())     {         int j = tobitstring(temp).length()-1;         int k = ps.length()-1;         q = p << (j-k);         temp = temp ^ q;     }     return temp; } 

this tested using following 2 matrices input, values given being explicitly in hexadecimal:

02 03 01 01 01 02 03 01 01 01 02 03 03 01 01 02 ---------- 10 26 03 15 23 41 33 04 51 32 14 43 12 12 12 12 

with inputs, output matrix (in hexadecimal, reduced mod 0x011b) is:

10 26 03 15 23 41 33 04 51 32 14 43 12 12 12 12 

however, completing reduction hand produces different result:

06 af 55 7c b7 e0 4b ca a7 35 2e a1 66 3d 06 5c 

so program written not working correctly. however, following second input matrix (using same first matrix) returns correct result:

30 ca 96 24 32 92 09 23 3f ca 43 63 7d 5a f8 96 

the expected (and received) result above input follows:

74 b2 97 d8  68 ea b9 51  fb 39 0a 60  a7 a9 00 1b  

clearly, output of multiplymatrices not expected result in cases. matrix multiplication algorithm used appears have worked poster of linked question, , have verified multiplypolynomials works expected. cause of inaccurate output multiplymatrices, , why inputs produce correct output anyway?


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -