algorithm - Polynomial multiplication in M2(R)? -
i trying implement fft-based multiplication algorithm in m2(r). algorithm gets input 2 polynoms elements given matrices, , builds product polynom. however, though algorithm should work, looks identical version wrote earlier on regular number, doesn't. coefficients off little.
i have not found articles on roots of unity in m2(c), have found(on paper) choosing eps = ((cos(2pi/n) , sin(2pi/n)) , ( sin(2pi/n) , cos(2pi/n))), nice cycle.
is there wrong in approach?
here code:
struct fft { polyc to, aux[17][2], res[17][2], ac, bc, resc, resd, arga, argb; void fft(polyc v, var depth, var n, polyc to, matc step) { if(n == 1) { to[0] = v[0]; } else { matc eps = matchelper.i2; //we "split" poly in 2 for(var i=0; i<n; i++) aux[depth+1][i&1][i>>1] = v[i]; //we recursively apply fft components fft(aux[depth+1][0], depth+1, n/2, res[depth+1][0], step*step); fft(aux[depth+1][1], depth+1, n/2, res[depth+1][1], step*step); //we compute result n roots for(var i=0; i<n/2; i++) { to[i] = res[depth+1][0][i] + eps * res[depth+1][1][i]; to[n/2+i] = res[depth+1][0][i] - eps * res[depth+1][1][i]; eps = eps * step; } } } void fftmultiply(poly res, poly a, poly b, var n1, var n2) { var m; for(m = 1; m <= 2*n1 || m <= 2*n2; m <<= 1); for(var i=0; i<n1; i++) arga[i] = a[i]; for(var i=n1; i<m; i++) arga[i] = matchelper.o2; for(var i=0; i<n2; i++) argb[i] = b[i]; for(var i=n2; i<m; i++) argb[i] = matchelper.o2; matc step( complex(cos(2*pi/m), 0) , complex(0, sin(2*pi/m)), complex(0, sin(2*pi/m)) , complex(cos(2*pi/m), 0) ); fft(arga, 0, m, ac, step); fft(argb, 0, m, bc, step); for(var i=0; i<m; i++) { rezc[i] = ac[i] * bc[i]; } step.b = -step.b; step.c = -step.c; fft(rezc, 0, m, rezd, step); for(var i=0; i<m; i++) { // divided m , copied every element of resd res modulo number } } };
you can not expect method work if coefficient matrices not commute matrix step
. working correctly, use diagonal matrix corresponding multiplication scalar exp(i*2*pi/m)
.
Comments
Post a Comment