/*---------------------------------------------------------------------- File : polymult.c Contents: multiply two polynomials Author : Christian Borgelt History : 07.04.1998 file created ----------------------------------------------------------------------*/ #include #include /*---------------------------------------------------------------------- Preprocessor Definitions ----------------------------------------------------------------------*/ #define MAXDEGREE 64 /* maximal degree of polynomial */ /*---------------------------------------------------------------------- Functions ----------------------------------------------------------------------*/ int polymult (int n1, double c1[], int n2, double c2[], double res[]) { /* --- multiply polynomials */ int n; /* number of coeff. in product */ int i, k; /* loop variables */ n = n1 +n2 -1; /* check the degree of the */ if (n > MAXDEGREE) return -1; /* resulting polynomial */ for (i = n; --i >= 0; ) /* clear the coefficients */ res[i] = 0; /* of the resulting polynomial */ for (i = n1; --i >= 0; ) /* traverse first polynomial */ for (k = n2; --k >= 0; ) /* traverse second polynomial */ res[i+k] += c1[i] *c2[k]; /* multiply coeff. and store result */ return n; /* return number of coeff. in product */ } /* polymult() */ /*--------------------------------------------------------------------*/ int main (int argc, char *argv[]) { /* --- main function */ int n1, n2, n; /* degrees of polynomials */ double c1 [MAXDEGREE+1]; /* coefficients of first polynomial */ double c2 [MAXDEGREE+1]; /* coefficients of second polynomial */ double res[MAXDEGREE+1]; /* coefficients of product */ FILE *in; /* file to read polynomial from */ /* --- print a usage message --- */ if (argc != 3) { /* if wrong number of arguments given */ printf("usage: %s in1 in2\n", argv[0]); printf("multiply two polynomials\n"); printf("The coefficients of the polynomials\n" "are read from the files `in1' and `in2'.\n"); return 0; /* print a usage message */ } /* and abort the function */ /* --- read first polynomial --- */ in = fopen(argv[1], "r"); /* open the file for reading */ if (!in) { /* if the file cannot be opened */ printf("%s: cannot open file `%s'\n", argv[0], argv[1]); return -1;} for (n1 = 0; n1 <= MAXDEGREE; n1++) { if (fscanf(in, "%lf", c1+n1) != 1) break; /* read the coefficients of the */ } /* polynomial from the input file */ fclose(in); /* close input file */ if (n1 > MAXDEGREE) { /* check degree of polynomial */ printf("%s: first polynomial too large\n", argv[0]); return 1; } /* --- read second polynomial --- */ in = fopen(argv[2], "r"); /* open the file for reading */ if (!in) { /* if the file cannot be opened */ printf("%s: cannot open file `%s'\n", argv[0], argv[2]); return -1;} for (n2 = 0; n2 <= MAXDEGREE; n2++) { if (fscanf(in, "%lf", c2+n2) != 1) break; /* read the coefficients of the */ } /* polynomial from the input file */ fclose(in); /* close input file */ if (n2 > MAXDEGREE) { /* check degree of polynomial */ printf("%s: second polynomial too large\n", argv[0]); return 1; } /* --- compute and print product --- */ n = polymult(n1, c1, n2, c2, res); /* multiply polynomials */ if (n < 0) { printf("%s: result too large\n", argv[0]); return 1; } for (n1 = 0; n1 < n; n1++) /* print the coefficients */ printf("%g\n", res[n1]); /* of the product */ return 0; /* return 'ok' */ } /* main() */