GFn_el_t & GFn_el_t::operator *= (int _a) { unsigned int a = gf_int(prime, _a); poly_mul(prime, degree, comp, degree, comp, 0, &a); return *this; }
void main() { char opt; clrscr(); printf("\nCreate the first list\n"); do { creat(&x1); printf("\nPress 'y' to continue"); flushall(); scanf("%c",&opt); }while(opt=='y'); printf("\nCreate the second list\n"); do { creat(&x2); printf("\nPress 'y' to continue"); flushall(); scanf("%c",&opt); }while(opt=='y'); getch(); clrscr(); printf("\n\nElements in list1\n\n"); traverse(&x1); printf("\n\nElements in list2\n\n"); traverse(&x2); printf("\n\n\n After addition\n\n"); x3=poly_add(x1,x2); traverse(&x3); printf("\n\n\n After multiplication\n\n"); x4=poly_mul(x1,x2); traverse(&x4); getch(); }
GFn_el_t & GFn_el_t::operator *= (const GFn_el_t &_h) { assert( &GFn == &_h.GFn ); unsigned int M[2*degree + 1]; poly_mul(prime, 2*degree, M, degree, comp, degree, _h.comp); poly_divmod(prime, -1, NULL, degree, comp, 2*degree, M, GFn.degree, GFn.mod); return *this; }
void main() { int n,num1,num2,i; node *head1=NULL,*head2=NULL,*head3=NULL; clrscr(); printf("FIRST POLYNOMIAL EXPRESSION ::"); printf("\n---------------------------\n\n"); printf("ENTER HOW MANY NO.OF TERM YOU WANT TO GIVE :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nENTER COEFFICIENT AND EXPONENT OF A TERM :"); scanf("%d",&num1); scanf("%d",&num2); poly_insert(num1,num2,&head1); } poly_reverse(&head1); poly_display(&head1); printf("\n\nSECOND POLYNOMIAL EXPRESSION ::"); printf("\n----------------------------\n\n"); printf("ENTER HOW MANY NO.OF TERM YOU WANT TO GIVE :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nENTER COEFFICIENT AND EXPONENT OF A TERM :"); scanf("%d",&num1); scanf("%d",&num2); poly_insert(num1,num2,&head2); } poly_reverse(&head2); poly_display(&head2); poly_mul(&head1,&head2,&head3); printf("\n\nAFTER MULTIPLICATION POLYNOMIAL EXPRESSION ::"); printf("\n------------------------------------------"); poly_display(&head3); getch(); }
int main(int argc, char* argv[]) { int n; int i; int next_power_of_2; int timed_test; int shift_val; int coeff; int ret_val; complex* a; complex* b; #ifdef TIMED_FFT timed_test = 1; #else timed_test = 0; #endif if (timed_test) { srand(time(NULL)); n = 1; shift_val = 0; while ((n = (n<<1)) <= MAX_N) { shift_val++; clock_t start = clock(); a = (complex*)malloc(2 * n * sizeof(complex)); b = (complex*)malloc(2 * n * sizeof(complex)); /* Randomize polynomials to multiply */ for (i = 0; i < (2*n); i++) { if (i < n) { a[i].r = rand()%MAX_COEFF; a[i].i = 0.0; b[i].r = rand()%MAX_COEFF; b[i].i = 0.0; } else /* pad with zeros */ { a[i].r = 0.0; a[i].i = 0.0; b[i].r = 0.0; b[i].i = 0.0; } } /* Perform polynomial multiplication and place results in a */ poly_mul(a, b, 2*n); free(a); free(b); printf("[N = 2^%-2d = %-7d] Time elapsed: %.9f sec\n", shift_val, n, ((double)clock() - start) / CLOCKS_PER_SEC); } } else { /* Read size of coefficient array from stdin */ ret_val = scanf("%d",&n); /* Determine the next biggest power of two */ next_power_of_2 = 1; while (next_power_of_2 < n) next_power_of_2 <<= 1; /* Allocate space for polynomials */ a = (complex*)malloc(2 * next_power_of_2 * sizeof(complex)); b = (complex*)malloc(2 * next_power_of_2 * sizeof(complex)); /* Read coefficients from stdin */ for (i = 0; i < n; i++) { ret_val = scanf("%d",&coeff); a[i].r = (double)coeff; a[i].i = 0.0; } #ifndef REC_FFT for (i = 0; i < n; i++) { ret_val = scanf("%d",&coeff); b[i].r = (double)coeff; b[i].i = 0.0; } #endif /* Pad the rest with zeros */ for (i = n; i < (2 * next_power_of_2); i++) { a[i].r = 0.0; a[i].i = 0.0; b[i].r = 0.0; b[i].i = 0.0; } n = next_power_of_2; #ifdef REC_FFT /* Execute recursive FFT (results placed in b) */ recursive_fft(a, b, n, 0); printf("\nPrinting coefficient evaluations at w_n^k = e^(2*PI*i*k/n):\n"); for (i = 0; i < n; i++) { if (i > 100) break; printf("[%d] = %.4f %s %.4fi\n", i, b[i].r, b[i].i < 0.0 ? "-" : "+", b[i].i < 0.0 ? (b[i].i*-1.0) : b[i].i); } #else /* Multiply polynomials */ poly_mul(a, b, 2*n); printf("\nPrinting coefficients for x^k:\n"); for (i = 0; i < (2*n - 1); i++) { if (i > 100) break; printf("[%d] = %.0f\n", i, a[i].r); } #endif free(a); free(b); } return 0; }
const GFn_el_t GFn_el_t::inv() const { int i; unsigned int u0[degree + 1], r0[degree + 1]; unsigned int u1[degree + 1], r1[degree + 1]; unsigned int q[degree + 1], r[degree + 1]; unsigned int u[2*degree + 1]; { for(i = degree; i >= 0 && 0 == comp[i]; --i) r1[i] = 0; assert( i >= 0 ); if( 0 == i ) return GFn_el_t(GFn, gf_inv(prime, comp[0])); for(; i >= 0; --i) r1[i] = comp[i]; for(i = degree; i >= 1; --i) u1[i] = 0; u1[0] = 1; } { poly_divmod(prime, degree, u0, degree, r0, GFn.degree, GFn.mod, degree, comp); poly_sub(prime, degree, u0, -1, NULL, degree, u0); } while( true ) { { for(i = degree; i >= 0 && 0 == r0[i] ; --i); assert( i >= 0 ); if( 0 == i ) { unsigned int inv = gf_inv(prime, r0[0]); poly_mul(prime, degree, u0, degree, u0, 0, &inv); return GFn_el_t(GFn, u0); } } poly_divmod(prime, degree, q, degree, r, degree, r1, degree, r0); for(i = degree; i >= 0; --i) { r1[i] = r0[i]; r0[i] = r[i]; } poly_mul(prime, 2*degree, u, degree, u0, degree, q); poly_sub(prime, 2*degree, u, degree, u1, 2*degree, u); for( i = degree; i >= 0; --i) u1[i] = u0[i]; poly_divmod(prime, -1, NULL, degree, u0, 2*degree, u, GFn.degree, GFn.mod); } }
int main (int argc, char* argv[]) { int min = 0; int max = 0; biggun cmb = 1; long int constant = 0; int nrolls = 0; int *rolls[argc]; for (int i = 1; i < argc; i++) { if (strchr(argv[i], 'd')) { int *roll; int j = 0; roll = calloc(2, sizeof(int)); for ( char *token = strtok(argv[i], "d"); token != NULL; token = strtok(NULL, "d") ) { roll[j] = atoi(token); j++; } rolls[nrolls] = roll; nrolls++; } else { constant += atol(argv[i]); } } for (int i = 0; i < nrolls; i++) { int n = rolls[i][0]; int d = rolls[i][1]; min += n; max += n*d; cmb *= power(d, n); } Poly **polys; polys = calloc(min, sizeof(Poly *)); int k = 0; for (int i = 0; i < nrolls; i++) { int n = rolls[i][0]; int d = rolls[i][1]; Poly **list = mkroll(n,d); for (int j = 0; j < n; j++) { polys[k] = list[j]; k++; } } Poly *biggie = polys[0]; for (int i = 1; i < min; i++) { biggie = poly_mul(biggie, polys[i]); } // debug(min, max, cmb, biggie); for (int i = min; i <= biggie->degree; i++) { long n = i + constant; ldub p = (ldub) biggie->coeffz[i] / (ldub) cmb; char *hg = mkpounds(p); printf("%ld\t\t%.5Lf\t\t%s\n", n, p, hg); } return 0; }